-
Notifications
You must be signed in to change notification settings - Fork 319
/
playcanvas.js
75 lines (64 loc) · 2.32 KB
/
playcanvas.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
let playcanvas = null
const PlayCanvasFactory = () => {
if (!playcanvas) {
playcanvas = create()
}
return playcanvas
}
function create() {
// Attach a shader to a material that makes it appear transparent while still receiving shadows.
const makeShadowMaterial = ({pc, material}) => {
const materialResource = material.resource || material
materialResource.chunks.APIVersion = pc.CHUNKAPI_1_62
materialResource.chunks.endPS = `
litShaderArgs.opacity = mix(light0_shadowIntensity, 0.0, shadow0);
gl_FragColor.rgb = vec3(0.0);
`
materialResource.blendType = pc.BLEND_PREMULTIPLIED
materialResource.update()
}
// Finds one camera entity in the scene graph of a given entity. If there are multiple cameras, a
// warning is printed and one of them is returned arbitrarily. If there are no cameras, an error
// is printed, and undefined is returned.
const findOneCamera = (entity) => {
// Find all camera components in the graph of an entity.
const cameras = entity.root.findComponents('camera')
if (!cameras.length) {
console.error(`Couldn't find any cameras in the scene graph of ${entity.name}`)
return
}
if (cameras.length > 1) {
console.warn(`Found too many cameras (${cameras.length}) in the scene graph of ${entity.name}`)
}
// Pick the first camera if there are multiple.
return cameras[0].entity
}
// Configures the playcanvas entity to to track the image target with the specified name. This
// matches the name set in the 8th Wall console.
const trackImageTargetWithName = ({name, entity, app}) => {
entity.enabled = false
const showImage = (detail) => {
if (name != detail.name) { return }
const {rotation, position, scale} = detail
entity.setRotation(rotation.x, rotation.y, rotation.z, rotation.w)
entity.setPosition(position.x, position.y, position.z)
entity.setLocalScale(scale, scale, scale)
entity.enabled = true
}
const hideImage = (detail) => {
if (name != detail.name) { return }
entity.enabled = false
}
app.on('xr:imagefound', showImage, {})
app.on('xr:imageupdated', showImage, {})
app.on('xr:imagelost', hideImage, {})
}
return {
findOneCamera,
makeShadowMaterial,
trackImageTargetWithName,
}
}
export {
PlayCanvasFactory,
}