BsAnimation.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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(const AnimationProxy&) = delete;
  88. ~AnimationProxy();
  89. AnimationProxy& operator=(const AnimationProxy&) = delete;
  90. /**
  91. * Rebuilds the internal proxy data according to the newly assigned skeleton and clips. This should be called
  92. * whenever the animation skeleton changes.
  93. *
  94. * @param[in] skeleton New skeleton to assign to the proxy.
  95. * @param[in, out] clipInfos Potentially new clip infos that will be used for rebuilding the proxy. Once the
  96. * method completes clip info layout and state indices will be populated for
  97. * further use in the update*() methods.
  98. *
  99. * @note Should be called from the sim thread when the caller is sure the animation thread is not using it.
  100. */
  101. void rebuild(const SPtr<Skeleton>& skeleton, Vector<AnimationClipInfo>& clipInfos);
  102. /**
  103. * Rebuilds the internal proxy data according to the newly clips. This should be called whenever clips are added
  104. * or removed, or clip layout indices change.
  105. *
  106. * @param[in, out] clipInfos New clip infos that will be used for rebuilding the proxy. Once the method completes
  107. * clip info layout and state indices will be populated for further use in the
  108. * update*() methods.
  109. *
  110. * @note Should be called from the sim thread when the caller is sure the animation thread is not using it.
  111. */
  112. void rebuild(Vector<AnimationClipInfo>& clipInfos);
  113. /**
  114. * Updates the proxy data with new information about the clips. Caller must guarantee that clip layout didn't
  115. * change since the last call to rebuild().
  116. *
  117. * @note Should be called from the sim thread when the caller is sure the animation thread is not using it.
  118. */
  119. void updateValues(const Vector<AnimationClipInfo>& clipInfos);
  120. /**
  121. * Updates the proxy data with new clip times. Caller must guarantee that clip layout didn't change since the last
  122. * call to rebuild().
  123. *
  124. * @note Should be called from the sim thread when the caller is sure the animation thread is not using it.
  125. */
  126. void updateTime(const Vector<AnimationClipInfo>& clipInfos);
  127. /** Destroys all dynamically allocated objects. */
  128. void clear();
  129. UINT64 id;
  130. AnimationStateLayer* layers;
  131. UINT32 numLayers;
  132. SPtr<Skeleton> skeleton;
  133. // Evaluation results
  134. LocalSkeletonPose localPose;
  135. float* genericCurveOutputs;
  136. };
  137. /**
  138. * Handles animation playback. Takes one or multiple animation clips as input and evaluates them every animation update
  139. * tick depending on set properties. The evaluated data is used by the core thread for skeletal animation, by the sim
  140. * thread for updating attached scene objects and bones (if skeleton is attached), or the data is made available for
  141. * manual queries in the case of generic animation.
  142. */
  143. class BS_CORE_EXPORT Animation : public CoreObject
  144. {
  145. public:
  146. ~Animation();
  147. /**
  148. * Changes the skeleton which will the translation/rotation/scale animation values manipulate. If no skeleton is set
  149. * the animation will only evaluate the generic curves, and the root translation/rotation/scale curves.
  150. */
  151. void setSkeleton(const SPtr<Skeleton>& skeleton);
  152. /**
  153. * Changes the wrap mode for all active animations. Wrap mode determines what happens when animation reaches the
  154. * first or last frame.
  155. *
  156. * @see AnimWrapMode
  157. */
  158. void setWrapMode(AnimWrapMode wrapMode);
  159. /** Changes the speed for all animations. The default value is 1.0f. Use negative values to play-back in reverse. */
  160. void setSpeed(float speed);
  161. /**
  162. * Plays the specified animation clip.
  163. *
  164. * @param[in] clip Clip to play.
  165. */
  166. void play(const HAnimationClip& clip);
  167. /**
  168. * Plays the specified animation clip on top of the animation currently playing in the main layer. Multiple
  169. * such clips can be playing at once, as long as you ensure each is given its own layer. Each animation can
  170. * also have a weight that determines how much it influences the main animation.
  171. *
  172. * @param[in] clip Clip to additively blend. Must contain additive animation curves.
  173. * @param[in] weight Determines how much of an effect will the blended animation have on the final output.
  174. * In range [0, 1].
  175. * @param[in] fadeLength Applies the blend over a specified time period, increasing the weight as the time
  176. * passes. Set to zero to blend immediately. In seconds.
  177. * @param[in] layer Layer to play the clip in. Multiple additive clips can be playing at once in separate
  178. * layers and each layer has its own weight.
  179. */
  180. void blendAdditive(const HAnimationClip& clip, float weight, float fadeLength = 0.0f, UINT32 layer = 0);
  181. /**
  182. * Blend multiple animation clips between each other using linear interpolation. Unlike normal animations these
  183. * animations are not advanced with the progress of time, and is instead expected the user manually changes the
  184. * @p t parameter.
  185. *
  186. * @param[in] info Information about the clips to blend. Clip positions must be sorted from lowest to highest.
  187. * @param[in] t Parameter that controls the blending. Range depends on the positions of the provided
  188. * animation clips.
  189. */
  190. void blend1D(const Blend1DInfo& info, float t);
  191. /**
  192. * Blend four animation clips between each other using bilinear interpolation. Unlike normal animations these
  193. * animations are not advanced with the progress of time, and is instead expected the user manually changes the
  194. * @p t parameter.
  195. *
  196. * @param[in] info Information about the clips to blend.
  197. * @param[in] t Parameter that controls the blending, in range [(0, 0), (1, 1)]. t = (0, 0) means top left
  198. * animation has full influence, t = (0, 1) means top right animation has full influence,
  199. * t = (1, 0) means bottom left animation has full influence, t = (1, 1) means bottom right
  200. * animation has full influence.
  201. */
  202. void blend2D(const Blend2DInfo& info, const Vector2& t);
  203. /**
  204. * Fades the specified animation clip in, while fading other playing animation out, over the specified time
  205. * period.
  206. *
  207. * @param[in] clip Clip to fade in.
  208. * @param[in] fadeLength Determines the time period over which the fade occurs. In seconds.
  209. */
  210. void crossFade(const HAnimationClip& clip, float fadeLength);
  211. /**
  212. * Stops playing all animations on the provided layer. Specify -1 to stop animation on the main layer
  213. * (non-additive animations).
  214. */
  215. void stop(UINT32 layer);
  216. /** Stops playing all animations. */
  217. void stopAll();
  218. /** Checks if any animation clips are currently playing. */
  219. bool isPlaying() const;
  220. /**
  221. * Retrieves detailed information about a currently playing animation clip.
  222. *
  223. * @param[in] clip Clip to retrieve the information for.
  224. * @param[out] state Animation clip state containing the requested information. Only valid if the method returns
  225. * true.
  226. * @return True if the state was found (animation clip is playing), false otherwise.
  227. */
  228. bool getState(const HAnimationClip& clip, AnimationClipState& state);
  229. /**
  230. * Changes the state of a playing animation clip. If animation clip is not currently playing the state change is
  231. * ignored.
  232. *
  233. * @param[in] clip Clip to change the state for.
  234. * @param[in] state New state of the animation (e.g. changing the time for seeking).
  235. */
  236. void setState(const HAnimationClip& clip, AnimationClipState state);
  237. /** Creates a new empty Animation object. */
  238. static SPtr<Animation> create();
  239. /** @name Internal
  240. * @{
  241. */
  242. /** Returns the unique ID for this animation object. */
  243. UINT64 _getId() const { return mId; }
  244. /** @} */
  245. private:
  246. friend class AnimationManager;
  247. Animation();
  248. /**
  249. * Updates the animation proxy object based on the currently set skeleton, playing clips and dirty flags.
  250. *
  251. * @param[in] timeDelta Seconds passed since the last call to this method.
  252. */
  253. void updateAnimProxy(float timeDelta);
  254. /**
  255. * Registers a new animation in the specified layer, or returns an existing animation clip info if the animation is
  256. * already registered. If @p stopExisting is true any existing animations in the layer will be stopped. Layout
  257. * will be marked as dirty if any changes were made.
  258. */
  259. AnimationClipInfo* addClip(const HAnimationClip& clip, UINT32 layer, bool stopExisting = true);
  260. UINT64 mId;
  261. AnimWrapMode mDefaultWrapMode;
  262. float mDefaultSpeed;
  263. AnimDirtyState mDirty;
  264. SPtr<Skeleton> mSkeleton;
  265. Vector<AnimationClipInfo> mClipInfos;
  266. // Animation thread only
  267. SPtr<AnimationProxy> mAnimProxy;
  268. };
  269. /** @} */
  270. }