TextureCurve.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace UnityEngine.Rendering
  4. {
  5. // Due to limitations in the builtin AnimationCurve we need this custom wrapper.
  6. // Improvements:
  7. // - Dirty state handling so we know when a curve has changed or not
  8. // - Looping support (infinite curve)
  9. // - Zero-value curve
  10. // - Cheaper length property
  11. /// <summary>
  12. /// A wrapper around <c>AnimationCurve</c> to automatically bake it into a texture.
  13. /// </summary>
  14. [Serializable]
  15. public class TextureCurve : IDisposable
  16. {
  17. const int k_Precision = 128; // Edit LutBuilder3D if you change this value
  18. const float k_Step = 1f / k_Precision;
  19. /// <summary>
  20. /// The number of keys in the curve.
  21. /// </summary>
  22. [field: SerializeField]
  23. public int length { get; private set; } // Calling AnimationCurve.length is very slow, let's cache it
  24. [SerializeField]
  25. bool m_Loop;
  26. [SerializeField]
  27. float m_ZeroValue;
  28. [SerializeField]
  29. float m_Range;
  30. [SerializeField]
  31. AnimationCurve m_Curve;
  32. AnimationCurve m_LoopingCurve;
  33. Texture2D m_Texture;
  34. bool m_IsCurveDirty;
  35. bool m_IsTextureDirty;
  36. /// <summary>
  37. /// Retrieves the key at index.
  38. /// </summary>
  39. /// <param name="index">The index to look for.</param>
  40. /// <returns>A key.</returns>
  41. public Keyframe this[int index] => m_Curve[index];
  42. /// <summary>
  43. /// Creates a new <see cref="TextureCurve"/> from an existing <c>AnimationCurve</c>.
  44. /// </summary>
  45. /// <param name="baseCurve">The source <c>AnimationCurve</c>.</param>
  46. /// <param name="zeroValue">The default value to use when the curve doesn't have any key.</param>
  47. /// <param name="loop">Should the curve automatically loop in the given <paramref name="bounds"/>?</param>
  48. /// <param name="bounds">The boundaries of the curve.</param>
  49. public TextureCurve(AnimationCurve baseCurve, float zeroValue, bool loop, in Vector2 bounds)
  50. : this(baseCurve.keys, zeroValue, loop, bounds) { }
  51. /// <summary>
  52. /// Creates a new <see cref="TextureCurve"/> from an arbitrary number of keyframes.
  53. /// </summary>
  54. /// <param name="keys">An array of Keyframes used to define the curve.</param>
  55. /// <param name="zeroValue">The default value to use when the curve doesn't have any key.</param>
  56. /// <param name="loop">Should the curve automatically loop in the given <paramref name="bounds"/>?</param>
  57. /// <param name="bounds">The boundaries of the curve.</param>
  58. public TextureCurve(Keyframe[] keys, float zeroValue, bool loop, in Vector2 bounds)
  59. {
  60. m_Curve = new AnimationCurve(keys);
  61. m_ZeroValue = zeroValue;
  62. m_Loop = loop;
  63. m_Range = bounds.magnitude;
  64. length = keys.Length;
  65. SetDirty();
  66. }
  67. /// <summary>
  68. /// Finalizer.
  69. /// </summary>
  70. ~TextureCurve()
  71. {
  72. ReleaseUnityResources();
  73. }
  74. /// <summary>
  75. /// Cleans up the internal texture resource.
  76. /// </summary>
  77. public void Dispose()
  78. {
  79. ReleaseUnityResources();
  80. GC.SuppressFinalize(this);
  81. }
  82. void ReleaseUnityResources()
  83. {
  84. CoreUtils.Destroy(m_Texture);
  85. m_Texture = null;
  86. }
  87. /// <summary>
  88. /// Marks the curve as dirty to trigger a redraw of the texture the next time <see cref="GetTexture"/>
  89. /// is called.
  90. /// </summary>
  91. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  92. public void SetDirty()
  93. {
  94. m_IsCurveDirty = true;
  95. m_IsTextureDirty = true;
  96. }
  97. static TextureFormat GetTextureFormat()
  98. {
  99. if (SystemInfo.SupportsTextureFormat(TextureFormat.RHalf))
  100. return TextureFormat.RHalf;
  101. if (SystemInfo.SupportsTextureFormat(TextureFormat.R8))
  102. return TextureFormat.R8;
  103. return TextureFormat.ARGB32;
  104. }
  105. /// <summary>
  106. /// Gets the texture representation of this curve.
  107. /// </summary>
  108. /// <returns>A 128x1 texture.</returns>
  109. public Texture2D GetTexture()
  110. {
  111. if (m_IsTextureDirty)
  112. {
  113. if (m_Texture == null)
  114. {
  115. m_Texture = new Texture2D(k_Precision, 1, GetTextureFormat(), false, true);
  116. m_Texture.name = "CurveTexture";
  117. m_Texture.hideFlags = HideFlags.HideAndDontSave;
  118. m_Texture.filterMode = FilterMode.Bilinear;
  119. m_Texture.wrapMode = TextureWrapMode.Clamp;
  120. }
  121. var pixels = new Color[k_Precision];
  122. for (int i = 0; i < pixels.Length; i++)
  123. pixels[i].r = Evaluate(i * k_Step);
  124. m_Texture.SetPixels(pixels);
  125. m_Texture.Apply(false, false);
  126. m_IsTextureDirty = false;
  127. }
  128. return m_Texture;
  129. }
  130. /// <summary>
  131. /// Evaluate a time value on the curve.
  132. /// </summary>
  133. /// <param name="time">The time within the curve you want to evaluate.</param>
  134. /// <returns>The value of the curve, at the point in time specified.</returns>
  135. public float Evaluate(float time)
  136. {
  137. if (m_IsCurveDirty)
  138. length = m_Curve.length;
  139. if (length == 0)
  140. return m_ZeroValue;
  141. if (!m_Loop || length == 1)
  142. return m_Curve.Evaluate(time);
  143. if (m_IsCurveDirty)
  144. {
  145. if (m_LoopingCurve == null)
  146. m_LoopingCurve = new AnimationCurve();
  147. var prev = m_Curve[length - 1];
  148. prev.time -= m_Range;
  149. var next = m_Curve[0];
  150. next.time += m_Range;
  151. m_LoopingCurve.keys = m_Curve.keys; // GC pressure
  152. m_LoopingCurve.AddKey(prev);
  153. m_LoopingCurve.AddKey(next);
  154. m_IsCurveDirty = false;
  155. }
  156. return m_LoopingCurve.Evaluate(time);
  157. }
  158. /// <summary>
  159. /// Adds a new key to the curve.
  160. /// </summary>
  161. /// <param name="time">The time at which to add the key.</param>
  162. /// <param name="value">The value for the key.</param>
  163. /// <returns>The index of the added key, or -1 if the key could not be added.</returns>
  164. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  165. public int AddKey(float time, float value)
  166. {
  167. int r = m_Curve.AddKey(time, value);
  168. if (r > -1)
  169. SetDirty();
  170. return r;
  171. }
  172. /// <summary>
  173. /// Removes the keyframe at <paramref name="index"/> and inserts <paramref name="key"/>.
  174. /// </summary>
  175. /// <param name="index"></param>
  176. /// <param name="key"></param>
  177. /// <returns>The index of the keyframe after moving it.</returns>
  178. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  179. public int MoveKey(int index, in Keyframe key)
  180. {
  181. int r = m_Curve.MoveKey(index, key);
  182. SetDirty();
  183. return r;
  184. }
  185. /// <summary>
  186. /// Removes a key.
  187. /// </summary>
  188. /// <param name="index">The index of the key to remove.</param>
  189. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  190. public void RemoveKey(int index)
  191. {
  192. m_Curve.RemoveKey(index);
  193. SetDirty();
  194. }
  195. /// <summary>
  196. /// Smoothes the in and out tangents of the keyframe at <paramref name="index"/>. A <paramref name="weight"/> of 0 evens out tangents.
  197. /// </summary>
  198. /// <param name="index">The index of the keyframe to be smoothed.</param>
  199. /// <param name="weight">The smoothing weight to apply to the keyframe's tangents.</param>
  200. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  201. public void SmoothTangents(int index, float weight)
  202. {
  203. m_Curve.SmoothTangents(index, weight);
  204. SetDirty();
  205. }
  206. }
  207. /// <summary>
  208. /// A <see cref="VolumeParameter"/> that holds a <see cref="TextureCurve"/> value.
  209. /// </summary>
  210. [Serializable]
  211. public class TextureCurveParameter : VolumeParameter<TextureCurve>
  212. {
  213. /// <summary>
  214. /// Creates a new <see cref="TextureCurveParameter"/> instance.
  215. /// </summary>
  216. /// <param name="value">The initial value to store in the parameter.</param>
  217. /// <param name="overrideState">The initial override state for the parameter.</param>
  218. public TextureCurveParameter(TextureCurve value, bool overrideState = false)
  219. : base(value, overrideState) { }
  220. // TODO: TextureCurve interpolation
  221. }
  222. }