Browse Source

Merge pull request #18723 from gkjohnson/csm-cache-objects

CSM Example: Use cache objects in initCascades
Mr.doob 5 years ago
parent
commit
1d11f3401c
2 changed files with 54 additions and 44 deletions
  1. 4 10
      examples/jsm/csm/CSM.js
  2. 50 34
      examples/jsm/csm/Frustum.js

+ 4 - 10
examples/jsm/csm/CSM.js

@@ -37,6 +37,8 @@ export default class CSM {
 		this.lightFar = data.lightFar || 2000;
 		this.lightMargin = data.lightMargin || 200;
 		this.customSplitsCallback = data.customSplitsCallback;
+		this.mainFrustum = new Frustum();
+		this.frustums = [];
 
 		this.lights = [];
 		this.materials = [];
@@ -74,16 +76,8 @@ export default class CSM {
 
 		const camera = this.camera;
 		camera.updateProjectionMatrix();
-		this.mainFrustum = new Frustum( {
-
-			maxFar: this.maxFar,
-			projectionMatrix: camera.projectionMatrix
-
-		} );
-
-		this.mainFrustum.getViewSpaceVertices();
-
-		this.frustums = this.mainFrustum.split( this.breaks );
+		this.mainFrustum.setFromProjectionMatrix( camera.projectionMatrix, this.maxFar );
+		this.mainFrustum.split( this.breaks, this.frustums );
 
 	}
 

+ 50 - 34
examples/jsm/csm/Frustum.js

@@ -12,47 +12,54 @@ export default class Frustum {
 
 		data = data || {};
 
-		this.projectionMatrix = data.projectionMatrix;
-		this.maxFar = data.maxFar || 10000;
-
 		this.vertices = {
-			near: [],
-			far: []
+			near: [
+				new Vector3(),
+				new Vector3(),
+				new Vector3(),
+				new Vector3()
+			],
+			far: [
+				new Vector3(),
+				new Vector3(),
+				new Vector3(),
+				new Vector3()
+			]
 		};
 
+		if ( data.projectionMatrix !== undefined ) {
+
+			this.setFromProjectionMatrix( data.projectionMatrix, data.maxFar || 10000 );
+
+		}
+
 	}
 
-	getViewSpaceVertices() {
+	setFromProjectionMatrix( projectionMatrix, maxFar ) {
 
-		const maxFar = this.maxFar;
-		const projectionMatrix = this.projectionMatrix;
 		const isOrthographic = projectionMatrix.elements[ 2 * 4 + 3 ] === 0;
 
-		inverseProjectionMatrix.getInverse( this.projectionMatrix );
+		inverseProjectionMatrix.getInverse( projectionMatrix );
 
 		// 3 --- 0  vertices.near/far order
 		// |     |
 		// 2 --- 1
 		// clip space spans from [-1, 1]
 
-		this.vertices.near.push(
-			new Vector3( 1, 1, - 1 ),
-			new Vector3( 1, - 1, - 1 ),
-			new Vector3( - 1, - 1, - 1 ),
-			new Vector3( - 1, 1, - 1 )
-		);
+		this.vertices.near[ 0 ].set( 1, 1, - 1 );
+		this.vertices.near[ 1 ].set( 1, - 1, - 1 );
+		this.vertices.near[ 2 ].set( - 1, - 1, - 1 );
+		this.vertices.near[ 3 ].set( - 1, 1, - 1 );
 		this.vertices.near.forEach( function( v ) {
 
 			v.applyMatrix4( inverseProjectionMatrix );
 
 		} );
 
-		this.vertices.far.push(
-			new Vector3( 1, 1, 1 ),
-			new Vector3( 1, - 1, 1 ),
-			new Vector3( - 1, - 1, 1 ),
-			new Vector3( - 1, 1, 1 )
-		)
+		this.vertices.far[ 0 ].set( 1, 1, 1 );
+		this.vertices.far[ 1 ].set( 1, - 1, 1 );
+		this.vertices.far[ 2 ].set( - 1, - 1, 1 );
+		this.vertices.far[ 3 ].set( - 1, 1, 1 );
 		this.vertices.far.forEach( function( v ) {
 
 			v.applyMatrix4( inverseProjectionMatrix );
@@ -74,23 +81,32 @@ export default class Frustum {
 
 	}
 
-	split( breaks ) {
+	split( breaks, target ) {
 
-		const result = [];
+		while ( breaks.length > target.length ) {
+
+			target.push( new Frustum() );
+
+		}
+		target.length = breaks.length;
 
 		for ( let i = 0; i < breaks.length; i ++ ) {
 
-			const cascade = new Frustum();
+			const cascade = target[ i ];
 
 			if ( i === 0 ) {
 
-				cascade.vertices.near = this.vertices.near;
+				for ( let j = 0; j < 4; j ++ ) {
+
+					cascade.vertices.near[ j ].copy( this.vertices.near[ j ] );
+
+				}
 
 			} else {
 
 				for ( let j = 0; j < 4; j ++ ) {
 
-					cascade.vertices.near.push( new Vector3().lerpVectors( this.vertices.near[ j ], this.vertices.far[ j ], breaks[ i - 1 ] ) );
+					cascade.vertices.near[ j ].lerpVectors( this.vertices.near[ j ], this.vertices.far[ j ], breaks[ i - 1 ] );
 
 				}
 
@@ -98,24 +114,24 @@ export default class Frustum {
 
 			if ( i === breaks - 1 ) {
 
-				cascade.vertices.far = this.vertices.far;
+				for ( let j = 0; j < 4; j ++ ) {
+
+					cascade.vertices.far[ j ].copy( this.vertices.far[ j ] );
+
+				}
 
 			} else {
 
 				for ( let j = 0; j < 4; j ++ ) {
 
-					cascade.vertices.far.push( new Vector3().lerpVectors( this.vertices.near[ j ], this.vertices.far[ j ], breaks[ i ] ) );
+					cascade.vertices.far[ j ].lerpVectors( this.vertices.near[ j ], this.vertices.far[ j ], breaks[ i ] );
 
 				}
 
 			}
 
-			result.push( cascade );
-
 		}
 
-		return result;
-
 	}
 
 	toSpace( cameraMatrix ) {
@@ -127,11 +143,11 @@ export default class Frustum {
 
 			point.set( this.vertices.near[ i ].x, this.vertices.near[ i ].y, this.vertices.near[ i ].z );
 			point.applyMatrix4( cameraMatrix );
-			result.vertices.near.push( point.clone() );
+			result.vertices.near[ i ] = point.clone();
 
 			point.set( this.vertices.far[ i ].x, this.vertices.far[ i ].y, this.vertices.far[ i ].z );
 			point.applyMatrix4( cameraMatrix );
-			result.vertices.far.push( point.clone() );
+			result.vertices.far[ i ] = point.clone();
 
 		}