From 18c493def6dec6646e53a8142dbe90e8cd2fceb0 Mon Sep 17 00:00:00 2001 From: Jeremy HU Date: Thu, 8 Dec 2022 21:13:56 +1100 Subject: [PATCH] Mix body bone color --- dust3d/rig/bone_generator.cc | 49 ++++++++++++++++++++++++++++++------ dust3d/rig/bone_generator.h | 4 ++- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/dust3d/rig/bone_generator.cc b/dust3d/rig/bone_generator.cc index 1be1292a..7eaa0d1a 100644 --- a/dust3d/rig/bone_generator.cc +++ b/dust3d/rig/bone_generator.cc @@ -45,7 +45,9 @@ void BoneGenerator::setPositionToNodeMap(const std::map& posi void BoneGenerator::addBone(const Uuid& boneId, const Bone& bone) { - m_boneMap.emplace(std::make_pair(boneId, bone)); + Bone newBone = bone; + newBone.index = m_boneMap.size(); + m_boneMap.emplace(std::make_pair(boneId, std::move(newBone))); } void BoneGenerator::addNode(const Uuid& nodeId, const Node& node) @@ -167,7 +169,8 @@ BoneGenerator::BonePreview& BoneGenerator::bodyPreview() void BoneGenerator::addBonePreviewTriangle(BonePreview& bonePreview, std::unordered_map& oldToNewVertexMap, - const std::vector& triangle) + const std::vector& triangle, + const Color& color) { std::vector newTriangle(3); for (size_t i = 0; i < 3; ++i) { @@ -176,7 +179,7 @@ void BoneGenerator::addBonePreviewTriangle(BonePreview& bonePreview, oldToNewVertexMap.insert(std::make_pair(triangle[i], bonePreview.vertices.size())); newTriangle[i] = bonePreview.vertices.size(); bonePreview.vertices.push_back(m_vertices[triangle[i]]); - bonePreview.vertexColors.push_back(Color(1.0, 1.0, 1.0)); + bonePreview.vertexColors.push_back(color); } else { newTriangle[i] = findVertex->second; } @@ -186,10 +189,26 @@ void BoneGenerator::addBonePreviewTriangle(BonePreview& bonePreview, void BoneGenerator::generateBonePreviews() { + const static std::array s_colors = { + Color(155.0 / 255.0, 95.0 / 255.0, 224.0 / 255.0), + Color(22.0 / 255.0, 164.0 / 255.0, 216.0 / 255.0), + Color(96.0 / 255.0, 219.0 / 255.0, 232.0 / 255.0), + Color(139.0 / 255.0, 211.0 / 255.0, 70.0 / 255.0), + Color(239.0 / 255.0, 223.0 / 255.0, 72.0 / 255.0), + Color(249.0 / 255.0, 165.0 / 255.0, 44.0 / 255.0), + Color(214.0 / 255.0, 78.0 / 255.0, 18.0 / 255.0), + }; + for (const auto& it : m_boneVertices) { + auto findBone = m_boneMap.find(it.first); + if (findBone == m_boneMap.end()) + continue; + BonePreview bonePreview; std::unordered_map oldToNewVertexMap; + const auto& color = s_colors[findBone->second.index % s_colors.size()]; + for (const auto& triangle : m_triangles) { size_t countedPoints = 0; for (size_t i = 0; i < 3; ++i) { @@ -198,16 +217,32 @@ void BoneGenerator::generateBonePreviews() } if (0 == countedPoints) continue; - addBonePreviewTriangle(bonePreview, oldToNewVertexMap, triangle); + addBonePreviewTriangle(bonePreview, oldToNewVertexMap, triangle, color); } m_bonePreviews.emplace(std::make_pair(it.first, std::move(bonePreview))); } - std::unordered_map bodyOldToNewVertexMap; - for (const auto& triangle : m_triangles) { - addBonePreviewTriangle(m_bodyPreview, bodyOldToNewVertexMap, triangle); + std::unordered_map> vertexSkinColors; + for (const auto& it : m_boneVertices) { + auto findBone = m_boneMap.find(it.first); + if (findBone == m_boneMap.end()) + continue; + const auto& color = s_colors[findBone->second.index % s_colors.size()]; + for (const auto& vertexIndex : it.second) + vertexSkinColors[vertexIndex].push_back(color); } + std::vector bodyVertexColors(m_vertices.size()); + for (const auto& it : vertexSkinColors) { + Color color; + for (const auto& colorIt : it.second) + color = color + colorIt; + color = color * (1.0 / it.second.size()); + bodyVertexColors[it.first] = color; + } + m_bodyPreview.vertices = m_vertices; + m_bodyPreview.triangles = m_triangles; + m_bodyPreview.vertexColors = std::move(bodyVertexColors); } } diff --git a/dust3d/rig/bone_generator.h b/dust3d/rig/bone_generator.h index c12d1341..4e288304 100644 --- a/dust3d/rig/bone_generator.h +++ b/dust3d/rig/bone_generator.h @@ -48,6 +48,7 @@ public: }; struct Bone { + size_t index; std::string name; std::vector joints; std::vector startPositions; @@ -100,7 +101,8 @@ private: void generateBonePreviews(); void addBonePreviewTriangle(BonePreview& bonePreview, std::unordered_map& oldToNewVertexMap, - const std::vector& triangle); + const std::vector& triangle, + const Color& color); }; }