Explorar o código

Texture.mipmaps serialization

Takahiro %!s(int64=7) %!d(string=hai) anos
pai
achega
f5ab28e6d3
Modificáronse 2 ficheiros con 76 adicións e 16 borrados
  1. 37 1
      src/loaders/ObjectLoader.js
  2. 39 15
      src/textures/Texture.js

+ 37 - 1
src/loaders/ObjectLoader.js

@@ -570,7 +570,43 @@ Object.assign( ObjectLoader.prototype, {
 				if ( data.minFilter !== undefined ) texture.minFilter = parseConstant( data.minFilter, TEXTURE_FILTER );
 				if ( data.magFilter !== undefined ) texture.magFilter = parseConstant( data.magFilter, TEXTURE_FILTER );
 				if ( data.anisotropy !== undefined ) texture.anisotropy = data.anisotropy;
-				if ( data.mipmaps !== undefined ) texture.mipmaps = data.mipmaps;
+
+				if ( data.mipmaps !== undefined ) {
+
+					var TYPED_ARRAYS = {
+						Int8Array: Int8Array,
+						Uint8Array: Uint8Array,
+						// Workaround for IE11 pre KB2929437. See #11440
+						Uint8ClampedArray: typeof Uint8ClampedArray !== 'undefined' ? Uint8ClampedArray : Uint8Array,
+						Int16Array: Int16Array,
+						Uint16Array: Uint16Array,
+						Int32Array: Int32Array,
+						Uint32Array: Uint32Array,
+						Float32Array: Float32Array,
+						Float64Array: Float64Array
+					};
+
+					for ( var j = 0, jl = data.mipmaps.length; j < jl; j ++ ) {
+
+						var mipmap = data.mipmaps[ j ];
+
+						if ( mipmap.array !== undefined ) {
+
+							texture.mipmaps.push( {
+								width: mipmap.width,
+								height: mipmap.height,
+								data: new TYPED_ARRAYS[ mipmap.type ]( mipmap.array )
+							} );
+
+						} else {
+
+							texture.mipmaps.push( images[ mipmap ] );
+
+						}
+
+					}
+
+				}
 
 				if ( data.flipY !== undefined ) texture.flipY = data.flipY;
 

+ 39 - 15
src/textures/Texture.js

@@ -170,6 +170,29 @@ Texture.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
 
 		}
 
+		function addImage( image ) {
+
+			// TODO: Move to THREE.Image
+
+			if ( image.uuid === undefined ) {
+
+				image.uuid = _Math.generateUUID(); // UGH
+
+			}
+
+			if ( ! isRootObject && meta.images[ image.uuid ] === undefined ) {
+
+				meta.images[ image.uuid ] = {
+					uuid: image.uuid,
+					url: getDataURL( image )
+				};
+
+			}
+
+			return image.uuid;
+
+		}
+
 		var output = {
 
 			metadata: {
@@ -197,7 +220,6 @@ Texture.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
 			minFilter: this.minFilter,
 			magFilter: this.magFilter,
 			anisotropy: this.anisotropy,
-			mipmaps: this.mipmaps.slice(),
 
 			flipY: this.flipY,
 
@@ -206,31 +228,33 @@ Texture.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
 
 		};
 
-		if ( this.image !== undefined ) {
+		var mipmaps = [];
 
-			// TODO: Move to THREE.Image
+		for ( var i = 0, il = this.mipmaps.length; i < il; i ++ ) {
 
-			var image = this.image;
+			var mipmap = this.mipmaps[ i ];
 
-			if ( image.uuid === undefined ) {
+			if ( mipmap.data !== undefined ) {
 
-				image.uuid = _Math.generateUUID(); // UGH
-
-			}
+				mipmaps.push( {
+					width: mipmap.width,
+					height: mipmap.height,
+					type: mipmap.data.constructor.name,
+					array:  Array.prototype.slice.call( mipmap.data )
+				} );
 
-			if ( ! isRootObject && meta.images[ image.uuid ] === undefined ) {
+			} else {
 
-				meta.images[ image.uuid ] = {
-					uuid: image.uuid,
-					url: getDataURL( image )
-				};
+				mipmaps.push( addImage( mipmap ) );
 
 			}
 
-			output.image = image.uuid;
-
 		}
 
+		if ( mipmaps.length > 0 ) output.mipmaps = mipmaps;
+
+		if ( this.image !== undefined ) output.image = addImage( this.image );
+
 		if ( ! isRootObject ) {
 
 			meta.textures[ this.uuid ] = output;