Browse Source

GLTFExporter: Only merge roughness and metalness maps. (#23616)

mrdoob 3 years ago
parent
commit
473c7e67e4
1 changed files with 38 additions and 50 deletions
  1. 38 50
      examples/jsm/exporters/GLTFExporter.js

+ 38 - 50
examples/jsm/exporters/GLTFExporter.js

@@ -689,80 +689,68 @@ class GLTFWriter {
 
 
 	}
 	}
 
 
-	buildORMTexture( material ) {
+	buildMetalRoughTexture( metalnessMap, roughnessMap ) {
 
 
-		const occlusion = material.aoMap?.image;
-		const roughness = material.roughnessMap?.image;
-		const metalness = material.metalnessMap?.image;
+		if ( metalnessMap === roughnessMap ) return metalnessMap;
 
 
-		if ( occlusion === roughness && roughness === metalness ) return material.aoMap;
+		console.warn( 'THREE.GLTFExporter: Merged metalnessMap and roughnessMap textures.' );
 
 
-		if ( occlusion || roughness || metalness ) {
+		const metalness = metalnessMap?.image;
+		const roughness = roughnessMap?.image;
 
 
-			const width = Math.max( occlusion?.width || 0, roughness?.width || 0, metalness?.width || 0 );
-			const height = Math.max( occlusion?.height || 0, roughness?.height || 0, metalness?.height || 0 );
+		const width = Math.max( metalness?.width || 0, roughness?.width || 0 );
+		const height = Math.max( metalness?.height || 0, roughness?.height || 0 );
 
 
-			const canvas = document.createElement( 'canvas' );
-			canvas.width = width;
-			canvas.height = height;
+		const canvas = document.createElement( 'canvas' );
+		canvas.width = width;
+		canvas.height = height;
 
 
-			const context = canvas.getContext( '2d' );
-			context.fillStyle = '#ffffff';
-			context.fillRect( 0, 0, width, height );
+		const context = canvas.getContext( '2d' );
+		context.fillStyle = '#00ffff';
+		context.fillRect( 0, 0, width, height );
 
 
-			const composite = context.getImageData( 0, 0, width, height );
+		const composite = context.getImageData( 0, 0, width, height );
 
 
-			if ( occlusion ) {
+		if ( metalness ) {
 
 
-				context.drawImage( occlusion, 0, 0, width, height );
+			context.drawImage( metalness, 0, 0, width, height );
 
 
-				const data = context.getImageData( 0, 0, width, height ).data;
+			const data = context.getImageData( 0, 0, width, height ).data;
 
 
-				for ( let i = 0; i < data.length; i += 4 ) {
-
-					composite.data[ i ] = data[ i ];
+			for ( let i = 2; i < data.length; i += 4 ) {
 
 
-				}
+				composite.data[ i ] = data[ i ];
 
 
 			}
 			}
 
 
-			if ( roughness ) {
+		}
 
 
-				context.drawImage( roughness, 0, 0, width, height );
+		if ( roughness ) {
 
 
-				const data = context.getImageData( 0, 0, width, height ).data;
+			context.drawImage( roughness, 0, 0, width, height );
 
 
-				for ( let i = 1; i < data.length; i += 4 ) {
+			const data = context.getImageData( 0, 0, width, height ).data;
 
 
-					composite.data[ i ] = data[ i ];
+			for ( let i = 1; i < data.length; i += 4 ) {
 
 
-				}
+				composite.data[ i ] = data[ i ];
 
 
 			}
 			}
 
 
-			if ( metalness ) {
-
-				context.drawImage( metalness, 0, 0, width, height );
-
-				const data = context.getImageData( 0, 0, width, height ).data;
-
-				for ( let i = 2; i < data.length; i += 4 ) {
-
-					composite.data[ i ] = data[ i ];
-
-				}
+		}
 
 
-			}
+		context.putImageData( composite, 0, 0 );
 
 
-			context.putImageData( composite, 0, 0 );
+		//
 
 
-			const texture = new Texture( canvas );
+		const reference = metalnessMap || roughnessMap;
 
 
-			texture.userData.mimeType = material.roughnessMap.userData.mimeType || material.metalnessMap.userData.mimeType || material.aoMap.userData.mimeType;
+		const texture = reference.clone();
 
 
-			return texture;
+		// TODO Use new Source() instead?
+		texture.source = new Texture( canvas ).source;
 
 
-		}
+		return texture;
 
 
 	}
 	}
 
 
@@ -1261,13 +1249,13 @@ class GLTFWriter {
 
 
 		}
 		}
 
 
-		const ormTexture = this.buildORMTexture( material );
-
 		// pbrMetallicRoughness.metallicRoughnessTexture
 		// pbrMetallicRoughness.metallicRoughnessTexture
 		if ( material.metalnessMap || material.roughnessMap ) {
 		if ( material.metalnessMap || material.roughnessMap ) {
 
 
-			const metalRoughMapDef = { index: this.processTexture( ormTexture ) };
-			this.applyTextureTransform( metalRoughMapDef, material.metalnessMap || material.roughnessMap );
+			const metalRoughTexture = this.buildMetalRoughTexture( material.metalnessMap, material.roughnessMap );
+
+			const metalRoughMapDef = { index: this.processTexture( metalRoughTexture ) };
+			this.applyTextureTransform( metalRoughMapDef, metalRoughTexture );
 			materialDef.pbrMetallicRoughness.metallicRoughnessTexture = metalRoughMapDef;
 			materialDef.pbrMetallicRoughness.metallicRoughnessTexture = metalRoughMapDef;
 
 
 		}
 		}
@@ -1334,7 +1322,7 @@ class GLTFWriter {
 		if ( material.aoMap ) {
 		if ( material.aoMap ) {
 
 
 			const occlusionMapDef = {
 			const occlusionMapDef = {
-				index: this.processTexture( ormTexture ),
+				index: this.processTexture( material.aoMap ),
 				texCoord: 1
 				texCoord: 1
 			};
 			};