-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathlight.js
388 lines (342 loc) · 12.3 KB
/
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import * as THREE from 'three';
import { LightProbeGenerator } from 'three/addons/lights/LightProbeGenerator.js';
import { diff, debug, srcLoader } from '../utils/index.js';
import { registerComponent } from '../core/component.js';
import * as mathUtils from '../utils/math.js';
var degToRad = THREE.MathUtils.degToRad;
var warn = debug('components:light:warn');
var CubeLoader = new THREE.CubeTextureLoader();
var probeCache = {};
/**
* Light component.
*/
export var Component = registerComponent('light', {
schema: {
angle: {default: 60, if: {type: ['spot']}},
color: {type: 'color', if: {type: ['ambient', 'directional', 'hemisphere', 'point', 'spot']}},
envMap: {default: '', if: {type: ['probe']}},
groundColor: {type: 'color', if: {type: ['hemisphere']}},
decay: {default: 1, if: {type: ['point', 'spot']}},
distance: {default: 0.0, min: 0, if: {type: ['point', 'spot']}},
intensity: {default: 3.14, min: 0, if: {type: ['ambient', 'directional', 'hemisphere', 'point', 'spot', 'probe']}},
penumbra: {default: 0, min: 0, max: 1, if: {type: ['spot']}},
type: {
default: 'directional',
oneOf: ['ambient', 'directional', 'hemisphere', 'point', 'spot', 'probe'],
schemaChange: true
},
target: {type: 'selector', if: {type: ['spot', 'directional']}},
// Shadows.
castShadow: {default: false, if: {type: ['point', 'spot', 'directional']}},
shadowBias: {default: 0, if: {castShadow: true}},
shadowCameraFar: {default: 500, if: {castShadow: true}},
shadowCameraFov: {default: 90, if: {castShadow: true}},
shadowCameraNear: {default: 0.5, if: {castShadow: true}},
shadowCameraTop: {default: 5, if: {castShadow: true}},
shadowCameraRight: {default: 5, if: {castShadow: true}},
shadowCameraBottom: {default: -5, if: {castShadow: true}},
shadowCameraLeft: {default: -5, if: {castShadow: true}},
shadowCameraVisible: {default: false, if: {castShadow: true}},
shadowCameraAutomatic: {default: '', if: {type: ['directional']}},
shadowMapHeight: {default: 512, if: {castShadow: true}},
shadowMapWidth: {default: 512, if: {castShadow: true}},
shadowRadius: {default: 1, if: {castShadow: true}}
},
/**
* Notifies scene a light has been added to remove default lighting.
*/
init: function () {
var el = this.el;
this.light = null;
this.defaultTarget = null;
this.system.registerLight(el);
},
/**
* (Re)create or update light.
*/
update: function (oldData) {
var data = this.data;
var diffData = diff(data, oldData);
var light = this.light;
var self = this;
// Existing light.
if (light && !('type' in diffData)) {
var shadowsLoaded = false;
// Light type has not changed. Update light.
Object.keys(diffData).forEach(function (key) {
var value = data[key];
switch (key) {
case 'color': {
light.color.set(value);
break;
}
case 'groundColor': {
light.groundColor.set(value);
break;
}
case 'angle': {
light.angle = degToRad(value);
break;
}
case 'target': {
// Reset target if selector is null.
if (value === null) {
if (data.type === 'spot' || data.type === 'directional') {
light.target = self.defaultTarget;
}
} else {
// Target specified, set target to entity's `object3D` when it is loaded.
if (value.hasLoaded) {
self.onSetTarget(value, light);
} else {
value.addEventListener('loaded', self.onSetTarget.bind(self, value, light));
}
}
break;
}
case 'envMap':
self.updateProbeMap(data, light);
break;
case 'castShadow':
case 'shadowBias':
case 'shadowCameraFar':
case 'shadowCameraFov':
case 'shadowCameraNear':
case 'shadowCameraTop':
case 'shadowCameraRight':
case 'shadowCameraBottom':
case 'shadowCameraLeft':
case 'shadowCameraVisible':
case 'shadowMapHeight':
case 'shadowMapWidth':
case 'shadowRadius':
if (!shadowsLoaded) {
self.updateShadow();
shadowsLoaded = true;
}
break;
case 'shadowCameraAutomatic':
if (data.shadowCameraAutomatic) {
self.shadowCameraAutomaticEls = Array.from(document.querySelectorAll(data.shadowCameraAutomatic));
} else {
self.shadowCameraAutomaticEls = [];
}
break;
default: {
light[key] = value;
}
}
});
return;
}
// No light yet or light type has changed. Create and add light.
this.setLight(this.data);
this.updateShadow();
},
tick: (function () {
var bbox = new THREE.Box3();
var normal = new THREE.Vector3();
var cameraWorldPosition = new THREE.Vector3();
var tempMat = new THREE.Matrix4();
var sphere = new THREE.Sphere();
var tempVector = new THREE.Vector3();
return function () {
if (!(
this.data.type === 'directional' &&
this.light.shadow &&
this.light.shadow.camera instanceof THREE.OrthographicCamera &&
this.shadowCameraAutomaticEls.length
)) return;
var camera = this.light.shadow.camera;
camera.getWorldDirection(normal);
camera.getWorldPosition(cameraWorldPosition);
tempMat.copy(camera.matrixWorld);
tempMat.invert();
camera.near = 1;
camera.left = 100000;
camera.right = -100000;
camera.top = -100000;
camera.bottom = 100000;
this.shadowCameraAutomaticEls.forEach(function (el) {
bbox.setFromObject(el.object3D);
bbox.getBoundingSphere(sphere);
var distanceToPlane = mathUtils.distanceOfPointFromPlane(cameraWorldPosition, normal, sphere.center);
var pointOnCameraPlane = mathUtils.nearestPointInPlane(cameraWorldPosition, normal, sphere.center, tempVector);
var pointInXYPlane = pointOnCameraPlane.applyMatrix4(tempMat);
camera.near = Math.min(-distanceToPlane - sphere.radius - 1, camera.near);
camera.left = Math.min(-sphere.radius + pointInXYPlane.x, camera.left);
camera.right = Math.max(sphere.radius + pointInXYPlane.x, camera.right);
camera.top = Math.max(sphere.radius + pointInXYPlane.y, camera.top);
camera.bottom = Math.min(-sphere.radius + pointInXYPlane.y, camera.bottom);
});
camera.updateProjectionMatrix();
};
}()),
setLight: function (data) {
var el = this.el;
var newLight = this.getLight(data);
if (newLight) {
if (this.light) {
el.removeObject3D('light');
}
this.light = newLight;
this.light.el = el;
el.setObject3D('light', this.light);
// HACK solution for issue #1624
if (data.type === 'spot' || data.type === 'directional' || data.type === 'hemisphere') {
el.getObject3D('light').translateY(-1);
}
// set and position default lighttarget as a child to enable spotlight orientation
if (data.type === 'spot') {
el.setObject3D('light-target', this.defaultTarget);
el.getObject3D('light-target').position.set(0, 0, -1);
}
if (data.shadowCameraAutomatic) {
this.shadowCameraAutomaticEls = Array.from(document.querySelectorAll(data.shadowCameraAutomatic));
} else {
this.shadowCameraAutomaticEls = [];
}
}
},
/**
* Updates shadow-related properties on the current light.
*/
updateShadow: function () {
var el = this.el;
var data = this.data;
var light = this.light;
light.castShadow = data.castShadow;
// Shadow camera helper.
var cameraHelper = el.getObject3D('cameraHelper');
if (data.shadowCameraVisible && !cameraHelper) {
cameraHelper = new THREE.CameraHelper(light.shadow.camera);
el.setObject3D('cameraHelper', cameraHelper);
} else if (!data.shadowCameraVisible && cameraHelper) {
el.removeObject3D('cameraHelper');
}
if (!data.castShadow) { return light; }
// Shadow appearance.
light.shadow.bias = data.shadowBias;
light.shadow.radius = data.shadowRadius;
light.shadow.mapSize.height = data.shadowMapHeight;
light.shadow.mapSize.width = data.shadowMapWidth;
// Shadow camera.
light.shadow.camera.near = data.shadowCameraNear;
light.shadow.camera.far = data.shadowCameraFar;
if (light.shadow.camera instanceof THREE.OrthographicCamera) {
light.shadow.camera.top = data.shadowCameraTop;
light.shadow.camera.right = data.shadowCameraRight;
light.shadow.camera.bottom = data.shadowCameraBottom;
light.shadow.camera.left = data.shadowCameraLeft;
} else {
light.shadow.camera.fov = data.shadowCameraFov;
}
light.shadow.camera.updateProjectionMatrix();
if (cameraHelper) { cameraHelper.update(); }
},
/**
* Creates a new three.js light object given data object defining the light.
*
* @param {object} data
*/
getLight: function (data) {
var angle = data.angle;
var color = new THREE.Color(data.color);
color = color.getHex();
var decay = data.decay;
var distance = data.distance;
var groundColor = new THREE.Color(data.groundColor);
groundColor = groundColor.getHex();
var intensity = data.intensity;
var type = data.type;
var target = data.target;
var light = null;
switch (type.toLowerCase()) {
case 'ambient': {
return new THREE.AmbientLight(color, intensity);
}
case 'directional': {
light = new THREE.DirectionalLight(color, intensity);
this.defaultTarget = light.target;
if (target) {
if (target.hasLoaded) {
this.onSetTarget(target, light);
} else {
target.addEventListener('loaded', this.onSetTarget.bind(this, target, light));
}
}
return light;
}
case 'hemisphere': {
return new THREE.HemisphereLight(color, groundColor, intensity);
}
case 'point': {
return new THREE.PointLight(color, intensity, distance, decay);
}
case 'spot': {
light = new THREE.SpotLight(color, intensity, distance, degToRad(angle), data.penumbra, decay);
this.defaultTarget = light.target;
if (target) {
if (target.hasLoaded) {
this.onSetTarget(target, light);
} else {
target.addEventListener('loaded', this.onSetTarget.bind(this, target, light));
}
}
return light;
}
case 'probe': {
light = new THREE.LightProbe();
this.updateProbeMap(data, light);
return light;
}
default: {
warn('%s is not a valid light type. ' +
'Choose from ambient, directional, hemisphere, point, spot.', type);
}
}
},
/**
* Generate the spherical harmonics for the LightProbe from a cube map
*/
updateProbeMap: function (data, light) {
if (!data.envMap) {
// reset parameters if no map
light.copy(new THREE.LightProbe());
return;
}
// Populate the cache if not done for this envMap yet
if (probeCache[data.envMap] === undefined) {
probeCache[data.envMap] = new window.Promise(function (resolve) {
srcLoader.validateCubemapSrc(data.envMap, function loadEnvMap (urls) {
CubeLoader.load(urls, function (cube) {
var tempLightProbe = LightProbeGenerator.fromCubeTexture(cube);
probeCache[data.envMap] = tempLightProbe;
resolve(tempLightProbe);
});
});
});
}
// Copy over light probe properties
if (probeCache[data.envMap] instanceof window.Promise) {
probeCache[data.envMap].then(function (tempLightProbe) {
light.copy(tempLightProbe);
});
} else if (probeCache[data.envMap] instanceof THREE.LightProbe) {
light.copy(probeCache[data.envMap]);
}
},
onSetTarget: function (targetEl, light) {
light.target = targetEl.object3D;
},
/**
* Remove light on remove (callback).
*/
remove: function () {
var el = this.el;
el.removeObject3D('light');
if (el.getObject3D('cameraHelper')) {
el.removeObject3D('cameraHelper');
}
}
});