Browse Source

remove array creation from `getBreaks`

Garrett Johnson 5 years ago
parent
commit
3e937e9dc0
1 changed files with 21 additions and 24 deletions
  1. 21 24
      examples/jsm/csm/CSM.js

+ 21 - 24
examples/jsm/csm/CSM.js

@@ -24,6 +24,8 @@ const _lightSpaceFrustum = new Frustum();
 const _frustum = new Frustum();
 const _frustum = new Frustum();
 const _center = new Vector3();
 const _center = new Vector3();
 const _bbox = new FrustumBoundingBox();
 const _bbox = new FrustumBoundingBox();
+const _uniformArray = [];
+const _logArray = [];
 
 
 export default class CSM {
 export default class CSM {
 
 
@@ -46,6 +48,7 @@ export default class CSM {
 		this.customSplitsCallback = data.customSplitsCallback;
 		this.customSplitsCallback = data.customSplitsCallback;
 		this.mainFrustum = new Frustum();
 		this.mainFrustum = new Frustum();
 		this.frustums = [];
 		this.frustums = [];
+		this.breaks = [];
 
 
 		this.lights = [];
 		this.lights = [];
 		this.materials = [];
 		this.materials = [];
@@ -92,70 +95,64 @@ export default class CSM {
 
 
 		const camera = this.camera;
 		const camera = this.camera;
 		const far = Math.min(camera.far, this.maxFar);
 		const far = Math.min(camera.far, this.maxFar);
-		this.breaks = [];
+		this.breaks.length = 0;
 
 
 		switch ( this.mode ) {
 		switch ( this.mode ) {
 
 
 			case 'uniform':
 			case 'uniform':
-				this.breaks = uniformSplit( this.cascades, camera.near, far );
+				uniformSplit( this.cascades, camera.near, far, this.breaks );
 				break;
 				break;
 			case 'logarithmic':
 			case 'logarithmic':
-				this.breaks = logarithmicSplit( this.cascades, camera.near, far );
+				logarithmicSplit( this.cascades, camera.near, far, this.breaks );
 				break;
 				break;
 			case 'practical':
 			case 'practical':
-				this.breaks = practicalSplit( this.cascades, camera.near, far, 0.5 );
+				practicalSplit( this.cascades, camera.near, far, 0.5, this.breaks );
 				break;
 				break;
 			case 'custom':
 			case 'custom':
 				if ( this.customSplitsCallback === undefined ) console.error( 'CSM: Custom split scheme callback not defined.' );
 				if ( this.customSplitsCallback === undefined ) console.error( 'CSM: Custom split scheme callback not defined.' );
-				this.breaks = this.customSplitsCallback( this.cascades, camera.near, far );
+				this.customSplitsCallback( this.cascades, camera.near, far, this.breaks );
 				break;
 				break;
 
 
 		}
 		}
 
 
-		function uniformSplit( amount, near, far ) {
-
-			const r = [];
+		function uniformSplit( amount, near, far, target ) {
 
 
 			for ( let i = 1; i < amount; i ++ ) {
 			for ( let i = 1; i < amount; i ++ ) {
 
 
-				r.push( ( near + ( far - near ) * i / amount ) / far );
+				target.push( ( near + ( far - near ) * i / amount ) / far );
 
 
 			}
 			}
 
 
-			r.push( 1 );
-			return r;
+			target.push( 1 );
 
 
 		}
 		}
 
 
-		function logarithmicSplit( amount, near, far ) {
-
-			const r = [];
+		function logarithmicSplit( amount, near, far, target ) {
 
 
 			for ( let i = 1; i < amount; i ++ ) {
 			for ( let i = 1; i < amount; i ++ ) {
 
 
-				r.push( ( near * ( far / near ) ** ( i / amount ) ) / far );
+				target.push( ( near * ( far / near ) ** ( i / amount ) ) / far );
 
 
 			}
 			}
 
 
-			r.push( 1 );
-			return r;
+			target.push( 1 );
 
 
 		}
 		}
 
 
-		function practicalSplit( amount, near, far, lambda ) {
+		function practicalSplit( amount, near, far, lambda, target ) {
 
 
-			const log = logarithmicSplit( amount, near, far );
-			const uni = uniformSplit( amount, near, far );
-			const r = [];
+			_uniformArray.length = 0;
+			_logArray.length = 0;
+			const log = logarithmicSplit( amount, near, far, _logArray );
+			const uni = uniformSplit( amount, near, far, _uniformArray );
 
 
 			for ( let i = 1; i < amount; i ++ ) {
 			for ( let i = 1; i < amount; i ++ ) {
 
 
-				r.push( MathUtils.lerp( uni[ i - 1 ], log[ i - 1 ], lambda ) );
+				target.push( MathUtils.lerp( _uniformArray[ i - 1 ], _logArray[ i - 1 ], lambda ) );
 
 
 			}
 			}
 
 
-			r.push( 1 );
-			return r;
+			target.push( 1 );
 
 
 		}
 		}