gr2: look up bones by a cached name index
FindBoneIndex builds a name->index map once per instance (first match wins, as the linear scan did); GetBoneWorldTransform gains an index overload and attached models cache their bone index at attach time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
16d16de90d
commit
53115d9ea2
@@ -209,26 +209,29 @@ void Gr2ModelInstance::EvaluatePose(float time) const {
|
||||
m_poseAnimation=anim; m_poseTime=keyTime; m_poseValid=true; ++m_poseEvaluations;
|
||||
}
|
||||
|
||||
int Gr2ModelInstance::FindBoneIndex(const std::string& bone_name) const {
|
||||
if (!m_file || m_file->file_info().skeletons.empty()) return -1;
|
||||
if (!m_boneIndexBuilt) {
|
||||
const auto& sk = m_file->file_info().skeletons[0];
|
||||
m_boneIndex.reserve(sk.bones.size());
|
||||
for (size_t i = 0; i < sk.bones.size(); ++i) m_boneIndex.emplace(sk.bones[i].name, static_cast<int>(i)); // first match wins
|
||||
m_boneIndexBuilt = true;
|
||||
}
|
||||
auto it = m_boneIndex.find(bone_name);
|
||||
return it == m_boneIndex.end() ? -1 : it->second;
|
||||
}
|
||||
|
||||
bool Gr2ModelInstance::GetBoneWorldTransform(const std::string& bone_name, float* out_matrix, float animation_time) const {
|
||||
if (!m_file || m_file->file_info().skeletons.empty() || !out_matrix) return false;
|
||||
const auto& sk = m_file->file_info().skeletons[0];
|
||||
int target_bone = -1;
|
||||
for (size_t i = 0; i < sk.bones.size(); ++i) {
|
||||
if (sk.bones[i].name == bone_name) {
|
||||
target_bone = static_cast<int>(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (target_bone < 0) return false;
|
||||
return GetBoneWorldTransform(FindBoneIndex(bone_name), out_matrix, animation_time);
|
||||
}
|
||||
|
||||
bool Gr2ModelInstance::GetBoneWorldTransform(int bone_index, float* out_matrix, float animation_time) const {
|
||||
if (!m_file || m_file->file_info().skeletons.empty() || !out_matrix || bone_index < 0) return false;
|
||||
if (static_cast<size_t>(bone_index) >= m_file->file_info().skeletons[0].bones.size()) return false;
|
||||
EvaluatePose(animation_time);
|
||||
const auto& world_pose = m_worldPose;
|
||||
|
||||
if (target_bone >= 0 && target_bone < static_cast<int>(world_pose.size())) {
|
||||
std::copy(world_pose[target_bone].data(), world_pose[target_bone].data() + 16, out_matrix);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
if (static_cast<size_t>(bone_index) >= m_worldPose.size()) return false;
|
||||
std::copy(m_worldPose[bone_index].data(), m_worldPose[bone_index].data() + 16, out_matrix);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Gr2ModelInstance::AttachModel(const std::string& bone_name, std::shared_ptr<Gr2ModelInstance> attachment, const float* offset_matrix) {
|
||||
@@ -236,6 +239,7 @@ void Gr2ModelInstance::AttachModel(const std::string& bone_name, std::shared_ptr
|
||||
for (auto& existing : m_attached_models) {
|
||||
if (existing.bone_name == bone_name) {
|
||||
existing.model = attachment;
|
||||
existing.bone_index = FindBoneIndex(bone_name);
|
||||
if (offset_matrix) {
|
||||
std::copy(offset_matrix, offset_matrix + 16, existing.offset);
|
||||
} else {
|
||||
@@ -246,6 +250,7 @@ void Gr2ModelInstance::AttachModel(const std::string& bone_name, std::shared_ptr
|
||||
}
|
||||
AttachedModel am;
|
||||
am.bone_name = bone_name;
|
||||
am.bone_index = FindBoneIndex(bone_name);
|
||||
am.model = attachment;
|
||||
if (offset_matrix) {
|
||||
std::copy(offset_matrix, offset_matrix + 16, am.offset);
|
||||
@@ -777,14 +782,8 @@ std::vector<Gr2MeshDraw> Gr2ModelInstance::BuildDrawCommands(float animation_tim
|
||||
const auto& sk = info.skeletons[0];
|
||||
for (const auto& am : m_attached_models) {
|
||||
if (!am.model) continue;
|
||||
int bone_idx = -1;
|
||||
for (size_t b = 0; b < sk.bones.size(); ++b) {
|
||||
if (sk.bones[b].name == am.bone_name) {
|
||||
bone_idx = static_cast<int>(b);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (bone_idx < 0) continue;
|
||||
const int bone_idx = am.bone_index;
|
||||
if (bone_idx < 0 || static_cast<size_t>(bone_idx) >= sk.bones.size() || static_cast<size_t>(bone_idx) >= world_pose.size()) continue;
|
||||
|
||||
const auto& B = world_pose[bone_idx];
|
||||
float attached_local[16];
|
||||
|
||||
Reference in New Issue
Block a user