From 498b56627f6d3fbcc0944a0c8ab6bd4785b8ed1b Mon Sep 17 00:00:00 2001 From: jaxry Date: Wed, 19 Apr 2017 12:46:32 -0600 Subject: [PATCH 1/2] WebGLShadowMap: cleanup --- src/renderers/WebGLRenderer.js | 13 +--------- src/renderers/webgl/WebGLShadowMap.js | 34 +++++++++++++++++---------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/renderers/WebGLRenderer.js b/src/renderers/WebGLRenderer.js index cccfe53b5c8b15..384ce58182f6e2 100644 --- a/src/renderers/WebGLRenderer.js +++ b/src/renderers/WebGLRenderer.js @@ -2416,18 +2416,7 @@ function WebGLRenderer( parameters ) { } _lights.pointShadowMap[ pointLength ] = shadowMap; - - if ( _lights.pointShadowMatrix[ pointLength ] === undefined ) { - - _lights.pointShadowMatrix[ pointLength ] = new Matrix4(); - - } - - // for point lights we set the shadow matrix to be a translation-only matrix - // equal to inverse of the light's position - _vector3.setFromMatrixPosition( light.matrixWorld ).negate(); - _lights.pointShadowMatrix[ pointLength ].identity().setPosition( _vector3 ); - + _lights.pointShadowMatrix[ pointLength ] = light.shadow.matrix; _lights.point[ pointLength ] = uniforms; pointLength ++; diff --git a/src/renderers/webgl/WebGLShadowMap.js b/src/renderers/webgl/WebGLShadowMap.js index e53fe838c4edb6..f68ef2e44d2120 100644 --- a/src/renderers/webgl/WebGLShadowMap.js +++ b/src/renderers/webgl/WebGLShadowMap.js @@ -135,6 +135,7 @@ function WebGLShadowMap( _renderer, _lights, _objects, capabilities ) { } var shadowCamera = shadow.camera; + var shadowMatrix = shadow.matrix; _shadowMapSize.copy( shadow.mapSize ); _shadowMapSize.min( _maxShadowMapSize ); @@ -176,11 +177,31 @@ function WebGLShadowMap( _renderer, _lights, _objects, capabilities ) { _shadowMapSize.x *= 4.0; _shadowMapSize.y *= 2.0; + + // for point lights we set the shadow matrix to be a translation-only matrix + // equal to inverse of the light's position + + var elements = light.matrixWorld.elements; + shadowMatrix.makeTranslation( - elements[ 12 ], - elements[ 13 ], - elements[ 14 ] ); + } else { faceCount = 1; isPointLight = false; + + // compute shadow matrix + + shadowMatrix.set( + 0.5, 0.0, 0.0, 0.5, + 0.0, 0.5, 0.0, 0.5, + 0.0, 0.0, 0.5, 0.5, + 0.0, 0.0, 0.0, 1.0 + ); + + shadowMatrix.multiply( shadowCamera.projectionMatrix ); + shadowMatrix.multiply( shadowCamera.matrixWorldInverse ); + } if ( shadow.map === null ) { @@ -201,7 +222,6 @@ function WebGLShadowMap( _renderer, _lights, _objects, capabilities ) { } var shadowMap = shadow.map; - var shadowMatrix = shadow.matrix; _lightPositionWorld.setFromMatrixPosition( light.matrixWorld ); shadowCamera.position.copy( _lightPositionWorld ); @@ -234,18 +254,6 @@ function WebGLShadowMap( _renderer, _lights, _objects, capabilities ) { shadowCamera.updateMatrixWorld(); shadowCamera.matrixWorldInverse.getInverse( shadowCamera.matrixWorld ); - // compute shadow matrix - - shadowMatrix.set( - 0.5, 0.0, 0.0, 0.5, - 0.0, 0.5, 0.0, 0.5, - 0.0, 0.0, 0.5, 0.5, - 0.0, 0.0, 0.0, 1.0 - ); - - shadowMatrix.multiply( shadowCamera.projectionMatrix ); - shadowMatrix.multiply( shadowCamera.matrixWorldInverse ); - // update camera matrices and frustum _projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse ); From 1adc579644e1bc3c4f25a41c623ad90f90e8b91a Mon Sep 17 00:00:00 2001 From: jaxry Date: Wed, 19 Apr 2017 13:52:56 -0600 Subject: [PATCH 2/2] WebGLShadowMap: fix order of matrix calculations --- src/renderers/webgl/WebGLShadowMap.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/renderers/webgl/WebGLShadowMap.js b/src/renderers/webgl/WebGLShadowMap.js index f68ef2e44d2120..7efb5d49a9f84f 100644 --- a/src/renderers/webgl/WebGLShadowMap.js +++ b/src/renderers/webgl/WebGLShadowMap.js @@ -137,6 +137,9 @@ function WebGLShadowMap( _renderer, _lights, _objects, capabilities ) { var shadowCamera = shadow.camera; var shadowMatrix = shadow.matrix; + _lightPositionWorld.setFromMatrixPosition( light.matrixWorld ); + shadowCamera.position.copy( _lightPositionWorld ); + _shadowMapSize.copy( shadow.mapSize ); _shadowMapSize.min( _maxShadowMapSize ); @@ -181,14 +184,17 @@ function WebGLShadowMap( _renderer, _lights, _objects, capabilities ) { // for point lights we set the shadow matrix to be a translation-only matrix // equal to inverse of the light's position - var elements = light.matrixWorld.elements; - shadowMatrix.makeTranslation( - elements[ 12 ], - elements[ 13 ], - elements[ 14 ] ); + shadowMatrix.makeTranslation( - _lightPositionWorld.x, - _lightPositionWorld.y, - _lightPositionWorld.z ); } else { faceCount = 1; isPointLight = false; + _lookTarget.setFromMatrixPosition( light.target.matrixWorld ); + shadowCamera.lookAt( _lookTarget ); + shadowCamera.updateMatrixWorld(); + shadowCamera.matrixWorldInverse.getInverse( shadowCamera.matrixWorld ); // compute shadow matrix @@ -223,9 +229,6 @@ function WebGLShadowMap( _renderer, _lights, _objects, capabilities ) { var shadowMap = shadow.map; - _lightPositionWorld.setFromMatrixPosition( light.matrixWorld ); - shadowCamera.position.copy( _lightPositionWorld ); - _renderer.setRenderTarget( shadowMap ); _renderer.clear(); @@ -240,20 +243,14 @@ function WebGLShadowMap( _renderer, _lights, _objects, capabilities ) { _lookTarget.add( cubeDirections[ face ] ); shadowCamera.up.copy( cubeUps[ face ] ); shadowCamera.lookAt( _lookTarget ); + shadowCamera.updateMatrixWorld(); + shadowCamera.matrixWorldInverse.getInverse( shadowCamera.matrixWorld ); var vpDimensions = cube2DViewPorts[ face ]; _state.viewport( vpDimensions ); - } else { - - _lookTarget.setFromMatrixPosition( light.target.matrixWorld ); - shadowCamera.lookAt( _lookTarget ); - } - shadowCamera.updateMatrixWorld(); - shadowCamera.matrixWorldInverse.getInverse( shadowCamera.matrixWorld ); - // update camera matrices and frustum _projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );