gr2_render: cache part->parent bone map and parent bind pose

BuildDrawCommandsWithPalette recomputed gr2::bind_pose of the parent
skeleton and an O(part bones x parent bones) name match every frame for
every hair/armor part. Both depend only on the two skeletons; rebuild
them when the parent skeleton changes and reuse the palette buffer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-12 16:13:22 +09:00
co-authored by Claude Opus 5.5
parent a1b3938159
commit e354b92e08
2 changed files with 37 additions and 19 deletions
+31 -19
View File
@@ -307,33 +307,45 @@ std::vector<Gr2MeshDraw> Gr2ModelInstance::BuildDrawCommandsWithPalette(const st
const auto& info = m_file->file_info();
bool has_skin = !skin_matrices.empty();
std::vector<gr2::Mat4> part_skin_matrices = skin_matrices;
m_partSkin.assign(skin_matrices.begin(), skin_matrices.end());
auto& part_skin_matrices = m_partSkin;
// Map this model's skeleton bones to parent skeleton bones by name if a parent skeleton is provided
std::vector<int> part_to_parent_bone_map;
static const std::vector<int> kNoBoneMap;
const std::vector<int>* part_to_parent = &kNoBoneMap;
if (parent_skeleton && !info.skeletons.empty()) {
const auto& my_skel = info.skeletons[0];
std::vector<gr2::Mat4> parent_bind_world, parent_bind_skin;
gr2::bind_pose(*parent_skeleton, parent_bind_world, parent_bind_skin);
part_to_parent_bone_map.resize(my_skel.bones.size(), -1);
if (m_partMapParent != parent_skeleton || m_partMapParentBones != parent_skeleton->bones.size()) {
m_partMapParent = parent_skeleton;
m_partMapParentBones = parent_skeleton->bones.size();
std::vector<gr2::Mat4> parent_bind_world, parent_bind_skin;
gr2::bind_pose(*parent_skeleton, parent_bind_world, parent_bind_skin);
std::unordered_map<std::string, int> parent_by_name;
for (size_t pb = 0; pb < parent_skeleton->bones.size(); ++pb)
parent_by_name.emplace(parent_skeleton->bones[pb].name, static_cast<int>(pb)); // first match wins
m_partToParent.assign(my_skel.bones.size(), -1);
m_partParentBind.assign(my_skel.bones.size(), gr2::Mat4{});
for (size_t b = 0; b < my_skel.bones.size(); ++b) {
auto it = parent_by_name.find(my_skel.bones[b].name);
if (it == parent_by_name.end()) continue;
m_partToParent[b] = it->second;
m_partParentBind[b] = parent_bind_world[it->second];
}
}
part_to_parent = &m_partToParent;
for (size_t b = 0; b < my_skel.bones.size(); ++b) {
const std::string& bname = my_skel.bones[b].name;
for (size_t pb = 0; pb < parent_skeleton->bones.size(); ++pb) {
if (parent_skeleton->bones[pb].name == bname) {
part_to_parent_bone_map[b] = static_cast<int>(pb);
// A shared animation pose does not imply shared inverse bind matrices.
// Recover the parent's animated world pose, then skin with THIS part's
// inverse bind pose (hair exports can have a different rest skeleton).
if (pb < skin_matrices.size()) {
gr2::Mat4 animated_world;
MultiplyMat4(parent_bind_world[pb].data(), skin_matrices[pb].data(), animated_world.data());
MultiplyMat4(my_skel.bones[b].inverse_world.data(), animated_world.data(), part_skin_matrices[pb].data());
}
break;
}
const int pb = m_partToParent[b];
// A shared animation pose does not imply shared inverse bind matrices.
// Recover the parent's animated world pose, then skin with THIS part's
// inverse bind pose (hair exports can have a different rest skeleton).
if (pb >= 0 && static_cast<size_t>(pb) < skin_matrices.size()) {
gr2::Mat4 animated_world;
MultiplyMat4(m_partParentBind[b].data(), skin_matrices[pb].data(), animated_world.data());
MultiplyMat4(my_skel.bones[b].inverse_world.data(), animated_world.data(), part_skin_matrices[pb].data());
}
}
}
const std::vector<int>& part_to_parent_bone_map = *part_to_parent;
hardwareSkinning = hardwareSkinning && part_skin_matrices.size()<=128;
float cachedWorld[16];