BsAnimationClip.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. Vector<TNamedAnimationCurve<Vector3>> position;
  19. Vector<TNamedAnimationCurve<Quaternion>> rotation;
  20. Vector<TNamedAnimationCurve<Vector3>> scale;
  21. Vector<TNamedAnimationCurve<float>> generic;
  22. };
  23. /** Types of curves in an AnimationClip. */
  24. enum class CurveType
  25. {
  26. Position,
  27. Rotation,
  28. Scale,
  29. Generic
  30. };
  31. /**
  32. * Contains animation curves for translation/rotation/scale of scene objects/skeleton bones, as well as curves for
  33. * generic property animation.
  34. */
  35. class BS_CORE_EXPORT AnimationClip : public Resource
  36. {
  37. public:
  38. virtual ~AnimationClip() { }
  39. /**
  40. * Registers a new curve used for animating position.
  41. *
  42. * @param[in] name Unique name of the curve. This name will be used mapping the curve to the relevant bone
  43. * in a skeleton, if any.
  44. * @param[in] curve Curve to add to the clip.
  45. *
  46. * @note Adding a new curve to a clip is a relatively slow operations and shouldn't be done at runtime.
  47. */
  48. void addPositionCurve(const String& name, const TAnimationCurve<Vector3>& curve);
  49. /**
  50. * Registers a new curve used for animating rotation.
  51. *
  52. * @param[in] name Unique name of the curve. This name will be used mapping the curve to the relevant bone
  53. * in a skeleton, if any.
  54. * @param[in] curve Curve to add to the clip.
  55. *
  56. * @note Adding a new curve to a clip is a relatively slow operations and shouldn't be done at runtime.
  57. */
  58. void addRotationCurve(const String& name, const TAnimationCurve<Quaternion>& curve);
  59. /**
  60. * Registers a new curve used for animating scale.
  61. *
  62. * @param[in] name Unique name of the curve. This name will be used mapping the curve to the relevant bone
  63. * in a skeleton, if any.
  64. * @param[in] curve Curve to add to the clip.
  65. *
  66. * @note Adding a new curve to a clip is a relatively slow operations and shouldn't be done at runtime.
  67. */
  68. void addScaleCurve(const String& name, const TAnimationCurve<Vector3>& curve);
  69. /**
  70. * Registers a new curve used for generic animation.
  71. *
  72. * @param[in] name Unique name of the curve. This can be used for retrieving the value of the curve
  73. * from animation.
  74. * @param[in] curve Curve to add to the clip.
  75. *
  76. * @note Adding a new curve to a clip is a relatively slow operations and shouldn't be done at runtime.
  77. */
  78. void addGenericCurve(const String& name, const TAnimationCurve<float>& curve);
  79. /**
  80. * Removes an existing curve from the clip.
  81. *
  82. * @param[in] name Name of the curve to remove.
  83. *
  84. * @note Removing curve from a clip is a relatively slow operations and shouldn't be done at runtime.
  85. */
  86. void removePositionCurve(const String& name);
  87. /**
  88. * Removes an existing curve from the clip.
  89. *
  90. * @param[in] name Name of the curve to remove.
  91. *
  92. * @note Removing curve from a clip is a relatively slow operations and shouldn't be done at runtime.
  93. */
  94. void removeRotationCurve(const String& name);
  95. /**
  96. * Removes an existing curve from the clip.
  97. *
  98. * @param[in] name Name of the curve to remove.
  99. *
  100. * @note Removing curve from a clip is a relatively slow operations and shouldn't be done at runtime.
  101. */
  102. void removeScaleCurve(const String& name);
  103. /**
  104. * Removes an existing curve from the clip.
  105. *
  106. * @param[in] name Name of the curve to remove.
  107. *
  108. * @note Removing curve from a clip is a relatively slow operations and shouldn't be done at runtime.
  109. */
  110. void removeGenericCurve(const String& name);
  111. /**
  112. * Returns all curves stored in the animation. Returned value will not be updated if the animation clip curves are
  113. * added or removed. Caller must monitor for changes and retrieve a new set of curves when that happens.
  114. */
  115. SPtr<AnimationCurves> getCurves() const { return mCurves; }
  116. /**
  117. * Maps skeleton bone names to animation clip names, and returns a set of indices that can be easily used for
  118. * locating an animation curve based on the bone index.
  119. *
  120. * @param[in] skeleton Skeleton to create the mapping for.
  121. * @param[out] mapping Pre-allocated array that will receive output animation clip indices. The array must
  122. * be large enough to store an index for every bone in the @p skeleton. Bones that have
  123. * no related animation curves will be assigned value -1.
  124. */
  125. void getBoneMapping(const Skeleton& skeleton, AnimationCurveMapping* mapping) const;
  126. /**
  127. * Checks are the curves contained within the clip additive. Additive clips are intended to be added on top of
  128. * other clips.
  129. */
  130. bool isAdditive() const { return mIsAdditive; }
  131. /**
  132. * Returns a version that can be used for detecting modifications on the clip by external systems. Whenever the clip
  133. * is modified the version is increased by one.
  134. */
  135. UINT64 getVersion() const { return mVersion; }
  136. /**
  137. * Creates an animation clip with no curves. After creation make sure to register some animation curves before
  138. * using it.
  139. */
  140. static HAnimationClip create(bool isAdditive = false);
  141. /**
  142. * Creates an animation clip with specified curves.
  143. *
  144. * @param[in] curves Curves to initialize the animation with.
  145. * @param[in] isAdditive Determines does the clip contain additive curve data. This will change the behaviour
  146. * how is the clip blended with other animations.
  147. */
  148. static HAnimationClip create(const SPtr<AnimationCurves>& curves, bool isAdditive = false);
  149. public: // ***** INTERNAL ******
  150. /** @name Internal
  151. * @{
  152. */
  153. /** Creates a new AnimationClip without initializing it. Use create() for normal use. */
  154. static SPtr<AnimationClip> _createPtr(const SPtr<AnimationCurves>& curves, bool isAdditive = false);
  155. /** @} */
  156. protected:
  157. AnimationClip();
  158. AnimationClip(const SPtr<AnimationCurves>& curves, bool isAdditive);
  159. /** @copydoc Resource::initialize() */
  160. void initialize() override;
  161. /** Creates a name -> curve index mapping for quicker curve lookup by name. */
  162. void buildNameMapping();
  163. UINT64 mVersion;
  164. /**
  165. * Contains all the animation curves in the clip. It's important this field is immutable so it may be used on other
  166. * threads. This means any modifications to the field will require a brand new data structure to be generated and
  167. * all existing data copied (plus the modification).
  168. */
  169. SPtr<AnimationCurves> mCurves;
  170. /**
  171. * Contains a map from curve name to curve index. Indices are stored as specified in CurveType enum.
  172. */
  173. UnorderedMap<String, UINT32[4]> mNameMapping;
  174. bool mIsAdditive;
  175. /************************************************************************/
  176. /* SERIALIZATION */
  177. /************************************************************************/
  178. public:
  179. friend class AnimationClipRTTI;
  180. static RTTITypeBase* getRTTIStatic();
  181. RTTITypeBase* getRTTI() const override;
  182. /**
  183. * Creates an AnimationClip with no data. You must populate its data manually followed by a call to initialize().
  184. *
  185. * @note For serialization use only.
  186. */
  187. static SPtr<AnimationClip> createEmpty();
  188. };
  189. /** @} */
  190. }