Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
52 changes: 0 additions & 52 deletions src/lights/PointLightShadow.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 );

}

}
Expand Down
50 changes: 49 additions & 1 deletion src/renderers/webgl/WebGLShadowMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {

Expand Down Expand Up @@ -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();

Expand Down