From 65c793e6707f2186880a38fd002cbc0e070c00b8 Mon Sep 17 00:00:00 2001 From: Jeremy HU Date: Mon, 14 Nov 2022 21:27:06 +1100 Subject: [PATCH] Fix object color --- application/sources/model_mesh.cc | 16 +++++---------- dust3d/base/object.h | 31 ++++++++++++++-------------- dust3d/mesh/mesh_generator.cc | 34 ++++++++++++++++++++++++------- dust3d/mesh/mesh_generator.h | 12 +++++++---- 4 files changed, 56 insertions(+), 37 deletions(-) diff --git a/application/sources/model_mesh.cc b/application/sources/model_mesh.cc index 3f6a9aa2..9118ee8e 100644 --- a/application/sources/model_mesh.cc +++ b/application/sources/model_mesh.cc @@ -130,12 +130,6 @@ ModelMesh::ModelMesh(dust3d::Object& object) m_vertices = object.vertices; m_faces = object.triangleAndQuads; - //std::map, const dust3d::ObjectNode*> nodeMap; - //for (size_t i = 0; i < object.nodes.size(); ++i) { - // const auto& node = object.nodes[i]; - // nodeMap.insert({ { node.partId, node.nodeId }, &node }); - //} - m_triangleVertexCount = (int)object.triangles.size() * 3; m_triangleVertices = new ModelOpenGLVertex[m_triangleVertexCount]; int destIndex = 0; @@ -146,10 +140,10 @@ ModelMesh::ModelMesh(dust3d::Object& object) const dust3d::Vector2 defaultUv = dust3d::Vector2(0, 0); const dust3d::Vector3 defaultTangent = dust3d::Vector3(0, 0, 0); for (size_t i = 0; i < object.triangles.size(); ++i) { - const auto& triangleColor = &object.triangleColors[i]; for (auto j = 0; j < 3; j++) { int vertexIndex = (int)object.triangles[i][j]; const dust3d::Vector3* srcVert = &object.vertices[vertexIndex]; + const dust3d::Color* srcColor = &object.vertexColors[vertexIndex]; const dust3d::Vector3* srcNormal = &defaultNormal; if (triangleVertexNormals) srcNormal = &(*triangleVertexNormals)[i][j]; @@ -160,10 +154,10 @@ ModelMesh::ModelMesh(dust3d::Object& object) if (triangleTangents) srcTangent = &(*triangleTangents)[i]; ModelOpenGLVertex* dest = &m_triangleVertices[destIndex]; - dest->colorR = triangleColor->r(); - dest->colorG = triangleColor->g(); - dest->colorB = triangleColor->b(); - dest->alpha = triangleColor->alpha(); + dest->colorR = srcColor->r(); + dest->colorG = srcColor->g(); + dest->colorB = srcColor->b(); + dest->alpha = srcColor->alpha(); dest->posX = srcVert->x(); dest->posY = srcVert->y(); dest->posZ = srcVert->z(); diff --git a/dust3d/base/object.h b/dust3d/base/object.h index bf3c8921..5701b905 100644 --- a/dust3d/base/object.h +++ b/dust3d/base/object.h @@ -38,32 +38,33 @@ namespace dust3d { struct ObjectNode { - Uuid partId; - Uuid nodeId; - Vector3 origin; - float radius = 0.0; + //Uuid partId; + //Uuid nodeId; + //Vector3 origin; + //float radius = 0.0; Color color; - float colorSolubility = 0.0; - float metalness = 0.0; - float roughness = 1.0; - Uuid materialId; - bool countershaded = false; - Uuid mirrorFromPartId; - Uuid mirroredByPartId; - Vector3 direction; - bool joined = true; + //float colorSolubility = 0.0; + //float metalness = 0.0; + //float roughness = 1.0; + //Uuid materialId; + //bool countershaded = false; + //Uuid mirrorFromPartId; + //Uuid mirroredByPartId; + //Vector3 direction; + //bool joined = true; }; class Object { public: std::vector vertices; - std::map sourceNodeMap; + std::map positionToNodeIdMap; + std::map nodeMap; std::vector> triangleAndQuads; std::vector> triangles; std::unordered_map, std::array>> partTriangleUvs; std::vector, std::array>> seamTriangleUvs; std::vector triangleNormals; - std::vector triangleColors; + std::vector vertexColors; bool alphaEnabled = false; uint64_t meshId = 0; diff --git a/dust3d/mesh/mesh_generator.cc b/dust3d/mesh/mesh_generator.cc index a88bb0b0..847e7fd2 100644 --- a/dust3d/mesh/mesh_generator.cc +++ b/dust3d/mesh/mesh_generator.cc @@ -639,6 +639,12 @@ std::unique_ptr MeshGenerator::combinePartMesh(const std::string& par partCache.isSuccessful = false; partCache.joined = (target == PartTarget::Model && !isDisabled); + partCache.nodeMap.clear(); + for (const auto& meshNode : meshNodes) { + partCache.nodeMap.emplace(std::make_pair(meshNode.sourceId, + ObjectNode { partColor })); + } + if (PartTarget::Model == target) { if (1 == meshNodes.size()) { size_t subdivideTimes = subdived ? 1 : 0; @@ -684,7 +690,7 @@ std::unique_ptr MeshGenerator::combinePartMesh(const std::string& par } const auto& vertexSources = tubeMeshBuilder->generatedVertexSources(); for (size_t i = 0; i < vertexSources.size(); ++i) { - partCache.sourceNodeMap.emplace(std::make_pair(PositionKey(partCache.vertices[i]), vertexSources[i])); + partCache.positionToNodeIdMap.emplace(std::make_pair(PositionKey(partCache.vertices[i]), vertexSources[i])); } } } @@ -828,8 +834,10 @@ std::unique_ptr MeshGenerator::combineComponentMesh(const std::string componentCache.noneSeamVertices.insert(vertex); collectSharedQuadEdges(partCache.vertices, partCache.faces, &componentCache.sharedQuadEdges); componentCache.partTriangleUvs.insert({ Uuid(partIdString), partCache.triangleUvs }); - for (const auto& it : partCache.sourceNodeMap) - componentCache.sourceNodeMap.emplace(it); + for (const auto& it : partCache.positionToNodeIdMap) + componentCache.positionToNodeIdMap.emplace(it); + for (const auto& it : partCache.nodeMap) + componentCache.nodeMap.emplace(it); } if (!partCache.joined) { if (mesh) @@ -963,8 +971,10 @@ std::unique_ptr MeshGenerator::combineComponentChildGroupMesh(const s componentCache.sharedQuadEdges.insert(it); for (const auto& it : childComponentCache.partTriangleUvs) componentCache.partTriangleUvs.insert({ it.first, it.second }); - for (const auto& it : childComponentCache.sourceNodeMap) - componentCache.sourceNodeMap.emplace(it); + for (const auto& it : childComponentCache.positionToNodeIdMap) + componentCache.positionToNodeIdMap.emplace(it); + for (const auto& it : childComponentCache.nodeMap) + componentCache.nodeMap.emplace(it); if (nullptr == subMesh || subMesh->isNull()) { continue; @@ -1029,7 +1039,16 @@ void MeshGenerator::postprocessObject(Object* object) object->triangleNormals = combinedFacesNormals; - object->triangleColors.resize(object->triangles.size(), Color::createWhite()); + object->vertexColors.resize(object->vertices.size(), Color::createWhite()); + for (size_t i = 0; i < object->vertices.size(); ++i) { + auto findSourceNode = object->positionToNodeIdMap.find(object->vertices[i]); + if (findSourceNode == object->positionToNodeIdMap.end()) + continue; + auto findObjectNode = object->nodeMap.find(findSourceNode->second); + if (findObjectNode == object->nodeMap.end()) + continue; + object->vertexColors[i] = findObjectNode->second.color; + } std::vector> triangleVertexNormals; smoothNormal(object->vertices, @@ -1247,7 +1266,8 @@ void MeshGenerator::generate() const auto& componentCache = m_cacheContext->components[to_string(Uuid())]; - m_object->sourceNodeMap = componentCache.sourceNodeMap; + m_object->positionToNodeIdMap = componentCache.positionToNodeIdMap; + m_object->nodeMap = componentCache.nodeMap; m_object->partTriangleUvs = componentCache.partTriangleUvs; std::vector combinedVertices; diff --git a/dust3d/mesh/mesh_generator.h b/dust3d/mesh/mesh_generator.h index cc7549d5..ac98807d 100644 --- a/dust3d/mesh/mesh_generator.h +++ b/dust3d/mesh/mesh_generator.h @@ -44,7 +44,8 @@ public: struct GeneratedPart { std::vector vertices; - std::map sourceNodeMap; + std::map positionToNodeIdMap; + std::map nodeMap; std::vector> faces; std::map, std::array> triangleUvs; Color color = Color(1.0, 1.0, 1.0); @@ -57,7 +58,8 @@ public: vertices.clear(); faces.clear(); triangleUvs.clear(); - sourceNodeMap.clear(); + positionToNodeIdMap.clear(); + nodeMap.clear(); color = Color(1.0, 1.0, 1.0); metalness = 0.0; roughness = 1.0; @@ -72,7 +74,8 @@ public: std::unordered_map, std::array>> partTriangleUvs; std::vector, std::array>> seamTriangleUvs; std::set noneSeamVertices; - std::map sourceNodeMap; + std::map positionToNodeIdMap; + std::map nodeMap; void reset() { mesh.reset(); @@ -80,7 +83,8 @@ public: partTriangleUvs.clear(); seamTriangleUvs.clear(); noneSeamVertices.clear(); - sourceNodeMap.clear(); + positionToNodeIdMap.clear(); + nodeMap.clear(); } };