Просмотр исходного кода

TSL: Fix `onDispose` listener not being removed in `Sampler` (#31868)

* Move onDispose to class field

* Rename method to be more specific

* Change listener to arrow function

* Fix undefined callback and it points to a wrong sampler
Shota Matsuda 7 месяцев назад
Родитель
Сommit
8c698fb9e4
1 измененных файлов с 25 добавлено и 10 удалено
  1. 25 10
      src/renderers/common/Sampler.js

+ 25 - 10
src/renderers/common/Sampler.js

@@ -21,8 +21,24 @@ class Sampler extends Binding {
 		/**
 		 * The texture the sampler is referring to.
 		 *
+		 * @private
 		 * @type {?Texture}
 		 */
+		this._texture = null;
+
+		/**
+		 * An event listener which is added to {@link texture}'s dispose event.
+		 *
+		 * @private
+		 * @type {Function}
+		 */
+		this._onTextureDispose = () => {
+
+			this.texture = null;
+
+		};
+
+		// Assignment to the texture via a setter must occur after "_onTextureDispose" is initialized.
 		this.texture = texture;
 
 		/**
@@ -60,17 +76,9 @@ class Sampler extends Binding {
 
 		if ( this._texture === value ) return;
 
-		const onDispose = () => {
-
-			this._texture = null;
-			this.generation = null;
-			this.version = 0;
-
-		};
-
 		if ( this._texture ) {
 
-			this._texture.removeEventListener( 'dispose', onDispose );
+			this._texture.removeEventListener( 'dispose', this._onTextureDispose );
 
 		}
 
@@ -81,7 +89,7 @@ class Sampler extends Binding {
 
 		if ( this._texture ) {
 
-			this._texture.addEventListener( 'dispose', onDispose );
+			this._texture.addEventListener( 'dispose', this._onTextureDispose );
 
 		}
 
@@ -128,6 +136,13 @@ class Sampler extends Binding {
 		// TODO: Find better solution, see #31747
 
 		clonedSampler._texture = null;
+
+		clonedSampler._onTextureDispose = () => {
+
+			clonedSampler.texture = null;
+
+		};
+
 		clonedSampler.texture = this.texture;
 
 		return clonedSampler;

粤ICP备19079148号