forked from Hubs-Foundation/hubs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint-light.js
71 lines (58 loc) · 1.92 KB
/
point-light.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
AFRAME.registerComponent("point-light", {
schema: {
color: { type: "color" },
intensity: { default: 1.0 },
range: { default: 0 },
decay: { default: 2 },
castShadow: { default: true },
shadowMapResolution: { default: [512, 512] },
shadowBias: { default: 0 },
shadowRadius: { default: 1 }
},
init() {
const el = this.el;
this.light = new THREE.PointLight();
this.light.shadow.camera.matrixAutoUpdate = true;
this.el.setObject3D("point-light", this.light);
this.el.sceneEl.systems.light.registerLight(el);
this.rendererSystem = this.el.sceneEl.systems.renderer;
},
update(prevData) {
const light = this.light;
if (this.data.color !== prevData.color) {
const color = new THREE.Color(this.data.color);
this.rendererSystem.applyColorCorrection(color);
light.color.copy(color);
}
if (this.data.intensity !== prevData.intensity) {
light.intensity = this.data.intensity;
}
if (this.data.range !== prevData.range) {
light.distance = this.data.range;
}
if (this.data.decay !== prevData.decay) {
light.decay = this.data.decay;
}
if (this.data.castShadow !== prevData.castShadow) {
light.castShadow = this.data.castShadow;
}
if (this.data.shadowBias !== prevData.shadowBias) {
light.shadow.bias = this.data.shadowBias;
}
if (this.data.shadowRadius !== prevData.shadowRadius) {
light.shadow.radius = this.data.shadowRadius;
}
const [width, height] = this.data.shadowMapResolution;
const [prevWidth, prevHeight] = prevData.shadowMapResolution ? prevData.shadowMapResolution : [512, 512];
if (width !== prevWidth || height !== prevHeight) {
light.shadow.mapSize.set(width, height);
if (light.shadow.map) {
light.shadow.map.dispose();
light.shadow.map = null;
}
}
},
remove: function() {
this.el.removeObject3D("point-light");
}
});