Animation.cs 12 KB

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