From dbf1865c46384617fe0164fd65a1e837016de485 Mon Sep 17 00:00:00 2001 From: kovacsv Date: Wed, 5 Jan 2022 19:10:12 +0100 Subject: [PATCH] Set vertex color for materials only if they have vertex color in all appearances. --- source/model/meshinstance.js | 5 ++++ source/model/model.js | 9 +++++++ source/model/modelfinalization.js | 41 ++++++++++++++++++++++++------- source/model/object.js | 5 ++++ 4 files changed, 51 insertions(+), 9 deletions(-) diff --git a/source/model/meshinstance.js b/source/model/meshinstance.js index ee913a7..255c7b3 100644 --- a/source/model/meshinstance.js +++ b/source/model/meshinstance.js @@ -47,6 +47,11 @@ OV.MeshInstance = class extends OV.ModelObject3D return this.mesh.VertexCount (); } + VertexColorCount () + { + return this.mesh.VertexColorCount (); + } + NormalCount () { return this.mesh.NormalCount (); diff --git a/source/model/model.js b/source/model/model.js index 31bad71..9d2f521 100644 --- a/source/model/model.js +++ b/source/model/model.js @@ -41,6 +41,15 @@ OV.Model = class extends OV.ModelObject3D return count; } + VertexColorCount () + { + let count = 0; + this.EnumerateMeshInstances ((meshInstance) => { + count += meshInstance.VertexColorCount (); + }); + return count; + } + NormalCount () { let count = 0; diff --git a/source/model/modelfinalization.js b/source/model/modelfinalization.js index c3c8aaf..e8bd1bf 100644 --- a/source/model/modelfinalization.js +++ b/source/model/modelfinalization.js @@ -16,17 +16,45 @@ OV.ModelFinalizer = class this.Reset (); this.FinalizeMeshes (model); + this.FinalizeMaterials (model); this.FinalizeNodes (model); } + FinalizeMaterials (model) + { + let hasVertexColors = (model.VertexColorCount () > 0); + if (!hasVertexColors) { + return; + } + + let materialHasVertexColors = new Map (); + for (let meshIndex = 0; meshIndex < model.MeshCount (); meshIndex++) { + let mesh = model.GetMesh (meshIndex); + for (let triangleIndex = 0; triangleIndex < mesh.TriangleCount (); triangleIndex++) { + let triangle = mesh.GetTriangle (triangleIndex); + let hasVertexColors = triangle.HasVertexColors (); + if (!materialHasVertexColors.has (triangle.mat)) { + materialHasVertexColors.set (triangle.mat, hasVertexColors); + } else if (!hasVertexColors) { + materialHasVertexColors.set (triangle.mat, false); + } + } + } + + for (let [materialIndex, hasVertexColors] of materialHasVertexColors) { + let material = model.GetMaterial (materialIndex); + material.vertexColors = hasVertexColors; + } + } + FinalizeMeshes (model) { - for (let i = 0; i < model.MeshCount (); i++) { - let mesh = model.GetMesh (i); + for (let meshIndex = 0; meshIndex < model.MeshCount (); meshIndex++) { + let mesh = model.GetMesh (meshIndex); let type = OV.GetMeshType (mesh); if (type === OV.MeshType.Empty) { - model.RemoveMesh (i); - i = i - 1; + model.RemoveMesh (meshIndex); + meshIndex = meshIndex - 1; continue; } this.FinalizeMesh (model, mesh); @@ -113,11 +141,6 @@ OV.ModelFinalizer = class if (triangle.mat === null) { triangle.mat = this.GetDefaultMaterialIndex (model); } - - if (triangle.HasVertexColors ()) { - let material = model.GetMaterial (triangle.mat); - material.vertexColors = true; - } } if (meshStatus.calculateCurveNormals) { diff --git a/source/model/object.js b/source/model/object.js index 3b2eb77..c9a4e48 100644 --- a/source/model/object.js +++ b/source/model/object.js @@ -10,6 +10,11 @@ OV.Object3D = class return 0; } + VertexColorCount () + { + return 0; + } + NormalCount () { return 0;