@@ -199,7 +199,7 @@ function WebGLLights( extensions, capabilities ) {
199199 const matrix4 = new Matrix4 ( ) ;
200200 const matrix42 = new Matrix4 ( ) ;
201201
202- function setup ( lights , shadows , camera ) {
202+ function setup ( lights ) {
203203
204204 let r = 0 , g = 0 , b = 0 ;
205205
@@ -215,8 +215,6 @@ function WebGLLights( extensions, capabilities ) {
215215 let numPointShadows = 0 ;
216216 let numSpotShadows = 0 ;
217217
218- const viewMatrix = camera . matrixWorldInverse ;
219-
220218 lights . sort ( shadowCastingLightsFirst ) ;
221219
222220 for ( let i = 0 , l = lights . length ; i < l ; i ++ ) {
@@ -248,10 +246,6 @@ function WebGLLights( extensions, capabilities ) {
248246 const uniforms = cache . get ( light ) ;
249247
250248 uniforms . color . copy ( light . color ) . multiplyScalar ( light . intensity ) ;
251- uniforms . direction . setFromMatrixPosition ( light . matrixWorld ) ;
252- vector3 . setFromMatrixPosition ( light . target . matrixWorld ) ;
253- uniforms . direction . sub ( vector3 ) ;
254- uniforms . direction . transformDirection ( viewMatrix ) ;
255249
256250 if ( light . castShadow ) {
257251
@@ -281,16 +275,10 @@ function WebGLLights( extensions, capabilities ) {
281275 const uniforms = cache . get ( light ) ;
282276
283277 uniforms . position . setFromMatrixPosition ( light . matrixWorld ) ;
284- uniforms . position . applyMatrix4 ( viewMatrix ) ;
285278
286279 uniforms . color . copy ( color ) . multiplyScalar ( intensity ) ;
287280 uniforms . distance = distance ;
288281
289- uniforms . direction . setFromMatrixPosition ( light . matrixWorld ) ;
290- vector3 . setFromMatrixPosition ( light . target . matrixWorld ) ;
291- uniforms . direction . sub ( vector3 ) ;
292- uniforms . direction . transformDirection ( viewMatrix ) ;
293-
294282 uniforms . coneCos = Math . cos ( light . angle ) ;
295283 uniforms . penumbraCos = Math . cos ( light . angle * ( 1 - light . penumbra ) ) ;
296284 uniforms . decay = light . decay ;
@@ -328,24 +316,9 @@ function WebGLLights( extensions, capabilities ) {
328316 // (b) intensity is the brightness of the light
329317 uniforms . color . copy ( color ) . multiplyScalar ( intensity ) ;
330318
331- uniforms . position . setFromMatrixPosition ( light . matrixWorld ) ;
332- uniforms . position . applyMatrix4 ( viewMatrix ) ;
333-
334- // extract local rotation of light to derive width/height half vectors
335- matrix42 . identity ( ) ;
336- matrix4 . copy ( light . matrixWorld ) ;
337- matrix4 . premultiply ( viewMatrix ) ;
338- matrix42 . extractRotation ( matrix4 ) ;
339-
340319 uniforms . halfWidth . set ( light . width * 0.5 , 0.0 , 0.0 ) ;
341320 uniforms . halfHeight . set ( 0.0 , light . height * 0.5 , 0.0 ) ;
342321
343- uniforms . halfWidth . applyMatrix4 ( matrix42 ) ;
344- uniforms . halfHeight . applyMatrix4 ( matrix42 ) ;
345-
346- // TODO (abelnation): RectAreaLight distance?
347- // uniforms.distance = distance;
348-
349322 state . rectArea [ rectAreaLength ] = uniforms ;
350323
351324 rectAreaLength ++ ;
@@ -354,9 +327,6 @@ function WebGLLights( extensions, capabilities ) {
354327
355328 const uniforms = cache . get ( light ) ;
356329
357- uniforms . position . setFromMatrixPosition ( light . matrixWorld ) ;
358- uniforms . position . applyMatrix4 ( viewMatrix ) ;
359-
360330 uniforms . color . copy ( light . color ) . multiplyScalar ( light . intensity ) ;
361331 uniforms . distance = light . distance ;
362332 uniforms . decay = light . decay ;
@@ -390,10 +360,6 @@ function WebGLLights( extensions, capabilities ) {
390360
391361 const uniforms = cache . get ( light ) ;
392362
393- uniforms . direction . setFromMatrixPosition ( light . matrixWorld ) ;
394- uniforms . direction . transformDirection ( viewMatrix ) ;
395- uniforms . direction . normalize ( ) ;
396-
397363 uniforms . skyColor . copy ( light . color ) . multiplyScalar ( intensity ) ;
398364 uniforms . groundColor . copy ( light . groundColor ) . multiplyScalar ( intensity ) ;
399365
@@ -485,8 +451,94 @@ function WebGLLights( extensions, capabilities ) {
485451
486452 }
487453
454+ function setupView ( lights , camera ) {
455+
456+ let directionalLength = 0 ;
457+ let pointLength = 0 ;
458+ let spotLength = 0 ;
459+ let rectAreaLength = 0 ;
460+ let hemiLength = 0 ;
461+
462+ const viewMatrix = camera . matrixWorldInverse ;
463+
464+ for ( let i = 0 , l = lights . length ; i < l ; i ++ ) {
465+
466+ const light = lights [ i ] ;
467+
468+ if ( light . isDirectionalLight ) {
469+
470+ const uniforms = state . directional [ directionalLength ] ;
471+
472+ uniforms . direction . setFromMatrixPosition ( light . matrixWorld ) ;
473+ vector3 . setFromMatrixPosition ( light . target . matrixWorld ) ;
474+ uniforms . direction . sub ( vector3 ) ;
475+ uniforms . direction . transformDirection ( viewMatrix ) ;
476+
477+ directionalLength ++ ;
478+
479+ } else if ( light . isSpotLight ) {
480+
481+ const uniforms = state . spot [ spotLength ] ;
482+
483+ uniforms . position . setFromMatrixPosition ( light . matrixWorld ) ;
484+ uniforms . position . applyMatrix4 ( viewMatrix ) ;
485+
486+ uniforms . direction . setFromMatrixPosition ( light . matrixWorld ) ;
487+ vector3 . setFromMatrixPosition ( light . target . matrixWorld ) ;
488+ uniforms . direction . sub ( vector3 ) ;
489+ uniforms . direction . transformDirection ( viewMatrix ) ;
490+
491+ spotLength ++ ;
492+
493+ } else if ( light . isRectAreaLight ) {
494+
495+ const uniforms = state . rectArea [ rectAreaLength ] ;
496+
497+ uniforms . position . setFromMatrixPosition ( light . matrixWorld ) ;
498+ uniforms . position . applyMatrix4 ( viewMatrix ) ;
499+
500+ // extract local rotation of light to derive width/height half vectors
501+ matrix42 . identity ( ) ;
502+ matrix4 . copy ( light . matrixWorld ) ;
503+ matrix4 . premultiply ( viewMatrix ) ;
504+ matrix42 . extractRotation ( matrix4 ) ;
505+
506+ uniforms . halfWidth . set ( light . width * 0.5 , 0.0 , 0.0 ) ;
507+ uniforms . halfHeight . set ( 0.0 , light . height * 0.5 , 0.0 ) ;
508+
509+ uniforms . halfWidth . applyMatrix4 ( matrix42 ) ;
510+ uniforms . halfHeight . applyMatrix4 ( matrix42 ) ;
511+
512+ rectAreaLength ++ ;
513+
514+ } else if ( light . isPointLight ) {
515+
516+ const uniforms = state . point [ pointLength ] ;
517+
518+ uniforms . position . setFromMatrixPosition ( light . matrixWorld ) ;
519+ uniforms . position . applyMatrix4 ( viewMatrix ) ;
520+
521+ pointLength ++ ;
522+
523+ } else if ( light . isHemisphereLight ) {
524+
525+ const uniforms = state . hemi [ hemiLength ] ;
526+
527+ uniforms . direction . setFromMatrixPosition ( light . matrixWorld ) ;
528+ uniforms . direction . transformDirection ( viewMatrix ) ;
529+ uniforms . direction . normalize ( ) ;
530+
531+ hemiLength ++ ;
532+
533+ }
534+
535+ }
536+
537+ }
538+
488539 return {
489540 setup : setup ,
541+ setupView : setupView ,
490542 state : state
491543 } ;
492544
0 commit comments