Animation.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../Container/FlagSet.h"
  6. #include "../Container/Ptr.h"
  7. #include "../Math/Quaternion.h"
  8. #include "../Math/Vector3.h"
  9. #include "../Resource/Resource.h"
  10. namespace Urho3D
  11. {
  12. enum class AnimationChannels : u8
  13. {
  14. None = 0,
  15. Position = 1 << 0,
  16. Rotation = 1 << 1,
  17. Scale = 1 << 2,
  18. };
  19. URHO3D_FLAGS(AnimationChannels);
  20. /// Skeletal animation keyframe.
  21. struct AnimationKeyFrame
  22. {
  23. /// Construct.
  24. AnimationKeyFrame() :
  25. time_(0.0f),
  26. scale_(Vector3::ONE)
  27. {
  28. }
  29. /// Keyframe time.
  30. float time_;
  31. /// Bone position.
  32. Vector3 position_;
  33. /// Bone rotation.
  34. Quaternion rotation_;
  35. /// Bone scale.
  36. Vector3 scale_;
  37. };
  38. /// Skeletal animation track, stores keyframes of a single bone.
  39. /// @nocount
  40. struct URHO3D_API AnimationTrack
  41. {
  42. /// Construct.
  43. AnimationTrack()
  44. {
  45. }
  46. /// Assign keyframe at index.
  47. /// @property{set_keyFrames}
  48. void SetKeyFrame(i32 index, const AnimationKeyFrame& keyFrame);
  49. /// Add a keyframe at the end.
  50. void AddKeyFrame(const AnimationKeyFrame& keyFrame);
  51. /// Insert a keyframe at index.
  52. void InsertKeyFrame(i32 index, const AnimationKeyFrame& keyFrame);
  53. /// Remove a keyframe at index.
  54. void RemoveKeyFrame(i32 index);
  55. /// Remove all keyframes.
  56. void RemoveAllKeyFrames();
  57. /// Return keyframe at index, or null if not found.
  58. AnimationKeyFrame* GetKeyFrame(i32 index);
  59. /// Return number of keyframes.
  60. /// @property
  61. i32 GetNumKeyFrames() const { return keyFrames_.Size(); }
  62. /// Return keyframe index based on time and previous index. Return false if animation is empty.
  63. bool GetKeyFrameIndex(float time, i32& index) const;
  64. /// Bone or scene node name.
  65. String name_;
  66. /// Name hash.
  67. StringHash nameHash_;
  68. /// Bitmask of included data (position, rotation, scale).
  69. AnimationChannels channelMask_{};
  70. /// Keyframes.
  71. Vector<AnimationKeyFrame> keyFrames_;
  72. };
  73. /// %Animation trigger point.
  74. struct AnimationTriggerPoint
  75. {
  76. /// Construct.
  77. AnimationTriggerPoint() :
  78. time_(0.0f)
  79. {
  80. }
  81. /// Trigger time.
  82. float time_;
  83. /// Trigger data.
  84. Variant data_;
  85. };
  86. /// Skeletal animation resource.
  87. class URHO3D_API Animation : public ResourceWithMetadata
  88. {
  89. URHO3D_OBJECT(Animation, ResourceWithMetadata);
  90. public:
  91. /// Construct.
  92. explicit Animation(Context* context);
  93. /// Destruct.
  94. ~Animation() override;
  95. /// Register object factory.
  96. /// @nobind
  97. static void RegisterObject(Context* context);
  98. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  99. bool BeginLoad(Deserializer& source) override;
  100. /// Save resource. Return true if successful.
  101. bool Save(Serializer& dest) const override;
  102. /// Set animation name.
  103. /// @property
  104. void SetAnimationName(const String& name);
  105. /// Set animation length.
  106. /// @property
  107. void SetLength(float length);
  108. /// Create and return a track by name. If track by same name already exists, returns the existing.
  109. AnimationTrack* CreateTrack(const String& name);
  110. /// Remove a track by name. Return true if was found and removed successfully. This is unsafe if the animation is currently used in playback.
  111. bool RemoveTrack(const String& name);
  112. /// Remove all tracks. This is unsafe if the animation is currently used in playback.
  113. void RemoveAllTracks();
  114. /// Set a trigger point at index.
  115. /// @property{set_triggers}
  116. void SetTrigger(i32 index, const AnimationTriggerPoint& trigger);
  117. /// Add a trigger point.
  118. void AddTrigger(const AnimationTriggerPoint& trigger);
  119. /// Add a trigger point.
  120. void AddTrigger(float time, bool timeIsNormalized, const Variant& data);
  121. /// Remove a trigger point by index.
  122. void RemoveTrigger(i32 index);
  123. /// Remove all trigger points.
  124. void RemoveAllTriggers();
  125. /// Resize trigger point vector.
  126. /// @property
  127. void SetNumTriggers(i32 num);
  128. /// Clone the animation.
  129. SharedPtr<Animation> Clone(const String& cloneName = String::EMPTY) const;
  130. /// Return animation name.
  131. /// @property
  132. const String& GetAnimationName() const { return animationName_; }
  133. /// Return animation name hash.
  134. StringHash GetAnimationNameHash() const { return animationNameHash_; }
  135. /// Return animation length.
  136. /// @property
  137. float GetLength() const { return length_; }
  138. /// Return all animation tracks.
  139. const HashMap<StringHash, AnimationTrack>& GetTracks() const { return tracks_; }
  140. /// Return number of animation tracks.
  141. /// @property
  142. i32 GetNumTracks() const { return tracks_.Size(); }
  143. /// Return animation track by index.
  144. AnimationTrack *GetTrack(i32 index);
  145. /// Return animation track by name.
  146. /// @property{get_tracks}
  147. AnimationTrack* GetTrack(const String& name);
  148. /// Return animation track by name hash.
  149. AnimationTrack* GetTrack(StringHash nameHash);
  150. /// Return animation trigger points.
  151. const Vector<AnimationTriggerPoint>& GetTriggers() const { return triggers_; }
  152. /// Return number of animation trigger points.
  153. /// @property
  154. i32 GetNumTriggers() const { return triggers_.Size(); }
  155. /// Return a trigger point by index.
  156. AnimationTriggerPoint* GetTrigger(i32 index);
  157. private:
  158. /// Animation name.
  159. String animationName_;
  160. /// Animation name hash.
  161. StringHash animationNameHash_;
  162. /// Animation length.
  163. float length_;
  164. /// Animation tracks.
  165. HashMap<StringHash, AnimationTrack> tracks_;
  166. /// Animation trigger points.
  167. Vector<AnimationTriggerPoint> triggers_;
  168. };
  169. }