ParameterOverride.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. using System;
  2. namespace UnityEngine.Rendering.PostProcessing
  3. {
  4. /// <summary>
  5. /// The base abstract class for all parameter override types.
  6. /// </summary>
  7. /// <seealso cref="ParameterOverride{T}"/>
  8. public abstract class ParameterOverride
  9. {
  10. /// <summary>
  11. /// The override state of this parameter.
  12. /// </summary>
  13. public bool overrideState;
  14. internal abstract void Interp(ParameterOverride from, ParameterOverride to, float t);
  15. /// <summary>
  16. /// Returns the computed hash code for this parameter.
  17. /// </summary>
  18. /// <returns>A computed hash code</returns>
  19. public abstract int GetHash();
  20. /// <summary>
  21. /// Casts and returns the value stored in this parameter.
  22. /// </summary>
  23. /// <typeparam name="T">The type to cast to</typeparam>
  24. /// <returns>The value stored in this parameter</returns>
  25. public T GetValue<T>()
  26. {
  27. return ((ParameterOverride<T>)this).value;
  28. }
  29. /// <summary>
  30. /// This method is called right after the parent <see cref="PostProcessEffectSettings"/> has
  31. /// been initialized. This is used in case you need to access fields or properties that
  32. /// can't be accessed in the constructor of a <see cref="ScriptableObject"/>
  33. /// (<c>ParameterOverride</c> objects are generally declared and initialized in a
  34. /// <see cref="PostProcessEffectSettings"/>).
  35. /// </summary>
  36. /// <seealso cref="OnDisable"/>
  37. protected internal virtual void OnEnable()
  38. {
  39. }
  40. /// <summary>
  41. /// This method is called right before the parent <see cref="PostProcessEffectSettings"/>
  42. /// gets de-initialized.
  43. /// </summary>
  44. /// <seealso cref="OnEnable"/>
  45. protected internal virtual void OnDisable()
  46. {
  47. }
  48. internal abstract void SetValue(ParameterOverride parameter);
  49. }
  50. /// <summary>
  51. /// The base typed class for all parameter override types.
  52. /// </summary>
  53. /// <typeparam name="T">The type of value to store in this <c>ParameterOverride</c></typeparam>
  54. /// <remarks>
  55. /// Due to limitations with the serialization system in Unity you shouldn't use this class
  56. /// directly. Use one of the pre-flatten types (like <see cref="FloatParameter"/> or make your
  57. /// own by extending this class.
  58. /// </remarks>
  59. /// <example>
  60. /// This sample code shows how to make a custom parameter holding a <c>float</c>.
  61. /// <code>
  62. /// [Serializable]
  63. /// public sealed class FloatParameter : ParameterOverride&lt;float&gt;
  64. /// {
  65. /// public override void Interp(float from, float to, float t)
  66. /// {
  67. /// value = from + (to - from) * t;
  68. /// }
  69. /// }
  70. /// </code>
  71. /// </example>
  72. [Serializable]
  73. public class ParameterOverride<T> : ParameterOverride
  74. {
  75. /// <summary>
  76. /// The value stored in this parameter.
  77. /// </summary>
  78. public T value;
  79. /// <summary>
  80. /// Creates a <c>ParameterOverride</c> with a default <see cref="value"/> and
  81. /// <see cref="ParameterOverride.overrideState"/> set to <c>false</c>.
  82. /// </summary>
  83. public ParameterOverride()
  84. : this(default(T), false)
  85. {
  86. }
  87. /// <summary>
  88. /// Creates a <c>ParameterOverride</c> with a given value and
  89. /// <see cref="ParameterOverride.overrideState"/> set to <c>false</c>.
  90. /// </summary>
  91. /// <param name="value">The value to set this parameter to</param>
  92. public ParameterOverride(T value)
  93. : this(value, false)
  94. {
  95. }
  96. /// <summary>
  97. /// Creates a <c>ParameterOverride</c> with a given value and override state.
  98. /// </summary>
  99. /// <param name="value">The value to set this parameter to</param>
  100. /// <param name="overrideState">The override state for this value</param>
  101. public ParameterOverride(T value, bool overrideState)
  102. {
  103. this.value = value;
  104. this.overrideState = overrideState;
  105. }
  106. internal override void Interp(ParameterOverride from, ParameterOverride to, float t)
  107. {
  108. // Note: this isn't completely safe but it'll do fine
  109. Interp(from.GetValue<T>(), to.GetValue<T>(), t);
  110. }
  111. /// <summary>
  112. /// Interpolates between two values given an interpolation factor <paramref name="t"/>.
  113. /// </summary>
  114. /// <param name="from">The value to interpolate from</param>
  115. /// <param name="to">The value to interpolate to</param>
  116. /// <param name="t">An interpolation factor (generally in range <c>[0,1]</c>)</param>
  117. /// <remarks>
  118. /// By default this method does a "snap" interpolation, meaning it will return the value
  119. /// <paramref name="to"/> if <paramref name="t"/> is higher than 0, <paramref name="from"/>
  120. /// otherwise.
  121. /// </remarks>
  122. public virtual void Interp(T from, T to, float t)
  123. {
  124. // Returns `to` if `dt > 0` by default so we don't have to write overrides for bools and
  125. // enumerations.
  126. value = t > 0f ? to : from;
  127. }
  128. /// <summary>
  129. /// Sets the value for this parameter to <paramref name="x"/> and mark the override state
  130. /// to <c>true</c>.
  131. /// </summary>
  132. /// <param name="x"></param>
  133. public void Override(T x)
  134. {
  135. overrideState = true;
  136. value = x;
  137. }
  138. internal override void SetValue(ParameterOverride parameter)
  139. {
  140. value = parameter.GetValue<T>();
  141. }
  142. /// <inheritdoc />
  143. public override int GetHash()
  144. {
  145. unchecked
  146. {
  147. int hash = 17;
  148. hash = hash * 23 + overrideState.GetHashCode();
  149. hash = hash * 23 + value.GetHashCode();
  150. return hash;
  151. }
  152. }
  153. /// <summary>
  154. /// Implicit conversion between <see cref="ParameterOverride{T}"/> and its value type.
  155. /// </summary>
  156. /// <param name="prop">The parameter to implicitly cast</param>
  157. public static implicit operator T(ParameterOverride<T> prop)
  158. {
  159. return prop.value;
  160. }
  161. }
  162. // Bypassing the limited unity serialization system...
  163. /// <summary>
  164. /// A <see cref="ParameterOverride{T}"/> that holds a <c>float</c> value.
  165. /// </summary>
  166. /// <remarks>
  167. /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>.
  168. /// </remarks>
  169. [Serializable]
  170. public sealed class FloatParameter : ParameterOverride<float>
  171. {
  172. /// <inheritdoc />
  173. public override void Interp(float from, float to, float t)
  174. {
  175. value = from + (to - from) * t;
  176. }
  177. }
  178. /// <summary>
  179. /// A <see cref="ParameterOverride{T}"/> that holds a <c>int</c> value.
  180. /// </summary>
  181. /// <remarks>
  182. /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>
  183. /// casted to <c>int</c>.
  184. /// </remarks>
  185. [Serializable]
  186. public sealed class IntParameter : ParameterOverride<int>
  187. {
  188. /// <inheritdoc />
  189. public override void Interp(int from, int to, float t)
  190. {
  191. // Int snapping interpolation. Don't use this for enums as they don't necessarily have
  192. // contiguous values. Use the default interpolator instead (same as bool).
  193. value = (int)(from + (to - from) * t);
  194. }
  195. }
  196. /// <summary>
  197. /// A <see cref="ParameterOverride{T}"/> that holds a <c>bool</c> value.
  198. /// </summary>
  199. [Serializable]
  200. public sealed class BoolParameter : ParameterOverride<bool> {}
  201. /// <summary>
  202. /// A <see cref="ParameterOverride{T}"/> that holds a <see cref="Color"/> value.
  203. /// </summary>
  204. /// <remarks>
  205. /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>
  206. /// for each channel.
  207. /// </remarks>
  208. [Serializable]
  209. public sealed class ColorParameter : ParameterOverride<Color>
  210. {
  211. /// <inheritdoc />
  212. public override void Interp(Color from, Color to, float t)
  213. {
  214. // Lerping color values is a sensitive subject... We looked into lerping colors using
  215. // HSV and LCH but they have some downsides that make them not work correctly in all
  216. // situations, so we stick with RGB lerping for now, at least its behavior is
  217. // predictable despite looking desaturated when `t ~= 0.5` and it's faster anyway.
  218. value.r = from.r + (to.r - from.r) * t;
  219. value.g = from.g + (to.g - from.g) * t;
  220. value.b = from.b + (to.b - from.b) * t;
  221. value.a = from.a + (to.a - from.a) * t;
  222. }
  223. /// <summary>
  224. /// Implicit conversion between <see cref="ColorParameter"/> and a <see cref="Vector4"/>.
  225. /// </summary>
  226. /// <param name="prop">The parameter to implicitly cast</param>
  227. public static implicit operator Vector4(ColorParameter prop)
  228. {
  229. return prop.value;
  230. }
  231. }
  232. /// <summary>
  233. /// A <see cref="ParameterOverride{T}"/> that holds a <see cref="Vector2"/> value.
  234. /// </summary>
  235. /// <remarks>
  236. /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>
  237. /// for each axis.
  238. /// </remarks>
  239. [Serializable]
  240. public sealed class Vector2Parameter : ParameterOverride<Vector2>
  241. {
  242. /// <inheritdoc />
  243. public override void Interp(Vector2 from, Vector2 to, float t)
  244. {
  245. value.x = from.x + (to.x - from.x) * t;
  246. value.y = from.y + (to.y - from.y) * t;
  247. }
  248. /// <summary>
  249. /// Implicit conversion between <see cref="Vector2Parameter"/> and a <see cref="Vector3"/>.
  250. /// </summary>
  251. /// <param name="prop">The parameter to implicitly cast</param>
  252. public static implicit operator Vector3(Vector2Parameter prop)
  253. {
  254. return prop.value;
  255. }
  256. /// <summary>
  257. /// Implicit conversion between <see cref="Vector2Parameter"/> and a <see cref="Vector4"/>.
  258. /// </summary>
  259. /// <param name="prop">The parameter to implicitly cast</param>
  260. public static implicit operator Vector4(Vector2Parameter prop)
  261. {
  262. return prop.value;
  263. }
  264. }
  265. /// <summary>
  266. /// A <see cref="ParameterOverride{T}"/> that holds a <see cref="Vector3"/> value.
  267. /// </summary>
  268. /// <remarks>
  269. /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>
  270. /// for each axis.
  271. /// </remarks>
  272. [Serializable]
  273. public sealed class Vector3Parameter : ParameterOverride<Vector3>
  274. {
  275. /// <inheritdoc />
  276. public override void Interp(Vector3 from, Vector3 to, float t)
  277. {
  278. value.x = from.x + (to.x - from.x) * t;
  279. value.y = from.y + (to.y - from.y) * t;
  280. value.z = from.z + (to.z - from.z) * t;
  281. }
  282. /// <summary>
  283. /// Implicit conversion between <see cref="Vector3Parameter"/> and a <see cref="Vector2"/>.
  284. /// </summary>
  285. /// <param name="prop">The parameter to implicitly cast</param>
  286. public static implicit operator Vector2(Vector3Parameter prop)
  287. {
  288. return prop.value;
  289. }
  290. /// <summary>
  291. /// Implicit conversion between <see cref="Vector3Parameter"/> and a <see cref="Vector4"/>.
  292. /// </summary>
  293. /// <param name="prop">The parameter to implicitly cast</param>
  294. public static implicit operator Vector4(Vector3Parameter prop)
  295. {
  296. return prop.value;
  297. }
  298. }
  299. /// <summary>
  300. /// A <see cref="ParameterOverride{T}"/> that holds a <see cref="Vector4"/> value.
  301. /// </summary>
  302. /// <remarks>
  303. /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>
  304. /// for each axis.
  305. /// </remarks>
  306. [Serializable]
  307. public sealed class Vector4Parameter : ParameterOverride<Vector4>
  308. {
  309. /// <inheritdoc />
  310. public override void Interp(Vector4 from, Vector4 to, float t)
  311. {
  312. value.x = from.x + (to.x - from.x) * t;
  313. value.y = from.y + (to.y - from.y) * t;
  314. value.z = from.z + (to.z - from.z) * t;
  315. value.w = from.w + (to.w - from.w) * t;
  316. }
  317. /// <summary>
  318. /// Implicit conversion between <see cref="Vector4Parameter"/> and a <see cref="Vector2"/>.
  319. /// </summary>
  320. /// <param name="prop">The parameter to implicitly cast</param>
  321. public static implicit operator Vector2(Vector4Parameter prop)
  322. {
  323. return prop.value;
  324. }
  325. /// <summary>
  326. /// Implicit conversion between <see cref="Vector4Parameter"/> and a <see cref="Vector3"/>.
  327. /// </summary>
  328. /// <param name="prop">The parameter to implicitly cast</param>
  329. public static implicit operator Vector3(Vector4Parameter prop)
  330. {
  331. return prop.value;
  332. }
  333. }
  334. /// <summary>
  335. /// A <see cref="ParameterOverride{T}"/> that holds a <see cref="Spline"/> value.
  336. /// </summary>
  337. /// <remarks>
  338. /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>
  339. /// for each point on the curve.
  340. /// </remarks>
  341. [Serializable]
  342. public sealed class SplineParameter : ParameterOverride<Spline>
  343. {
  344. /// <inheritdoc />
  345. protected internal override void OnEnable()
  346. {
  347. if (value != null)
  348. value.Cache(int.MinValue);
  349. }
  350. internal override void SetValue(ParameterOverride parameter)
  351. {
  352. base.SetValue(parameter);
  353. if (value != null)
  354. value.Cache(Time.renderedFrameCount);
  355. }
  356. /// <inheritdoc />
  357. public override void Interp(Spline from, Spline to, float t)
  358. {
  359. if (from == null || to == null)
  360. {
  361. base.Interp(from, to, t);
  362. return;
  363. }
  364. int frameCount = Time.renderedFrameCount;
  365. from.Cache(frameCount);
  366. to.Cache(frameCount);
  367. for (int i = 0; i < Spline.k_Precision; i++)
  368. {
  369. float a = from.cachedData[i];
  370. float b = to.cachedData[i];
  371. value.cachedData[i] = a + (b - a) * t;
  372. }
  373. }
  374. }
  375. /// <summary>
  376. /// A set of default textures to use as default values for <see cref="TextureParameter"/>.
  377. /// </summary>
  378. public enum TextureParameterDefault
  379. {
  380. /// <summary>
  381. /// No texture, or <c>null</c>.
  382. /// </summary>
  383. None,
  384. /// <summary>
  385. /// A black texture.
  386. /// </summary>
  387. Black,
  388. /// <summary>
  389. /// A white texture.
  390. /// </summary>
  391. White,
  392. /// <summary>
  393. /// A transparent texture.
  394. /// </summary>
  395. Transparent,
  396. /// <summary>
  397. /// A 2D lookup table in strip format with <c>width = height * height</c>.
  398. /// </summary>
  399. Lut2D
  400. }
  401. /// <summary>
  402. /// A <see cref="ParameterOverride{T}"/> that holds a <see cref="Texture"/> value.
  403. /// </summary>
  404. /// <remarks>
  405. /// Texture interpolation is done using a classic linear interpolation method.
  406. /// </remarks>
  407. [Serializable]
  408. public sealed class TextureParameter : ParameterOverride<Texture>
  409. {
  410. public TextureParameterDefault defaultState = TextureParameterDefault.Black;
  411. /// <inheritdoc />
  412. public override void Interp(Texture from, Texture to, float t)
  413. {
  414. // Both are null, do nothing
  415. if (from == null && to == null)
  416. {
  417. value = null;
  418. return;
  419. }
  420. // Both aren't null we're ready to blend
  421. if (from != null && to != null)
  422. {
  423. value = TextureLerper.instance.Lerp(from, to, t);
  424. return;
  425. }
  426. // One of them is null, blend to/from a default value is applicable
  427. {
  428. if (defaultState == TextureParameterDefault.Lut2D)
  429. {
  430. int size = from != null ? from.height : to.height;
  431. Texture defaultTexture = RuntimeUtilities.GetLutStrip(size);
  432. if (from == null) from = defaultTexture;
  433. if (to == null) to = defaultTexture;
  434. }
  435. Color tgtColor;
  436. switch (defaultState)
  437. {
  438. case TextureParameterDefault.Black:
  439. tgtColor = Color.black;
  440. break;
  441. case TextureParameterDefault.White:
  442. tgtColor = Color.white;
  443. break;
  444. case TextureParameterDefault.Transparent:
  445. tgtColor = Color.clear;
  446. break;
  447. case TextureParameterDefault.Lut2D:
  448. {
  449. // Find the current lut size
  450. int size = from != null ? from.height : to.height;
  451. Texture defaultTexture = RuntimeUtilities.GetLutStrip(size);
  452. if (from == null) from = defaultTexture;
  453. if (to == null) to = defaultTexture;
  454. // Fail safe in case the lut size is incorrect
  455. if (from.width != to.width || from.height != to.height)
  456. {
  457. value = null;
  458. return;
  459. }
  460. value = TextureLerper.instance.Lerp(from, to, t);
  461. // All done, return
  462. return;
  463. }
  464. default:
  465. // defaultState is none, so just interpolate the base and return
  466. base.Interp(from, to, t);
  467. return;
  468. }
  469. // If we made it this far, tgtColor contains the color we'll be lerping into (or out of)
  470. if (from == null)
  471. {
  472. // color -> texture lerp, invert ratio
  473. value = TextureLerper.instance.Lerp(to, tgtColor, 1f - t);
  474. }
  475. else
  476. {
  477. value = TextureLerper.instance.Lerp(from, tgtColor, t);
  478. }
  479. }
  480. }
  481. }
  482. }