Explorar o código

GLTFExporter: Throw exception when processing textures with no image data. (#25106)

Michael Herzog %!s(int64=2) %!d(string=hai) anos
pai
achega
2373d72345
Modificáronse 1 ficheiros con 68 adicións e 60 borrados
  1. 68 60
      examples/jsm/exporters/GLTFExporter.js

+ 68 - 60
examples/jsm/exporters/GLTFExporter.js

@@ -1077,112 +1077,120 @@ class GLTFWriter {
 	 */
 	processImage( image, format, flipY, mimeType = 'image/png' ) {
 
-		const writer = this;
-		const cache = writer.cache;
-		const json = writer.json;
-		const options = writer.options;
-		const pending = writer.pending;
+		if ( image !== null ) {
 
-		if ( ! cache.images.has( image ) ) cache.images.set( image, {} );
+			const writer = this;
+			const cache = writer.cache;
+			const json = writer.json;
+			const options = writer.options;
+			const pending = writer.pending;
 
-		const cachedImages = cache.images.get( image );
+			if ( ! cache.images.has( image ) ) cache.images.set( image, {} );
 
-		const key = mimeType + ':flipY/' + flipY.toString();
+			const cachedImages = cache.images.get( image );
 
-		if ( cachedImages[ key ] !== undefined ) return cachedImages[ key ];
+			const key = mimeType + ':flipY/' + flipY.toString();
 
-		if ( ! json.images ) json.images = [];
+			if ( cachedImages[ key ] !== undefined ) return cachedImages[ key ];
 
-		const imageDef = { mimeType: mimeType };
+			if ( ! json.images ) json.images = [];
 
-		const canvas = getCanvas();
+			const imageDef = { mimeType: mimeType };
 
-		canvas.width = Math.min( image.width, options.maxTextureSize );
-		canvas.height = Math.min( image.height, options.maxTextureSize );
+			const canvas = getCanvas();
 
-		const ctx = canvas.getContext( '2d' );
+			canvas.width = Math.min( image.width, options.maxTextureSize );
+			canvas.height = Math.min( image.height, options.maxTextureSize );
 
-		if ( flipY === true ) {
+			const ctx = canvas.getContext( '2d' );
 
-			ctx.translate( 0, canvas.height );
-			ctx.scale( 1, - 1 );
+			if ( flipY === true ) {
 
-		}
+				ctx.translate( 0, canvas.height );
+				ctx.scale( 1, - 1 );
 
-		if ( image.data !== undefined ) { // THREE.DataTexture
+			}
 
-			if ( format !== RGBAFormat ) {
+			if ( image.data !== undefined ) { // THREE.DataTexture
 
-				console.error( 'GLTFExporter: Only RGBAFormat is supported.' );
+				if ( format !== RGBAFormat ) {
 
-			}
+					console.error( 'GLTFExporter: Only RGBAFormat is supported.' );
 
-			if ( image.width > options.maxTextureSize || image.height > options.maxTextureSize ) {
+				}
 
-				console.warn( 'GLTFExporter: Image size is bigger than maxTextureSize', image );
+				if ( image.width > options.maxTextureSize || image.height > options.maxTextureSize ) {
 
-			}
+					console.warn( 'GLTFExporter: Image size is bigger than maxTextureSize', image );
 
-			const data = new Uint8ClampedArray( image.height * image.width * 4 );
+				}
 
-			for ( let i = 0; i < data.length; i += 4 ) {
+				const data = new Uint8ClampedArray( image.height * image.width * 4 );
 
-				data[ i + 0 ] = image.data[ i + 0 ];
-				data[ i + 1 ] = image.data[ i + 1 ];
-				data[ i + 2 ] = image.data[ i + 2 ];
-				data[ i + 3 ] = image.data[ i + 3 ];
+				for ( let i = 0; i < data.length; i += 4 ) {
 
-			}
+					data[ i + 0 ] = image.data[ i + 0 ];
+					data[ i + 1 ] = image.data[ i + 1 ];
+					data[ i + 2 ] = image.data[ i + 2 ];
+					data[ i + 3 ] = image.data[ i + 3 ];
 
-			ctx.putImageData( new ImageData( data, image.width, image.height ), 0, 0 );
+				}
 
-		} else {
+				ctx.putImageData( new ImageData( data, image.width, image.height ), 0, 0 );
 
-			ctx.drawImage( image, 0, 0, canvas.width, canvas.height );
+			} else {
 
-		}
+				ctx.drawImage( image, 0, 0, canvas.width, canvas.height );
 
-		if ( options.binary === true ) {
+			}
 
-			pending.push(
+			if ( options.binary === true ) {
 
-				getToBlobPromise( canvas, mimeType )
-					.then( blob => writer.processBufferViewImage( blob ) )
-					.then( bufferViewIndex => {
+				pending.push(
 
-						imageDef.bufferView = bufferViewIndex;
+					getToBlobPromise( canvas, mimeType )
+						.then( blob => writer.processBufferViewImage( blob ) )
+						.then( bufferViewIndex => {
 
-					} )
+							imageDef.bufferView = bufferViewIndex;
 
-			);
+						} )
 
-		} else {
+				);
 
-			if ( canvas.toDataURL !== undefined ) {
+			} else {
 
-				imageDef.uri = canvas.toDataURL( mimeType );
+				if ( canvas.toDataURL !== undefined ) {
 
-			} else {
+					imageDef.uri = canvas.toDataURL( mimeType );
 
-				pending.push(
+				} else {
 
-					getToBlobPromise( canvas, mimeType )
-						.then( blob => new FileReader().readAsDataURL( blob ) )
-						.then( dataURL => {
+					pending.push(
 
-							imageDef.uri = dataURL;
+						getToBlobPromise( canvas, mimeType )
+							.then( blob => new FileReader().readAsDataURL( blob ) )
+							.then( dataURL => {
 
-						} )
+								imageDef.uri = dataURL;
 
-				);
+							} )
+
+					);
+
+				}
 
 			}
 
-		}
+			const index = json.images.push( imageDef ) - 1;
+			cachedImages[ key ] = index;
+			return index;
 
-		const index = json.images.push( imageDef ) - 1;
-		cachedImages[ key ] = index;
-		return index;
+		} else {
+
+			throw new Error( 'THREE.GLTFExporter: No valid image data found. Unable to process texture.' );
+
+		}
 
 	}