AnimationCurve.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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
  42. }
  43. /// <summary>
  44. /// Animation spline represented by a set of keyframes, each representing an endpoint of a cubic hermite curve. The
  45. /// spline can be evaluated at any time, and uses caching to speed up multiple sequential evaluations.
  46. /// </summary>
  47. public class AnimationCurve : ScriptObject
  48. {
  49. /// <summary>
  50. /// Constructor for internal runtime use only.
  51. /// </summary>
  52. private AnimationCurve()
  53. { }
  54. /// <summary>
  55. /// Creates a new animation curve.
  56. /// </summary>
  57. /// <param name="keyFrames">Keyframes to initialize the curve with.</param>
  58. public AnimationCurve(KeyFrame[] keyFrames)
  59. {
  60. Internal_Create(this, keyFrames);
  61. }
  62. /// <summary>
  63. /// Keyframes that represent the curve.
  64. /// </summary>
  65. public KeyFrame[] KeyFrames
  66. {
  67. get { return Internal_GetKeyFrames(mCachedPtr); }
  68. set { Internal_SetKeyFrames(mCachedPtr, value); }
  69. }
  70. /// <summary>
  71. /// Evaluate the animation curve at the specified time.
  72. /// </summary>
  73. /// <param name="time">Time to evaluate the curve at. </param>
  74. /// <param name="loop">If true the curve will loop when it goes past the end or beggining. Otherwise the curve
  75. /// value will be clamped.</param>
  76. /// <returns>Interpolated value from the curve at provided time.</returns>
  77. public float Evaluate(float time, bool loop = true)
  78. {
  79. return Internal_Evaluate(mCachedPtr, time, loop);
  80. }
  81. [MethodImpl(MethodImplOptions.InternalCall)]
  82. private static extern void Internal_Create(AnimationCurve instance, KeyFrame[] keyframes);
  83. [MethodImpl(MethodImplOptions.InternalCall)]
  84. private static extern KeyFrame[] Internal_GetKeyFrames(IntPtr thisPtr);
  85. [MethodImpl(MethodImplOptions.InternalCall)]
  86. private static extern void Internal_SetKeyFrames(IntPtr thisPtr, KeyFrame[] keyframes);
  87. [MethodImpl(MethodImplOptions.InternalCall)]
  88. private static extern float Internal_Evaluate(IntPtr thisPtr, float time, bool loop);
  89. }
  90. /// <summary>
  91. /// A set of animation curves for a 3D vector paired with a name.
  92. /// </summary>
  93. public class NamedVector3Curve
  94. {
  95. /// <summary>
  96. /// Constructor for internal runtime use only.
  97. /// </summary>
  98. /// <param name="name">Name of the curve.</param>
  99. /// <param name="flags">Flags that describe the animation curve, of type <see cref="AnimationCurveFlags"/>.</param>
  100. /// <param name="x">Curve representing the x axis of the vector.</param>
  101. /// <param name="y">Curve representing the y axis of the vector.</param>
  102. /// <param name="z">Curve representing the z axis of the vector.</param>
  103. private NamedVector3Curve(string name, int flags, AnimationCurve x, AnimationCurve y, AnimationCurve z)
  104. {
  105. Name = name;
  106. Flags = (AnimationCurveFlags) flags;
  107. X = x;
  108. Y = y;
  109. Z = z;
  110. }
  111. /// <summary>
  112. /// Constructs a new named animation curve.
  113. /// </summary>
  114. /// <param name="name">Name of the curve.</param>
  115. /// <param name="x">Curve representing the x axis of the vector.</param>
  116. /// <param name="y">Curve representing the y axis of the vector.</param>
  117. /// <param name="z">Curve representing the z axis of the vector.</param>
  118. public NamedVector3Curve(string name, AnimationCurve x, AnimationCurve y, AnimationCurve z)
  119. {
  120. Name = name;
  121. X = x;
  122. Y = y;
  123. Z = z;
  124. }
  125. /// <summary>
  126. /// Name of the curve.
  127. /// </summary>
  128. public string Name;
  129. /// <summary>
  130. /// Flags that describe the animation curve.
  131. /// </summary>
  132. public AnimationCurveFlags Flags;
  133. /// <summary>
  134. /// Animation curve for the x axis.
  135. /// </summary>
  136. public AnimationCurve X;
  137. /// <summary>
  138. /// Animation curve for the y axis.
  139. /// </summary>
  140. public AnimationCurve Y;
  141. /// <summary>
  142. /// Animation curve for the z axis.
  143. /// </summary>
  144. public AnimationCurve Z;
  145. }
  146. /// <summary>
  147. /// An animation curve for a single floating point value paired with a name.
  148. /// </summary>
  149. public class NamedFloatCurve
  150. {
  151. /// <summary>
  152. /// Constructor for internal runtime use only.
  153. /// </summary>
  154. /// <param name="name">Name of the curve.</param>
  155. /// <param name="flags">Flags that describe the animation curve, of type <see cref="AnimationCurveFlags"/>.</param>
  156. /// <param name="curve">Curve representing the floating point values.</param>
  157. private NamedFloatCurve(string name, int flags, AnimationCurve curve)
  158. {
  159. Name = name;
  160. Flags = (AnimationCurveFlags)flags;
  161. Curve = curve;
  162. }
  163. /// <summary>
  164. /// Constructs a new named animation curve.
  165. /// </summary>
  166. /// <param name="name">Name of the curve.</param>
  167. /// <param name="curve">Curve representing the floating point values.</param>
  168. public NamedFloatCurve(string name, AnimationCurve curve)
  169. {
  170. Name = name;
  171. Curve = curve;
  172. }
  173. /// <summary>
  174. /// Name of the curve.
  175. /// </summary>
  176. public string Name;
  177. /// <summary>
  178. /// Flags that describe the animation curve.
  179. /// </summary>
  180. public AnimationCurveFlags Flags;
  181. /// <summary>
  182. /// Animation curve.
  183. /// </summary>
  184. public AnimationCurve Curve;
  185. }
  186. }