Use ColorToMaterialConverter where possible.
This commit is contained in:
parent
9481761a58
commit
006e5aafc2
@ -3,15 +3,13 @@ import { Coord3D } from '../geometry/coord3d.js';
|
||||
import { Direction } from '../geometry/geometry.js';
|
||||
import { ArrayBufferToUtf8String } from '../io/bufferutils.js';
|
||||
import { Node, NodeType } from '../model/node.js';
|
||||
import { PhongMaterial } from '../model/material.js';
|
||||
import { Color, IntegerToHexString } from '../model/color.js';
|
||||
import { Mesh } from '../model/mesh.js';
|
||||
import { Triangle } from '../model/triangle.js';
|
||||
import { ImporterBase } from './importerbase.js';
|
||||
import { Quaternion } from '../geometry/quaternion.js';
|
||||
import { Matrix } from '../geometry/matrix.js';
|
||||
import { Transformation } from '../geometry/transformation.js';
|
||||
import { UpdateMaterialTransparency } from './importerutils.js';
|
||||
import { ColorToMaterialConverter } from './importerutils.js';
|
||||
import { Property, PropertyGroup, PropertyType } from '../model/property.js';
|
||||
|
||||
export class ImporterBim extends ImporterBase
|
||||
@ -34,13 +32,13 @@ export class ImporterBim extends ImporterBase
|
||||
ClearContent ()
|
||||
{
|
||||
this.meshIdToMesh = null;
|
||||
this.colorToMaterialIndex = null;
|
||||
this.colorToMaterial = null;
|
||||
}
|
||||
|
||||
ResetContent ()
|
||||
{
|
||||
this.meshIdToMesh = new Map ();
|
||||
this.colorToMaterialIndex = new Map ();
|
||||
this.colorToMaterial = new ColorToMaterialConverter (this.model);
|
||||
}
|
||||
|
||||
ImportContent (fileContent, onFinish)
|
||||
@ -71,7 +69,7 @@ export class ImporterBim extends ImporterBase
|
||||
|
||||
ImportElement (bimElement)
|
||||
{
|
||||
let defaultMaterialIndex = this.GetMaterialIndexForColor (
|
||||
let defaultMaterialIndex = this.colorToMaterial.GetMaterialIndex (
|
||||
bimElement.color.r,
|
||||
bimElement.color.g,
|
||||
bimElement.color.b,
|
||||
@ -83,7 +81,7 @@ export class ImporterBim extends ImporterBase
|
||||
let bimMesh = this.meshIdToMesh.get (bimElement.mesh_id);
|
||||
let mesh = this.ImportMesh (bimMesh, (triangleIndex) => {
|
||||
if (bimElement.face_colors) {
|
||||
let faceMaterialIndex = this.GetMaterialIndexForColor (
|
||||
let faceMaterialIndex = this.colorToMaterial.GetMaterialIndex (
|
||||
bimElement.face_colors[triangleIndex * 4 + 0],
|
||||
bimElement.face_colors[triangleIndex * 4 + 1],
|
||||
bimElement.face_colors[triangleIndex * 4 + 2],
|
||||
@ -178,23 +176,4 @@ export class ImporterBim extends ImporterBase
|
||||
}
|
||||
target.AddPropertyGroup (propertyGroup);
|
||||
}
|
||||
|
||||
GetMaterialIndexForColor (r, g, b, a)
|
||||
{
|
||||
let colorKey = IntegerToHexString (r) + IntegerToHexString (g) + IntegerToHexString (b) + IntegerToHexString (a);
|
||||
if (this.colorToMaterialIndex.has (colorKey)) {
|
||||
return this.colorToMaterialIndex.get (colorKey);
|
||||
} else {
|
||||
let material = new PhongMaterial ();
|
||||
material.name = colorKey;
|
||||
material.color = new Color (r, g, b);
|
||||
if (a < 255) {
|
||||
material.opacity = a / 255.0;
|
||||
UpdateMaterialTransparency (material);
|
||||
}
|
||||
let materialIndex = this.model.AddMaterial (material);
|
||||
this.colorToMaterialIndex.set (colorKey, materialIndex);
|
||||
return materialIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,8 +1,7 @@
|
||||
import { Direction } from '../geometry/geometry.js';
|
||||
import { GetExternalLibPath } from '../io/externallibs.js';
|
||||
import { Node } from '../model/node.js';
|
||||
import { ColorFromFloatComponents, ColorToHexString } from '../model/color.js';
|
||||
import { PhongMaterial } from '../model/material.js';
|
||||
import { ColorFromFloatComponents } from '../model/color.js';
|
||||
import { ConvertThreeGeometryToMesh } from '../threejs/threeutils.js';
|
||||
import { ImporterBase } from './importerbase.js';
|
||||
import { ColorToMaterialConverter } from './importerutils.js';
|
||||
@ -63,12 +62,7 @@ export class ImporterStp extends ImporterBase
|
||||
if (!stepContent.success) {
|
||||
return;
|
||||
}
|
||||
let colorToMaterial = new ColorToMaterialConverter ((color) => {
|
||||
let material = new PhongMaterial ();
|
||||
material.name = ColorToHexString (color).toUpperCase ();
|
||||
material.color = color;
|
||||
return this.model.AddMaterial (material);
|
||||
});
|
||||
let colorToMaterial = new ColorToMaterialConverter (this.model);
|
||||
let rootNode = this.model.GetRootNode ();
|
||||
this.ImportNode (stepContent, stepContent.root, rootNode, colorToMaterial);
|
||||
onFinish ();
|
||||
@ -95,7 +89,7 @@ export class ImporterStp extends ImporterBase
|
||||
let materialIndex = null;
|
||||
if (occtMesh.color) {
|
||||
let color = ColorFromFloatComponents (occtMesh.color[0], occtMesh.color[1], occtMesh.color[2]);
|
||||
materialIndex = colorToMaterial.GetMaterialIndex (color);
|
||||
materialIndex = colorToMaterial.GetMaterialIndex (color.r, color.g, color.b, null);
|
||||
}
|
||||
let mesh = ConvertThreeGeometryToMesh (occtMesh, materialIndex);
|
||||
if (occtMesh.name) {
|
||||
@ -104,7 +98,7 @@ export class ImporterStp extends ImporterBase
|
||||
if (occtMesh.face_colors) {
|
||||
for (let faceColorGroup of occtMesh.face_colors) {
|
||||
let faceColor = ColorFromFloatComponents (faceColorGroup.color[0], faceColorGroup.color[1], faceColorGroup.color[2]);
|
||||
let faceMaterialIndex = colorToMaterial.GetMaterialIndex (faceColor);
|
||||
let faceMaterialIndex = colorToMaterial.GetMaterialIndex (faceColor.r, faceColor.g, faceColor.b, null);
|
||||
for (let i = faceColorGroup.first; i <= faceColorGroup.last; i++) {
|
||||
let triangle = mesh.GetTriangle (i);
|
||||
triangle.SetMaterial (faceMaterialIndex);
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import { IsLower } from '../geometry/geometry.js';
|
||||
import { ColorToHexString } from '../model/color.js';
|
||||
import { PhongMaterial } from '../model/material.js';
|
||||
import { Color, IntegerToHexString } from '../model/color.js';
|
||||
|
||||
export function NameFromLine (line, startIndex, commentChar)
|
||||
{
|
||||
@ -66,21 +67,36 @@ export function UpdateMaterialTransparency (material)
|
||||
|
||||
export class ColorToMaterialConverter
|
||||
{
|
||||
constructor (createMaterialFunc)
|
||||
constructor (model)
|
||||
{
|
||||
this.createMaterialFunc = createMaterialFunc;
|
||||
this.model = model;
|
||||
this.colorToMaterialIndex = new Map ();
|
||||
}
|
||||
|
||||
GetMaterialIndex (color)
|
||||
GetMaterialIndex (r, g, b, a)
|
||||
{
|
||||
let colorKey = ColorToHexString (color);
|
||||
let colorKey =
|
||||
IntegerToHexString (r) +
|
||||
IntegerToHexString (g) +
|
||||
IntegerToHexString (b);
|
||||
let hasAlpha = (a !== undefined && a !== null);
|
||||
if (hasAlpha) {
|
||||
colorKey += IntegerToHexString (a);
|
||||
}
|
||||
|
||||
if (this.colorToMaterialIndex.has (colorKey)) {
|
||||
return this.colorToMaterialIndex.get (colorKey);
|
||||
} else {
|
||||
let materialIndex = this.createMaterialFunc (color);
|
||||
this.colorToMaterialIndex.set (colorKey, materialIndex);
|
||||
return materialIndex;
|
||||
let material = new PhongMaterial ();
|
||||
material.name = colorKey.toUpperCase ();
|
||||
material.color = new Color (r, g, b);
|
||||
if (hasAlpha && a < 255) {
|
||||
material.opacity = a / 255.0;
|
||||
UpdateMaterialTransparency (material);
|
||||
}
|
||||
let materialIndex = this.model.AddMaterial (material);
|
||||
this.colorToMaterialIndex.set (colorKey, materialIndex);
|
||||
return materialIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user