Browse Source

Started WebGLTextures.

Mr.doob 10 years ago
parent
commit
48bc32d60a
2 changed files with 46 additions and 24 deletions
  1. 5 24
      src/renderers/WebGLRenderer.js
  2. 41 0
      src/renderers/webgl/WebGLTextures.js

+ 5 - 24
src/renderers/WebGLRenderer.js

@@ -254,6 +254,8 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 	}
 
+	var textures = new THREE.WebGLTextures( _gl );
+
 	//
 
 	var glClearColor = function ( r, g, b, a ) {
@@ -887,17 +889,6 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 			delete texture.image.__webglTextureCube;
 
-		} else {
-
-			// 2D texture
-
-			if ( texture.__webglInit === undefined ) return;
-
-			_gl.deleteTexture( texture.__webglTexture );
-
-			delete texture.__webglTexture;
-			delete texture.__webglInit;
-
 		}
 
 	};
@@ -5684,19 +5675,9 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 	this.uploadTexture = function ( texture ) {
 
-		if ( texture.__webglInit === undefined ) {
-
-			texture.__webglInit = true;
-
-			texture.addEventListener( 'dispose', onTextureDispose );
-
-			texture.__webglTexture = _gl.createTexture();
-
-			_this.info.memory.textures ++;
-
-		}
+		var webglTexture = textures.get( texture );
 
-		_gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
+		_gl.bindTexture( _gl.TEXTURE_2D, webglTexture );
 
 		_gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
 		_gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
@@ -5805,7 +5786,7 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 		} else {
 
-			_gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
+			_gl.bindTexture( _gl.TEXTURE_2D, textures.get( texture ) );
 
 		}
 

+ 41 - 0
src/renderers/webgl/WebGLTextures.js

@@ -0,0 +1,41 @@
+/**
+* @author mrdoob / http://mrdoob.com/
+*/
+
+THREE.WebGLTextures = function ( gl ) {
+
+	var textures = {};
+
+	this.get = function ( texture ) {
+
+		if ( textures[ texture.id ] !== undefined ) {
+
+			return textures[ texture.id ];
+
+		}
+
+		return this.create( texture );
+
+	};
+
+	this.create = function ( texture ) {
+
+		texture.addEventListener( 'dispose', this.delete );
+
+		textures[ texture.id ] = gl.createTexture();
+
+		return textures[ texture.id ];
+
+	};
+
+	this.delete = function ( texture ) {
+
+		texture.removeEventListener( 'dispose', this.delete );
+
+		gl.deleteTexture( textures[ texture.id ] );
+
+		delete textures[ texture.id ];
+
+	};
+
+};