Animation.h 6.8 KB

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