BsAnimationClip.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsResource.h"
  6. #include "BsVector3.h"
  7. #include "BsQuaternion.h"
  8. #include "BsAnimationCurve.h"
  9. namespace BansheeEngine
  10. {
  11. /** @addtogroup Animation
  12. * @{
  13. */
  14. struct AnimationCurveMapping;
  15. /** A set of animation curves representing translation/rotation/scale and generic animation. */
  16. struct AnimationCurves
  17. {
  18. /**
  19. * Registers a new curve used for animating position.
  20. *
  21. * @param[in] name Unique name of the curve. This name will be used mapping the curve to the relevant bone
  22. * in a skeleton, if any.
  23. * @param[in] curve Curve to add to the clip.
  24. */
  25. void addPositionCurve(const String& name, const TAnimationCurve<Vector3>& curve);
  26. /**
  27. * Registers a new curve used for animating rotation.
  28. *
  29. * @param[in] name Unique name of the curve. This name will be used mapping the curve to the relevant bone
  30. * in a skeleton, if any.
  31. * @param[in] curve Curve to add to the clip.
  32. */
  33. void addRotationCurve(const String& name, const TAnimationCurve<Quaternion>& curve);
  34. /**
  35. * Registers a new curve used for animating scale.
  36. *
  37. * @param[in] name Unique name of the curve. This name will be used mapping the curve to the relevant bone
  38. * in a skeleton, if any.
  39. * @param[in] curve Curve to add to the clip.
  40. */
  41. void addScaleCurve(const String& name, const TAnimationCurve<Vector3>& curve);
  42. /**
  43. * Registers a new curve used for generic animation.
  44. *
  45. * @param[in] name Unique name of the curve. This can be used for retrieving the value of the curve
  46. * from animation.
  47. * @param[in] curve Curve to add to the clip.
  48. */
  49. void addGenericCurve(const String& name, const TAnimationCurve<float>& curve);
  50. /** Removes an existing curve from the clip. */
  51. void removePositionCurve(const String& name);
  52. /** Removes an existing curve from the clip. */
  53. void removeRotationCurve(const String& name);
  54. /** Removes an existing curve from the clip. */
  55. void removeScaleCurve(const String& name);
  56. /** Removes an existing curve from the clip. */
  57. void removeGenericCurve(const String& name);
  58. Vector<TNamedAnimationCurve<Vector3>> position;
  59. Vector<TNamedAnimationCurve<Quaternion>> rotation;
  60. Vector<TNamedAnimationCurve<Vector3>> scale;
  61. Vector<TNamedAnimationCurve<float>> generic;
  62. };
  63. /** Event that is triggered when animation reaches a certain point. */
  64. struct AnimationEvent
  65. {
  66. AnimationEvent()
  67. :time(0.0f)
  68. { }
  69. AnimationEvent(const String& name, float time)
  70. :name(name), time(time)
  71. { }
  72. String name;
  73. float time;
  74. };
  75. /** Types of curves in an AnimationClip. */
  76. enum class CurveType
  77. {
  78. Position,
  79. Rotation,
  80. Scale,
  81. Generic
  82. };
  83. /**
  84. * Contains animation curves for translation/rotation/scale of scene objects/skeleton bones, as well as curves for
  85. * generic property animation.
  86. */
  87. class BS_CORE_EXPORT AnimationClip : public Resource
  88. {
  89. public:
  90. virtual ~AnimationClip() { }
  91. /**
  92. * Returns all curves stored in the animation. Returned value will not be updated if the animation clip curves are
  93. * added or removed. Caller must monitor for changes and retrieve a new set of curves when that happens.
  94. */
  95. SPtr<AnimationCurves> getCurves() const { return mCurves; }
  96. /** Assigns a new set of curves to be used by the animation. The clip will store a copy of this object.*/
  97. void setCurves(const AnimationCurves& curves);
  98. /** Returns all events that will be triggered by the animation. */
  99. const Vector<AnimationEvent>& getEvents() const { return mEvents; }
  100. /** Sets events that will be triggered as the animation is playing. */
  101. void setEvents(const Vector<AnimationEvent>& events) { mEvents = events; }
  102. /**
  103. * Maps skeleton bone names to animation curve names, and returns a set of indices that can be easily used for
  104. * locating an animation curve based on the bone index.
  105. *
  106. * @param[in] skeleton Skeleton to create the mapping for.
  107. * @param[out] mapping Pre-allocated array that will receive output animation clip indices. The array must
  108. * be large enough to store an index for every bone in the @p skeleton. Bones that have
  109. * no related animation curves will be assigned value -1.
  110. */
  111. void getBoneMapping(const Skeleton& skeleton, AnimationCurveMapping* mapping) const;
  112. /**
  113. * Attempts to find translation/rotation/scale curves with the specified name and fills the mapping structure with
  114. * their indices, which can then be used for quick lookup.
  115. *
  116. * @param[in] name Name of the curves to look up.
  117. * @param[out] mapping Triple containing the translation/rotation/scale indices of the found curves. Indices
  118. * will be -1 for curves that haven't been found.
  119. */
  120. void getCurveMapping(const String& name, AnimationCurveMapping& mapping) const;
  121. /**
  122. * Checks are the curves contained within the clip additive. Additive clips are intended to be added on top of
  123. * other clips.
  124. */
  125. bool isAdditive() const { return mIsAdditive; }
  126. /** Returns the length of the animation clip, in seconds. */
  127. float getLength() const { return mLength; }
  128. /**
  129. * Returns the number of samples per second the animation clip curves were sampled at.
  130. *
  131. * @note This value is not used by the animation clip or curves directly since unevenly spaced keyframes are
  132. * supported. But it can be of value when determining the original sample rate of an imported animation
  133. * or similar.
  134. */
  135. UINT32 getSampleRate() const { return mSampleRate; }
  136. /**
  137. * Sets the number of samples per second the animation clip curves were sampled at.
  138. *
  139. * @see getSampleRate()
  140. */
  141. void setSampleRate(UINT32 sampleRate) { mSampleRate = sampleRate; }
  142. /**
  143. * Returns a version that can be used for detecting modifications on the clip by external systems. Whenever the clip
  144. * is modified the version is increased by one.
  145. */
  146. UINT64 getVersion() const { return mVersion; }
  147. /**
  148. * Creates an animation clip with no curves. After creation make sure to register some animation curves before
  149. * using it.
  150. */
  151. static HAnimationClip create(bool isAdditive = false);
  152. /**
  153. * Creates an animation clip with specified curves.
  154. *
  155. * @param[in] curves Curves to initialize the animation with.
  156. * @param[in] isAdditive Determines does the clip contain additive curve data. This will change the behaviour
  157. * how is the clip blended with other animations.
  158. * @param[in] sampleRate If animation uses evenly spaced keyframes, number of samples per second. Not relevant
  159. * if keyframes are unevenly spaced.
  160. */
  161. static HAnimationClip create(const SPtr<AnimationCurves>& curves, bool isAdditive = false, UINT32 sampleRate = 1);
  162. public: // ***** INTERNAL ******
  163. /** @name Internal
  164. * @{
  165. */
  166. /** Creates a new AnimationClip without initializing it. Use create() for normal use. */
  167. static SPtr<AnimationClip> _createPtr(const SPtr<AnimationCurves>& curves, bool isAdditive = false, UINT32 sampleRate = 1);
  168. /** @} */
  169. protected:
  170. AnimationClip();
  171. AnimationClip(const SPtr<AnimationCurves>& curves, bool isAdditive, UINT32 sampleRate);
  172. /** @copydoc Resource::initialize() */
  173. void initialize() override;
  174. /** Creates a name -> curve index mapping for quicker curve lookup by name. */
  175. void buildNameMapping();
  176. /** Calculate the length of the clip based on assigned curves. */
  177. void calculateLength();
  178. UINT64 mVersion;
  179. /**
  180. * Contains all the animation curves in the clip. It's important this field is immutable so it may be used on other
  181. * threads. This means any modifications to the field will require a brand new data structure to be generated and
  182. * all existing data copied (plus the modification).
  183. */
  184. SPtr<AnimationCurves> mCurves;
  185. /**
  186. * Contains a map from curve name to curve index. Indices are stored as specified in CurveType enum.
  187. */
  188. UnorderedMap<String, UINT32[4]> mNameMapping;
  189. Vector<AnimationEvent> mEvents;
  190. bool mIsAdditive;
  191. float mLength;
  192. UINT32 mSampleRate;
  193. /************************************************************************/
  194. /* SERIALIZATION */
  195. /************************************************************************/
  196. public:
  197. friend class AnimationClipRTTI;
  198. static RTTITypeBase* getRTTIStatic();
  199. RTTITypeBase* getRTTI() const override;
  200. /**
  201. * Creates an AnimationClip with no data. You must populate its data manually followed by a call to initialize().
  202. *
  203. * @note For serialization use only.
  204. */
  205. static SPtr<AnimationClip> createEmpty();
  206. };
  207. /** @} */
  208. }