AnimationState.h 5.3 KB

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