BsAnimation.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 "BsCoreObject.h"
  6. #include "BsFlags.h"
  7. #include "BsSkeleton.h"
  8. #include "BsVector2.h"
  9. namespace BansheeEngine
  10. {
  11. /** @addtogroup Animation-Internal
  12. * @{
  13. */
  14. /** Determines how an animation clip behaves when it reaches the end. */
  15. enum class AnimWrapMode
  16. {
  17. Loop, /**< Loop around to the beginning/end when the last/first frame is reached. */
  18. Clamp /**< Clamp to end/beginning, keeping the last/first frame active. */
  19. };
  20. /** Flags that determine which portion of Animation was changed and needs to be updated. */
  21. enum class AnimDirtyStateFlag
  22. {
  23. Clean = 0,
  24. Value = 1 << 0,
  25. Layout = 1 << 1,
  26. Skeleton = 1 << 2
  27. };
  28. typedef Flags<AnimDirtyStateFlag> AnimDirtyState;
  29. BS_FLAGS_OPERATORS(AnimDirtyStateFlag)
  30. /** Contains information about a currently playing animation clip. */
  31. struct AnimationClipState
  32. {
  33. AnimationClipState() { }
  34. /** Layer the clip is playing on. Multiple clips can be played simulatenously on different layers. */
  35. UINT32 layer = 0;
  36. float time = 0.0f; /**< Current time the animation is playing from. */
  37. float speed = 1.0f; /**< Speed at which the animation is playing. */
  38. float weight = 1.0f; /**< Determines how much of an influence does the clip have on the final pose. */
  39. /** Determines what happens to other animation clips when a new clip starts playing. */
  40. AnimWrapMode wrapMode = AnimWrapMode::Loop;
  41. };
  42. /** Internal information about a single playing animation clip within Animation. */
  43. struct AnimationClipInfo
  44. {
  45. AnimationClipInfo();
  46. AnimationClipInfo(const HAnimationClip& clip);
  47. HAnimationClip clip;
  48. AnimationClipState state;
  49. float fadeDirection;
  50. float fadeTime;
  51. float fadeLength;
  52. /**
  53. * Version of the animation curves used by the AnimationProxy. Used to detecting the internal animation curves
  54. * changed.
  55. */
  56. UINT64 curveVersion;
  57. UINT32 layerIdx; /**< Layer index this clip belongs to in AnimationProxy structure. */
  58. UINT32 stateIdx; /**< State index this clip belongs to in AnimationProxy structure. */
  59. };
  60. /** Represents an animation clip used in 1D blending. Each clip has a position on the number line. */
  61. struct BS_CORE_EXPORT BlendClipInfo
  62. {
  63. BlendClipInfo() { }
  64. HAnimationClip clip;
  65. float position = 0.0f;
  66. };
  67. /** Defines a 1D blend where multiple animation clips are blended between each other using linear interpolation. */
  68. struct BS_CORE_EXPORT Blend1DInfo
  69. {
  70. Blend1DInfo(UINT32 numClips);
  71. ~Blend1DInfo();
  72. UINT32 numClips;
  73. BlendClipInfo* clips;
  74. };
  75. /** Defines a 2D blend where two animation clips are blended between each other using bilinear interpolation. */
  76. struct Blend2DInfo
  77. {
  78. HAnimationClip topLeftClip;
  79. HAnimationClip topRightClip;
  80. HAnimationClip botLeftClip;
  81. HAnimationClip botRightClip;
  82. };
  83. /** Represents a copy of the Animation data for use specifically on the animation thread. */
  84. struct AnimationProxy
  85. {
  86. AnimationProxy(UINT64 id);
  87. ~AnimationProxy();
  88. /**
  89. * Rebuilds the internal proxy data according to the newly assigned skeleton and clips. This should be called
  90. * whenever the animation skeleton changes.
  91. *
  92. * @param[in] skeleton New skeleton to assign to the proxy.
  93. * @param[in, out] clipInfos Potentially new clip infos that will be used for rebuilding the proxy. Once the
  94. * method completes clip info layout and state indices will be populated for
  95. * further use in the update*() methods.
  96. *
  97. * @note Should be called from the sim thread when the caller is sure the animation thread is not using it.
  98. */
  99. void rebuild(const SPtr<Skeleton>& skeleton, Vector<AnimationClipInfo>& clipInfos);
  100. /**
  101. * Rebuilds the internal proxy data according to the newly clips. This should be called whenever clips are added
  102. * or removed, or clip layout indices change.
  103. *
  104. * @param[in, out] clipInfos New clip infos that will be used for rebuilding the proxy. Once the method completes
  105. * clip info layout and state indices will be populated for further use in the
  106. * update*() methods.
  107. *
  108. * @note Should be called from the sim thread when the caller is sure the animation thread is not using it.
  109. */
  110. void rebuild(Vector<AnimationClipInfo>& clipInfos);
  111. /**
  112. * Updates the proxy data with new information about the clips. Caller must guarantee that clip layout didn't
  113. * change since the last call to rebuild().
  114. *
  115. * @note Should be called from the sim thread when the caller is sure the animation thread is not using it.
  116. */
  117. void updateValues(const Vector<AnimationClipInfo>& clipInfos);
  118. /**
  119. * Updates the proxy data with new clip times. Caller must guarantee that clip layout didn't change since the last
  120. * call to rebuild().
  121. *
  122. * @note Should be called from the sim thread when the caller is sure the animation thread is not using it.
  123. */
  124. void updateTime(const Vector<AnimationClipInfo>& clipInfos);
  125. UINT64 id;
  126. AnimationStateLayer* layers;
  127. UINT32 numLayers;
  128. SPtr<Skeleton> skeleton;
  129. // Evaluation results
  130. LocalSkeletonPose localPose;
  131. float* genericCurveOutputs;
  132. };
  133. /**
  134. * Handles animation playback. Takes one or multiple animation clips as input and evaluates them every animation update
  135. * tick depending on set properties. The evaluated data is used by the core thread for skeletal animation, by the sim
  136. * thread for updating attached scene objects and bones (if skeleton is attached), or the data is made available for
  137. * manual queries in the case of generic animation.
  138. */
  139. class BS_CORE_EXPORT Animation : public CoreObject
  140. {
  141. public:
  142. ~Animation();
  143. /**
  144. * Changes the skeleton which will the translation/rotation/scale animation values manipulate. If no skeleton is set
  145. * the animation will only evaluate the generic curves, and the root translation/rotation/scale curves.
  146. */
  147. void setSkeleton(const SPtr<Skeleton>& skeleton);
  148. /**
  149. * Changes the wrap mode for all active animations. Wrap mode determines what happens when animation reaches the
  150. * first or last frame.
  151. *
  152. * @see AnimWrapMode
  153. */
  154. void setWrapMode(AnimWrapMode wrapMode);
  155. /** Changes the speed for all animations. The default value is 1.0f. Use negative values to play-back in reverse. */
  156. void setSpeed(float speed);
  157. /**
  158. * Plays the specified animation clip.
  159. *
  160. * @param[in] clip Clip to play.
  161. */
  162. void play(const HAnimationClip& clip);
  163. /**
  164. * Plays the specified animation clip on top of the animation currently playing in the main layer. Multiple
  165. * such clips can be playing at once, as long as you ensure each is given its own layer. Each animation can
  166. * also have a weight that determines how much it influences the main animation.
  167. *
  168. * @param[in] clip Clip to additively blend. Must contain additive animation curves.
  169. * @param[in] weight Determines how much of an effect will the blended animation have on the final output.
  170. * In range [0, 1].
  171. * @param[in] fadeLength Applies the blend over a specified time period, increasing the weight as the time
  172. * passes. Set to zero to blend immediately. In seconds.
  173. * @param[in] layer Layer to play the clip in. Multiple additive clips can be playing at once in separate
  174. * layers and each layer has its own weight.
  175. */
  176. void blendAdditive(const HAnimationClip& clip, float weight, float fadeLength = 0.0f, UINT32 layer = 0);
  177. /**
  178. * Blend multiple animation clips between each other using linear interpolation. Unlike normal animations these
  179. * animations are not advanced with the progress of time, and is instead expected the user manually changes the
  180. * @p t parameter.
  181. *
  182. * @param[in] info Information about the clips to blend. Clip positions must be sorted from lowest to highest.
  183. * @param[in] t Parameter that controls the blending. Range depends on the positions of the provided
  184. * animation clips.
  185. */
  186. void blend1D(const Blend1DInfo& info, float t);
  187. /**
  188. * Blend four animation clips between each other using bilinear interpolation. Unlike normal animations these
  189. * animations are not advanced with the progress of time, and is instead expected the user manually changes the
  190. * @p t parameter.
  191. *
  192. * @param[in] info Information about the clips to blend.
  193. * @param[in] t Parameter that controls the blending, in range [(0, 0), (1, 1)]. t = (0, 0) means top left
  194. * animation has full influence, t = (0, 1) means top right animation has full influence,
  195. * t = (1, 0) means bottom left animation has full influence, t = (1, 1) means bottom right
  196. * animation has full influence.
  197. */
  198. void blend2D(const Blend2DInfo& info, const Vector2& t);
  199. /**
  200. * Fades the specified animation clip in, while fading other playing animation out, over the specified time
  201. * period.
  202. *
  203. * @param[in] clip Clip to fade in.
  204. * @param[in] fadeLength Determines the time period over which the fade occurs. In seconds.
  205. */
  206. void crossFade(const HAnimationClip& clip, float fadeLength);
  207. /**
  208. * Stops playing all animations on the provided layer. Specify -1 to stop animation on the main layer
  209. * (non-additive animations).
  210. */
  211. void stop(UINT32 layer);
  212. /** Stops playing all animations. */
  213. void stopAll();
  214. /** Checks if any animation clips are currently playing. */
  215. bool isPlaying() const;
  216. /**
  217. * Retrieves detailed information about a currently playing animation clip.
  218. *
  219. * @param[in] clip Clip to retrieve the information for.
  220. * @param[out] state Animation clip state containing the requested information. Only valid if the method returns
  221. * true.
  222. * @return True if the state was found (animation clip is playing), false otherwise.
  223. */
  224. bool getState(const HAnimationClip& clip, AnimationClipState& state);
  225. /**
  226. * Changes the state of a playing animation clip. If animation clip is not currently playing the state change is
  227. * ignored.
  228. *
  229. * @param[in] clip Clip to change the state for.
  230. * @param[in] state New state of the animation (e.g. changing the time for seeking).
  231. */
  232. void setState(const HAnimationClip& clip, AnimationClipState state);
  233. /** Creates a new empty Animation object. */
  234. static SPtr<Animation> create();
  235. /** @name Internal
  236. * @{
  237. */
  238. /** Returns the unique ID for this animation object. */
  239. UINT64 _getId() const { return mId; }
  240. /** @} */
  241. private:
  242. friend class AnimationManager;
  243. Animation();
  244. /**
  245. * Updates the animation proxy object based on the currently set skeleton, playing clips and dirty flags.
  246. *
  247. * @param[in] timeDelta Seconds passed since the last call to this method.
  248. */
  249. void updateAnimProxy(float timeDelta);
  250. /**
  251. * Registers a new animation in the specified layer, or returns an existing animation clip info if the animation is
  252. * already registered. If @p stopExisting is true any existing animations in the layer will be stopped. Layout
  253. * will be marked as dirty if any changes were made.
  254. */
  255. AnimationClipInfo* addClip(const HAnimationClip& clip, UINT32 layer, bool stopExisting = true);
  256. UINT64 mId;
  257. AnimWrapMode mDefaultWrapMode;
  258. float mDefaultSpeed;
  259. AnimDirtyState mDirty;
  260. SPtr<Skeleton> mSkeleton;
  261. Vector<AnimationClipInfo> mClipInfos;
  262. // Animation thread only
  263. SPtr<AnimationProxy> mAnimProxy;
  264. };
  265. /** @} */
  266. }