Browse Source

Merge pull request #18768 from gkjohnson/csm-fixes

CSM: Miscellaneous Fixes
Mr.doob 5 năm trước cách đây
mục cha
commit
99d26d1d69
2 tập tin đã thay đổi với 38 bổ sung18 xóa
  1. 37 17
      examples/jsm/csm/CSM.js
  2. 1 1
      examples/webgl_shadowmap_csm.html

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

@@ -53,8 +53,7 @@ export default class CSM {
 		this.breaks = [];
 
 		this.lights = [];
-		this.shaders = [];
-		this.materials = [];
+		this.shaders = new Map();
 		this.createLights();
 
 		this.getBreaks();
@@ -236,41 +235,41 @@ export default class CSM {
 		}
 
 		const breaksVec2 = [];
-		this.getExtendedBreaks( breaksVec2 );
-
 		const self = this;
-		const far = Math.min( this.camera.far, this.maxFar );
+		const shaders = this.shaders;
 
 		material.onBeforeCompile = function ( shader ) {
 
+			const far = Math.min( self.camera.far, self.maxFar );
+			self.getExtendedBreaks( breaksVec2 );
+
 			shader.uniforms.CSM_cascades = { value: breaksVec2 };
 			shader.uniforms.cameraNear = { value: self.camera.near };
 			shader.uniforms.shadowFar = { value: far };
 
-			self.shaders.push( shader );
+			shaders.set( material, shader );
 
 		};
-		this.materials.push( material );
+		shaders.set( material, null );
 
 	}
 
 	updateUniforms() {
 
 		const far = Math.min( this.camera.far, this.maxFar );
+		const shaders = this.shaders;
 
-		for ( let i = 0; i < this.shaders.length; i ++ ) {
+		shaders.forEach( function ( shader, material ) {
 
-			const shader = this.shaders[ i ];
-			const uniforms = shader.uniforms;
-			this.getExtendedBreaks( uniforms.CSM_cascades.value );
-			uniforms.cameraNear.value = this.camera.near;
-			uniforms.shadowFar.value = far;
+			if ( shader !== null ) {
 
-		}
+				const uniforms = shader.uniforms;
+				this.getExtendedBreaks( uniforms.CSM_cascades.value );
+				uniforms.cameraNear.value = this.camera.near;
+				uniforms.shadowFar.value = far;
 
-		for ( let i = 0; i < this.materials.length; i ++ ) {
+			}
 
-			const material = this.materials[ i ];
 			if ( ! this.fade && 'CSM_FADE' in material.defines ) {
 
 				delete material.defines.CSM_FADE;
@@ -283,7 +282,7 @@ export default class CSM {
 
 			}
 
-		}
+		}, this );
 
 	}
 
@@ -387,4 +386,25 @@ export default class CSM {
 
 	}
 
+	dispose() {
+
+		const shaders = this.shaders;
+		shaders.forEach( function ( shader, material ) {
+
+			delete material.onBeforeCompile;
+			delete material.defines.USE_CSM;
+			delete material.defines.CSM_CASCADES;
+			delete material.defines.CSM_FADE;
+
+			delete shader.uniforms.CSM_cascades;
+			delete shader.uniforms.cameraNear;
+			delete shader.uniforms.shadowFar;
+
+			material.needsUpdate = true;
+
+		} );
+		shaders.clear();
+
+	}
+
 }

+ 1 - 1
examples/webgl_shadowmap_csm.html

@@ -140,7 +140,7 @@
 				gui.add( params, 'fade' ).onChange( function ( value ) { 
 
 					csm.fade = value;
-					csm.updateUniforms();
+					csm.updateFrustums();
 
 				} );