Implement face colors for dotbim #270
This commit is contained in:
parent
48fc88cb70
commit
dc60397102
@ -1,6 +1,5 @@
|
||||
import { FileFormat } from '../io/fileutils.js';
|
||||
import { ColorComponentFromFloat } from '../model/color.js';
|
||||
import { ConvertMeshToMeshBuffer } from '../model/meshbuffer.js';
|
||||
import { PropertyToString } from '../model/property.js';
|
||||
import { ExportedFile, ExporterBase } from './exporterbase.js';
|
||||
|
||||
@ -39,42 +38,77 @@ export class ExporterBim extends ExporterBase
|
||||
|
||||
let meshId = 0;
|
||||
exporterModel.EnumerateTransformedMeshes ((mesh) => {
|
||||
let meshBuffer = ConvertMeshToMeshBuffer (mesh);
|
||||
for (let primitiveIndex = 0; primitiveIndex < meshBuffer.PrimitiveCount (); primitiveIndex++) {
|
||||
let primitive = meshBuffer.GetPrimitive (primitiveIndex);
|
||||
let material = exporterModel.GetMaterial (primitive.material);
|
||||
let bimMesh = {
|
||||
mesh_id : meshId,
|
||||
coordinates : primitive.vertices,
|
||||
indices : primitive.indices
|
||||
let bimMesh = {
|
||||
mesh_id : meshId,
|
||||
coordinates : [],
|
||||
indices : []
|
||||
};
|
||||
|
||||
mesh.EnumerateVertices ((vertex) => {
|
||||
bimMesh.coordinates.push (vertex.x, vertex.y, vertex.z);
|
||||
});
|
||||
mesh.EnumerateTriangleVertexIndices ((v0, v1, v2) => {
|
||||
bimMesh.indices.push (v0, v1, v2);
|
||||
});
|
||||
|
||||
let bimElement = {
|
||||
mesh_id : meshId,
|
||||
type : 'Other',
|
||||
color : {
|
||||
r : 200,
|
||||
g : 200,
|
||||
b : 200,
|
||||
a : 255
|
||||
},
|
||||
vector : {
|
||||
x : 0.0,
|
||||
y : 0.0,
|
||||
z : 0.0
|
||||
},
|
||||
rotation : {
|
||||
qx: 0.0,
|
||||
qy: 0.0,
|
||||
qz: 0.0,
|
||||
qw: 1.0
|
||||
},
|
||||
guid : GenerateGuid ()
|
||||
};
|
||||
|
||||
let defaultColor = null;
|
||||
let hasOnlyOneColor = true;
|
||||
let faceColors = [];
|
||||
for (let i = 0; i < mesh.TriangleCount (); i++) {
|
||||
let triangle = mesh.GetTriangle (i);
|
||||
let material = exporterModel.GetMaterial (triangle.mat);
|
||||
let faceColor = {
|
||||
r : material.color.r,
|
||||
g : material.color.g,
|
||||
b : material.color.b,
|
||||
a : ColorComponentFromFloat (material.opacity),
|
||||
};
|
||||
let bimElement = {
|
||||
mesh_id : meshId,
|
||||
type : 'Other',
|
||||
color : {
|
||||
r : material.color.r,
|
||||
g : material.color.g,
|
||||
b : material.color.b,
|
||||
a : ColorComponentFromFloat (material.opacity)
|
||||
},
|
||||
vector : {
|
||||
x : 0.0,
|
||||
y : 0.0,
|
||||
z : 0.0
|
||||
},
|
||||
rotation : {
|
||||
qx: 0.0,
|
||||
qy: 0.0,
|
||||
qz: 0.0,
|
||||
qw: 1.0
|
||||
},
|
||||
guid : GenerateGuid ()
|
||||
};
|
||||
this.ExportProperties (mesh, bimElement);
|
||||
bimContent.meshes.push (bimMesh);
|
||||
bimContent.elements.push (bimElement);
|
||||
meshId += 1;
|
||||
faceColors.push (faceColor.r, faceColor.g, faceColor.b, faceColor.a);
|
||||
if (hasOnlyOneColor) {
|
||||
if (defaultColor === null) {
|
||||
defaultColor = faceColor;
|
||||
} else {
|
||||
if (defaultColor.r !== faceColor.r || defaultColor.g !== faceColor.g || defaultColor.b !== faceColor.b || defaultColor.a !== faceColor.a) {
|
||||
hasOnlyOneColor = false;
|
||||
defaultColor = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (hasOnlyOneColor) {
|
||||
bimElement.color = defaultColor;
|
||||
} else {
|
||||
bimElement.face_colors = faceColors;
|
||||
}
|
||||
|
||||
this.ExportProperties (mesh, bimElement);
|
||||
bimContent.meshes.push (bimMesh);
|
||||
bimContent.elements.push (bimElement);
|
||||
meshId += 1;
|
||||
});
|
||||
|
||||
let bimFile = new ExportedFile ('model.bim');
|
||||
|
||||
@ -71,32 +71,29 @@ export class ImporterBim extends ImporterBase
|
||||
|
||||
ImportElement (bimElement)
|
||||
{
|
||||
let materialIndex = null;
|
||||
if (bimElement.color) {
|
||||
let colorKey =
|
||||
IntegerToHexString (bimElement.color.r) +
|
||||
IntegerToHexString (bimElement.color.g) +
|
||||
IntegerToHexString (bimElement.color.b) +
|
||||
IntegerToHexString (bimElement.color.a);
|
||||
if (this.colorToMaterialIndex.has (colorKey)) {
|
||||
materialIndex = this.colorToMaterialIndex.get (colorKey);
|
||||
} else {
|
||||
let material = new PhongMaterial ();
|
||||
material.name = colorKey;
|
||||
material.color = new Color (bimElement.color.r, bimElement.color.g, bimElement.color.b);
|
||||
if (bimElement.color.a < 255) {
|
||||
material.opacity = bimElement.color.a / 255.0;
|
||||
UpdateMaterialTransparency (material);
|
||||
}
|
||||
materialIndex = this.model.AddMaterial (material);
|
||||
this.colorToMaterialIndex.set (colorKey, materialIndex);
|
||||
}
|
||||
}
|
||||
let defaultMaterialIndex = this.GetMaterialIndexForColor (
|
||||
bimElement.color.r,
|
||||
bimElement.color.g,
|
||||
bimElement.color.b,
|
||||
bimElement.color.a
|
||||
);
|
||||
|
||||
let rootNode = this.model.GetRootNode ();
|
||||
|
||||
let bimMesh = this.meshIdToMesh.get (bimElement.mesh_id);
|
||||
let mesh = this.ImportMesh (bimMesh, materialIndex);
|
||||
let mesh = this.ImportMesh (bimMesh, (triangleIndex) => {
|
||||
if (bimElement.face_colors) {
|
||||
let faceMaterialIndex = this.GetMaterialIndexForColor (
|
||||
bimElement.face_colors[triangleIndex * 4 + 0],
|
||||
bimElement.face_colors[triangleIndex * 4 + 1],
|
||||
bimElement.face_colors[triangleIndex * 4 + 2],
|
||||
bimElement.face_colors[triangleIndex * 4 + 3]
|
||||
);
|
||||
return faceMaterialIndex;
|
||||
} else {
|
||||
return defaultMaterialIndex;
|
||||
}
|
||||
});
|
||||
let meshIndex = this.model.AddMesh (mesh);
|
||||
|
||||
let elementNode = new Node ();
|
||||
@ -128,7 +125,7 @@ export class ImporterBim extends ImporterBase
|
||||
return mesh;
|
||||
}
|
||||
|
||||
ImportMesh (bimMesh, materialIndex)
|
||||
ImportMesh (bimMesh, getMaterialIndex)
|
||||
{
|
||||
let mesh = new Mesh ();
|
||||
|
||||
@ -146,9 +143,7 @@ export class ImporterBim extends ImporterBase
|
||||
bimMesh.indices[i + 1],
|
||||
bimMesh.indices[i + 2]
|
||||
);
|
||||
if (materialIndex !== null) {
|
||||
triangle.SetMaterial (materialIndex);
|
||||
}
|
||||
triangle.SetMaterial (getMaterialIndex (i / 3));
|
||||
mesh.AddTriangle (triangle);
|
||||
}
|
||||
|
||||
@ -183,4 +178,23 @@ 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,4 +1,4 @@
|
||||
import { IsDefined, ValueOrDefault, CopyObjectAttributes } from './core/core.js';
|
||||
import { IsDefined, ValueOrDefault, CopyObjectAttributes, IsObjectEmpty } from './core/core.js';
|
||||
import { TaskRunner, RunTaskAsync, RunTasks, RunTasksBatch, WaitWhile } from './core/taskrunner.js';
|
||||
import { Exporter } from './export/exporter.js';
|
||||
import { Exporter3dm } from './export/exporter3dm.js';
|
||||
@ -74,6 +74,7 @@ export {
|
||||
IsDefined,
|
||||
ValueOrDefault,
|
||||
CopyObjectAttributes,
|
||||
IsObjectEmpty,
|
||||
TaskRunner,
|
||||
RunTaskAsync,
|
||||
RunTasks,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user