ソースを参照

Remove newly created objects in `update`

Garrett Johnson 5 年 前
コミット
ca3d63fe94
3 ファイル変更25 行追加24 行削除
  1. 17 11
      examples/jsm/csm/CSM.js
  2. 7 12
      examples/jsm/csm/Frustum.js
  3. 1 1
      examples/webgl_shadowmap_csm.html

+ 17 - 11
examples/jsm/csm/CSM.js

@@ -12,12 +12,18 @@ import {
 	Object3D,
 	Object3D,
 	BufferGeometry,
 	BufferGeometry,
 	BufferAttribute,
 	BufferAttribute,
-	Line
+	Line,
+	Matrix4
 } from '../../../build/three.module.js';
 } from '../../../build/three.module.js';
 import Frustum from './Frustum.js';
 import Frustum from './Frustum.js';
 import FrustumBoundingBox from './FrustumBoundingBox.js';
 import FrustumBoundingBox from './FrustumBoundingBox.js';
 import Shader from './Shader.js';
 import Shader from './Shader.js';
 
 
+const _cameraToLightMatrix = new Matrix4();
+const _frustum = new Frustum();
+const _center = new Vector3();
+const _bbox = new FrustumBoundingBox();
+
 export default class CSM {
 export default class CSM {
 
 
 	constructor( data ) {
 	constructor( data ) {
@@ -158,28 +164,28 @@ export default class CSM {
 
 
 		for ( let i = 0; i < this.frustums.length; i ++ ) {
 		for ( let i = 0; i < this.frustums.length; i ++ ) {
 
 
-			const worldSpaceFrustum = this.frustums[ i ].toSpace( cameraMatrix );
 			const light = this.lights[ i ];
 			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, _frustum );
 
 
 			light.shadow.camera.updateMatrixWorld( true );
 			light.shadow.camera.updateMatrixWorld( true );
 
 
-			const bbox = new FrustumBoundingBox().fromFrustum( lightSpaceFrustum );
-			bbox.getSize();
-			bbox.getCenter( this.lightMargin );
+			_bbox.fromFrustum( _frustum );
+			_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.left = - squaredBBWidth / 2;
 			light.shadow.camera.right = squaredBBWidth / 2;
 			light.shadow.camera.right = squaredBBWidth / 2;
 			light.shadow.camera.top = squaredBBWidth / 2;
 			light.shadow.camera.top = squaredBBWidth / 2;
 			light.shadow.camera.bottom = - 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.x += this.lightDirection.x;
 			light.target.position.y += this.lightDirection.y;
 			light.target.position.y += this.lightDirection.y;

+ 7 - 12
examples/jsm/csm/Frustum.js

@@ -134,25 +134,20 @@ export default class Frustum {
 
 
 	}
 	}
 
 
-	toSpace( cameraMatrix ) {
-
-		const result = new Frustum();
-		const point = new Vector3();
+	toSpace( cameraMatrix, target ) {
 
 
 		for ( var i = 0; i < 4; i ++ ) {
 		for ( var i = 0; i < 4; i ++ ) {
 
 
-			point.set( this.vertices.near[ i ].x, this.vertices.near[ i ].y, this.vertices.near[ i ].z );
-			point.applyMatrix4( cameraMatrix );
-			result.vertices.near[ i ] = point.clone();
+			target.vertices.near[ i ]
+				.copy( this.vertices.near[ i ] )
+				.applyMatrix4( cameraMatrix );
 
 
-			point.set( this.vertices.far[ i ].x, this.vertices.far[ i ].y, this.vertices.far[ i ].z );
-			point.applyMatrix4( cameraMatrix );
-			result.vertices.far[ i ] = point.clone();
+			target.vertices.far[ i ]
+				.copy( this.vertices.far[ i ] )
+				.applyMatrix4( cameraMatrix );
 
 
 		}
 		}
 
 
-		return result;
-
 	}
 	}
 
 
 }
 }

+ 1 - 1
examples/webgl_shadowmap_csm.html

@@ -214,7 +214,7 @@
 
 
 				requestAnimationFrame( animate );
 				requestAnimationFrame( animate );
 
 
-				csm.update( camera.matrix );
+				csm.update( camera.matrixWorld );
 				controls.update();
 				controls.update();
 
 
 				if ( params.orthographic ) {
 				if ( params.orthographic ) {