AnimationState.h 6.2 KB

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