Browse Source

Updated processImage function to take image type with export params.

This locks the function down to process one specific image and prevents use of texture properties after cache retrieval that would violate the cache scheme.
Alan Chambers 7 years ago
parent
commit
17ca5922f0
1 changed files with 17 additions and 14 deletions
  1. 17 14
      examples/js/exporters/GLTFExporter.js

+ 17 - 14
examples/js/exporters/GLTFExporter.js

@@ -599,14 +599,16 @@ THREE.GLTFExporter.prototype = {
 
 
 		/**
 		/**
 		 * Process image
 		 * Process image
-		 * @param  {Texture} map Texture to process
+		 * @param  {Image} image to process
+		 * @param  {String} mimeType to use for writing out
+		 * @param  {Boolean} flip the image in the Y before writing out
 		 * @return {Integer}     Index of the processed texture in the "images" array
 		 * @return {Integer}     Index of the processed texture in the "images" array
 		 */
 		 */
-		function processImage( map ) {
+		function processImage( image, mimeType, flipY ) {
 
 
-			if ( cachedData.images.has( map.image ) ) {
+			if ( cachedData.images.has( image ) ) {
 
 
-				return cachedData.images.get( map.image );
+				return cachedData.images.get( image );
 
 
 			}
 			}
 
 
@@ -616,19 +618,18 @@ THREE.GLTFExporter.prototype = {
 
 
 			}
 			}
 
 
-			var mimeType = map.format === THREE.RGBAFormat ? 'image/png' : 'image/jpeg';
 			var gltfImage = { mimeType: mimeType };
 			var gltfImage = { mimeType: mimeType };
 
 
 			if ( options.embedImages ) {
 			if ( options.embedImages ) {
 
 
 				var canvas = cachedCanvas = cachedCanvas || document.createElement( 'canvas' );
 				var canvas = cachedCanvas = cachedCanvas || document.createElement( 'canvas' );
 
 
-				canvas.width = map.image.width;
-				canvas.height = map.image.height;
+				canvas.width = image.width;
+				canvas.height = image.height;
 
 
-				if ( options.forcePowerOfTwoTextures && ! isPowerOfTwo( map.image ) ) {
+				if ( options.forcePowerOfTwoTextures && ! isPowerOfTwo( image ) ) {
 
 
-					console.warn( 'GLTFExporter: Resized non-power-of-two image.', map.image );
+					console.warn( 'GLTFExporter: Resized non-power-of-two image.', image );
 
 
 					canvas.width = THREE.Math.floorPowerOfTwo( canvas.width );
 					canvas.width = THREE.Math.floorPowerOfTwo( canvas.width );
 					canvas.height = THREE.Math.floorPowerOfTwo( canvas.height );
 					canvas.height = THREE.Math.floorPowerOfTwo( canvas.height );
@@ -637,14 +638,14 @@ THREE.GLTFExporter.prototype = {
 
 
 				var ctx = canvas.getContext( '2d' );
 				var ctx = canvas.getContext( '2d' );
 
 
-				if ( map.flipY === true ) {
+				if ( flipY === true ) {
 
 
 					ctx.translate( 0, canvas.height );
 					ctx.translate( 0, canvas.height );
 					ctx.scale( 1, - 1 );
 					ctx.scale( 1, - 1 );
 
 
 				}
 				}
 
 
-				ctx.drawImage( map.image, 0, 0, canvas.width, canvas.height );
+				ctx.drawImage( image, 0, 0, canvas.width, canvas.height );
 
 
 				if ( options.binary === true ) {
 				if ( options.binary === true ) {
 
 
@@ -672,16 +673,17 @@ THREE.GLTFExporter.prototype = {
 
 
 			} else {
 			} else {
 
 
-				gltfImage.uri = map.image.src;
+				gltfImage.uri = image.src;
 
 
 			}
 			}
 
 
 			outputJSON.images.push( gltfImage );
 			outputJSON.images.push( gltfImage );
 
 
 			var index = outputJSON.images.length - 1;
 			var index = outputJSON.images.length - 1;
-			cachedData.images.set( map.image, index );
+			cachedData.images.set( image, index );
 
 
 			return index;
 			return index;
+
 		}
 		}
 
 
 		/**
 		/**
@@ -731,10 +733,11 @@ THREE.GLTFExporter.prototype = {
 
 
 			}
 			}
 
 
+			var mimeType = map.format === THREE.RGBAFormat ? 'image/png' : 'image/jpeg';
 			var gltfTexture = {
 			var gltfTexture = {
 
 
 				sampler: processSampler( map ),
 				sampler: processSampler( map ),
-				source: processImage( map )
+				source: processImage( map.image, mimeType, map.flipY  )
 
 
 			};
 			};