diff --git a/gr2/include/gr2_render/gr2_render.h b/gr2/include/gr2_render/gr2_render.h index 9f69cec..a011dce 100644 --- a/gr2/include/gr2_render/gr2_render.h +++ b/gr2/include/gr2_render/gr2_render.h @@ -64,6 +64,10 @@ public: // Bone queries & attachments bool GetBoneWorldTransform(const std::string& bone_name, float* out_matrix, float animation_time = -1.0f) const; + // Bone index in skeleton 0 (first bone with that name), or -1. Callers that query the same bone + // every frame can cache it and use the index overload instead of a name lookup. + int FindBoneIndex(const std::string& bone_name) const; + bool GetBoneWorldTransform(int bone_index, float* out_matrix, float animation_time = -1.0f) const; void AttachModel(const std::string& bone_name, std::shared_ptr attachment, const float* offset_matrix = nullptr); void AttachPart(std::shared_ptr part); void DetachAll(); @@ -94,6 +98,7 @@ public: private: struct AttachedModel { std::string bone_name; + int bone_index = -1; std::shared_ptr model; float offset[16]; bool animated = false; @@ -124,6 +129,8 @@ private: mutable std::vector m_trackBones; mutable size_t m_poseEvaluations = 0; void ApplySkins(std::vector& draws, size_t first) const; + mutable std::unordered_map m_boneIndex; + mutable bool m_boneIndexBuilt = false; std::shared_ptr m_file; std::string m_filepath; diff --git a/gr2/src_render/gr2_model_instance.cpp b/gr2/src_render/gr2_model_instance.cpp index acb6a35..7d9e352 100644 --- a/gr2/src_render/gr2_model_instance.cpp +++ b/gr2/src_render/gr2_model_instance.cpp @@ -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(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(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(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(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(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 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 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(b); - break; - } - } - if (bone_idx < 0) continue; + const int bone_idx = am.bone_index; + if (bone_idx < 0 || static_cast(bone_idx) >= sk.bones.size() || static_cast(bone_idx) >= world_pose.size()) continue; const auto& B = world_pose[bone_idx]; float attached_local[16];