AnimationCurve.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. /// Creates a new animation curve.
  39. /// </summary>
  40. /// <param name="keyFrames">Keyframes to initialize the curve with.</param>
  41. public AnimationCurve(KeyFrame[] keyFrames)
  42. {
  43. Internal_Create(this, keyFrames);
  44. }
  45. /// <summary>
  46. /// Keyframes that represent the curve.
  47. /// </summary>
  48. public KeyFrame[] KeyFrames
  49. {
  50. get { return Internal_GetKeyFrames(mCachedPtr); }
  51. set { Internal_SetKeyFrames(mCachedPtr, value); }
  52. }
  53. /// <summary>
  54. /// Evaluate the animation curve at the specified time.
  55. /// </summary>
  56. /// <param name="time">Time to evaluate the curve at. </param>
  57. /// <param name="loop">If true the curve will loop when it goes past the end or beggining. Otherwise the curve
  58. /// value will be clamped.</param>
  59. /// <returns>Interpolated value from the curve at provided time.</returns>
  60. public float Evaluate(float time, bool loop = true)
  61. {
  62. return Internal_Evaluate(mCachedPtr, time, loop);
  63. }
  64. [MethodImpl(MethodImplOptions.InternalCall)]
  65. private static extern void Internal_Create(AnimationCurve instance, KeyFrame[] keyframes);
  66. [MethodImpl(MethodImplOptions.InternalCall)]
  67. private static extern KeyFrame[] Internal_GetKeyFrames(IntPtr thisPtr);
  68. [MethodImpl(MethodImplOptions.InternalCall)]
  69. private static extern void Internal_SetKeyFrames(IntPtr thisPtr, KeyFrame[] keyframes);
  70. [MethodImpl(MethodImplOptions.InternalCall)]
  71. private static extern float Internal_Evaluate(IntPtr thisPtr, float time, bool loop);
  72. }
  73. }