Skip to content

Commit

Permalink
Mix body bone color
Browse files Browse the repository at this point in the history
  • Loading branch information
huxingyi committed Dec 8, 2022
1 parent d2bc771 commit 18c493d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
49 changes: 42 additions & 7 deletions dust3d/rig/bone_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ void BoneGenerator::setPositionToNodeMap(const std::map<PositionKey, Uuid>& 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)
Expand Down Expand Up @@ -167,7 +169,8 @@ BoneGenerator::BonePreview& BoneGenerator::bodyPreview()

void BoneGenerator::addBonePreviewTriangle(BonePreview& bonePreview,
std::unordered_map<size_t, size_t>& oldToNewVertexMap,
const std::vector<size_t>& triangle)
const std::vector<size_t>& triangle,
const Color& color)
{
std::vector<size_t> newTriangle(3);
for (size_t i = 0; i < 3; ++i) {
Expand All @@ -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;
}
Expand All @@ -186,10 +189,26 @@ void BoneGenerator::addBonePreviewTriangle(BonePreview& bonePreview,

void BoneGenerator::generateBonePreviews()
{
const static std::array<Color, 7> 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<size_t, size_t> 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) {
Expand All @@ -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<size_t, size_t> bodyOldToNewVertexMap;
for (const auto& triangle : m_triangles) {
addBonePreviewTriangle(m_bodyPreview, bodyOldToNewVertexMap, triangle);
std::unordered_map<size_t, std::vector<Color>> 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<Color> 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);
}

}
4 changes: 3 additions & 1 deletion dust3d/rig/bone_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class BoneGenerator {
};

struct Bone {
size_t index;
std::string name;
std::vector<Uuid> joints;
std::vector<Vector3> startPositions;
Expand Down Expand Up @@ -100,7 +101,8 @@ class BoneGenerator {
void generateBonePreviews();
void addBonePreviewTriangle(BonePreview& bonePreview,
std::unordered_map<size_t, size_t>& oldToNewVertexMap,
const std::vector<size_t>& triangle);
const std::vector<size_t>& triangle,
const Color& color);
};

}
Expand Down

0 comments on commit 18c493d

Please sign in to comment.