2
0

BsAnimationClip.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. /** Types of curves in an AnimationClip. */
  64. enum class CurveType
  65. {
  66. Position,
  67. Rotation,
  68. Scale,
  69. Generic
  70. };
  71. /**
  72. * Contains animation curves for translation/rotation/scale of scene objects/skeleton bones, as well as curves for
  73. * generic property animation.
  74. */
  75. class BS_CORE_EXPORT AnimationClip : public Resource
  76. {
  77. public:
  78. virtual ~AnimationClip() { }
  79. /**
  80. * Returns all curves stored in the animation. Returned value will not be updated if the animation clip curves are
  81. * added or removed. Caller must monitor for changes and retrieve a new set of curves when that happens.
  82. */
  83. SPtr<AnimationCurves> getCurves() const { return mCurves; }
  84. /** Assigns a new set of curves to be used by the animation. The clip will store a copy of this object.*/
  85. void setCurves(const AnimationCurves& curves);
  86. /**
  87. * Maps skeleton bone names to animation clip names, and returns a set of indices that can be easily used for
  88. * locating an animation curve based on the bone index.
  89. *
  90. * @param[in] skeleton Skeleton to create the mapping for.
  91. * @param[out] mapping Pre-allocated array that will receive output animation clip indices. The array must
  92. * be large enough to store an index for every bone in the @p skeleton. Bones that have
  93. * no related animation curves will be assigned value -1.
  94. */
  95. void getBoneMapping(const Skeleton& skeleton, AnimationCurveMapping* mapping) const;
  96. /**
  97. * Checks are the curves contained within the clip additive. Additive clips are intended to be added on top of
  98. * other clips.
  99. */
  100. bool isAdditive() const { return mIsAdditive; }
  101. /**
  102. * Returns a version that can be used for detecting modifications on the clip by external systems. Whenever the clip
  103. * is modified the version is increased by one.
  104. */
  105. UINT64 getVersion() const { return mVersion; }
  106. /**
  107. * Creates an animation clip with no curves. After creation make sure to register some animation curves before
  108. * using it.
  109. */
  110. static HAnimationClip create(bool isAdditive = false);
  111. /**
  112. * Creates an animation clip with specified curves.
  113. *
  114. * @param[in] curves Curves to initialize the animation with.
  115. * @param[in] isAdditive Determines does the clip contain additive curve data. This will change the behaviour
  116. * how is the clip blended with other animations.
  117. */
  118. static HAnimationClip create(const SPtr<AnimationCurves>& curves, bool isAdditive = false);
  119. public: // ***** INTERNAL ******
  120. /** @name Internal
  121. * @{
  122. */
  123. /** Creates a new AnimationClip without initializing it. Use create() for normal use. */
  124. static SPtr<AnimationClip> _createPtr(const SPtr<AnimationCurves>& curves, bool isAdditive = false);
  125. /** @} */
  126. protected:
  127. AnimationClip();
  128. AnimationClip(const SPtr<AnimationCurves>& curves, bool isAdditive);
  129. /** @copydoc Resource::initialize() */
  130. void initialize() override;
  131. /** Creates a name -> curve index mapping for quicker curve lookup by name. */
  132. void buildNameMapping();
  133. UINT64 mVersion;
  134. /**
  135. * Contains all the animation curves in the clip. It's important this field is immutable so it may be used on other
  136. * threads. This means any modifications to the field will require a brand new data structure to be generated and
  137. * all existing data copied (plus the modification).
  138. */
  139. SPtr<AnimationCurves> mCurves;
  140. /**
  141. * Contains a map from curve name to curve index. Indices are stored as specified in CurveType enum.
  142. */
  143. UnorderedMap<String, UINT32[4]> mNameMapping;
  144. bool mIsAdditive;
  145. /************************************************************************/
  146. /* SERIALIZATION */
  147. /************************************************************************/
  148. public:
  149. friend class AnimationClipRTTI;
  150. static RTTITypeBase* getRTTIStatic();
  151. RTTITypeBase* getRTTI() const override;
  152. /**
  153. * Creates an AnimationClip with no data. You must populate its data manually followed by a call to initialize().
  154. *
  155. * @note For serialization use only.
  156. */
  157. static SPtr<AnimationClip> createEmpty();
  158. };
  159. /** @} */
  160. }