using System; using UnityEngine.Assertions; namespace UnityEngine.Rendering.PostProcessing { /// /// A wrapper on top of to handle zero-key curves and keyframe /// loops. /// [Serializable] public sealed class Spline { /// /// Precision of the curve. /// public const int k_Precision = 128; /// /// The inverse of the precision of the curve. /// public const float k_Step = 1f / k_Precision; /// /// The underlying animation curve instance. /// public AnimationCurve curve; [SerializeField] bool m_Loop; [SerializeField] float m_ZeroValue; [SerializeField] float m_Range; AnimationCurve m_InternalLoopingCurve; // Used to track frame changes for data caching int frameCount = -1; /// /// An array holding pre-computed curve values. /// public float[] cachedData; /// /// Creates a new spline. /// /// The animation curve to base this spline off /// The value to return when the curve has no keyframe /// Should this curve loop? /// The curve bounds public Spline(AnimationCurve curve, float zeroValue, bool loop, Vector2 bounds) { Assert.IsNotNull(curve); this.curve = curve; m_ZeroValue = zeroValue; m_Loop = loop; m_Range = bounds.magnitude; cachedData = new float[k_Precision]; } /// /// Caches the curve data at a given frame. The curve data will only be cached once per /// frame. /// /// A frame number public void Cache(int frame) { // Note: it would be nice to have a way to check if a curve has changed in any way, that // would save quite a few CPU cycles instead of having to force cache it once per frame :/ // Only cache once per frame if (frame == frameCount) return; var length = curve.length; if (m_Loop && length > 1) { if (m_InternalLoopingCurve == null) m_InternalLoopingCurve = new AnimationCurve(); var prev = curve[length - 1]; prev.time -= m_Range; var next = curve[0]; next.time += m_Range; m_InternalLoopingCurve.keys = curve.keys; m_InternalLoopingCurve.AddKey(prev); m_InternalLoopingCurve.AddKey(next); } for (int i = 0; i < k_Precision; i++) cachedData[i] = Evaluate((float)i * k_Step, length); frameCount = Time.renderedFrameCount; } /// /// Evaluates the curve at a point in time. /// /// The time to evaluate /// The number of keyframes in the curve /// The value of the curve at time public float Evaluate(float t, int length) { if (length == 0) return m_ZeroValue; if (!m_Loop || length == 1) return curve.Evaluate(t); return m_InternalLoopingCurve.Evaluate(t); } /// /// Evaluates the curve at a point in time. /// /// The time to evaluate /// The value of the curve at time /// /// Calling the length getter on a curve is expensive to it's better to cache its length and /// call instead of getting the length for every call. /// public float Evaluate(float t) { // Calling the length getter on a curve is expensive (!?) so it's better to cache its // length and call Evaluate(t, length) instead of getting the length for every call to // Evaluate(t) return Evaluate(t, curve.length); } /// /// Returns the computed hash code for this parameter. /// /// A computed hash code public override int GetHashCode() { unchecked { int hash = 17; hash = hash * 23 + curve.GetHashCode(); // Not implemented in Unity, so it'll always return the same value :( return hash; } } } }