Show textures in Rhino files #230

This commit is contained in:
kovacsv 2022-03-14 19:41:56 +01:00
parent af06da7471
commit dc19691fa5

View File

@ -2,6 +2,7 @@ import { Direction } from '../geometry/geometry.js';
import { Matrix } from '../geometry/matrix.js'; import { Matrix } from '../geometry/matrix.js';
import { Transformation } from '../geometry/transformation.js'; import { Transformation } from '../geometry/transformation.js';
import { LoadExternalLibrary } from '../io/externallibs.js'; import { LoadExternalLibrary } from '../io/externallibs.js';
import { GetFileName } from '../io/fileutils.js';
import { PhongMaterial, PhysicalMaterial } from '../model/material.js'; import { PhongMaterial, PhysicalMaterial } from '../model/material.js';
import { TransformMesh } from '../model/meshutils.js'; import { TransformMesh } from '../model/meshutils.js';
import { IsModelEmpty } from '../model/modelutils.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 { ConvertThreeGeometryToMesh } from '../threejs/threeutils.js';
import { ImporterBase } from './importerbase.js'; import { ImporterBase } from './importerbase.js';
import { UpdateMaterialTransparency } from './importerutils.js'; import { UpdateMaterialTransparency } from './importerutils.js';
import { TextureMap } from '../model/material.js';
export class Importer3dm extends ImporterBase export class Importer3dm extends ImporterBase
{ {
@ -242,7 +244,7 @@ export class Importer3dm extends ImporterBase
return null; return null;
} }
function FindMatchingMaterial (model, rhinoMaterial) function ConvertRhinoMaterial (rhinoMaterial, callbacks)
{ {
function SetColor (color, rhinoColor) function SetColor (color, rhinoColor)
{ {
@ -260,10 +262,6 @@ export class Importer3dm extends ImporterBase
} }
let material = null; let material = null;
if (rhinoMaterial === null) {
material = new PhongMaterial ();
material.color.Set (255, 255, 255);
} else {
let physicallyBased = rhinoMaterial.physicallyBased (); let physicallyBased = rhinoMaterial.physicallyBased ();
if (physicallyBased.supported) { if (physicallyBased.supported) {
material = new PhysicalMaterial (); material = new PhysicalMaterial ();
@ -274,18 +272,39 @@ export class Importer3dm extends ImporterBase
SetColor (material.ambient, rhinoMaterial.ambientColor); SetColor (material.ambient, rhinoMaterial.ambientColor);
SetColor (material.specular, rhinoMaterial.specularColor); SetColor (material.specular, rhinoMaterial.specularColor);
} }
material.name = rhinoMaterial.name; material.name = rhinoMaterial.name;
SetColor (material.color, rhinoMaterial.diffuseColor); SetColor (material.color, rhinoMaterial.diffuseColor);
material.opacity = 1.0 - rhinoMaterial.transparency; material.opacity = 1.0 - rhinoMaterial.transparency;
UpdateMaterialTransparency (material); UpdateMaterialTransparency (material);
// material.shininess = rhinoMaterial.shine / 255.0;
if (IsBlack (material.color) && !IsWhite (rhinoMaterial.reflectionColor)) { if (IsBlack (material.color) && !IsWhite (rhinoMaterial.reflectionColor)) {
SetColor (material.color, rhinoMaterial.reflectionColor); SetColor (material.color, rhinoMaterial.reflectionColor);
} }
if (IsBlack (material.color) && !IsWhite (rhinoMaterial.transparentColor)) { if (IsBlack (material.color) && !IsWhite (rhinoMaterial.transparentColor)) {
SetColor (material.color, 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++) { for (let i = 0; i < model.MaterialCount (); i++) {
let current = model.GetMaterial (i); let current = model.GetMaterial (i);
if (current.IsEqual (material)) { if (current.IsEqual (material)) {
@ -296,6 +315,9 @@ export class Importer3dm extends ImporterBase
} }
let rhinoMaterial = GetRhinoMaterial (this.rhino, rhinoObject, rhinoInstanceReferences); let rhinoMaterial = GetRhinoMaterial (this.rhino, rhinoObject, rhinoInstanceReferences);
return FindMatchingMaterial (this.model, rhinoMaterial); if (rhinoMaterial === null) {
return null;
}
return FindMatchingMaterial (this.model, rhinoMaterial, this.callbacks);
} }
} }