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 { FileFormat } from '../io/fileutils.js';
|
||||||
import { ColorComponentFromFloat } from '../model/color.js';
|
import { ColorComponentFromFloat } from '../model/color.js';
|
||||||
import { ConvertMeshToMeshBuffer } from '../model/meshbuffer.js';
|
|
||||||
import { PropertyToString } from '../model/property.js';
|
import { PropertyToString } from '../model/property.js';
|
||||||
import { ExportedFile, ExporterBase } from './exporterbase.js';
|
import { ExportedFile, ExporterBase } from './exporterbase.js';
|
||||||
|
|
||||||
@ -39,42 +38,77 @@ export class ExporterBim extends ExporterBase
|
|||||||
|
|
||||||
let meshId = 0;
|
let meshId = 0;
|
||||||
exporterModel.EnumerateTransformedMeshes ((mesh) => {
|
exporterModel.EnumerateTransformedMeshes ((mesh) => {
|
||||||
let meshBuffer = ConvertMeshToMeshBuffer (mesh);
|
let bimMesh = {
|
||||||
for (let primitiveIndex = 0; primitiveIndex < meshBuffer.PrimitiveCount (); primitiveIndex++) {
|
mesh_id : meshId,
|
||||||
let primitive = meshBuffer.GetPrimitive (primitiveIndex);
|
coordinates : [],
|
||||||
let material = exporterModel.GetMaterial (primitive.material);
|
indices : []
|
||||||
let bimMesh = {
|
};
|
||||||
mesh_id : meshId,
|
|
||||||
coordinates : primitive.vertices,
|
mesh.EnumerateVertices ((vertex) => {
|
||||||
indices : primitive.indices
|
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 = {
|
faceColors.push (faceColor.r, faceColor.g, faceColor.b, faceColor.a);
|
||||||
mesh_id : meshId,
|
if (hasOnlyOneColor) {
|
||||||
type : 'Other',
|
if (defaultColor === null) {
|
||||||
color : {
|
defaultColor = faceColor;
|
||||||
r : material.color.r,
|
} else {
|
||||||
g : material.color.g,
|
if (defaultColor.r !== faceColor.r || defaultColor.g !== faceColor.g || defaultColor.b !== faceColor.b || defaultColor.a !== faceColor.a) {
|
||||||
b : material.color.b,
|
hasOnlyOneColor = false;
|
||||||
a : ColorComponentFromFloat (material.opacity)
|
defaultColor = null;
|
||||||
},
|
}
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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');
|
let bimFile = new ExportedFile ('model.bim');
|
||||||
|
|||||||
@ -71,32 +71,29 @@ export class ImporterBim extends ImporterBase
|
|||||||
|
|
||||||
ImportElement (bimElement)
|
ImportElement (bimElement)
|
||||||
{
|
{
|
||||||
let materialIndex = null;
|
let defaultMaterialIndex = this.GetMaterialIndexForColor (
|
||||||
if (bimElement.color) {
|
bimElement.color.r,
|
||||||
let colorKey =
|
bimElement.color.g,
|
||||||
IntegerToHexString (bimElement.color.r) +
|
bimElement.color.b,
|
||||||
IntegerToHexString (bimElement.color.g) +
|
bimElement.color.a
|
||||||
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 rootNode = this.model.GetRootNode ();
|
let rootNode = this.model.GetRootNode ();
|
||||||
|
|
||||||
let bimMesh = this.meshIdToMesh.get (bimElement.mesh_id);
|
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 meshIndex = this.model.AddMesh (mesh);
|
||||||
|
|
||||||
let elementNode = new Node ();
|
let elementNode = new Node ();
|
||||||
@ -128,7 +125,7 @@ export class ImporterBim extends ImporterBase
|
|||||||
return mesh;
|
return mesh;
|
||||||
}
|
}
|
||||||
|
|
||||||
ImportMesh (bimMesh, materialIndex)
|
ImportMesh (bimMesh, getMaterialIndex)
|
||||||
{
|
{
|
||||||
let mesh = new Mesh ();
|
let mesh = new Mesh ();
|
||||||
|
|
||||||
@ -146,9 +143,7 @@ export class ImporterBim extends ImporterBase
|
|||||||
bimMesh.indices[i + 1],
|
bimMesh.indices[i + 1],
|
||||||
bimMesh.indices[i + 2]
|
bimMesh.indices[i + 2]
|
||||||
);
|
);
|
||||||
if (materialIndex !== null) {
|
triangle.SetMaterial (getMaterialIndex (i / 3));
|
||||||
triangle.SetMaterial (materialIndex);
|
|
||||||
}
|
|
||||||
mesh.AddTriangle (triangle);
|
mesh.AddTriangle (triangle);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -183,4 +178,23 @@ export class ImporterBim extends ImporterBase
|
|||||||
}
|
}
|
||||||
target.AddPropertyGroup (propertyGroup);
|
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 { TaskRunner, RunTaskAsync, RunTasks, RunTasksBatch, WaitWhile } from './core/taskrunner.js';
|
||||||
import { Exporter } from './export/exporter.js';
|
import { Exporter } from './export/exporter.js';
|
||||||
import { Exporter3dm } from './export/exporter3dm.js';
|
import { Exporter3dm } from './export/exporter3dm.js';
|
||||||
@ -74,6 +74,7 @@ export {
|
|||||||
IsDefined,
|
IsDefined,
|
||||||
ValueOrDefault,
|
ValueOrDefault,
|
||||||
CopyObjectAttributes,
|
CopyObjectAttributes,
|
||||||
|
IsObjectEmpty,
|
||||||
TaskRunner,
|
TaskRunner,
|
||||||
RunTaskAsync,
|
RunTaskAsync,
|
||||||
RunTasks,
|
RunTasks,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user