Browse Source

3DMLoader: started material texture support

Luis Fraguada 4 years ago
parent
commit
d26feb07e0
1 changed files with 151 additions and 9 deletions
  1. 151 9
      examples/jsm/loaders/3DMLoader.js

+ 151 - 9
examples/jsm/loaders/3DMLoader.js

@@ -20,7 +20,8 @@ import {
 	SpriteMaterial,
 	SpriteMaterial,
 	CanvasTexture,
 	CanvasTexture,
 	LinearFilter,
 	LinearFilter,
-	ClampToEdgeWrapping
+	ClampToEdgeWrapping,
+	TextureLoader
 } from "../../../build/three.module.js";
 } from "../../../build/three.module.js";
 
 
 var Rhino3dmLoader = function ( manager ) {
 var Rhino3dmLoader = function ( manager ) {
@@ -221,13 +222,61 @@ Rhino3dmLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
 
 
 		}
 		}
 
 
-		return new MeshStandardMaterial( {
+		console.log( material );
+
+		var mat = new MeshStandardMaterial( {
 			color: diffusecolor,
 			color: diffusecolor,
-			metalness: 0.8,
 			name: material.name,
 			name: material.name,
-			side: 2
+			side: 2,
+			transparent: material.transparency > 0 ? true : false,
+			opacity: 1.0 - material.transparency
 		} );
 		} );
 
 
+		var textureLoader = new TextureLoader();
+
+		for ( var i = 0; i < material.textures.length; i ++ ) {
+
+			var texture = material.textures[ i ];
+
+			if ( texture.image !== null ) {
+
+				var map = textureLoader.load( texture.image );
+
+				switch ( texture.type ) {
+
+					case 'Diffuse':
+
+						mat.map = map;
+
+						break;
+
+					case 'Bump':
+
+						mat.bumpMap = map;
+
+						break;
+
+					case 'Transparency':
+
+						mat.alphaMap = map;
+						mat.transparent = true;
+
+						break;
+
+					case 'Emap':
+
+						mat.envMap = map;
+
+						break;
+
+				}
+
+			}
+
+		}
+
+		return mat;
+
 	},
 	},
 
 
 	_createGeometry: function ( data ) {
 	_createGeometry: function ( data ) {
@@ -763,17 +812,110 @@ Rhino3dmLoader.Rhino3dmWorker = function () {
 
 
 		// Handle materials
 		// Handle materials
 
 
+		var textureTypes = [
+			// rhino.TextureType.Bitmap,
+			rhino.TextureType.Diffuse,
+			rhino.TextureType.Bump,
+			rhino.TextureType.Transparency,
+			rhino.TextureType.Opacity,
+			rhino.TextureType.Emap
+		];
+
+		var pbrTextureTypes = [
+			rhino.TextureType.PBR_BaseColor,
+			rhino.TextureType.PBR_Subsurface,
+			rhino.TextureType.PBR_SubsurfaceScattering,
+			rhino.TextureType.PBR_SubsurfaceScatteringRadius,
+			rhino.TextureType.PBR_Metallic,
+			rhino.TextureType.PBR_Specular,
+			rhino.TextureType.PBR_SpecularTint,
+			rhino.TextureType.PBR_Roughness,
+			rhino.TextureType.PBR_Anisotropic,
+			rhino.TextureType.PBR_Anisotropic_Rotation,
+			rhino.TextureType.PBR_Sheen,
+			rhino.TextureType.PBR_SheenTint,
+			rhino.TextureType.PBR_Clearcoat,
+			rhino.TextureType.PBR_ClearcoatBump,
+			rhino.TextureType.PBR_ClearcoatRoughness,
+			rhino.TextureType.PBR_OpacityIor,
+			rhino.TextureType.PBR_OpacityRoughness,
+			rhino.TextureType.PBR_Emission,
+			rhino.TextureType.PBR_AmbientOcclusion,
+			rhino.TextureType.PBR_Displacement
+		];
+
 		for ( var i = 0; i < doc.materials().count(); i ++ ) {
 		for ( var i = 0; i < doc.materials().count(); i ++ ) {
 
 
 			var _material = doc.materials().get( i );
 			var _material = doc.materials().get( i );
-			var materialProperties = extractProperties( _material );
-			var pbMaterialProperties = extractProperties( _material.physicallyBased() );
+			var _pbrMaterial = _material.physicallyBased();
+
+			var material = extractProperties( _material );
+
+			var textures = [];
+
+			for ( var j = 0; j < textureTypes.length; j ++ ) {
 
 
-			var material = Object.assign( materialProperties, pbMaterialProperties );
+				var _texture = _material.getTexture( textureTypes[ j ] );
+				if ( _texture ) {
+
+					var textureType = textureTypes[ j ].constructor.name;
+					textureType = textureType.substring( 12, textureType.length );
+					var texture = { type: textureType };
+
+					var image = doc.getEmbeddedFileAsBase64( _texture.fileName );
+
+					if ( image ) {
+
+						texture.image = 'data:image/png;base64,' + image;
+
+					} else {
+
+						console.warn( `THREE.3DMLoader: Image for ${textureType} texture not embedded in file.` );
+						texture.image = null;
+
+					}
+
+					textures.push( texture );
+
+					_texture.delete();
+
+				}
+
+			}
+
+			material.textures = textures;
+
+			if ( _pbrMaterial.supported ) {
+
+				console.log( 'pbr true' );
+
+				for ( var j = 0; j < pbrTextureTypes.length; j ++ ) {
+
+					var _texture = _material.getTexture( textureTypes[ j ] );
+					if ( _texture ) {
+
+						var image = doc.getEmbeddedFileAsBase64( _texture.fileName );
+						var textureType = textureTypes[ j ].constructor.name;
+						textureType = textureType.substring( 12, textureType.length );
+						var texture = { type: textureType, image: 'data:image/png;base64,' + image };
+						textures.push( texture );
+
+						_texture.delete();
+
+					}
+
+				}
+
+				var pbMaterialProperties = extractProperties( _material.physicallyBased() );
+
+				material = Object.assign( pbMaterialProperties, material );
+
+			}
 
 
 			materials.push( material );
 			materials.push( material );
 
 
 			_material.delete();
 			_material.delete();
+			_pbrMaterial.delete();
 
 
 		}
 		}
 
 
@@ -836,10 +978,10 @@ Rhino3dmLoader.Rhino3dmWorker = function () {
 		//TODO: Handle other document stuff like dimstyles, instance definitions, bitmaps etc.
 		//TODO: Handle other document stuff like dimstyles, instance definitions, bitmaps etc.
 
 
 		// Handle dimstyles
 		// Handle dimstyles
-		// console.log(`Dimstyle Count: ${doc.dimstyles().count()}`);
+		// console.log( `Dimstyle Count: ${doc.dimstyles().count()}` );
 
 
 		// Handle bitmaps
 		// Handle bitmaps
-		// console.log(`Bitmap Count: ${doc.bitmaps().count()}`);
+		// console.log( `Bitmap Count: ${doc.bitmaps().count()}` );
 
 
 		// Handle instance definitions
 		// Handle instance definitions
 		// console.log(`Instance Definitions Count: ${doc.instanceDefinitions().count()}`);
 		// console.log(`Instance Definitions Count: ${doc.instanceDefinitions().count()}`);