Animation.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. struct URHO3D_API AnimationTrack
  59. {
  60. /// Construct.
  61. AnimationTrack()
  62. {
  63. }
  64. /// Assign keyframe at index.
  65. void SetKeyFrame(unsigned index, const AnimationKeyFrame& keyFrame);
  66. /// Add a keyframe at the end.
  67. void AddKeyFrame(const AnimationKeyFrame& keyFrame);
  68. /// Insert a keyframe at index.
  69. void InsertKeyFrame(unsigned index, const AnimationKeyFrame& keyFrame);
  70. /// Remove a keyframe at index.
  71. void RemoveKeyFrame(unsigned index);
  72. /// Remove all keyframes.
  73. void RemoveAllKeyFrames();
  74. /// Return keyframe at index, or null if not found.
  75. AnimationKeyFrame* GetKeyFrame(unsigned index);
  76. /// Return number of keyframes.
  77. unsigned GetNumKeyFrames() const { return keyFrames_.Size(); }
  78. /// Return keyframe index based on time and previous index. Return false if animation is empty.
  79. bool GetKeyFrameIndex(float time, unsigned& index) const;
  80. /// Bone or scene node name.
  81. String name_;
  82. /// Name hash.
  83. StringHash nameHash_;
  84. /// Bitmask of included data (position, rotation, scale).
  85. AnimationChannelFlags channelMask_{};
  86. /// Keyframes.
  87. Vector<AnimationKeyFrame> keyFrames_;
  88. };
  89. /// %Animation trigger point.
  90. struct AnimationTriggerPoint
  91. {
  92. /// Construct.
  93. AnimationTriggerPoint() :
  94. time_(0.0f)
  95. {
  96. }
  97. /// Trigger time.
  98. float time_;
  99. /// Trigger data.
  100. Variant data_;
  101. };
  102. /// Skeletal animation resource.
  103. class URHO3D_API Animation : public ResourceWithMetadata
  104. {
  105. URHO3D_OBJECT(Animation, ResourceWithMetadata);
  106. public:
  107. /// Construct.
  108. explicit Animation(Context* context);
  109. /// Destruct.
  110. ~Animation() override;
  111. /// Register object factory.
  112. static void RegisterObject(Context* context);
  113. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  114. bool BeginLoad(Deserializer& source) override;
  115. /// Save resource. Return true if successful.
  116. bool Save(Serializer& dest) const override;
  117. /// Set animation name.
  118. void SetAnimationName(const String& name);
  119. /// Set animation length.
  120. void SetLength(float length);
  121. /// Create and return a track by name. If track by same name already exists, returns the existing.
  122. AnimationTrack* CreateTrack(const String& name);
  123. /// Remove a track by name. Return true if was found and removed successfully. This is unsafe if the animation is currently used in playback.
  124. bool RemoveTrack(const String& name);
  125. /// Remove all tracks. This is unsafe if the animation is currently used in playback.
  126. void RemoveAllTracks();
  127. /// Set a trigger point at index.
  128. void SetTrigger(unsigned index, const AnimationTriggerPoint& trigger);
  129. /// Add a trigger point.
  130. void AddTrigger(const AnimationTriggerPoint& trigger);
  131. /// Add a trigger point.
  132. void AddTrigger(float time, bool timeIsNormalized, const Variant& data);
  133. /// Remove a trigger point by index.
  134. void RemoveTrigger(unsigned index);
  135. /// Remove all trigger points.
  136. void RemoveAllTriggers();
  137. /// Resize trigger point vector.
  138. void SetNumTriggers(unsigned num);
  139. /// Clone the animation.
  140. SharedPtr<Animation> Clone(const String& cloneName = String::EMPTY) const;
  141. /// Return animation name.
  142. const String& GetAnimationName() const { return animationName_; }
  143. /// Return animation name hash.
  144. StringHash GetAnimationNameHash() const { return animationNameHash_; }
  145. /// Return animation length.
  146. float GetLength() const { return length_; }
  147. /// Return all animation tracks.
  148. const HashMap<StringHash, AnimationTrack>& GetTracks() const { return tracks_; }
  149. /// Return number of animation tracks.
  150. unsigned GetNumTracks() const { return tracks_.Size(); }
  151. /// Return animation track by index.
  152. AnimationTrack *GetTrack(unsigned index);
  153. /// Return animation track by name.
  154. AnimationTrack* GetTrack(const String& name);
  155. /// Return animation track by name hash.
  156. AnimationTrack* GetTrack(StringHash nameHash);
  157. /// Return animation trigger points.
  158. const Vector<AnimationTriggerPoint>& GetTriggers() const { return triggers_; }
  159. /// Return number of animation trigger points.
  160. unsigned GetNumTriggers() const { return triggers_.Size(); }
  161. /// Return a trigger point by index.
  162. AnimationTriggerPoint* GetTrigger(unsigned index);
  163. private:
  164. /// Animation name.
  165. String animationName_;
  166. /// Animation name hash.
  167. StringHash animationNameHash_;
  168. /// Animation length.
  169. float length_;
  170. /// Animation tracks.
  171. HashMap<StringHash, AnimationTrack> tracks_;
  172. /// Animation trigger points.
  173. Vector<AnimationTriggerPoint> triggers_;
  174. };
  175. }