Browse Source

Avoid creating new arrays in updateMaterials

Garrett Johnson 5 years ago
parent
commit
7caae66a3d
1 changed files with 14 additions and 16 deletions
  1. 14 16
      examples/jsm/csm/CSM.js

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

@@ -213,14 +213,7 @@ export default class CSM {
 		material.defines.CSM_CASCADES = this.cascades;
 
 		const breaksVec2 = [];
-
-		for ( let i = 0; i < this.cascades; i ++ ) {
-
-			let amount = this.breaks[ i ];
-			let prev = this.breaks[ i - 1 ] || 0;
-			breaksVec2.push( new Vector2( prev, amount ) );
-
-		}
+		this.getExtendedBreaks( breaksVec2 );
 
 		const self = this;
 		const far = Math.min(this.camera.far, this.maxFar);
@@ -243,28 +236,33 @@ export default class CSM {
 
 		for ( let i = 0; i < this.materials.length; i ++ ) {
 
-			this.materials[ i ].uniforms.CSM_cascades.value = this.getExtendedBreaks();
-			this.materials[ i ].uniforms.cameraNear.value = this.camera.near;
-			this.materials[ i ].uniforms.shadowFar.value = far;
+			const uniforms = this.materials[ i ].uniforms;
+			this.getExtendedBreaks( uniforms.CSM_cascades.value );
+			uniforms.cameraNear.value = this.camera.near;
+			uniforms.shadowFar.value = far;
 
 		}
 
 	}
 
-	getExtendedBreaks() {
+	getExtendedBreaks( target ) {
+
+		while ( target.length < this.breaks.length ) {
 
-		let breaksVec2 = [];
+			target.push( new Vector2() );
+
+		}
+		target.length = this.breaks.length;
 
 		for ( let i = 0; i < this.cascades; i ++ ) {
 
 			let amount = this.breaks[ i ];
 			let prev = this.breaks[ i - 1 ] || 0;
-			breaksVec2.push( new Vector2( prev, amount ) );
+			target[ i ].x = prev;
+			target[ i ].y = amount;
 
 		}
 
-		return breaksVec2;
-
 	}
 
 	updateFrustums() {