BsAnimationClip.h 7.2 KB

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