浏览代码

Added Texture.DEFAULT_IMAGE. Different solution for #4559.

Mr.doob 11 年之前
父节点
当前提交
dd089570da

+ 1 - 1
examples/js/loaders/ColladaLoader.js

@@ -3491,7 +3491,7 @@ THREE.ColladaLoader = function () {
 
 
 									} else {
 									} else {
 
 
-										texture = new THREE.Texture( new Image() );
+										texture = new THREE.Texture();
 										loader = new THREE.ImageLoader();
 										loader = new THREE.ImageLoader();
 										loader.load( url, function ( image ) {
 										loader.load( url, function ( image ) {
 
 

+ 1 - 3
examples/js/loaders/MTLLoader.js

@@ -369,12 +369,10 @@ THREE.MTLLoader.MaterialCreator.prototype = {
 		if ( loader !== null ) {
 		if ( loader !== null ) {
 
 
 			texture = loader.load( url, onLoad );
 			texture = loader.load( url, onLoad );
-			texture.mapping = mapping;
 
 
 		} else {
 		} else {
 
 
-			texture = new THREE.Texture( new Image() );
-			texture.mapping = mapping;
+			texture = new THREE.Texture();
 
 
 			loader = new THREE.ImageLoader();
 			loader = new THREE.ImageLoader();
 			loader.crossOrigin = this.crossOrigin;
 			loader.crossOrigin = this.crossOrigin;

+ 1 - 1
examples/js/loaders/SceneLoader.js

@@ -983,7 +983,7 @@ THREE.SceneLoader.prototype = {
 
 
 				} else {
 				} else {
 
 
-					texture = new THREE.Texture( new Image() );
+					texture = new THREE.Texture();
 					loader = new THREE.ImageLoader();
 					loader = new THREE.ImageLoader();
 					loader.load( fullUrl, function ( image ) {
 					loader.load( fullUrl, function ( image ) {
 
 

+ 3 - 6
examples/webgl_animation_cloth.html

@@ -231,15 +231,12 @@
 
 
 				// ground
 				// ground
 
 
-				var initColor = new THREE.Color( 0x497f13 );
-				var initTexture = THREE.ImageUtils.generateDataTexture( 1, 1, initColor );
-
-				var groundMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, map: initTexture } );
-
-				var groundTexture = THREE.ImageUtils.loadTexture( "textures/terrain/grasslight-big.jpg", undefined, function() { groundMaterial.map = groundTexture } );
+				var groundTexture = THREE.ImageUtils.loadTexture( "textures/terrain/grasslight-big.jpg" );
 				groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
 				groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
 				groundTexture.repeat.set( 25, 25 );
 				groundTexture.repeat.set( 25, 25 );
 				groundTexture.anisotropy = 16;
 				groundTexture.anisotropy = 16;
+				
+				var groundMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, map: groundTexture } );
 
 
 				var mesh = new THREE.Mesh( new THREE.PlaneGeometry( 20000, 20000 ), groundMaterial );
 				var mesh = new THREE.Mesh( new THREE.PlaneGeometry( 20000, 20000 ), groundMaterial );
 				mesh.position.y = -250;
 				mesh.position.y = -250;

+ 4 - 3
src/extras/ImageUtils.js

@@ -13,10 +13,12 @@ THREE.ImageUtils = {
 		var loader = new THREE.ImageLoader();
 		var loader = new THREE.ImageLoader();
 		loader.crossOrigin = this.crossOrigin;
 		loader.crossOrigin = this.crossOrigin;
 
 
-		var texture = new THREE.Texture( undefined, mapping );
+		var texture = new THREE.Texture();
+		texture.mapping = mapping;
 
 
-		var image = loader.load( url, function () {
+		loader.load( url, function ( image ) {
 
 
+			texture.image = image;
 			texture.needsUpdate = true;
 			texture.needsUpdate = true;
 
 
 			if ( onLoad ) onLoad( texture );
 			if ( onLoad ) onLoad( texture );
@@ -27,7 +29,6 @@ THREE.ImageUtils = {
 
 
 		} );
 		} );
 
 
-		texture.image = image;
 		texture.sourceFile = url;
 		texture.sourceFile = url;
 
 
 		return texture;
 		return texture;

+ 1 - 10
src/extras/renderers/plugins/SpritePlugin.js

@@ -66,16 +66,7 @@ THREE.SpritePlugin = function () {
 			alphaTest:			_gl.getUniformLocation( program, 'alphaTest' )
 			alphaTest:			_gl.getUniformLocation( program, 'alphaTest' )
 		};
 		};
 
 
-		var canvas = document.createElement( 'canvas' );
-		canvas.width = 8;
-		canvas.height = 8;
-
-		var context = canvas.getContext( '2d' );
-		context.fillStyle = '#ffffff';
-		context.fillRect( 0, 0, canvas.width, canvas.height );
-
-		_texture = new THREE.Texture( canvas );
-		_texture.needsUpdate = true;
+		_texture = new THREE.Texture();
 
 
 	};
 	};
 
 

+ 1 - 1
src/loaders/Loader.js

@@ -126,7 +126,7 @@ THREE.Loader.prototype = {
 
 
 			} else {
 			} else {
 
 
-				texture = new THREE.Texture( document.createElement( 'canvas' ) );
+				texture = new THREE.Texture();
 
 
 				loader = scope.imageLoader;
 				loader = scope.imageLoader;
 				loader.crossOrigin = scope.crossOrigin;
 				loader.crossOrigin = scope.crossOrigin;

+ 19 - 3
src/textures/Texture.js

@@ -11,10 +11,10 @@ THREE.Texture = function ( image, mapping, wrapS, wrapT, magFilter, minFilter, f
 
 
 	this.name = '';
 	this.name = '';
 
 
-	this.image = image;
+	this.image = image !== undefined ? image : THREE.Texture.DEFAULT_IMAGE;
 	this.mipmaps = [];
 	this.mipmaps = [];
 
 
-	this.mapping = mapping !== undefined ? mapping : new THREE.UVMapping();
+	this.mapping = mapping !== undefined ? mapping : THREE.Texture.DEFAULT_MAPPING;
 
 
 	this.wrapS = wrapS !== undefined ? wrapS : THREE.ClampToEdgeWrapping;
 	this.wrapS = wrapS !== undefined ? wrapS : THREE.ClampToEdgeWrapping;
 	this.wrapT = wrapT !== undefined ? wrapT : THREE.ClampToEdgeWrapping;
 	this.wrapT = wrapT !== undefined ? wrapT : THREE.ClampToEdgeWrapping;
@@ -35,11 +35,27 @@ THREE.Texture = function ( image, mapping, wrapS, wrapT, magFilter, minFilter, f
 	this.flipY = true;
 	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.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 = true;
 	this.onUpdate = null;
 	this.onUpdate = null;
 
 
 };
 };
 
 
+THREE.Texture.DEFAULT_IMAGE = ( function () {
+
+	var canvas = document.createElement( 'canvas' );
+	canvas.width = 8;
+	canvas.height = 8;
+
+	var context = canvas.getContext( '2d' );
+	context.fillStyle = '#ff0000';
+	context.fillRect( 0, 0, canvas.width, canvas.height );
+
+	return canvas;
+
+}() );
+
+THREE.Texture.DEFAULT_MAPPING = new THREE.UVMapping();
+
 THREE.Texture.prototype = {
 THREE.Texture.prototype = {
 
 
 	constructor: THREE.Texture,
 	constructor: THREE.Texture,