Skeleton.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. struct Bone
  40. {
  41. /// Construct with defaults.
  42. Bone() :
  43. parentIndex_(0),
  44. initialPosition_(Vector3::ZERO),
  45. initialRotation_(Quaternion::IDENTITY),
  46. initialScale_(Vector3::ONE),
  47. animated_(true),
  48. radius_(0.0f)
  49. {
  50. }
  51. /// Bone name.
  52. String name_;
  53. /// Bone name hash.
  54. StringHash nameHash_;
  55. /// Parent bone index.
  56. unsigned parentIndex_;
  57. /// Reset position.
  58. Vector3 initialPosition_;
  59. /// Reset rotation.
  60. Quaternion initialRotation_;
  61. /// Reset scale.
  62. Vector3 initialScale_;
  63. /// Offset matrix.
  64. Matrix3x4 offsetMatrix_;
  65. /// Animation enable flag.
  66. bool animated_;
  67. /// Supported collision types.
  68. BoneCollisionShapeFlags collisionMask_ = BONECOLLISION_NONE;
  69. /// Radius.
  70. float radius_;
  71. /// Local-space bounding box.
  72. BoundingBox boundingBox_;
  73. /// Scene node.
  74. WeakPtr<Node> node_;
  75. };
  76. /// Hierarchical collection of bones.
  77. class URHO3D_API Skeleton
  78. {
  79. public:
  80. /// Construct an empty skeleton.
  81. Skeleton();
  82. /// Destruct.
  83. ~Skeleton();
  84. /// Read from a stream. Return true if successful.
  85. bool Load(Deserializer& source);
  86. /// Write to a stream. Return true if successful.
  87. bool Save(Serializer& dest) const;
  88. /// Define from another skeleton.
  89. void Define(const Skeleton& src);
  90. /// Set root bone's index.
  91. void SetRootBoneIndex(unsigned index);
  92. /// Clear bones.
  93. void ClearBones();
  94. /// Reset all animating bones to initial positions.
  95. void Reset();
  96. /// Return all bones.
  97. const Vector<Bone>& GetBones() const { return bones_; }
  98. /// Return modifiable bones.
  99. Vector<Bone>& GetModifiableBones() { return bones_; }
  100. /// Return number of bones.
  101. unsigned GetNumBones() const { return bones_.Size(); }
  102. /// Return root bone.
  103. Bone* GetRootBone();
  104. /// Return index of the bone by name. Return M_MAX_UNSIGNED if not found.
  105. unsigned GetBoneIndex(const String& boneName) const;
  106. /// Return index of the bone by name hash. Return M_MAX_UNSIGNED if not found.
  107. unsigned GetBoneIndex(const StringHash& boneNameHash) const;
  108. /// Return index of the bone by the bone pointer. Return M_MAX_UNSIGNED if not found.
  109. unsigned GetBoneIndex(const Bone* bone) const;
  110. /// Return parent of the given bone. Return null for root bones.
  111. Bone* GetBoneParent(const Bone* bone);
  112. /// Return bone by index.
  113. Bone* GetBone(unsigned index);
  114. /// Return bone by name.
  115. Bone* GetBone(const String& name);
  116. /// Return bone by name.
  117. Bone* GetBone(const char* name);
  118. /// Return bone by name hash.
  119. Bone* GetBone(const StringHash& boneNameHash);
  120. /// Reset all animating bones to initial positions without marking the nodes dirty. Requires the node dirtying to be performed later.
  121. void ResetSilent();
  122. private:
  123. /// Bones.
  124. Vector<Bone> bones_;
  125. /// Root bone index.
  126. unsigned rootBoneIndex_;
  127. };
  128. }