Add test for exporting vertex colors to glTF.
This commit is contained in:
parent
7603537142
commit
f57d31eca8
@ -66,11 +66,11 @@ OV.ConvertMeshToMeshBuffer = function (mesh)
|
||||
{
|
||||
function AddVertexToPrimitiveBuffer (mesh, indices, primitiveBuffer, meshVertexToPrimitiveVertices)
|
||||
{
|
||||
function GetVertexColorOrDefault (mesh, vertexColorIndex, forceVertexColors)
|
||||
function GetColorOrDefault (mesh, colorIndex, forceColors)
|
||||
{
|
||||
if (vertexColorIndex !== null) {
|
||||
return mesh.GetVertexColor (vertexColorIndex);
|
||||
} else if (forceVertexColors) {
|
||||
if (colorIndex !== null) {
|
||||
return mesh.GetVertexColor (colorIndex);
|
||||
} else if (forceColors) {
|
||||
return new OV.Color (0, 0, 0);
|
||||
} else {
|
||||
return null;
|
||||
@ -90,7 +90,7 @@ OV.ConvertMeshToMeshBuffer = function (mesh)
|
||||
|
||||
function AddVertex (mesh, indices, primitiveBuffer)
|
||||
{
|
||||
let forceVertexColors = mesh.VertexColorCount () > 0;
|
||||
let forceColors = mesh.VertexColorCount () > 0;
|
||||
let forceUVs = mesh.TextureUVCount () > 0;
|
||||
|
||||
let vertex = mesh.GetVertex (indices.vertex);
|
||||
@ -100,9 +100,9 @@ OV.ConvertMeshToMeshBuffer = function (mesh)
|
||||
primitiveBuffer.indices.push (primitiveVertexIndex);
|
||||
primitiveBuffer.vertices.push (vertex.x, vertex.y, vertex.z);
|
||||
|
||||
let vertexColor = GetVertexColorOrDefault (mesh, indices.color, forceVertexColors);
|
||||
if (vertexColor !== null) {
|
||||
primitiveBuffer.colors.push (vertexColor.r / 255.0, vertexColor.g / 255.0, vertexColor.b / 255.0);
|
||||
let color = GetColorOrDefault (mesh, indices.color, forceColors);
|
||||
if (color !== null) {
|
||||
primitiveBuffer.colors.push (color.r / 255.0, color.g / 255.0, color.b / 255.0);
|
||||
}
|
||||
|
||||
primitiveBuffer.normals.push (normal.x, normal.y, normal.z);
|
||||
@ -114,7 +114,7 @@ OV.ConvertMeshToMeshBuffer = function (mesh)
|
||||
|
||||
return {
|
||||
index : primitiveVertexIndex,
|
||||
vertexColor : vertexColor,
|
||||
color : color,
|
||||
normal : normal,
|
||||
uv : uv
|
||||
};
|
||||
@ -122,13 +122,13 @@ OV.ConvertMeshToMeshBuffer = function (mesh)
|
||||
|
||||
function FindMatchingPrimitiveVertex (mesh, primitiveVertices, indices)
|
||||
{
|
||||
function IsEqualVertexColor (mesh, vertexColorIndex, existingVertexColor)
|
||||
function IsEqualColor (mesh, colorIndex, existingColor)
|
||||
{
|
||||
if (existingVertexColor === null && vertexColorIndex === null) {
|
||||
if (existingColor === null && colorIndex === null) {
|
||||
return true;
|
||||
}
|
||||
let vertexColor = GetVertexColorOrDefault (mesh, vertexColorIndex, true);
|
||||
return OV.ColorIsEqual (existingVertexColor, vertexColor);
|
||||
let color = GetColorOrDefault (mesh, colorIndex, true);
|
||||
return OV.ColorIsEqual (existingColor, color);
|
||||
}
|
||||
|
||||
function IsEqualNormal (mesh, normalIndex, existingNormal)
|
||||
@ -148,10 +148,10 @@ OV.ConvertMeshToMeshBuffer = function (mesh)
|
||||
|
||||
for (let i = 0; i < primitiveVertices.length; i++) {
|
||||
let primitiveVertex = primitiveVertices[i];
|
||||
let equalVertexColor = IsEqualVertexColor (mesh, indices.color, primitiveVertex.vertexColor);
|
||||
let equalColor = IsEqualColor (mesh, indices.color, primitiveVertex.color);
|
||||
let equalNormal = IsEqualNormal (mesh, indices.normal, primitiveVertex.normal);
|
||||
let equalUv = IsEqualUV (mesh, indices.uv, primitiveVertex.uv);
|
||||
if (equalVertexColor && equalNormal && equalUv) {
|
||||
if (equalColor && equalNormal && equalUv) {
|
||||
return primitiveVertex;
|
||||
}
|
||||
}
|
||||
|
||||
@ -35,11 +35,6 @@ OV.TransformMesh = function (mesh, transformation)
|
||||
}
|
||||
};
|
||||
|
||||
OV.MeshHasVertexColors = function (mesh)
|
||||
{
|
||||
return mesh.VertexCount () === mesh.VertexColorCount ();
|
||||
};
|
||||
|
||||
OV.FlipMeshTrianglesOrientation = function (mesh)
|
||||
{
|
||||
for (let i = 0; i < mesh.TriangleCount (); i++) {
|
||||
|
||||
@ -227,7 +227,7 @@ OV.ConvertModelToThreeObject = function (model, params, output, callbacks)
|
||||
end : -1
|
||||
});
|
||||
|
||||
let meshHasVertexColors = OV.MeshHasVertexColors (mesh);
|
||||
let meshHasVertexColors = (mesh.VertexColorCount () > 0);
|
||||
let meshHasUVs = (mesh.TextureUVCount () > 0);
|
||||
for (let i = 0; i < triangleIndices.length; i++) {
|
||||
let triangleIndex = triangleIndices[i];
|
||||
|
||||
@ -219,3 +219,29 @@ describe ('Export-Import Test', function () {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe ('Export-Import Vertex Colors Test', function () {
|
||||
it ('Export-Import Vertex Colors glTF', function (done) {
|
||||
let model = new OV.Model ();
|
||||
let mesh = new OV.Mesh ();
|
||||
mesh.AddVertex (new OV.Coord3D (0.0, 0.0, 0.0));
|
||||
mesh.AddVertex (new OV.Coord3D (1.0, 0.0, 0.0));
|
||||
mesh.AddVertex (new OV.Coord3D (1.0, 1.0, 0.0));
|
||||
mesh.AddVertexColor (new OV.Color (1.0, 0.0, 0.0));
|
||||
mesh.AddTriangle (new OV.Triangle (0, 1, 2).SetVertexColors (0, 0, 0));
|
||||
model.AddMeshToRootNode (mesh);
|
||||
OV.FinalizeModel (model, () => { return new OV.PhongMaterial (); });
|
||||
ExportImport (model, OV.FileFormat.Binary, 'glb', (model2) => {
|
||||
assert.strictEqual (model2.MeshCount (), 1);
|
||||
let mesh2 = model2.GetMesh (0);
|
||||
assert.strictEqual (mesh2.VertexCount (), 3);
|
||||
assert.strictEqual (mesh2.VertexColorCount (), 3);
|
||||
assert.strictEqual (mesh2.TriangleCount (), 1);
|
||||
let triangle2 = mesh2.GetTriangle (0);
|
||||
assert.strictEqual (triangle2.c0, 0);
|
||||
assert.strictEqual (triangle2.c1, 1);
|
||||
assert.strictEqual (triangle2.c2, 2);
|
||||
done ();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user