-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path5-networking.js
54 lines (47 loc) · 1.27 KB
/
5-networking.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
/**
* Check out each component file for a description of what they do
*/
import '../components/proximity-event.js'
import '../components/randomize-networked-color.js'
/**
* Step 1: Add a new NAF template to <a-assets>
*
* IMPORTANT: the template ID must end in "-media"
*/
const assets = document.querySelector('a-assets')
assets.insertAdjacentHTML(
'beforeend',
`
<template id="color-media">
<a-entity
geometry="primitive: sphere"
material="color: white; shader: flat"
proximity-event="event: bump; radius: 3"
randomize-networked-color="event: bump;">
</a-entity>
</template>
`
)
/**
* Step 2: Define a NAF schema to indicate what properties are networked
*/
NAF.schemas.add({
template: '#color-media',
components: [
{
component: 'material',
property: 'color',
},
],
})
/**
* Step 3: Attach the template to an entity
*/
const entity = document.createElement('a-entity')
entity.setAttribute('position', { x: 0, y: 2, z: 0 })
entity.setAttribute('networked', {
template: '#color-media', // Selector for our template
networkId: 'sphere', // A fixed networkId makes this entity shared for all clients
owner: 'scene', // Prevents newly joined clients from re-initializing the color
})
APP.scene.appendChild(entity)