AnimationCurve.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /// Animation spline represented by a set of keyframes, each representing an endpoint of a cubic hermite curve. The
  33. /// spline can be evaluated at any time, and uses caching to speed up multiple sequential evaluations.
  34. /// </summary>
  35. public class AnimationCurve : ScriptObject
  36. {
  37. /// <summary>
  38. /// Constructor for internal runtime use only.
  39. /// </summary>
  40. private AnimationCurve()
  41. { }
  42. /// <summary>
  43. /// Creates a new animation curve.
  44. /// </summary>
  45. /// <param name="keyFrames">Keyframes to initialize the curve with.</param>
  46. public AnimationCurve(KeyFrame[] keyFrames)
  47. {
  48. Internal_Create(this, keyFrames);
  49. }
  50. /// <summary>
  51. /// Keyframes that represent the curve.
  52. /// </summary>
  53. public KeyFrame[] KeyFrames
  54. {
  55. get { return Internal_GetKeyFrames(mCachedPtr); }
  56. set { Internal_SetKeyFrames(mCachedPtr, value); }
  57. }
  58. /// <summary>
  59. /// Evaluate the animation curve at the specified time.
  60. /// </summary>
  61. /// <param name="time">Time to evaluate the curve at. </param>
  62. /// <param name="loop">If true the curve will loop when it goes past the end or beggining. Otherwise the curve
  63. /// value will be clamped.</param>
  64. /// <returns>Interpolated value from the curve at provided time.</returns>
  65. public float Evaluate(float time, bool loop = true)
  66. {
  67. return Internal_Evaluate(mCachedPtr, time, loop);
  68. }
  69. [MethodImpl(MethodImplOptions.InternalCall)]
  70. private static extern void Internal_Create(AnimationCurve instance, KeyFrame[] keyframes);
  71. [MethodImpl(MethodImplOptions.InternalCall)]
  72. private static extern KeyFrame[] Internal_GetKeyFrames(IntPtr thisPtr);
  73. [MethodImpl(MethodImplOptions.InternalCall)]
  74. private static extern void Internal_SetKeyFrames(IntPtr thisPtr, KeyFrame[] keyframes);
  75. [MethodImpl(MethodImplOptions.InternalCall)]
  76. private static extern float Internal_Evaluate(IntPtr thisPtr, float time, bool loop);
  77. }
  78. /// <summary>
  79. /// A set of animation curves for a 3D vector paired with a name.
  80. /// </summary>
  81. public class NamedVector3Curve
  82. {
  83. /// <summary>
  84. /// Constructs a new named animation curve.
  85. /// </summary>
  86. /// <param name="name">Name of the curve.</param>
  87. /// <param name="x">Curve representing the x axis of the vector.</param>
  88. /// <param name="y">Curve representing the y axis of the vector.</param>
  89. /// <param name="z">Curve representing the z axis of the vector.</param>
  90. public NamedVector3Curve(string name, AnimationCurve x, AnimationCurve y, AnimationCurve z)
  91. {
  92. Name = name;
  93. X = x;
  94. Y = y;
  95. Z = z;
  96. }
  97. /// <summary>
  98. /// Name of the curve.
  99. /// </summary>
  100. public string Name;
  101. /// <summary>
  102. /// Animation curve for the x axis.
  103. /// </summary>
  104. public AnimationCurve X;
  105. /// <summary>
  106. /// Animation curve for the y axis.
  107. /// </summary>
  108. public AnimationCurve Y;
  109. /// <summary>
  110. /// Animation curve for the z axis.
  111. /// </summary>
  112. public AnimationCurve Z;
  113. }
  114. /// <summary>
  115. /// An animatio curve for a single floating point value paired with a name.
  116. /// </summary>
  117. public class NamedFloatCurve
  118. {
  119. /// <summary>
  120. /// Constructs a new named animation curve.
  121. /// </summary>
  122. /// <param name="name">Name of the curve.</param>
  123. /// <param name="curve">Curve representing the floating point values.</param>
  124. public NamedFloatCurve(string name, AnimationCurve curve)
  125. {
  126. Name = name;
  127. Curve = curve;
  128. }
  129. /// <summary>
  130. /// Name of the curve.
  131. /// </summary>
  132. public string Name;
  133. /// <summary>
  134. /// Animation curve.
  135. /// </summary>
  136. public AnimationCurve Curve;
  137. }
  138. }