From b526bea0bbd06429c73ab89de90a757e66bc7c1c Mon Sep 17 00:00:00 2001 From: kovacsv Date: Mon, 3 Jan 2022 19:34:08 +0100 Subject: [PATCH] Add mesh material which is inherited to triangles during finalization. --- source/model/generator.js | 6 +++--- source/model/mesh.js | 13 +++++++++++++ source/model/modelfinalization.js | 28 +++++++++++++++++++++++++++- test/tests/generator_test.js | 5 +---- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/source/model/generator.js b/source/model/generator.js index 5247a16..91c19cc 100644 --- a/source/model/generator.js +++ b/source/model/generator.js @@ -28,6 +28,9 @@ OV.Generator = class if (this.params.name !== null) { this.mesh.SetName (this.params.name); } + if (this.params.material !== null) { + this.mesh.SetMaterial (this.params.material); + } this.curve = null; } @@ -65,9 +68,6 @@ OV.Generator = class AddTriangle (v0, v1, v2) { let triangle = new OV.Triangle (v0, v1, v2); - if (this.params.material !== null) { - triangle.SetMaterial (this.params.material); - } if (this.curve !== null) { triangle.SetCurve (this.curve); } diff --git a/source/model/mesh.js b/source/model/mesh.js index 739a14c..f6d7ce7 100644 --- a/source/model/mesh.js +++ b/source/model/mesh.js @@ -8,6 +8,7 @@ OV.Mesh = class extends OV.ModelObject3D this.normals = []; this.uvs = []; this.triangles = []; + this.mat = null; } VertexCount () @@ -110,6 +111,16 @@ OV.Mesh = class extends OV.ModelObject3D return this.triangles[index]; } + SetMaterial (mat) + { + this.mat = mat; + } + + GetMaterial () + { + return this.mat; + } + EnumerateVertices (onVertex) { for (const vertex of this.vertices) { @@ -165,6 +176,8 @@ OV.Mesh = class extends OV.ModelObject3D cloned.AddTriangle (triangle.Clone ()); } + cloned.SetMaterial (this.GetMaterial ()); + return cloned; } }; diff --git a/source/model/modelfinalization.js b/source/model/modelfinalization.js index eaf9401..1cf2746 100644 --- a/source/model/modelfinalization.js +++ b/source/model/modelfinalization.js @@ -111,7 +111,12 @@ OV.ModelFinalizer = class this.FinalizeTriangle (mesh, triangle, meshStatus); if (triangle.mat === null) { - triangle.mat = this.GetDefaultMaterialIndex (model); + let meshMaterial = mesh.GetMaterial (); + if (meshMaterial !== null) { + triangle.mat = meshMaterial; + } else { + triangle.mat = this.GetDefaultMaterialIndex (model); + } } if (triangle.HasVertexColors ()) { @@ -292,6 +297,19 @@ OV.CheckModel = function (model) } } + for (let i = 0; i < mesh.VertexColorCount (); i++) { + let color = mesh.GetVertexColor (i); + if (!IsCorrectNumber (color.r)) { + return false; + } + if (!IsCorrectNumber (color.g)) { + return false; + } + if (!IsCorrectNumber (color.b)) { + return false; + } + } + for (let i = 0; i < mesh.NormalCount (); i++) { let normal = mesh.GetNormal (i); if (!IsCorrectNumber (normal.x)) { @@ -322,6 +340,13 @@ OV.CheckModel = function (model) } } + let meshMaterial = mesh.GetMaterial (); + if (meshMaterial !== null) { + if (!IsCorrectIndex (meshMaterial, model.MaterialCount ())) { + return false; + } + } + return true; } @@ -331,5 +356,6 @@ OV.CheckModel = function (model) return false; } } + return true; }; diff --git a/test/tests/generator_test.js b/test/tests/generator_test.js index acb1be6..7fc4773 100644 --- a/test/tests/generator_test.js +++ b/test/tests/generator_test.js @@ -10,10 +10,7 @@ describe ('Generator', function () { it ('Cuboid with Material', function () { const params = new OV.GeneratorParams ().SetMaterial (1); const cuboid = OV.GenerateCuboid (params, 1.0, 1.0, 1.0); - for (let i = 0; i < cuboid.TriangleCount (); i++) { - const triangle = cuboid.GetTriangle (i); - assert.strictEqual (triangle.mat, 1); - } + assert.strictEqual (cuboid.GetMaterial (), 1); }); it ('Cylinder with Default Parameters', function () {