diff --git a/src/lights/PointLightShadow.js b/src/lights/PointLightShadow.js index 6518b48c4b2e16..6748fc91249f13 100644 --- a/src/lights/PointLightShadow.js +++ b/src/lights/PointLightShadow.js @@ -1,11 +1,5 @@ import { LightShadow } from './LightShadow.js'; import { PerspectiveCamera } from '../cameras/PerspectiveCamera.js'; -import { Matrix4 } from '../math/Matrix4.js'; -import { Vector3 } from '../math/Vector3.js'; - -const _projScreenMatrix = /*@__PURE__*/ new Matrix4(); -const _lightPositionWorld = /*@__PURE__*/ new Vector3(); -const _lookTarget = /*@__PURE__*/ new Vector3(); /** * Represents the shadow configuration of point lights. @@ -30,52 +24,6 @@ class PointLightShadow extends LightShadow { */ this.isPointLightShadow = true; - this._cubeDirections = [ - new Vector3( 1, 0, 0 ), new Vector3( - 1, 0, 0 ), new Vector3( 0, 1, 0 ), - new Vector3( 0, - 1, 0 ), new Vector3( 0, 0, 1 ), new Vector3( 0, 0, - 1 ) - ]; - - this._cubeUps = [ - new Vector3( 0, - 1, 0 ), new Vector3( 0, - 1, 0 ), new Vector3( 0, 0, 1 ), - new Vector3( 0, 0, - 1 ), new Vector3( 0, - 1, 0 ), new Vector3( 0, - 1, 0 ) - ]; - - } - - /** - * Update the matrices for the camera and shadow, used internally by the renderer. - * - * @param {Light} light - The light for which the shadow is being rendered. - * @param {number} [faceIndex=0] - The cube face index (0-5). - */ - updateMatrices( light, faceIndex = 0 ) { - - const camera = this.camera; - const shadowMatrix = this.matrix; - - const far = light.distance || camera.far; - - if ( far !== camera.far ) { - - camera.far = far; - camera.updateProjectionMatrix(); - - } - - _lightPositionWorld.setFromMatrixPosition( light.matrixWorld ); - camera.position.copy( _lightPositionWorld ); - - _lookTarget.copy( camera.position ); - _lookTarget.add( this._cubeDirections[ faceIndex ] ); - camera.up.copy( this._cubeUps[ faceIndex ] ); - camera.lookAt( _lookTarget ); - camera.updateMatrixWorld(); - - shadowMatrix.makeTranslation( - _lightPositionWorld.x, - _lightPositionWorld.y, - _lightPositionWorld.z ); - - _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse ); - this._frustum.setFromProjectionMatrix( _projScreenMatrix, camera.coordinateSystem, camera.reversedDepth ); - } } diff --git a/src/renderers/webgl/WebGLShadowMap.js b/src/renderers/webgl/WebGLShadowMap.js index b82720085af56f..9020cdee054501 100644 --- a/src/renderers/webgl/WebGLShadowMap.js +++ b/src/renderers/webgl/WebGLShadowMap.js @@ -9,12 +9,28 @@ import { BufferGeometry } from '../../core/BufferGeometry.js'; import { Mesh } from '../../objects/Mesh.js'; import { Vector4 } from '../../math/Vector4.js'; import { Vector2 } from '../../math/Vector2.js'; +import { Matrix4 } from '../../math/Matrix4.js'; import { Frustum } from '../../math/Frustum.js'; import { DepthTexture } from '../../textures/DepthTexture.js'; import { CubeDepthTexture } from '../../textures/CubeDepthTexture.js'; import * as vsm from '../shaders/ShaderLib/vsm.glsl.js'; import { warn } from '../../utils.js'; +import { Vector3 } from '../../math/Vector3.js'; + +const _cubeDirections = [ + /*@__PURE__*/ new Vector3( 1, 0, 0 ), /*@__PURE__*/ new Vector3( - 1, 0, 0 ), /*@__PURE__*/ new Vector3( 0, 1, 0 ), + /*@__PURE__*/ new Vector3( 0, - 1, 0 ), /*@__PURE__*/ new Vector3( 0, 0, 1 ), /*@__PURE__*/ new Vector3( 0, 0, - 1 ) +]; + +const _cubeUps = [ + /*@__PURE__*/ new Vector3( 0, - 1, 0 ), /*@__PURE__*/ new Vector3( 0, - 1, 0 ), /*@__PURE__*/ new Vector3( 0, 0, 1 ), + /*@__PURE__*/ new Vector3( 0, 0, - 1 ), /*@__PURE__*/ new Vector3( 0, - 1, 0 ), /*@__PURE__*/ new Vector3( 0, - 1, 0 ) +]; + +const _projScreenMatrix = /*@__PURE__*/ new Matrix4(); +const _lightPositionWorld = /*@__PURE__*/ new Vector3(); +const _lookTarget = /*@__PURE__*/ new Vector3(); function WebGLShadowMap( renderer, objects, capabilities ) { @@ -295,7 +311,39 @@ function WebGLShadowMap( renderer, objects, capabilities ) { } - shadow.updateMatrices( light, face ); + if ( light.isPointLight ) { + + const camera = shadow.camera; + const shadowMatrix = shadow.matrix; + + const far = light.distance || camera.far; + + if ( far !== camera.far ) { + + camera.far = far; + camera.updateProjectionMatrix(); + + } + + _lightPositionWorld.setFromMatrixPosition( light.matrixWorld ); + camera.position.copy( _lightPositionWorld ); + + _lookTarget.copy( camera.position ); + _lookTarget.add( _cubeDirections[ face ] ); + camera.up.copy( _cubeUps[ face ] ); + camera.lookAt( _lookTarget ); + camera.updateMatrixWorld(); + + shadowMatrix.makeTranslation( - _lightPositionWorld.x, - _lightPositionWorld.y, - _lightPositionWorld.z ); + + _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse ); + shadow._frustum.setFromProjectionMatrix( _projScreenMatrix, camera.coordinateSystem, camera.reversedDepth ); + + } else { + + shadow.updateMatrices( light ); + + } _frustum = shadow.getFrustum();