BsAnimation.h 12 KB

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