Skeleton.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. /// \file
  23. #pragma once
  24. #include "../Math/BoundingBox.h"
  25. #include "../Scene/Node.h"
  26. namespace Urho3D
  27. {
  28. enum BoneCollisionShape : unsigned char
  29. {
  30. BONECOLLISION_NONE = 0x0,
  31. BONECOLLISION_SPHERE = 0x1,
  32. BONECOLLISION_BOX = 0x2,
  33. };
  34. URHO3D_FLAGSET(BoneCollisionShape, BoneCollisionShapeFlags);
  35. class Deserializer;
  36. class ResourceCache;
  37. class Serializer;
  38. /// %Bone in a skeleton.
  39. /// @fakeref
  40. struct Bone
  41. {
  42. /// Construct with defaults.
  43. Bone() :
  44. parentIndex_(0),
  45. initialPosition_(Vector3::ZERO),
  46. initialRotation_(Quaternion::IDENTITY),
  47. initialScale_(Vector3::ONE),
  48. animated_(true),
  49. radius_(0.0f)
  50. {
  51. }
  52. /// Bone name.
  53. String name_;
  54. /// Bone name hash.
  55. StringHash nameHash_;
  56. /// Parent bone index.
  57. unsigned parentIndex_;
  58. /// Reset position.
  59. Vector3 initialPosition_;
  60. /// Reset rotation.
  61. Quaternion initialRotation_;
  62. /// Reset scale.
  63. Vector3 initialScale_;
  64. /// Offset matrix.
  65. Matrix3x4 offsetMatrix_;
  66. /// Animation enable flag.
  67. bool animated_;
  68. /// Supported collision types.
  69. BoneCollisionShapeFlags collisionMask_ = BONECOLLISION_NONE;
  70. /// Radius.
  71. float radius_;
  72. /// Local-space bounding box.
  73. BoundingBox boundingBox_;
  74. /// Scene node.
  75. WeakPtr<Node> node_;
  76. };
  77. /// Hierarchical collection of bones.
  78. /// @fakeref
  79. class URHO3D_API Skeleton
  80. {
  81. public:
  82. /// Construct an empty skeleton.
  83. Skeleton();
  84. /// Destruct.
  85. ~Skeleton();
  86. /// Read from a stream. Return true if successful.
  87. bool Load(Deserializer& source);
  88. /// Write to a stream. Return true if successful.
  89. bool Save(Serializer& dest) const;
  90. /// Define from another skeleton.
  91. void Define(const Skeleton& src);
  92. /// Set root bone's index.
  93. void SetRootBoneIndex(unsigned index);
  94. /// Clear bones.
  95. void ClearBones();
  96. /// Reset all animating bones to initial positions.
  97. void Reset();
  98. /// Return all bones.
  99. const Vector<Bone>& GetBones() const { return bones_; }
  100. /// Return modifiable bones.
  101. Vector<Bone>& GetModifiableBones() { return bones_; }
  102. /// Return number of bones.
  103. /// @property
  104. unsigned GetNumBones() const { return bones_.Size(); }
  105. /// Return root bone.
  106. /// @property
  107. Bone* GetRootBone();
  108. /// Return index of the bone by name. Return M_MAX_UNSIGNED if not found.
  109. unsigned GetBoneIndex(const String& boneName) const;
  110. /// Return index of the bone by name hash. Return M_MAX_UNSIGNED if not found.
  111. unsigned GetBoneIndex(const StringHash& boneNameHash) const;
  112. /// Return index of the bone by the bone pointer. Return M_MAX_UNSIGNED if not found.
  113. unsigned GetBoneIndex(const Bone* bone) const;
  114. /// Return parent of the given bone. Return null for root bones.
  115. Bone* GetBoneParent(const Bone* bone);
  116. /// Return bone by index.
  117. /// @property{get_bones}
  118. Bone* GetBone(unsigned index);
  119. /// Return bone by name.
  120. Bone* GetBone(const String& name);
  121. /// Return bone by name.
  122. Bone* GetBone(const char* name);
  123. /// Return bone by name hash.
  124. Bone* GetBone(const StringHash& boneNameHash);
  125. /// Reset all animating bones to initial positions without marking the nodes dirty. Requires the node dirtying to be performed later.
  126. void ResetSilent();
  127. private:
  128. /// Bones.
  129. Vector<Bone> bones_;
  130. /// Root bone index.
  131. unsigned rootBoneIndex_;
  132. };
  133. }