Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebGLShadowMap: cleanup and optimization #11198

Merged
merged 2 commits into from
Apr 19, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern resembles the pattern used by spotlights and directinal lights a few blocks above.

_lights.point[ pointLength ] = uniforms;

pointLength ++;
Expand Down
34 changes: 21 additions & 13 deletions src/renderers/webgl/WebGLShadowMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 ] );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of creating shadowMatrix declaratively as before, I create it directly. Avoids an intermediate Vector3 but loses clarity. Is an intermediate Vector3 preferable after all?


} 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 ) {
Expand All @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down