-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathshadow.js
50 lines (43 loc) · 1.41 KB
/
shadow.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
import * as THREE from 'three';
import { registerComponent } from '../core/component.js';
/**
* Shadow component.
*
* When applied to an entity, that entity's geometry and any descendants will cast or receive
* shadows as specified by the `cast` and `receive` properties.
*/
export var Component = registerComponent('shadow', {
schema: {
cast: {default: true},
receive: {default: true}
},
init: function () {
this.onMeshChanged = this.update.bind(this);
this.el.addEventListener('object3dset', this.onMeshChanged);
this.system.setShadowMapEnabled(true);
},
update: function () {
var data = this.data;
this.updateDescendants(data.cast, data.receive);
},
remove: function () {
var el = this.el;
el.removeEventListener('object3dset', this.onMeshChanged);
this.updateDescendants(false, false);
},
updateDescendants: function (cast, receive) {
var sceneEl = this.el.sceneEl;
this.el.object3D.traverse(function (node) {
if (!(node instanceof THREE.Mesh)) { return; }
node.castShadow = cast;
node.receiveShadow = receive;
// If scene has already rendered, materials must be updated.
if (sceneEl.hasLoaded && node.material) {
var materials = Array.isArray(node.material) ? node.material : [node.material];
for (var i = 0; i < materials.length; i++) {
materials[i].needsUpdate = true;
}
}
});
}
});