AnimationCurve.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.InteropServices;
  6. namespace BansheeEngine
  7. {
  8. /// <summary>
  9. /// Animation keyframe, represented as an endpoint of a cubic hermite spline.
  10. /// </summary>
  11. [StructLayout(LayoutKind.Sequential), SerializeObject]
  12. public struct KeyFrame // Note: Must match C++ struct TKeyFrame<float>
  13. {
  14. /// <summary>
  15. /// Value of the key.
  16. /// </summary>
  17. public float value;
  18. /// <summary>
  19. /// Input tangent (going from the previous key to this one) of the key.
  20. /// </summary>
  21. public float inTangent;
  22. /// <summary>
  23. /// Output tangent (going from this key to next one) of the key.
  24. /// </summary>
  25. public float outTangent;
  26. /// <summary>
  27. /// Position of the key along the animation spline.
  28. /// </summary>
  29. public float time;
  30. }
  31. /// <summary>
  32. /// Flags that describe an <see cref="AnimationCurve"/>
  33. /// </summary>
  34. public enum AnimationCurveFlags // Note: Must match C++ enum AnimationCurveFlags
  35. {
  36. /// <summary>
  37. /// If enabled, the curve was imported from an external file and not created within the engine. This will affect
  38. /// how are animation results applied to scene objects (with imported animations it is assumed the curve is
  39. /// animating bones and with in-engine curves it is assumed the curve is animating scene objects).
  40. /// </summary>
  41. ImportedCurve = 1 << 0,
  42. /// <summary>
  43. /// Signifies the curve is used to animate between different frames within a morph channel. In range [0, 1].
  44. /// </summary>
  45. MorphFrame = 1 << 1,
  46. /// <summary>
  47. /// Signifies the curve is used to adjust the weight of a morph channel. In range [0, 1].
  48. /// </summary>
  49. MorphWeight = 1 << 2
  50. }
  51. /// <summary>
  52. /// Animation spline represented by a set of keyframes, each representing an endpoint of a cubic hermite curve. The
  53. /// spline can be evaluated at any time, and uses caching to speed up multiple sequential evaluations.
  54. /// </summary>
  55. public class AnimationCurve : ScriptObject
  56. {
  57. /// <summary>
  58. /// Constructor for internal runtime use only.
  59. /// </summary>
  60. private AnimationCurve()
  61. { }
  62. /// <summary>
  63. /// Creates a new animation curve.
  64. /// </summary>
  65. /// <param name="keyFrames">Keyframes to initialize the curve with.</param>
  66. public AnimationCurve(KeyFrame[] keyFrames)
  67. {
  68. Internal_Create(this, keyFrames);
  69. }
  70. /// <summary>
  71. /// Keyframes that represent the curve.
  72. /// </summary>
  73. public KeyFrame[] KeyFrames
  74. {
  75. get { return Internal_GetKeyFrames(mCachedPtr); }
  76. set { Internal_SetKeyFrames(mCachedPtr, value); }
  77. }
  78. /// <summary>
  79. /// Evaluate the animation curve at the specified time.
  80. /// </summary>
  81. /// <param name="time">Time to evaluate the curve at. </param>
  82. /// <param name="loop">If true the curve will loop when it goes past the end or beggining. Otherwise the curve
  83. /// value will be clamped.</param>
  84. /// <returns>Interpolated value from the curve at provided time.</returns>
  85. public float Evaluate(float time, bool loop = true)
  86. {
  87. return Internal_Evaluate(mCachedPtr, time, loop);
  88. }
  89. [MethodImpl(MethodImplOptions.InternalCall)]
  90. private static extern void Internal_Create(AnimationCurve instance, KeyFrame[] keyframes);
  91. [MethodImpl(MethodImplOptions.InternalCall)]
  92. private static extern KeyFrame[] Internal_GetKeyFrames(IntPtr thisPtr);
  93. [MethodImpl(MethodImplOptions.InternalCall)]
  94. private static extern void Internal_SetKeyFrames(IntPtr thisPtr, KeyFrame[] keyframes);
  95. [MethodImpl(MethodImplOptions.InternalCall)]
  96. private static extern float Internal_Evaluate(IntPtr thisPtr, float time, bool loop);
  97. }
  98. /// <summary>
  99. /// A set of animation curves for a 3D vector paired with a name.
  100. /// </summary>
  101. public class NamedVector3Curve
  102. {
  103. /// <summary>
  104. /// Constructor for internal runtime use only.
  105. /// </summary>
  106. /// <param name="name">Name of the curve.</param>
  107. /// <param name="flags">Flags that describe the animation curve, of type <see cref="AnimationCurveFlags"/>.</param>
  108. /// <param name="x">Curve representing the x axis of the vector.</param>
  109. /// <param name="y">Curve representing the y axis of the vector.</param>
  110. /// <param name="z">Curve representing the z axis of the vector.</param>
  111. private NamedVector3Curve(string name, int flags, AnimationCurve x, AnimationCurve y, AnimationCurve z)
  112. {
  113. Name = name;
  114. Flags = (AnimationCurveFlags) flags;
  115. X = x;
  116. Y = y;
  117. Z = z;
  118. }
  119. /// <summary>
  120. /// Constructs a new named animation curve.
  121. /// </summary>
  122. /// <param name="name">Name of the curve.</param>
  123. /// <param name="x">Curve representing the x axis of the vector.</param>
  124. /// <param name="y">Curve representing the y axis of the vector.</param>
  125. /// <param name="z">Curve representing the z axis of the vector.</param>
  126. public NamedVector3Curve(string name, AnimationCurve x, AnimationCurve y, AnimationCurve z)
  127. {
  128. Name = name;
  129. X = x;
  130. Y = y;
  131. Z = z;
  132. }
  133. /// <summary>
  134. /// Name of the curve.
  135. /// </summary>
  136. public string Name;
  137. /// <summary>
  138. /// Flags that describe the animation curve.
  139. /// </summary>
  140. public AnimationCurveFlags Flags;
  141. /// <summary>
  142. /// Animation curve for the x axis.
  143. /// </summary>
  144. public AnimationCurve X;
  145. /// <summary>
  146. /// Animation curve for the y axis.
  147. /// </summary>
  148. public AnimationCurve Y;
  149. /// <summary>
  150. /// Animation curve for the z axis.
  151. /// </summary>
  152. public AnimationCurve Z;
  153. }
  154. /// <summary>
  155. /// An animation curve for a single floating point value paired with a name.
  156. /// </summary>
  157. public class NamedFloatCurve
  158. {
  159. /// <summary>
  160. /// Constructor for internal runtime use only.
  161. /// </summary>
  162. /// <param name="name">Name of the curve.</param>
  163. /// <param name="flags">Flags that describe the animation curve, of type <see cref="AnimationCurveFlags"/>.</param>
  164. /// <param name="curve">Curve representing the floating point values.</param>
  165. private NamedFloatCurve(string name, int flags, AnimationCurve curve)
  166. {
  167. Name = name;
  168. Flags = (AnimationCurveFlags)flags;
  169. Curve = curve;
  170. }
  171. /// <summary>
  172. /// Constructs a new named animation curve.
  173. /// </summary>
  174. /// <param name="name">Name of the curve.</param>
  175. /// <param name="curve">Curve representing the floating point values.</param>
  176. public NamedFloatCurve(string name, AnimationCurve curve)
  177. {
  178. Name = name;
  179. Curve = curve;
  180. }
  181. /// <summary>
  182. /// Name of the curve.
  183. /// </summary>
  184. public string Name;
  185. /// <summary>
  186. /// Flags that describe the animation curve.
  187. /// </summary>
  188. public AnimationCurveFlags Flags;
  189. /// <summary>
  190. /// Animation curve.
  191. /// </summary>
  192. public AnimationCurve Curve;
  193. }
  194. }