Skeleton.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../Math/BoundingBox.h"
  6. #include "../Scene/Node.h"
  7. namespace Urho3D
  8. {
  9. enum BoneCollisionShape : unsigned char
  10. {
  11. BONECOLLISION_NONE = 0x0,
  12. BONECOLLISION_SPHERE = 0x1,
  13. BONECOLLISION_BOX = 0x2,
  14. };
  15. URHO3D_FLAGSET(BoneCollisionShape, BoneCollisionShapeFlags);
  16. class Deserializer;
  17. class ResourceCache;
  18. class Serializer;
  19. /// %Bone in a skeleton.
  20. /// @nocount
  21. struct Bone
  22. {
  23. /// Construct with defaults.
  24. Bone() :
  25. parentIndex_(0),
  26. initialPosition_(Vector3::ZERO),
  27. initialRotation_(Quaternion::IDENTITY),
  28. initialScale_(Vector3::ONE),
  29. animated_(true),
  30. radius_(0.0f)
  31. {
  32. }
  33. /// Bone name.
  34. String name_;
  35. /// Bone name hash.
  36. StringHash nameHash_;
  37. /// Parent bone index.
  38. i32 parentIndex_;
  39. /// Reset position.
  40. Vector3 initialPosition_;
  41. /// Reset rotation.
  42. Quaternion initialRotation_;
  43. /// Reset scale.
  44. Vector3 initialScale_;
  45. /// Offset matrix.
  46. Matrix3x4 offsetMatrix_;
  47. /// Animation enable flag.
  48. bool animated_;
  49. /// Supported collision types.
  50. BoneCollisionShapeFlags collisionMask_ = BONECOLLISION_NONE;
  51. /// Radius.
  52. float radius_;
  53. /// Local-space bounding box.
  54. BoundingBox boundingBox_;
  55. /// Scene node.
  56. WeakPtr<Node> node_;
  57. };
  58. /// Hierarchical collection of bones.
  59. /// @nocount
  60. class URHO3D_API Skeleton
  61. {
  62. public:
  63. /// Construct an empty skeleton.
  64. Skeleton();
  65. /// Destruct.
  66. ~Skeleton();
  67. /// Read from a stream. Return true if successful.
  68. bool Load(Deserializer& source);
  69. /// Write to a stream. Return true if successful.
  70. bool Save(Serializer& dest) const;
  71. /// Define from another skeleton.
  72. void Define(const Skeleton& src);
  73. /// Set root bone's index.
  74. void SetRootBoneIndex(i32 index);
  75. /// Clear bones.
  76. void ClearBones();
  77. /// Reset all animating bones to initial positions.
  78. void Reset();
  79. /// Return all bones.
  80. /// @nobindtemp
  81. const Vector<Bone>& GetBones() const { return bones_; }
  82. /// Return modifiable bones.
  83. Vector<Bone>& GetModifiableBones() { return bones_; }
  84. /// Return number of bones.
  85. /// @property
  86. i32 GetNumBones() const { return bones_.Size(); }
  87. /// Return root bone.
  88. /// @property
  89. Bone* GetRootBone();
  90. /// Return index of the bone by name. Return NINDEX if not found.
  91. i32 GetBoneIndex(const String& boneName) const;
  92. /// Return index of the bone by name hash. Return NINDEX if not found.
  93. i32 GetBoneIndex(const StringHash& boneNameHash) const;
  94. /// Return index of the bone by the bone pointer. Return NINDEX if not found.
  95. i32 GetBoneIndex(const Bone* bone) const;
  96. /// Return parent of the given bone. Return null for root bones.
  97. Bone* GetBoneParent(const Bone* bone);
  98. /// Return bone by index.
  99. /// @property{get_bones}
  100. Bone* GetBone(i32 index);
  101. /// Return bone by name.
  102. Bone* GetBone(const String& name);
  103. /// Return bone by name.
  104. Bone* GetBone(const char* name);
  105. /// Return bone by name hash.
  106. Bone* GetBone(const StringHash& boneNameHash);
  107. /// Reset all animating bones to initial positions without marking the nodes dirty. Requires the node dirtying to be performed later.
  108. void ResetSilent();
  109. private:
  110. /// Bones.
  111. Vector<Bone> bones_;
  112. /// Root bone index.
  113. i32 rootBoneIndex_;
  114. };
  115. }