2
0
Эх сурвалжийг харах

Moving texture.needsUpdate to events.
This approach solves the problem of parallel renderers overwriting needsUpdate.

Mr.doob 11 жил өмнө
parent
commit
769b0195e8

+ 26 - 14
src/renderers/CanvasRenderer.js

@@ -793,26 +793,38 @@ THREE.CanvasRenderer = function ( parameters ) {
 
 	}
 
+	function onTextureUpdate ( event ) {
+
+		textureToPattern( event.target );
+
+	}
+
+	function textureToPattern( texture ) {
+
+		var repeatX = texture.wrapS === THREE.RepeatWrapping;
+		var repeatY = texture.wrapT === THREE.RepeatWrapping;
+
+		_patterns[ texture.id ] = _context.createPattern(
+			texture.image, repeatX === true && repeatY === true
+				? 'repeat'
+				: repeatX === true && repeatY === false
+					? 'repeat-x'
+					: repeatX === false && repeatY === true
+						? 'repeat-y'
+						: 'no-repeat'
+		);
+
+	}
+
 	function patternPath( x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2, texture ) {
 
 		if ( texture instanceof THREE.DataTexture || texture.image === undefined || texture.image.width === 0 ) return;
 
-		if ( texture.needsUpdate === true ) {
-
-			var repeatX = texture.wrapS === THREE.RepeatWrapping;
-			var repeatY = texture.wrapT === THREE.RepeatWrapping;
+		if ( texture.hasEventListener( 'update', onTextureUpdate ) === false ) {
 
-			_patterns[ texture.id ] = _context.createPattern(
-				texture.image, repeatX === true && repeatY === true
-					? 'repeat'
-					: repeatX === true && repeatY === false
-						? 'repeat-x'
-						: repeatX === false && repeatY === true
-							? 'repeat-y'
-							: 'no-repeat'
-			);
+			textureToPattern( texture );
 
-			texture.needsUpdate = false;
+			texture.addEventListener( 'update', onTextureUpdate );
 
 		}
 

+ 21 - 1
src/textures/Texture.js

@@ -35,7 +35,7 @@ THREE.Texture = function ( image, mapping, wrapS, wrapT, magFilter, minFilter, f
 	this.flipY = true;
 	this.unpackAlignment = 4; // valid values: 1, 2, 4, 8 (see http://www.khronos.org/opengles/sdk/docs/man/xhtml/glPixelStorei.xml)
 
-	this.needsUpdate = false;
+	this._needsUpdate = false;
 	this.onUpdate = null;
 
 };
@@ -44,6 +44,20 @@ THREE.Texture.prototype = {
 
 	constructor: THREE.Texture,
 
+	get needsUpdate () {
+
+		return this._needsUpdate;
+
+	},
+
+	set needsUpdate ( value ) {
+
+		if ( value === true ) this.update();
+
+		this._needsUpdate = value;
+
+	},
+
 	clone: function ( texture ) {
 
 		if ( texture === undefined ) texture = new THREE.Texture();
@@ -76,6 +90,12 @@ THREE.Texture.prototype = {
 
 	},
 
+	update: function () {
+
+		this.dispatchEvent( { type: 'update' } );
+
+	},
+
 	dispose: function () {
 
 		this.dispatchEvent( { type: 'dispose' } );