Animation.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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="Blend1D"/>, <see cref="Blend2D"/> and <see cref="CrossFade"/>
  106. /// methods to queue clips for playback manually, and <see cref="SetState"/> method for modify their states
  107. /// individually.
  108. /// </summary>
  109. public AnimationClip DefaultClip
  110. {
  111. get { return serializableData.defaultClip; }
  112. set
  113. {
  114. serializableData.defaultClip = value;
  115. if (value != null && _native != null)
  116. _native.Play(value);
  117. }
  118. }
  119. /// <summary>
  120. /// Determines the wrap mode for all active animations. Wrap mode determines what happens when animation reaches the
  121. /// first or last frame.
  122. /// <see cref="AnimWrapMode"/>
  123. /// </summary>
  124. public AnimWrapMode WrapMode
  125. {
  126. get { return serializableData.wrapMode; }
  127. set
  128. {
  129. serializableData.wrapMode = value;
  130. if (_native != null)
  131. _native.WrapMode = value;
  132. }
  133. }
  134. /// <summary>
  135. /// Determines the speed for all animations. The default value is 1.0f. Use negative values to play-back in reverse.
  136. /// </summary>
  137. public float Speed
  138. {
  139. get { return serializableData.speed; }
  140. set
  141. {
  142. serializableData.speed = value;
  143. if (_native != null)
  144. _native.Speed = value;
  145. }
  146. }
  147. /// <summary>
  148. /// Checks if any animation clips are currently playing.
  149. /// </summary>
  150. public bool IsPlaying
  151. {
  152. get
  153. {
  154. if (_native != null)
  155. return _native.IsPlaying();
  156. return false;
  157. }
  158. }
  159. /// <summary>
  160. /// Plays the specified animation clip.
  161. /// </summary>
  162. /// <param name="clip">Clip to play.</param>
  163. public void Play(AnimationClip clip)
  164. {
  165. if(_native != null)
  166. _native.Play(clip);
  167. }
  168. /// <summary>
  169. /// Plays the specified animation clip on top of the animation currently playing in the main layer. Multiple such
  170. /// clips can be playing at once, as long as you ensure each is given its own layer. Each animation can also have a
  171. /// weight that determines how much it influences the main animation.
  172. /// </summary>
  173. /// <param name="clip">Clip to additively blend. Must contain additive animation curves.</param>
  174. /// <param name="weight">Determines how much of an effect will the blended animation have on the final output.
  175. /// In range [0, 1].</param>
  176. /// <param name="fadeLength">Applies the blend over a specified time period, increasing the weight as the time
  177. /// passes. Set to zero to blend immediately. In seconds.</param>
  178. /// <param name="layer">Layer to play the clip in. Multiple additive clips can be playing at once in separate layers
  179. /// and each layer has its own weight.</param>
  180. public void BlendAdditive(AnimationClip clip, float weight, float fadeLength, int layer)
  181. {
  182. if (_native != null)
  183. _native.BlendAdditive(clip, weight, fadeLength, layer);
  184. }
  185. /// <summary>
  186. /// Blends multiple animation clips between each other using linear interpolation. Unlike normal animations these
  187. /// animations are not advanced with the progress of time, and is instead expected the user manually changes the
  188. /// <see cref="t"/> parameter.
  189. /// </summary>
  190. /// <param name="info">Information about the clips to blend. Clip positions must be sorted from lowest to highest.
  191. /// </param>
  192. /// <param name="t">Parameter that controls the blending, in range [0, 1]. t = 0 means left animation has full
  193. /// influence, t = 1 means right animation has full influence.</param>
  194. public void Blend1D(Blend1DInfo info, float t)
  195. {
  196. if (_native != null)
  197. _native.Blend1D(info, t);
  198. }
  199. /// <summary>
  200. /// Blend four animation clips between each other using bilinear interpolation. Unlike normal animations these
  201. /// animations are not advanced with the progress of time, and is instead expected the user manually changes the
  202. /// <see cref="t"/> parameter.
  203. /// </summary>
  204. /// <param name="info">Information about the clips to blend.</param>
  205. /// <param name="t">Parameter that controls the blending, in range [(0, 0), (1, 1)]. t = (0, 0) means top left
  206. /// animation has full influence, t = (0, 1) means top right animation has full influence,
  207. /// t = (1, 0) means bottom left animation has full influence, t = (1, 1) means bottom right
  208. /// animation has full influence.
  209. /// </param>
  210. public void Blend2D(Blend2DInfo info, Vector2 t)
  211. {
  212. if (_native != null)
  213. _native.Blend2D(info, t);
  214. }
  215. /// <summary>
  216. /// Fades the specified animation clip in, while fading other playing animation out, over the specified time period.
  217. /// </summary>
  218. /// <param name="clip">Clip to fade in.</param>
  219. /// <param name="fadeLength">Determines the time period over which the fade occurs. In seconds.</param>
  220. public void CrossFade(AnimationClip clip, float fadeLength)
  221. {
  222. if (_native != null)
  223. _native.CrossFade(clip, fadeLength);
  224. }
  225. /// <summary>
  226. /// Stops playing all animations on the provided layer.
  227. /// </summary>
  228. /// <param name="layer">Layer on which to stop animations on.</param>
  229. public void Stop(int layer)
  230. {
  231. if (_native != null)
  232. _native.Stop(layer);
  233. }
  234. /// <summary>
  235. /// Stops playing all animations.
  236. /// </summary>
  237. public void StopAll()
  238. {
  239. if (_native != null)
  240. _native.StopAll();
  241. }
  242. /// <summary>
  243. /// Retrieves detailed information about a currently playing animation clip.
  244. /// </summary>
  245. /// <param name="clip">Clip to retrieve the information for.</param>
  246. /// <param name="state">Animation clip state containing the requested information. Only valid if the method returns
  247. /// true.</param>
  248. /// <returns>True if the state was found (animation clip is playing), false otherwise.</returns>
  249. public bool GetState(AnimationClip clip, out AnimationClipState state)
  250. {
  251. if (_native != null)
  252. return _native.GetState(clip, out state);
  253. state = new AnimationClipState();
  254. return false;
  255. }
  256. /// <summary>
  257. /// Changes the state of a playing animation clip. If animation clip is not currently playing the state change is
  258. /// ignored.
  259. /// </summary>
  260. /// <param name="clip">Clip to change the state for.</param>
  261. /// <param name="state">New state of the animation (e.g. changing the time for seeking).</param>
  262. public void SetState(AnimationClip clip, AnimationClipState state)
  263. {
  264. if (_native != null)
  265. _native.SetState(clip, state);
  266. }
  267. private void OnEnable()
  268. {
  269. RestoreNative();
  270. }
  271. private void OnDisable()
  272. {
  273. DestroyNative();
  274. }
  275. private void OnDestroy()
  276. {
  277. DestroyNative();
  278. }
  279. /// <summary>
  280. /// Creates the internal representation of the animation and restores the values saved by the component.
  281. /// </summary>
  282. private void RestoreNative()
  283. {
  284. if (_native != null)
  285. _native.Destroy();
  286. _native = new NativeAnimation();
  287. _native.OnEventTriggered += EventTriggered;
  288. // Restore saved values after reset
  289. _native.WrapMode = serializableData.wrapMode;
  290. _native.Speed = serializableData.speed;
  291. if (serializableData.defaultClip != null)
  292. _native.Play(serializableData.defaultClip);
  293. Renderable renderable = SceneObject.GetComponent<Renderable>();
  294. if (renderable == null)
  295. return;
  296. NativeRenderable nativeRenderable = renderable.Native;
  297. if (nativeRenderable != null)
  298. nativeRenderable.Animation = _native;
  299. }
  300. /// <summary>
  301. /// Destroys the internal animation representation.
  302. /// </summary>
  303. private void DestroyNative()
  304. {
  305. Renderable renderableComponent = SceneObject.GetComponent<Renderable>();
  306. if (renderableComponent != null)
  307. {
  308. NativeRenderable renderable = renderableComponent.Native;
  309. if (renderable != null)
  310. renderable.Animation = null;
  311. }
  312. if (_native != null)
  313. {
  314. _native.Destroy();
  315. _native = null;
  316. }
  317. }
  318. /// <summary>
  319. /// Called whenever an animation event triggers.
  320. /// </summary>
  321. /// <param name="clip">Clip that the event originated from.</param>
  322. /// <param name="name">Name of the event.</param>
  323. private void EventTriggered(AnimationClip clip, string name)
  324. {
  325. // TODO - Find a scene object, component and method based on the event name, and call it
  326. }
  327. /// <summary>
  328. /// Holds all data the animation component needs to persist through serialization.
  329. /// </summary>
  330. [SerializeObject]
  331. private class SerializableData
  332. {
  333. public AnimationClip defaultClip;
  334. public AnimWrapMode wrapMode = AnimWrapMode.Loop;
  335. public float speed = 1.0f;
  336. }
  337. }
  338. /** @} */
  339. }