BsAnimationClip.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. * Returns a version that can be used for detecting modifications on the clip by external systems. Whenever the clip
  128. * is modified the version is increased by one.
  129. */
  130. UINT64 getVersion() const { return mVersion; }
  131. /**
  132. * Creates an animation clip with no curves. After creation make sure to register some animation curves before
  133. * using it.
  134. */
  135. static HAnimationClip create();
  136. /**
  137. * Creates an animation clip with specified curves.
  138. *
  139. * @param[in] curves Curves to initialize the animation with.
  140. */
  141. static HAnimationClip create(const SPtr<AnimationCurves>& curves);
  142. public: // ***** INTERNAL ******
  143. /** @name Internal
  144. * @{
  145. */
  146. /** Creates a new AnimationClip without initializing it. Use create() for normal use. */
  147. static SPtr<AnimationClip> _createPtr(const SPtr<AnimationCurves>& curves);
  148. /** @} */
  149. protected:
  150. AnimationClip();
  151. AnimationClip(const SPtr<AnimationCurves>& curves);
  152. /** @copydoc Resource::initialize() */
  153. void initialize() override;
  154. /** Creates a name -> curve index mapping for quicker curve lookup by name. */
  155. void buildNameMapping();
  156. UINT64 mVersion;
  157. /**
  158. * Contains all the animation curves in the clip. It's important this field is immutable so it may be used on other
  159. * threads. This means any modifications to the field will require a brand new data structure to be generated and
  160. * all existing data copied (plus the modification).
  161. */
  162. SPtr<AnimationCurves> mCurves;
  163. /**
  164. * Contains a map from curve name to curve index. Indices are stored as specified in CurveType enum.
  165. */
  166. UnorderedMap<String, UINT32[4]> mNameMapping;
  167. /************************************************************************/
  168. /* SERIALIZATION */
  169. /************************************************************************/
  170. public:
  171. friend class AnimationClipRTTI;
  172. static RTTITypeBase* getRTTIStatic();
  173. RTTITypeBase* getRTTI() const override;
  174. /**
  175. * Creates an AnimationClip with no data. You must populate its data manually followed by a call to initialize().
  176. *
  177. * @note For serialization use only.
  178. */
  179. static SPtr<AnimationClip> createEmpty();
  180. };
  181. /** @} */
  182. }