BsAnimationClip.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. MorphFrame,
  83. MorphWeight,
  84. Count // Keep at end
  85. };
  86. /**
  87. * Contains animation curves for translation/rotation/scale of scene objects/skeleton bones, as well as curves for
  88. * generic property animation.
  89. */
  90. class BS_CORE_EXPORT AnimationClip : public Resource
  91. {
  92. public:
  93. virtual ~AnimationClip() { }
  94. /**
  95. * Returns all curves stored in the animation. Returned value will not be updated if the animation clip curves are
  96. * added or removed. Caller must monitor for changes and retrieve a new set of curves when that happens.
  97. */
  98. SPtr<AnimationCurves> getCurves() const { return mCurves; }
  99. /** Assigns a new set of curves to be used by the animation. The clip will store a copy of this object.*/
  100. void setCurves(const AnimationCurves& curves);
  101. /** Returns all events that will be triggered by the animation. */
  102. const Vector<AnimationEvent>& getEvents() const { return mEvents; }
  103. /** Sets events that will be triggered as the animation is playing. */
  104. void setEvents(const Vector<AnimationEvent>& events) { mEvents = events; }
  105. /**
  106. * Maps skeleton bone names to animation curve names, and returns a set of indices that can be easily used for
  107. * locating an animation curve based on the bone index.
  108. *
  109. * @param[in] skeleton Skeleton to create the mapping for.
  110. * @param[out] mapping Pre-allocated array that will receive output animation clip indices. The array must
  111. * be large enough to store an index for every bone in the @p skeleton. Bones that have
  112. * no related animation curves will be assigned value -1.
  113. */
  114. void getBoneMapping(const Skeleton& skeleton, AnimationCurveMapping* mapping) const;
  115. /**
  116. * Attempts to find translation/rotation/scale curves with the specified name and fills the mapping structure with
  117. * their indices, which can then be used for quick lookup.
  118. *
  119. * @param[in] name Name of the curves to look up.
  120. * @param[out] mapping Triple containing the translation/rotation/scale indices of the found curves. Indices
  121. * will be -1 for curves that haven't been found.
  122. */
  123. void getCurveMapping(const String& name, AnimationCurveMapping& mapping) const;
  124. /**
  125. * Attempts to find a generic curve with the specified name and fills output with found index, which can then be
  126. * used for quick lookup.
  127. *
  128. * @param[in] name Name of the curve to look up.
  129. * @param[out] frameIdx Index of the curve animating the morph shape frames, or -1 if not found.
  130. * @param[out] weightIdx Index of the curve animating the channel weight, or -1 if not found.
  131. */
  132. void getMorphMapping(const String& name, UINT32& frameIdx, UINT32& weightIdx) const;
  133. /**
  134. * Checks are the curves contained within the clip additive. Additive clips are intended to be added on top of
  135. * other clips.
  136. */
  137. bool isAdditive() const { return mIsAdditive; }
  138. /** Returns the length of the animation clip, in seconds. */
  139. float getLength() const { return mLength; }
  140. /**
  141. * Returns the number of samples per second the animation clip curves were sampled at.
  142. *
  143. * @note This value is not used by the animation clip or curves directly since unevenly spaced keyframes are
  144. * supported. But it can be of value when determining the original sample rate of an imported animation
  145. * or similar.
  146. */
  147. UINT32 getSampleRate() const { return mSampleRate; }
  148. /**
  149. * Sets the number of samples per second the animation clip curves were sampled at.
  150. *
  151. * @see getSampleRate()
  152. */
  153. void setSampleRate(UINT32 sampleRate) { mSampleRate = sampleRate; }
  154. /**
  155. * Returns a version that can be used for detecting modifications on the clip by external systems. Whenever the clip
  156. * is modified the version is increased by one.
  157. */
  158. UINT64 getVersion() const { return mVersion; }
  159. /**
  160. * Creates an animation clip with no curves. After creation make sure to register some animation curves before
  161. * using it.
  162. */
  163. static HAnimationClip create(bool isAdditive = false);
  164. /**
  165. * Creates an animation clip with specified curves.
  166. *
  167. * @param[in] curves Curves to initialize the animation with.
  168. * @param[in] isAdditive Determines does the clip contain additive curve data. This will change the behaviour
  169. * how is the clip blended with other animations.
  170. * @param[in] sampleRate If animation uses evenly spaced keyframes, number of samples per second. Not relevant
  171. * if keyframes are unevenly spaced.
  172. */
  173. static HAnimationClip create(const SPtr<AnimationCurves>& curves, bool isAdditive = false, UINT32 sampleRate = 1);
  174. public: // ***** INTERNAL ******
  175. /** @name Internal
  176. * @{
  177. */
  178. /** Creates a new AnimationClip without initializing it. Use create() for normal use. */
  179. static SPtr<AnimationClip> _createPtr(const SPtr<AnimationCurves>& curves, bool isAdditive = false, UINT32 sampleRate = 1);
  180. /** @} */
  181. protected:
  182. AnimationClip();
  183. AnimationClip(const SPtr<AnimationCurves>& curves, bool isAdditive, UINT32 sampleRate);
  184. /** @copydoc Resource::initialize() */
  185. void initialize() override;
  186. /** Creates a name -> curve index mapping for quicker curve lookup by name. */
  187. void buildNameMapping();
  188. /** Calculate the length of the clip based on assigned curves. */
  189. void calculateLength();
  190. UINT64 mVersion;
  191. /**
  192. * Contains all the animation curves in the clip. It's important this field is immutable so it may be used on other
  193. * threads. This means any modifications to the field will require a brand new data structure to be generated and
  194. * all existing data copied (plus the modification).
  195. */
  196. SPtr<AnimationCurves> mCurves;
  197. /**
  198. * Contains a map from curve name to curve index. Indices are stored as specified in CurveType enum.
  199. */
  200. UnorderedMap<String, UINT32[(int)CurveType::Count]> mNameMapping;
  201. Vector<AnimationEvent> mEvents;
  202. bool mIsAdditive;
  203. float mLength;
  204. UINT32 mSampleRate;
  205. /************************************************************************/
  206. /* SERIALIZATION */
  207. /************************************************************************/
  208. public:
  209. friend class AnimationClipRTTI;
  210. static RTTITypeBase* getRTTIStatic();
  211. RTTITypeBase* getRTTI() const override;
  212. /**
  213. * Creates an AnimationClip with no data. You must populate its data manually followed by a call to initialize().
  214. *
  215. * @note For serialization use only.
  216. */
  217. static SPtr<AnimationClip> createEmpty();
  218. };
  219. /** @} */
  220. }