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:
shenlei
2026-10-12 16:38:18 +09:00
co-authored by Claude Opus 5.5
parent 16d16de90d
commit 53115d9ea2
2 changed files with 31 additions and 25 deletions
+7
View File
@@ -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<Gr2ModelInstance> attachment, const float* offset_matrix = nullptr);
void AttachPart(std::shared_ptr<Gr2ModelInstance> part);
void DetachAll();
@@ -94,6 +98,7 @@ public:
private:
struct AttachedModel {
std::string bone_name;
int bone_index = -1;
std::shared_ptr<Gr2ModelInstance> model;
float offset[16];
bool animated = false;
@@ -124,6 +129,8 @@ private:
mutable std::vector<int> m_trackBones;
mutable size_t m_poseEvaluations = 0;
void ApplySkins(std::vector<Gr2MeshDraw>& draws, size_t first) const;
mutable std::unordered_map<std::string, int> m_boneIndex;
mutable bool m_boneIndexBuilt = false;
std::shared_ptr<const gr2::File> m_file;
std::string m_filepath;