Pārlūkot izejas kodu

Avoid making new frustums

Garrett Johnson 5 gadi atpakaļ
vecāks
revīzija
dfe8dc9c53
2 mainītis faili ar 20 papildinājumiem un 24 dzēšanām
  1. 4 10
      examples/jsm/csm/CSM.js
  2. 16 14
      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 );
 
 	}
 

+ 16 - 14
examples/jsm/csm/Frustum.js

@@ -12,9 +12,6 @@ export default class Frustum {
 
 		data = data || {};
 
-		this.projectionMatrix = data.projectionMatrix;
-		this.maxFar = data.maxFar || 10000;
-
 		this.vertices = {
 			near: [
 				new Vector3(),
@@ -30,15 +27,19 @@ export default class Frustum {
 			]
 		};
 
+		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
 		// |     |
@@ -80,13 +81,18 @@ export default class Frustum {
 
 	}
 
-	split( breaks ) {
+	split( breaks, target ) {
+
+		while ( breaks.length > target.length ) {
 
-		const result = [];
+			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 ) {
 
@@ -124,12 +130,8 @@ export default class Frustum {
 
 			}
 
-			result.push( cascade );
-
 		}
 
-		return result;
-
 	}
 
 	toSpace( cameraMatrix ) {