AnimationState.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "../Container/HashMap.h"
  25. #include "../Container/Ptr.h"
  26. namespace Urho3D
  27. {
  28. class Animation;
  29. class AnimatedModel;
  30. class Deserializer;
  31. class Serializer;
  32. class Skeleton;
  33. struct AnimationTrack;
  34. struct Bone;
  35. /// %Animation blending mode.
  36. enum AnimationBlendMode
  37. {
  38. // Lerp blending (default)
  39. ABM_LERP = 0,
  40. // Additive blending based on difference from bind pose
  41. ABM_ADDITIVE
  42. };
  43. /// %Animation instance per-track data.
  44. struct AnimationStateTrack
  45. {
  46. /// Construct with defaults.
  47. AnimationStateTrack();
  48. /// Destruct.
  49. ~AnimationStateTrack();
  50. /// Animation track.
  51. const AnimationTrack* track_;
  52. /// Bone pointer.
  53. Bone* bone_;
  54. /// Scene node pointer.
  55. WeakPtr<Node> node_;
  56. /// Blending weight.
  57. float weight_;
  58. /// Last key frame.
  59. unsigned keyFrame_;
  60. };
  61. /// %Animation instance.
  62. class URHO3D_API AnimationState : public RefCounted
  63. {
  64. public:
  65. /// Construct with animated model and animation pointers.
  66. AnimationState(AnimatedModel* model, Animation* animation);
  67. /// Construct with root scene node and animation pointers.
  68. AnimationState(Node* node, Animation* animation);
  69. /// Destruct.
  70. ~AnimationState() override;
  71. /// Set start bone. Not supported in node animation mode. Resets any assigned per-bone weights.
  72. /// @property
  73. void SetStartBone(Bone* startBone);
  74. /// Set looping enabled/disabled.
  75. /// @property
  76. void SetLooped(bool looped);
  77. /// Set blending weight.
  78. /// @property
  79. void SetWeight(float weight);
  80. /// Set blending mode.
  81. /// @property
  82. void SetBlendMode(AnimationBlendMode mode);
  83. /// Set time position. Does not fire animation triggers.
  84. /// @property
  85. void SetTime(float time);
  86. /// Set per-bone blending weight by track index. Default is 1.0 (full), is multiplied with the state's blending weight when applying the animation. Optionally recurses to child bones.
  87. void SetBoneWeight(unsigned index, float weight, bool recursive = false);
  88. /// Set per-bone blending weight by name.
  89. void SetBoneWeight(const String& name, float weight, bool recursive = false);
  90. /// Set per-bone blending weight by name hash.
  91. void SetBoneWeight(StringHash nameHash, float weight, bool recursive = false);
  92. /// Modify blending weight.
  93. void AddWeight(float delta);
  94. /// Modify time position. %Animation triggers will be fired.
  95. void AddTime(float delta);
  96. /// Set blending layer.
  97. /// @property
  98. void SetLayer(unsigned char layer);
  99. /// Return animation.
  100. /// @property
  101. Animation* GetAnimation() const { return animation_; }
  102. /// Return animated model this state belongs to (model mode).
  103. /// @property
  104. AnimatedModel* GetModel() const;
  105. /// Return root scene node this state controls (node hierarchy mode).
  106. /// @property
  107. Node* GetNode() const;
  108. /// Return start bone.
  109. /// @property
  110. Bone* GetStartBone() const;
  111. /// Return per-bone blending weight by track index.
  112. float GetBoneWeight(unsigned index) const;
  113. /// Return per-bone blending weight by name.
  114. /// @property{get_boneWeights}
  115. float GetBoneWeight(const String& name) const;
  116. /// Return per-bone blending weight by name.
  117. float GetBoneWeight(StringHash nameHash) const;
  118. /// Return track index with matching bone node, or M_MAX_UNSIGNED if not found.
  119. unsigned GetTrackIndex(Node* node) const;
  120. /// Return track index by bone name, or M_MAX_UNSIGNED if not found.
  121. unsigned GetTrackIndex(const String& name) const;
  122. /// Return track index by bone name hash, or M_MAX_UNSIGNED if not found.
  123. unsigned GetTrackIndex(StringHash nameHash) const;
  124. /// Return whether weight is nonzero.
  125. /// @property
  126. bool IsEnabled() const { return weight_ > 0.0f; }
  127. /// Return whether looped.
  128. /// @property
  129. bool IsLooped() const { return looped_; }
  130. /// Return blending weight.
  131. /// @property
  132. float GetWeight() const { return weight_; }
  133. /// Return blending mode.
  134. /// @property
  135. AnimationBlendMode GetBlendMode() const { return blendingMode_; }
  136. /// Return time position.
  137. /// @property
  138. float GetTime() const { return time_; }
  139. /// Return animation length.
  140. /// @property
  141. float GetLength() const;
  142. /// Return blending layer.
  143. /// @property
  144. unsigned char GetLayer() const { return layer_; }
  145. /// Apply the animation at the current time position.
  146. void Apply();
  147. private:
  148. /// Apply animation to a skeleton. Transform changes are applied silently, so the model needs to dirty its root model afterward.
  149. void ApplyToModel();
  150. /// Apply animation to a scene node hierarchy.
  151. void ApplyToNodes();
  152. /// Apply track.
  153. void ApplyTrack(AnimationStateTrack& stateTrack, float weight, bool silent);
  154. /// Animated model (model mode).
  155. WeakPtr<AnimatedModel> model_;
  156. /// Root scene node (node hierarchy mode).
  157. WeakPtr<Node> node_;
  158. /// Animation.
  159. SharedPtr<Animation> animation_;
  160. /// Start bone.
  161. Bone* startBone_;
  162. /// Per-track data.
  163. Vector<AnimationStateTrack> stateTracks_;
  164. /// Looped flag.
  165. bool looped_;
  166. /// Blending weight.
  167. float weight_;
  168. /// Time position.
  169. float time_;
  170. /// Blending layer.
  171. unsigned char layer_;
  172. /// Blending mode.
  173. AnimationBlendMode blendingMode_;
  174. };
  175. }