Spline.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using UnityEngine.Assertions;
  3. namespace UnityEngine.Rendering.PostProcessing
  4. {
  5. /// <summary>
  6. /// A wrapper on top of <see cref="AnimationCurve"/> to handle zero-key curves and keyframe
  7. /// loops.
  8. /// </summary>
  9. [Serializable]
  10. public sealed class Spline
  11. {
  12. /// <summary>
  13. /// Precision of the curve.
  14. /// </summary>
  15. public const int k_Precision = 128;
  16. /// <summary>
  17. /// The inverse of the precision of the curve.
  18. /// </summary>
  19. public const float k_Step = 1f / k_Precision;
  20. /// <summary>
  21. /// The underlying animation curve instance.
  22. /// </summary>
  23. public AnimationCurve curve;
  24. [SerializeField]
  25. bool m_Loop;
  26. [SerializeField]
  27. float m_ZeroValue;
  28. [SerializeField]
  29. float m_Range;
  30. AnimationCurve m_InternalLoopingCurve;
  31. // Used to track frame changes for data caching
  32. int frameCount = -1;
  33. /// <summary>
  34. /// An array holding pre-computed curve values.
  35. /// </summary>
  36. public float[] cachedData;
  37. /// <summary>
  38. /// Creates a new spline.
  39. /// </summary>
  40. /// <param name="curve">The animation curve to base this spline off</param>
  41. /// <param name="zeroValue">The value to return when the curve has no keyframe</param>
  42. /// <param name="loop">Should this curve loop?</param>
  43. /// <param name="bounds">The curve bounds</param>
  44. public Spline(AnimationCurve curve, float zeroValue, bool loop, Vector2 bounds)
  45. {
  46. Assert.IsNotNull(curve);
  47. this.curve = curve;
  48. m_ZeroValue = zeroValue;
  49. m_Loop = loop;
  50. m_Range = bounds.magnitude;
  51. cachedData = new float[k_Precision];
  52. }
  53. /// <summary>
  54. /// Caches the curve data at a given frame. The curve data will only be cached once per
  55. /// frame.
  56. /// </summary>
  57. /// <param name="frame">A frame number</param>
  58. public void Cache(int frame)
  59. {
  60. // Note: it would be nice to have a way to check if a curve has changed in any way, that
  61. // would save quite a few CPU cycles instead of having to force cache it once per frame :/
  62. // Only cache once per frame
  63. if (frame == frameCount)
  64. return;
  65. var length = curve.length;
  66. if (m_Loop && length > 1)
  67. {
  68. if (m_InternalLoopingCurve == null)
  69. m_InternalLoopingCurve = new AnimationCurve();
  70. var prev = curve[length - 1];
  71. prev.time -= m_Range;
  72. var next = curve[0];
  73. next.time += m_Range;
  74. m_InternalLoopingCurve.keys = curve.keys;
  75. m_InternalLoopingCurve.AddKey(prev);
  76. m_InternalLoopingCurve.AddKey(next);
  77. }
  78. for (int i = 0; i < k_Precision; i++)
  79. cachedData[i] = Evaluate((float)i * k_Step, length);
  80. frameCount = Time.renderedFrameCount;
  81. }
  82. /// <summary>
  83. /// Evaluates the curve at a point in time.
  84. /// </summary>
  85. /// <param name="t">The time to evaluate</param>
  86. /// <param name="length">The number of keyframes in the curve</param>
  87. /// <returns>The value of the curve at time <paramref name="t"/></returns>
  88. public float Evaluate(float t, int length)
  89. {
  90. if (length == 0)
  91. return m_ZeroValue;
  92. if (!m_Loop || length == 1)
  93. return curve.Evaluate(t);
  94. return m_InternalLoopingCurve.Evaluate(t);
  95. }
  96. /// <summary>
  97. /// Evaluates the curve at a point in time.
  98. /// </summary>
  99. /// <param name="t">The time to evaluate</param>
  100. /// <returns>The value of the curve at time <paramref name="t"/></returns>
  101. /// <remarks>
  102. /// Calling the length getter on a curve is expensive to it's better to cache its length and
  103. /// call <see cref="Evaluate(float,int)"/> instead of getting the length for every call.
  104. /// </remarks>
  105. public float Evaluate(float t)
  106. {
  107. // Calling the length getter on a curve is expensive (!?) so it's better to cache its
  108. // length and call Evaluate(t, length) instead of getting the length for every call to
  109. // Evaluate(t)
  110. return Evaluate(t, curve.length);
  111. }
  112. /// <summary>
  113. /// Returns the computed hash code for this parameter.
  114. /// </summary>
  115. /// <returns>A computed hash code</returns>
  116. public override int GetHashCode()
  117. {
  118. unchecked
  119. {
  120. int hash = 17;
  121. hash = hash * 23 + curve.GetHashCode(); // Not implemented in Unity, so it'll always return the same value :(
  122. return hash;
  123. }
  124. }
  125. }
  126. }