AnimationState.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // Copyright (c) 2008-2013 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 "HashMap.h"
  24. #include "Ptr.h"
  25. namespace Urho3D
  26. {
  27. class Animation;
  28. class AnimatedModel;
  29. class Deserializer;
  30. class Serializer;
  31. class Skeleton;
  32. struct AnimationTrack;
  33. struct Bone;
  34. /// %Animation instance per-track data.
  35. struct AnimationStateTrack
  36. {
  37. /// Construct with defaults.
  38. AnimationStateTrack();
  39. /// Destruct
  40. ~AnimationStateTrack();
  41. /// Animation track.
  42. const AnimationTrack* track_;
  43. /// Bone pointer.
  44. Bone* bone_;
  45. /// Scene node pointer.
  46. WeakPtr<Node> node_;
  47. /// Blending weight.
  48. float weight_;
  49. /// Last key frame.
  50. unsigned keyFrame_;
  51. };
  52. /// %Animation instance.
  53. class URHO3D_API AnimationState : public RefCounted
  54. {
  55. public:
  56. /// Construct with animated model and animation pointers.
  57. AnimationState(AnimatedModel* model, Animation* animation);
  58. /// Construct with root scene node and animation pointers.
  59. AnimationState(Node* node, Animation* animation);
  60. /// Destruct.
  61. ~AnimationState();
  62. /// Set start bone. Not supported in node animation mode. Resets any assigned per-bone weights.
  63. void SetStartBone(Bone* bone);
  64. /// Set looping enabled/disabled.
  65. void SetLooped(bool looped);
  66. /// Set blending weight.
  67. void SetWeight(float weight);
  68. /// Set time position. Does not fire animation triggers.
  69. void SetTime(float time);
  70. /// 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.
  71. void SetBoneWeight(unsigned index, float weight);
  72. /// Set per-bone blending weight by name.
  73. void SetBoneWeight(const String& name, float weight);
  74. /// Set per-bone blending weight by name hash.
  75. void SetBoneWeight(StringHash nameHash, float weight);
  76. /// Modify blending weight.
  77. void AddWeight(float delta);
  78. /// Modify time position. %Animation triggers will be fired.
  79. void AddTime(float delta);
  80. /// Set blending layer.
  81. void SetLayer(unsigned char layer);
  82. /// Return animation.
  83. Animation* GetAnimation() const { return animation_; }
  84. /// Return animated model this state belongs to (model mode.)
  85. AnimatedModel* GetModel() const;
  86. /// Return root scene node this state controls (node hierarchy mode.)
  87. Node* GetNode() const;
  88. /// Return start bone.
  89. Bone* GetStartBone() const;
  90. /// Return per-bone blending weight by track index.
  91. float GetBoneWeight(unsigned index) const;
  92. /// Return per-bone blending weight by name.
  93. float GetBoneWeight(const String& name) const;
  94. /// Return per-bone blending weight by name.
  95. float GetBoneWeight(StringHash nameHash) const;
  96. /// Return track index by bone name, or M_MAX_UNSIGNED if not found.
  97. unsigned GetTrackIndex(const String& name) const;
  98. /// Return track index by bone name hash, or M_MAX_UNSIGNED if not found.
  99. unsigned GetTrackIndex(StringHash nameHash) const;
  100. /// Return whether weight is nonzero.
  101. bool IsEnabled() const { return weight_ > 0.0f; }
  102. /// Return whether looped.
  103. bool IsLooped() const { return looped_; }
  104. /// Return blending weight.
  105. float GetWeight() const { return weight_; }
  106. /// Return time position.
  107. float GetTime() const { return time_; }
  108. /// Return animation length.
  109. float GetLength() const;
  110. /// Return blending layer.
  111. unsigned char GetLayer() const { return layer_; }
  112. /// Apply the animation at the current time position.
  113. void Apply();
  114. private:
  115. /// Apply animation to a skeleton.
  116. void ApplyToModel();
  117. /// Apply animation to a scene node hierarchy.
  118. void ApplyToNodes();
  119. /// Apply animation track to a scene node, full weight.
  120. void ApplyTrackFullWeight(AnimationStateTrack& stateTrack);
  121. /// Apply animation track to a scene node, blended with current node transform.
  122. void ApplyTrackBlended(AnimationStateTrack& stateTrack, float weight);
  123. /// Animated model (model mode.)
  124. WeakPtr<AnimatedModel> model_;
  125. /// Root scene node (node hierarchy mode.)
  126. WeakPtr<Node> node_;
  127. /// Animation.
  128. SharedPtr<Animation> animation_;
  129. /// Start bone.
  130. Bone* startBone_;
  131. /// Per-track data.
  132. Vector<AnimationStateTrack> stateTracks_;
  133. /// Looped flag.
  134. bool looped_;
  135. /// Blending weight.
  136. float weight_;
  137. /// Time position.
  138. float time_;
  139. /// Blending layer.
  140. unsigned char layer_;
  141. };
  142. }