暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TextureCurve.cs 9.6KB

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