Browse Source

added ior and specular export (#25335)

* added ior and specular export

* Update GLTFExporter.js

* Update GLTFExporter.js

* Update GLTFExporter.js

Co-authored-by: mrdoob <[email protected]>
Emmett Lalish 2 years ago
parent
commit
67e8a08964
1 changed files with 104 additions and 2 deletions
  1. 104 2
      examples/jsm/exporters/GLTFExporter.js

+ 104 - 2
examples/jsm/exporters/GLTFExporter.js

@@ -1,6 +1,7 @@
 import {
 import {
 	BufferAttribute,
 	BufferAttribute,
 	ClampToEdgeWrapping,
 	ClampToEdgeWrapping,
+	Color,
 	DoubleSide,
 	DoubleSide,
 	InterpolateDiscrete,
 	InterpolateDiscrete,
 	InterpolateLinear,
 	InterpolateLinear,
@@ -53,6 +54,18 @@ class GLTFExporter {
 
 
 		} );
 		} );
 
 
+		this.register( function ( writer ) {
+
+			return new GLTFMaterialsIorExtension( writer );
+
+		} );
+
+		this.register( function ( writer ) {
+
+			return new GLTFMaterialsSpecularExtension( writer );
+
+		} );
+
 		this.register( function ( writer ) {
 		this.register( function ( writer ) {
 
 
 			return new GLTFMaterialsClearcoatExtension( writer );
 			return new GLTFMaterialsClearcoatExtension( writer );
@@ -180,6 +193,8 @@ const PATH_PROPERTIES = {
 	morphTargetInfluences: 'weights'
 	morphTargetInfluences: 'weights'
 };
 };
 
 
+const DEFAULT_SPECULAR_COLOR = new Color();
+
 // GLB constants
 // GLB constants
 // https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#glb-file-format-specification
 // https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#glb-file-format-specification
 
 
@@ -2313,7 +2328,7 @@ class GLTFMaterialsClearcoatExtension {
 
 
 	writeMaterial( material, materialDef ) {
 	writeMaterial( material, materialDef ) {
 
 
-		if ( ! material.isMeshPhysicalMaterial ) return;
+		if ( ! material.isMeshPhysicalMaterial || material.clearcoat === 0 ) return;
 
 
 		const writer = this.writer;
 		const writer = this.writer;
 		const extensionsUsed = writer.extensionsUsed;
 		const extensionsUsed = writer.extensionsUsed;
@@ -2374,7 +2389,7 @@ class GLTFMaterialsIridescenceExtension {
 
 
 	writeMaterial( material, materialDef ) {
 	writeMaterial( material, materialDef ) {
 
 
-		if ( ! material.isMeshPhysicalMaterial ) return;
+		if ( ! material.isMeshPhysicalMaterial || material.iridescence === 0 ) return;
 
 
 		const writer = this.writer;
 		const writer = this.writer;
 		const extensionsUsed = writer.extensionsUsed;
 		const extensionsUsed = writer.extensionsUsed;
@@ -2499,6 +2514,93 @@ class GLTFMaterialsVolumeExtension {
 
 
 }
 }
 
 
+/**
+ * Materials ior Extension
+ *
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_ior
+ */
+class GLTFMaterialsIorExtension {
+
+	constructor( writer ) {
+
+		this.writer = writer;
+		this.name = 'KHR_materials_ior';
+
+	}
+
+	writeMaterial( material, materialDef ) {
+
+		if ( ! material.isMeshPhysicalMaterial || material.ior === 1.5 ) return;
+
+		const writer = this.writer;
+		const extensionsUsed = writer.extensionsUsed;
+
+		const extensionDef = {};
+
+		extensionDef.ior = material.ior;
+
+		materialDef.extensions = materialDef.extensions || {};
+		materialDef.extensions[ this.name ] = extensionDef;
+
+		extensionsUsed[ this.name ] = true;
+
+	}
+
+}
+
+/**
+ * Materials specular Extension
+ *
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_specular
+ */
+class GLTFMaterialsSpecularExtension {
+
+	constructor( writer ) {
+
+		this.writer = writer;
+		this.name = 'KHR_materials_specular';
+
+	}
+
+	writeMaterial( material, materialDef ) {
+
+		if ( ! material.isMeshPhysicalMaterial || ( material.specularIntensity === 1.0 && 
+		       material.specularColor.equals( DEFAULT_SPECULAR_COLOR ) && 
+		     ! material.specularIntensityMap && ! material.specularColorTexture ) ) return;
+
+		const writer = this.writer;
+		const extensionsUsed = writer.extensionsUsed;
+
+		const extensionDef = {};
+
+		if ( material.specularIntensityMap ) {
+
+			const specularIntensityMapDef = { index: writer.processTexture( material.specularIntensityMap ) };
+			writer.applyTextureTransform( specularIntensityMapDef, material.specularIntensityMap );
+			extensionDef.specularTexture = specularIntensityMapDef;
+
+		}
+
+		if ( material.specularColorMap ) {
+
+			const specularColorMapDef = { index: writer.processTexture( material.specularColorMap ) };
+			writer.applyTextureTransform( specularColorMapDef, material.specularColorMap );
+			extensionDef.specularColorTexture = specularColorMapDef;
+
+		}
+
+		extensionDef.specularFactor = material.specularIntensity;
+		extensionDef.specularColorFactor = material.specularColor.toArray();
+
+		materialDef.extensions = materialDef.extensions || {};
+		materialDef.extensions[ this.name ] = extensionDef;
+
+		extensionsUsed[ this.name ] = true;
+
+	}
+
+}
+
 /**
 /**
  * Static utility functions
  * Static utility functions
  */
  */