Animation.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.InteropServices;
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup Animation
  8. * @{
  9. */
  10. /// <summary>
  11. /// Determines how an animation clip behaves when it reaches the end.
  12. /// </summary>
  13. public enum AnimWrapMode // Note: Must match C++ enum AnimWrapMode
  14. {
  15. /// <summary>
  16. /// Loop around to the beginning/end when the last/first frame is reached.
  17. /// </summary>
  18. Loop,
  19. /// <summary>
  20. /// Clamp to end/beginning, keeping the last/first frame active.
  21. /// </summary>
  22. Clamp
  23. }
  24. /// <summary>
  25. /// Defines a single animation clip in <see cref="BlendSequentialInfo"/>.
  26. /// </summary>
  27. public class BlendSequentialClipInfo
  28. {
  29. public AnimationClip clip;
  30. public float fadeTime = 0.0f;
  31. public float startTime = 0.0f;
  32. public float endTime = 0.0f;
  33. }
  34. /// <summary>
  35. /// Defines a sequential blend where one animation clip is played after another, with an optional fade between them.
  36. /// </summary>
  37. public class BlendSequentialInfo
  38. {
  39. public BlendSequentialClipInfo[] clips;
  40. }
  41. /// <summary>
  42. /// Defines a 1D blend where two animation clips are blended between each other using linear interpolation.
  43. /// </summary>
  44. public class Blend1DInfo
  45. {
  46. public AnimationClip leftClip;
  47. public AnimationClip rightClip;
  48. }
  49. /// <summary>
  50. /// Defines a 2D blend where two animation clips are blended between each other using bilinear interpolation.
  51. /// </summary>
  52. public class Blend2DInfo
  53. {
  54. public AnimationClip topLeftClip;
  55. public AnimationClip topRightClip;
  56. public AnimationClip botLeftClip;
  57. public AnimationClip botRightClip;
  58. }
  59. /// <summary>
  60. /// Contains information about a currently playing animation clip.
  61. /// </summary>
  62. [StructLayout(LayoutKind.Sequential), SerializeObject]
  63. public struct AnimationClipState // Note: Must match C++ struct AnimationClipState
  64. {
  65. /// <summary>
  66. /// Layer the clip is playing on. Multiple clips can be played simulatenously on different layers.
  67. /// </summary>
  68. public int layer;
  69. /// <summary>
  70. /// Current time the animation is playing from.
  71. /// </summary>
  72. public float time;
  73. /// <summary>
  74. /// Speed at which the animation is playing.
  75. /// </summary>
  76. public float speed;
  77. /// <summary>
  78. /// Determines how much of an influence does the clip have on the final pose.
  79. /// </summary>
  80. public float weight;
  81. /// <summary>
  82. /// Determines what happens to other animation clips when a new clip starts playing.
  83. /// </summary>
  84. public AnimWrapMode wrapMode;
  85. /// <summary>
  86. /// Initializes the state with default values.
  87. /// </summary>
  88. public void InitDefault()
  89. {
  90. speed = 1.0f;
  91. weight = 1.0f;
  92. wrapMode = AnimWrapMode.Loop;
  93. }
  94. }
  95. /// <summary>
  96. /// Handles animation playback. Takes one or multiple animation clips as input and evaluates them every animation update
  97. /// tick depending on set properties.The evaluated data is used by the core thread for skeletal animation, by the sim
  98. /// thread for updating attached scene objects and bones (if skeleton is attached), or the data is made available for
  99. /// manual queries in the case of generic animation.
  100. /// </summary>
  101. public class Animation : Component
  102. {
  103. private NativeAnimation _native;
  104. [SerializeField]
  105. private SerializableData serializableData = new SerializableData();
  106. /// <summary>
  107. /// Returns the non-component version of Animation that is wrapped by this component.
  108. /// </summary>
  109. internal NativeAnimation Native
  110. {
  111. get { return _native; }
  112. }
  113. /// <summary>
  114. /// Determines the default clip to play as soon as the component is enabled. If more control over playing clips is
  115. /// needed use the <see cref="Play"/>, <see cref="Blend"/> and <see cref="CrossFade"/> methods to queue clips for
  116. /// playback manually, and <see cref="SetState"/> method for modify their states individually.
  117. /// </summary>
  118. public AnimationClip DefaultClip
  119. {
  120. get { return serializableData.defaultClip; }
  121. set
  122. {
  123. serializableData.defaultClip = value;
  124. if (value != null && _native != null)
  125. _native.Play(value);
  126. }
  127. }
  128. /// <summary>
  129. /// Determines the wrap mode for all active animations. Wrap mode determines what happens when animation reaches the
  130. /// first or last frame.
  131. /// <see cref="AnimWrapMode"/>
  132. /// </summary>
  133. public AnimWrapMode WrapMode
  134. {
  135. get { return serializableData.wrapMode; }
  136. set
  137. {
  138. serializableData.wrapMode = value;
  139. if (_native != null)
  140. _native.WrapMode = value;
  141. }
  142. }
  143. /// <summary>
  144. /// Determines the speed for all animations. The default value is 1.0f. Use negative values to play-back in reverse.
  145. /// </summary>
  146. public float Speed
  147. {
  148. get { return serializableData.speed; }
  149. set
  150. {
  151. serializableData.speed = value;
  152. if (_native != null)
  153. _native.Speed = value;
  154. }
  155. }
  156. /// <summary>
  157. /// Checks if any animation clips are currently playing.
  158. /// </summary>
  159. public bool IsPlaying
  160. {
  161. get
  162. {
  163. if (_native != null)
  164. return _native.IsPlaying();
  165. return false;
  166. }
  167. }
  168. /// <summary>
  169. /// Plays the specified animation clip.
  170. /// </summary>
  171. /// <param name="clip">Clip to play.</param>
  172. public void Play(AnimationClip clip)
  173. {
  174. if(_native != null)
  175. _native.Play(clip);
  176. }
  177. /// <summary>
  178. /// Plays the specified animation clip on top of the animation currently playing in the main layer. Multiple such
  179. /// clips can be playing at once, as long as you ensure each is given its own layer. Each animation can also have a
  180. /// weight that determines how much it influences the main animation.
  181. /// </summary>
  182. /// <param name="clip">Clip to additively blend. Must contain additive animation curves.</param>
  183. /// <param name="weight">Determines how much of an effect will the blended animation have on the final output.
  184. /// In range [0, 1].</param>
  185. /// <param name="fadeLength">Applies the blend over a specified time period, increasing the weight as the time
  186. /// passes. Set to zero to blend immediately. In seconds.</param>
  187. /// <param name="layer">Layer to play the clip in. Multiple additive clips can be playing at once in separate layers
  188. /// and each layer has its own weight.</param>
  189. public void BlendAdditive(AnimationClip clip, float weight, float fadeLength, int layer)
  190. {
  191. if (_native != null)
  192. _native.BlendAdditive(clip, weight, fadeLength, layer);
  193. }
  194. /// <summary>
  195. /// Plays a set of animation clips sequentially one after another, with an optional fade between them.
  196. /// </summary>
  197. /// <param name="info">Describes all animation clips to play.</param>
  198. public void BlendSequential(BlendSequentialInfo info)
  199. {
  200. if (_native != null)
  201. _native.BlendSequential(info);
  202. }
  203. /// <summary>
  204. /// Blend two animation clips between each other using linear interpolation. Unlike normal animations these
  205. /// animations are not advanced with the progress of time, and is instead expected the user manually changes the
  206. /// <see cref="t"/> parameter.
  207. /// </summary>
  208. /// <param name="info">Information about the clips to blend.</param>
  209. /// <param name="t">Parameter that controls the blending, in range [0, 1]. t = 0 means left animation has full
  210. /// influence, t = 1 means right animation has full influence.</param>
  211. public void Blend1D(Blend1DInfo info, float t)
  212. {
  213. if (_native != null)
  214. _native.Blend1D(info, t);
  215. }
  216. /// <summary>
  217. /// Blend four animation clips between each other using bilinear interpolation. Unlike normal animations these
  218. /// animations are not advanced with the progress of time, and is instead expected the user manually changes the
  219. /// <see cref="t"/> parameter.
  220. /// </summary>
  221. /// <param name="info">Information about the clips to blend.</param>
  222. /// <param name="t">Parameter that controls the blending, in range [(0, 0), (1, 1)]. t = (0, 0) means top left
  223. /// animation has full influence, t = (0, 1) means top right animation has full influence,
  224. /// t = (1, 0) means bottom left animation has full influence, t = (1, 1) means bottom right
  225. /// animation has full influence.
  226. /// </param>
  227. public void Blend2D(Blend2DInfo info, Vector2 t)
  228. {
  229. if (_native != null)
  230. _native.Blend2D(info, t);
  231. }
  232. /// <summary>
  233. /// Fades the specified animation clip in, while fading other playing animation out, over the specified time period.
  234. /// </summary>
  235. /// <param name="clip">Clip to fade in.</param>
  236. /// <param name="fadeLength">Determines the time period over which the fade occurs. In seconds.</param>
  237. public void CrossFade(AnimationClip clip, float fadeLength)
  238. {
  239. if (_native != null)
  240. _native.CrossFade(clip, fadeLength);
  241. }
  242. /// <summary>
  243. /// Stops playing all animations on the provided layer.
  244. /// </summary>
  245. /// <param name="layer">Layer on which to stop animations on.</param>
  246. public void Stop(int layer)
  247. {
  248. if (_native != null)
  249. _native.Stop(layer);
  250. }
  251. /// <summary>
  252. /// Stops playing all animations.
  253. /// </summary>
  254. public void StopAll()
  255. {
  256. if (_native != null)
  257. _native.StopAll();
  258. }
  259. /// <summary>
  260. /// Retrieves detailed information about a currently playing animation clip.
  261. /// </summary>
  262. /// <param name="clip">Clip to retrieve the information for.</param>
  263. /// <param name="state">Animation clip state containing the requested information. Only valid if the method returns
  264. /// true.</param>
  265. /// <returns>True if the state was found (animation clip is playing), false otherwise.</returns>
  266. public bool GetState(AnimationClip clip, out AnimationClipState state)
  267. {
  268. if (_native != null)
  269. return _native.GetState(clip, out state);
  270. state = new AnimationClipState();
  271. return false;
  272. }
  273. /// <summary>
  274. /// Changes the state of a playing animation clip. If animation clip is not currently playing the state change is
  275. /// ignored.
  276. /// </summary>
  277. /// <param name="clip">Clip to change the state for.</param>
  278. /// <param name="state">New state of the animation (e.g. changing the time for seeking).</param>
  279. public void SetState(AnimationClip clip, AnimationClipState state)
  280. {
  281. if (_native != null)
  282. _native.SetState(clip, state);
  283. }
  284. private void OnEnabled()
  285. {
  286. RestoreNative();
  287. }
  288. private void OnDisabled()
  289. {
  290. DestroyNative();
  291. }
  292. private void OnDestroy()
  293. {
  294. DestroyNative();
  295. }
  296. /// <summary>
  297. /// Creates the internal representation of the animation and restores the values saved by the component.
  298. /// </summary>
  299. private void RestoreNative()
  300. {
  301. if (_native != null)
  302. _native.Destroy();
  303. _native = new NativeAnimation();
  304. // Restore saved values after reset
  305. _native.WrapMode = serializableData.wrapMode;
  306. _native.Speed = serializableData.speed;
  307. if (serializableData.defaultClip != null)
  308. _native.Play(serializableData.defaultClip);
  309. Renderable renderable = SceneObject.GetComponent<Renderable>();
  310. if (renderable == null)
  311. return;
  312. NativeRenderable nativeRenderable = renderable.Native;
  313. if (nativeRenderable != null)
  314. nativeRenderable.Animation = _native;
  315. }
  316. /// <summary>
  317. /// Destroys the internal animation representation.
  318. /// </summary>
  319. private void DestroyNative()
  320. {
  321. Renderable renderableComponent = SceneObject.GetComponent<Renderable>();
  322. if (renderableComponent != null)
  323. {
  324. NativeRenderable renderable = renderableComponent.Native;
  325. if (renderable != null)
  326. renderable.Animation = null;
  327. }
  328. if (_native != null)
  329. {
  330. _native.Destroy();
  331. _native = null;
  332. }
  333. }
  334. /// <summary>
  335. /// Holds all data the animation component needs to persist through serialization.
  336. /// </summary>
  337. [SerializeObject]
  338. private class SerializableData
  339. {
  340. public AnimationClip defaultClip;
  341. public AnimWrapMode wrapMode = AnimWrapMode.Loop;
  342. public float speed = 1.0f;
  343. }
  344. }
  345. /** @} */
  346. }