Animation.h 7.0 KB

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