Animation.cs 13 KB

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