Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Off export color and import alpha color. #517

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions source/engine/export/exportermodel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CopyObjectAttributes } from '../core/core.js';
import { Transformation } from '../geometry/transformation.js';
import { CalculateTriangleNormal, TransformMesh } from '../model/meshutils.js';
import { ColorComponentFromFloat } from '../model/color.js';

export class ExporterSettings
{
Expand Down Expand Up @@ -147,6 +148,46 @@ export class ExporterModel
}
}

EnumerateVerticesAndTrianglesColor (callbacks)
{
let transformedMeshes = [];
this.EnumerateTransformedMeshInstances ((mesh) => {
transformedMeshes.push (mesh);
});

for (let mesh of transformedMeshes) {
let hasVertexColors = (mesh.VertexCount () === mesh.VertexColorCount ());
let vertexColor = undefined;
for (let vertexIndex = 0; vertexIndex < mesh.VertexCount (); vertexIndex++) {
if (hasVertexColors)
vertexColor = mesh.GetVertexColor (vertexIndex);

let vertex = mesh.GetVertex (vertexIndex);
callbacks.onVertexColor (vertex.x, vertex.y, vertex.z, vertexColor);
}
}

let vertexOffset = 0;
for (let mesh of transformedMeshes) {
let hasVertexColors = (mesh.VertexCount () === mesh.VertexColorCount ());
for (const triangle of mesh.triangles) {
let material = this.GetMaterial (triangle.mat);
let triangleColor = {
r : Math.round (material.color.r),
g : Math.round (material.color.g),
b : Math.round (material.color.b),
a : ColorComponentFromFloat (material.opacity),
};

if (hasVertexColors)
triangleColor = undefined;

callbacks.onTriangleColor (triangle.v0 + vertexOffset, triangle.v1 + vertexOffset, triangle.v2 + vertexOffset, triangleColor);
}
vertexOffset += mesh.VertexCount ();
}
}

EnumerateTrianglesWithNormals (onTriangle)
{
this.EnumerateTransformedMeshInstances ((mesh) => {
Expand Down
28 changes: 23 additions & 5 deletions source/engine/export/exporteroff.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,30 @@ export class ExporterOff extends ExporterBase
offWriter.WriteLine ('OFF');
offWriter.WriteArrayLine ([exporterModel.VertexCount (), exporterModel.TriangleCount (), 0]);

exporterModel.EnumerateVerticesAndTriangles ({
onVertex : function (x, y, z) {
offWriter.WriteArrayLine ([x, y, z]);
exporterModel.EnumerateVerticesAndTrianglesColor ({
onVertexColor : function (x, y, z, color) {
if (color)
{
offWriter.WriteArrayLine ([x, y, z, color.r, color.g, color.b]);
}
else
{
offWriter.WriteArrayLine ([x, y, z]);
}
},
onTriangle : function (v0, v1, v2) {
offWriter.WriteArrayLine ([3, v0, v1, v2]);
onTriangleColor : function (v0, v1, v2, color) {
if (color)
{
// ignore alpha with max value
if (color.a === 255)
offWriter.WriteArrayLine ([3, v0, v1, v2, color.r, color.g, color.b]);
else
offWriter.WriteArrayLine ([3, v0, v1, v2, color.r, color.g, color.b, color.a]);
}
else
{
offWriter.WriteArrayLine ([3, v0, v1, v2]);
}
}
});

Expand Down
28 changes: 20 additions & 8 deletions source/engine/import/importeroff.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Coord3D } from '../geometry/coord3d.js';
import { Direction } from '../geometry/geometry.js';
import { ArrayBufferToUtf8String } from '../io/bufferutils.js';
import { RGBColor, ColorComponentFromFloat } from '../model/color.js';
import { RGBColor, RGBAColor, ColorComponentFromFloat } from '../model/color.js';
import { Mesh } from '../model/mesh.js';
import { Triangle } from '../model/triangle.js';
import { ImporterBase } from './importerbase.js';
Expand Down Expand Up @@ -114,13 +114,25 @@ export class ImporterOff extends ImporterBase
return;
}
let materialIndex = null;
if (!hasVertexColors && parameters.length >= vertexCount + 4) {
let color = new RGBColor (
CreateColorComponent (parameters[vertexCount + 1]),
CreateColorComponent (parameters[vertexCount + 2]),
CreateColorComponent (parameters[vertexCount + 3])
);
materialIndex = this.colorToMaterial.GetMaterialIndex (color.r, color.g, color.b);
if (!hasVertexColors)
{
if (parameters.length >= vertexCount + 5) {
let color = new RGBAColor (
CreateColorComponent (parameters[vertexCount + 1]),
CreateColorComponent (parameters[vertexCount + 2]),
CreateColorComponent (parameters[vertexCount + 3]),
CreateColorComponent (parameters[vertexCount + 4])
);
materialIndex = this.colorToMaterial.GetMaterialIndex (color.r, color.g, color.b, color.a);
}
else if (parameters.length >= vertexCount + 4) {
let color = new RGBColor (
CreateColorComponent (parameters[vertexCount + 1]),
CreateColorComponent (parameters[vertexCount + 2]),
CreateColorComponent (parameters[vertexCount + 3])
);
materialIndex = this.colorToMaterial.GetMaterialIndex (color.r, color.g, color.b);
}
}
for (let i = 0; i < vertexCount - 2; i++) {
let v0 = parseInt (parameters[1]);
Expand Down
8 changes: 4 additions & 4 deletions test/tests/exporter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,10 @@ describe ('Exporter', function () {
'0 1 0',
'-1 0 0',
'0 0 1',
'3 0 1 2',
'3 3 4 5',
'3 3 5 6',
'3 3 7 5',
'3 0 1 2 255 0 0',
'3 3 4 5 255 0 0',
'3 3 5 6 0 255 0',
'3 3 7 5 0 255 0',
''
].join ('\n'));

Expand Down
6 changes: 5 additions & 1 deletion test/tests/exportimport_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ describe ('Export-Import Test', function () {
it ('Export-Import Off', function (done) {
let model = CreateTestModel ();
ExportImport (model, OV.FileFormat.Text, 'off', (model2) => {
CheckSingleMeshModel (model, model2);
// Single Mesh Model with 2 materials
assert.strictEqual (model2.MaterialCount (), 2);
assert.strictEqual (model2.MeshInstanceCount (), 1);
assert.strictEqual (model.TriangleCount (), model2.TriangleCount ());
CheckModelBounds (model, model2);
done ();
});
});
Expand Down
8 changes: 4 additions & 4 deletions website/assets/models/cube.off
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
OFF
# cube.off
# A cube

8 6 12
1.0 0.0 1.4142
0.0 1.0 1.4142
Expand All @@ -14,6 +14,6 @@ OFF
4 0 1 2 3 255 0 0 #red
4 7 4 0 3 0 255 0 #green
4 4 5 1 0 0 0 255 #blue
4 5 6 2 1 0 255 0
4 3 2 6 7 0 0 255
4 6 5 4 7 255 0 0
4 5 6 2 1 0 255 0 100
4 3 2 6 7 0 0 255 100
4 6 5 4 7 255 0 0 100
19 changes: 19 additions & 0 deletions website/assets/models/cube_vertex.off
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
OFF
# cube_vertex.off
# A cube

8 6 12
1.0 0.0 1.4142 255 0 0 #red
0.0 1.0 1.4142 255 0 0 #red
-1.0 0.0 1.4142 255 0 0 #red
0.0 -1.0 1.4142 255 0 0 #red
1.0 0.0 0.0 0 0 255 #blue
0.0 1.0 0.0 0 0 255 #blue
-1.0 0.0 0.0 0 0 255 #blue
0.0 -1.0 0.0 0 0 255 #blue
4 0 1 2 3
4 7 4 0 3
4 4 5 1 0
4 5 6 2 1
4 3 2 6 7
4 6 5 4 7
Loading