|
@@ -12,12 +12,19 @@ import {
|
|
|
Object3D,
|
|
|
BufferGeometry,
|
|
|
BufferAttribute,
|
|
|
- Line
|
|
|
+ Line,
|
|
|
+ Matrix4
|
|
|
} from '../../../build/three.module.js';
|
|
|
import Frustum from './Frustum.js';
|
|
|
import FrustumBoundingBox from './FrustumBoundingBox.js';
|
|
|
import Shader from './Shader.js';
|
|
|
|
|
|
+const _cameraToLightMatrix = new Matrix4();
|
|
|
+const _lightSpaceFrustum = new Frustum();
|
|
|
+const _frustum = new Frustum();
|
|
|
+const _center = new Vector3();
|
|
|
+const _bbox = new FrustumBoundingBox();
|
|
|
+
|
|
|
export default class CSM {
|
|
|
|
|
|
constructor( data ) {
|
|
@@ -158,28 +165,28 @@ export default class CSM {
|
|
|
|
|
|
for ( let i = 0; i < this.frustums.length; i ++ ) {
|
|
|
|
|
|
- const worldSpaceFrustum = this.frustums[ i ].toSpace( cameraMatrix );
|
|
|
const light = this.lights[ i ];
|
|
|
- const lightSpaceFrustum = worldSpaceFrustum.toSpace( light.shadow.camera.matrixWorldInverse );
|
|
|
+ _cameraToLightMatrix.multiplyMatrices( light.shadow.camera.matrixWorldInverse, cameraMatrix );
|
|
|
+ this.frustums[ i ].toSpace( _cameraToLightMatrix, _lightSpaceFrustum );
|
|
|
|
|
|
light.shadow.camera.updateMatrixWorld( true );
|
|
|
|
|
|
- const bbox = new FrustumBoundingBox().fromFrustum( lightSpaceFrustum );
|
|
|
- bbox.getSize();
|
|
|
- bbox.getCenter( this.lightMargin );
|
|
|
+ _bbox.fromFrustum( _lightSpaceFrustum );
|
|
|
+ _bbox.getSize();
|
|
|
+ _bbox.getCenter( this.lightMargin );
|
|
|
|
|
|
- const squaredBBWidth = Math.max( bbox.size.x, bbox.size.y );
|
|
|
+ const squaredBBWidth = Math.max( _bbox.size.x, _bbox.size.y );
|
|
|
|
|
|
- let center = new Vector3( bbox.center.x, bbox.center.y, bbox.center.z );
|
|
|
- center.applyMatrix4( light.shadow.camera.matrixWorld );
|
|
|
+ _center.copy( _bbox.center );
|
|
|
+ _center.applyMatrix4( light.shadow.camera.matrixWorld );
|
|
|
|
|
|
light.shadow.camera.left = - squaredBBWidth / 2;
|
|
|
light.shadow.camera.right = squaredBBWidth / 2;
|
|
|
light.shadow.camera.top = squaredBBWidth / 2;
|
|
|
light.shadow.camera.bottom = - squaredBBWidth / 2;
|
|
|
|
|
|
- light.position.copy( center );
|
|
|
- light.target.position.copy( center );
|
|
|
+ light.position.copy( _center );
|
|
|
+ light.target.position.copy( _center );
|
|
|
|
|
|
light.target.position.x += this.lightDirection.x;
|
|
|
light.target.position.y += this.lightDirection.y;
|
|
@@ -270,14 +277,13 @@ export default class CSM {
|
|
|
|
|
|
helper( cameraMatrix ) {
|
|
|
|
|
|
- let frustum;
|
|
|
let geometry, vertices;
|
|
|
const material = new LineBasicMaterial( { color: 0xffffff } );
|
|
|
const object = new Object3D();
|
|
|
|
|
|
for ( let i = 0; i < this.frustums.length; i ++ ) {
|
|
|
|
|
|
- frustum = this.frustums[ i ].toSpace( cameraMatrix );
|
|
|
+ this.frustums[ i ].toSpace( cameraMatrix, _frustum );
|
|
|
|
|
|
geometry = new BufferGeometry();
|
|
|
vertices = [];
|
|
@@ -285,7 +291,7 @@ export default class CSM {
|
|
|
|
|
|
for ( let i = 0; i < 5; i ++ ) {
|
|
|
|
|
|
- const point = frustum.vertices.near[ i === 4 ? 0 : i ];
|
|
|
+ const point = _frustum.vertices.near[ i === 4 ? 0 : i ];
|
|
|
vertices.push( point.x, point.y, point.z );
|
|
|
|
|
|
}
|
|
@@ -299,7 +305,7 @@ export default class CSM {
|
|
|
|
|
|
for ( let i = 0; i < 5; i ++ ) {
|
|
|
|
|
|
- const point = frustum.vertices.far[ i === 4 ? 0 : i ];
|
|
|
+ const point = _frustum.vertices.far[ i === 4 ? 0 : i ];
|
|
|
vertices.push( point.x, point.y, point.z );
|
|
|
|
|
|
}
|
|
@@ -313,8 +319,8 @@ export default class CSM {
|
|
|
geometry = new BufferGeometry();
|
|
|
vertices = [];
|
|
|
|
|
|
- const near = frustum.vertices.near[ i ];
|
|
|
- const far = frustum.vertices.far[ i ];
|
|
|
+ const near = _frustum.vertices.near[ i ];
|
|
|
+ const far = _frustum.vertices.far[ i ];
|
|
|
|
|
|
vertices.push( near.x, near.y, near.z );
|
|
|
vertices.push( far.x, far.y, far.z );
|