Animation.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // Copyright (c) 2008-2020 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. /// @fakeref
  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. static void RegisterObject(Context* context);
  116. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  117. bool BeginLoad(Deserializer& source) override;
  118. /// Save resource. Return true if successful.
  119. bool Save(Serializer& dest) const override;
  120. /// Set animation name.
  121. /// @property
  122. void SetAnimationName(const String& name);
  123. /// Set animation length.
  124. /// @property
  125. void SetLength(float length);
  126. /// Create and return a track by name. If track by same name already exists, returns the existing.
  127. AnimationTrack* CreateTrack(const String& name);
  128. /// Remove a track by name. Return true if was found and removed successfully. This is unsafe if the animation is currently used in playback.
  129. bool RemoveTrack(const String& name);
  130. /// Remove all tracks. This is unsafe if the animation is currently used in playback.
  131. void RemoveAllTracks();
  132. /// Set a trigger point at index.
  133. /// @property{set_triggers}
  134. void SetTrigger(unsigned index, const AnimationTriggerPoint& trigger);
  135. /// Add a trigger point.
  136. void AddTrigger(const AnimationTriggerPoint& trigger);
  137. /// Add a trigger point.
  138. void AddTrigger(float time, bool timeIsNormalized, const Variant& data);
  139. /// Remove a trigger point by index.
  140. void RemoveTrigger(unsigned index);
  141. /// Remove all trigger points.
  142. void RemoveAllTriggers();
  143. /// Resize trigger point vector.
  144. /// @property
  145. void SetNumTriggers(unsigned num);
  146. /// Clone the animation.
  147. SharedPtr<Animation> Clone(const String& cloneName = String::EMPTY) const;
  148. /// Return animation name.
  149. /// @property
  150. const String& GetAnimationName() const { return animationName_; }
  151. /// Return animation name hash.
  152. StringHash GetAnimationNameHash() const { return animationNameHash_; }
  153. /// Return animation length.
  154. /// @property
  155. float GetLength() const { return length_; }
  156. /// Return all animation tracks.
  157. const HashMap<StringHash, AnimationTrack>& GetTracks() const { return tracks_; }
  158. /// Return number of animation tracks.
  159. /// @property
  160. unsigned GetNumTracks() const { return tracks_.Size(); }
  161. /// Return animation track by index.
  162. AnimationTrack *GetTrack(unsigned index);
  163. /// Return animation track by name.
  164. /// @property{get_tracks}
  165. AnimationTrack* GetTrack(const String& name);
  166. /// Return animation track by name hash.
  167. AnimationTrack* GetTrack(StringHash nameHash);
  168. /// Return animation trigger points.
  169. const Vector<AnimationTriggerPoint>& GetTriggers() const { return triggers_; }
  170. /// Return number of animation trigger points.
  171. /// @property
  172. unsigned GetNumTriggers() const { return triggers_.Size(); }
  173. /// Return a trigger point by index.
  174. AnimationTriggerPoint* GetTrigger(unsigned index);
  175. private:
  176. /// Animation name.
  177. String animationName_;
  178. /// Animation name hash.
  179. StringHash animationNameHash_;
  180. /// Animation length.
  181. float length_;
  182. /// Animation tracks.
  183. HashMap<StringHash, AnimationTrack> tracks_;
  184. /// Animation trigger points.
  185. Vector<AnimationTriggerPoint> triggers_;
  186. };
  187. }