From dc19691fa5bf6c87b5791e51e6b63c4c915773fa Mon Sep 17 00:00:00 2001 From: kovacsv Date: Mon, 14 Mar 2022 19:41:56 +0100 Subject: [PATCH] Show textures in Rhino files #230 --- source/engine/import/importer3dm.js | 74 +++++++++++++++++++---------- 1 file changed, 48 insertions(+), 26 deletions(-) diff --git a/source/engine/import/importer3dm.js b/source/engine/import/importer3dm.js index de6ef6e..1768aee 100644 --- a/source/engine/import/importer3dm.js +++ b/source/engine/import/importer3dm.js @@ -2,6 +2,7 @@ import { Direction } from '../geometry/geometry.js'; import { Matrix } from '../geometry/matrix.js'; import { Transformation } from '../geometry/transformation.js'; import { LoadExternalLibrary } from '../io/externallibs.js'; +import { GetFileName } from '../io/fileutils.js'; import { PhongMaterial, PhysicalMaterial } from '../model/material.js'; import { TransformMesh } from '../model/meshutils.js'; import { IsModelEmpty } from '../model/modelutils.js'; @@ -9,6 +10,7 @@ import { Property, PropertyGroup, PropertyType } from '../model/property.js'; import { ConvertThreeGeometryToMesh } from '../threejs/threeutils.js'; import { ImporterBase } from './importerbase.js'; import { UpdateMaterialTransparency } from './importerutils.js'; +import { TextureMap } from '../model/material.js'; export class Importer3dm extends ImporterBase { @@ -242,7 +244,7 @@ export class Importer3dm extends ImporterBase return null; } - function FindMatchingMaterial (model, rhinoMaterial) + function ConvertRhinoMaterial (rhinoMaterial, callbacks) { function SetColor (color, rhinoColor) { @@ -260,32 +262,49 @@ export class Importer3dm extends ImporterBase } let material = null; - if (rhinoMaterial === null) { - material = new PhongMaterial (); - material.color.Set (255, 255, 255); + let physicallyBased = rhinoMaterial.physicallyBased (); + if (physicallyBased.supported) { + material = new PhysicalMaterial (); + material.metalness = physicallyBased.metallic ? 1.0 : 0.0; + material.roughness = physicallyBased.roughness; } else { - let physicallyBased = rhinoMaterial.physicallyBased (); - if (physicallyBased.supported) { - material = new PhysicalMaterial (); - material.metalness = physicallyBased.metallic ? 1.0 : 0.0; - material.roughness = physicallyBased.roughness; - } else { - material = new PhongMaterial (); - SetColor (material.ambient, rhinoMaterial.ambientColor); - SetColor (material.specular, rhinoMaterial.specularColor); - } - material.name = rhinoMaterial.name; - SetColor (material.color, rhinoMaterial.diffuseColor); - material.opacity = 1.0 - rhinoMaterial.transparency; - UpdateMaterialTransparency (material); - // material.shininess = rhinoMaterial.shine / 255.0; - if (IsBlack (material.color) && !IsWhite (rhinoMaterial.reflectionColor)) { - SetColor (material.color, rhinoMaterial.reflectionColor); - } - if (IsBlack (material.color) && !IsWhite (rhinoMaterial.transparentColor)) { - SetColor (material.color, rhinoMaterial.transparentColor); - } + material = new PhongMaterial (); + SetColor (material.ambient, rhinoMaterial.ambientColor); + SetColor (material.specular, rhinoMaterial.specularColor); } + + material.name = rhinoMaterial.name; + + SetColor (material.color, rhinoMaterial.diffuseColor); + material.opacity = 1.0 - rhinoMaterial.transparency; + UpdateMaterialTransparency (material); + + if (IsBlack (material.color) && !IsWhite (rhinoMaterial.reflectionColor)) { + SetColor (material.color, rhinoMaterial.reflectionColor); + } + if (IsBlack (material.color) && !IsWhite (rhinoMaterial.transparentColor)) { + SetColor (material.color, rhinoMaterial.transparentColor); + } + + let rhinoTexture = rhinoMaterial.getBitmapTexture (); + if (rhinoTexture) { + let texture = new TextureMap (); + let textureName = GetFileName (rhinoTexture.fileName); + let textureBuffer = callbacks.getTextureBuffer (textureName); + texture.name = textureName; + if (textureBuffer !== null) { + texture.url = textureBuffer.url; + texture.buffer = textureBuffer.buffer; + } + material.diffuseMap = texture; + } + + return material; + } + + function FindMatchingMaterial (model, rhinoMaterial, callbacks) + { + let material = ConvertRhinoMaterial (rhinoMaterial, callbacks); for (let i = 0; i < model.MaterialCount (); i++) { let current = model.GetMaterial (i); if (current.IsEqual (material)) { @@ -296,6 +315,9 @@ export class Importer3dm extends ImporterBase } let rhinoMaterial = GetRhinoMaterial (this.rhino, rhinoObject, rhinoInstanceReferences); - return FindMatchingMaterial (this.model, rhinoMaterial); + if (rhinoMaterial === null) { + return null; + } + return FindMatchingMaterial (this.model, rhinoMaterial, this.callbacks); } }