tree sway and shadow improvements
[WindSway-HDRP.git] / Library / PackageCache / com.unity.postprocessing@2.1.2 / PostProcessing / Runtime / ParameterOverride.cs
blob164be7c991de1b7208ec49b520077e05558f7ae0
1 using System;
3 namespace UnityEngine.Rendering.PostProcessing
5 /// <summary>
6 /// The base abstract class for all parameter override types.
7 /// </summary>
8 /// <seealso cref="ParameterOverride{T}"/>
9 public abstract class ParameterOverride
11 /// <summary>
12 /// The override state of this parameter.
13 /// </summary>
14 public bool overrideState;
16 internal abstract void Interp(ParameterOverride from, ParameterOverride to, float t);
18 /// <summary>
19 /// Returns the computed hash code for this parameter.
20 /// </summary>
21 /// <returns>A computed hash code</returns>
22 public abstract int GetHash();
24 /// <summary>
25 /// Casts and returns the value stored in this parameter.
26 /// </summary>
27 /// <typeparam name="T">The type to cast to</typeparam>
28 /// <returns>The value stored in this parameter</returns>
29 public T GetValue<T>()
31 return ((ParameterOverride<T>)this).value;
34 /// <summary>
35 /// This method is called right after the parent <see cref="PostProcessEffectSettings"/> has
36 /// been initialized. This is used in case you need to access fields or properties that
37 /// can't be accessed in the constructor of a <see cref="ScriptableObject"/>
38 /// (<c>ParameterOverride</c> objects are generally declared and initialized in a
39 /// <see cref="PostProcessEffectSettings"/>).
40 /// </summary>
41 /// <seealso cref="OnDisable"/>
42 protected internal virtual void OnEnable()
46 /// <summary>
47 /// This method is called right before the parent <see cref="PostProcessEffectSettings"/>
48 /// gets de-initialized.
49 /// </summary>
50 /// <seealso cref="OnEnable"/>
51 protected internal virtual void OnDisable()
55 internal abstract void SetValue(ParameterOverride parameter);
58 /// <summary>
59 /// The base typed class for all parameter override types.
60 /// </summary>
61 /// <typeparam name="T">The type of value to store in this <c>ParameterOverride</c></typeparam>
62 /// <remarks>
63 /// Due to limitations with the serialization system in Unity you shouldn't use this class
64 /// directly. Use one of the pre-flatten types (like <see cref="FloatParameter"/> or make your
65 /// own by extending this class.
66 /// </remarks>
67 /// <example>
68 /// This sample code shows how to make a custom parameter holding a <c>float</c>.
69 /// <code>
70 /// [Serializable]
71 /// public sealed class FloatParameter : ParameterOverride&lt;float&gt;
72 /// {
73 /// public override void Interp(float from, float to, float t)
74 /// {
75 /// value = from + (to - from) * t;
76 /// }
77 /// }
78 /// </code>
79 /// </example>
80 [Serializable]
81 public class ParameterOverride<T> : ParameterOverride
83 /// <summary>
84 /// The value stored in this parameter.
85 /// </summary>
86 public T value;
88 /// <summary>
89 /// Creates a <c>ParameterOverride</c> with a default <see cref="value"/> and
90 /// <see cref="ParameterOverride.overrideState"/> set to <c>false</c>.
91 /// </summary>
92 public ParameterOverride()
93 : this(default(T), false)
97 /// <summary>
98 /// Creates a <c>ParameterOverride</c> with a given value and
99 /// <see cref="ParameterOverride.overrideState"/> set to <c>false</c>.
100 /// </summary>
101 /// <param name="value">The value to set this parameter to</param>
102 public ParameterOverride(T value)
103 : this(value, false)
107 /// <summary>
108 /// Creates a <c>ParameterOverride</c> with a given value and override state.
109 /// </summary>
110 /// <param name="value">The value to set this parameter to</param>
111 /// <param name="overrideState">The override state for this value</param>
112 public ParameterOverride(T value, bool overrideState)
114 this.value = value;
115 this.overrideState = overrideState;
118 internal override void Interp(ParameterOverride from, ParameterOverride to, float t)
120 // Note: this isn't completely safe but it'll do fine
121 Interp(from.GetValue<T>(), to.GetValue<T>(), t);
124 /// <summary>
125 /// Interpolates between two values given an interpolation factor <paramref name="t"/>.
126 /// </summary>
127 /// <param name="from">The value to interpolate from</param>
128 /// <param name="to">The value to interpolate to</param>
129 /// <param name="t">An interpolation factor (generally in range <c>[0,1]</c>)</param>
130 /// <remarks>
131 /// By default this method does a "snap" interpolation, meaning it will return the value
132 /// <paramref name="to"/> if <paramref name="t"/> is higher than 0, <paramref name="from"/>
133 /// otherwise.
134 /// </remarks>
135 public virtual void Interp(T from, T to, float t)
137 // Returns `to` if `dt > 0` by default so we don't have to write overrides for bools and
138 // enumerations.
139 value = t > 0f ? to : from;
142 /// <summary>
143 /// Sets the value for this parameter to <paramref name="x"/> and mark the override state
144 /// to <c>true</c>.
145 /// </summary>
146 /// <param name="x"></param>
147 public void Override(T x)
149 overrideState = true;
150 value = x;
153 internal override void SetValue(ParameterOverride parameter)
155 value = parameter.GetValue<T>();
158 /// <inheritdoc />
159 public override int GetHash()
161 unchecked
163 int hash = 17;
164 hash = hash * 23 + overrideState.GetHashCode();
165 hash = hash * 23 + value.GetHashCode();
166 return hash;
170 /// <summary>
171 /// Implicit conversion between <see cref="ParameterOverride{T}"/> and its value type.
172 /// </summary>
173 /// <param name="prop">The parameter to implicitly cast</param>
174 public static implicit operator T(ParameterOverride<T> prop)
176 return prop.value;
180 // Bypassing the limited unity serialization system...
182 /// <summary>
183 /// A <see cref="ParameterOverride{T}"/> that holds a <c>float</c> value.
184 /// </summary>
185 /// <remarks>
186 /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>.
187 /// </remarks>
188 [Serializable]
189 public sealed class FloatParameter : ParameterOverride<float>
191 /// <inheritdoc />
192 public override void Interp(float from, float to, float t)
194 value = from + (to - from) * t;
198 /// <summary>
199 /// A <see cref="ParameterOverride{T}"/> that holds a <c>int</c> value.
200 /// </summary>
201 /// <remarks>
202 /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>
203 /// casted to <c>int</c>.
204 /// </remarks>
205 [Serializable]
206 public sealed class IntParameter : ParameterOverride<int>
208 /// <inheritdoc />
209 public override void Interp(int from, int to, float t)
211 // Int snapping interpolation. Don't use this for enums as they don't necessarily have
212 // contiguous values. Use the default interpolator instead (same as bool).
213 value = (int)(from + (to - from) * t);
217 /// <summary>
218 /// A <see cref="ParameterOverride{T}"/> that holds a <c>bool</c> value.
219 /// </summary>
220 [Serializable]
221 public sealed class BoolParameter : ParameterOverride<bool> {}
223 /// <summary>
224 /// A <see cref="ParameterOverride{T}"/> that holds a <see cref="Color"/> value.
225 /// </summary>
226 /// <remarks>
227 /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>
228 /// for each channel.
229 /// </remarks>
230 [Serializable]
231 public sealed class ColorParameter : ParameterOverride<Color>
233 /// <inheritdoc />
234 public override void Interp(Color from, Color to, float t)
236 // Lerping color values is a sensitive subject... We looked into lerping colors using
237 // HSV and LCH but they have some downsides that make them not work correctly in all
238 // situations, so we stick with RGB lerping for now, at least its behavior is
239 // predictable despite looking desaturated when `t ~= 0.5` and it's faster anyway.
240 value.r = from.r + (to.r - from.r) * t;
241 value.g = from.g + (to.g - from.g) * t;
242 value.b = from.b + (to.b - from.b) * t;
243 value.a = from.a + (to.a - from.a) * t;
246 /// <summary>
247 /// Implicit conversion between <see cref="ColorParameter"/> and a <see cref="Vector4"/>.
248 /// </summary>
249 /// <param name="prop">The parameter to implicitly cast</param>
250 public static implicit operator Vector4(ColorParameter prop)
252 return prop.value;
256 /// <summary>
257 /// A <see cref="ParameterOverride{T}"/> that holds a <see cref="Vector2"/> value.
258 /// </summary>
259 /// <remarks>
260 /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>
261 /// for each axis.
262 /// </remarks>
263 [Serializable]
264 public sealed class Vector2Parameter : ParameterOverride<Vector2>
266 /// <inheritdoc />
267 public override void Interp(Vector2 from, Vector2 to, float t)
269 value.x = from.x + (to.x - from.x) * t;
270 value.y = from.y + (to.y - from.y) * t;
273 /// <summary>
274 /// Implicit conversion between <see cref="Vector2Parameter"/> and a <see cref="Vector3"/>.
275 /// </summary>
276 /// <param name="prop">The parameter to implicitly cast</param>
277 public static implicit operator Vector3(Vector2Parameter prop)
279 return prop.value;
282 /// <summary>
283 /// Implicit conversion between <see cref="Vector2Parameter"/> and a <see cref="Vector4"/>.
284 /// </summary>
285 /// <param name="prop">The parameter to implicitly cast</param>
286 public static implicit operator Vector4(Vector2Parameter prop)
288 return prop.value;
292 /// <summary>
293 /// A <see cref="ParameterOverride{T}"/> that holds a <see cref="Vector3"/> value.
294 /// </summary>
295 /// <remarks>
296 /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>
297 /// for each axis.
298 /// </remarks>
299 [Serializable]
300 public sealed class Vector3Parameter : ParameterOverride<Vector3>
302 /// <inheritdoc />
303 public override void Interp(Vector3 from, Vector3 to, float t)
305 value.x = from.x + (to.x - from.x) * t;
306 value.y = from.y + (to.y - from.y) * t;
307 value.z = from.z + (to.z - from.z) * t;
310 /// <summary>
311 /// Implicit conversion between <see cref="Vector3Parameter"/> and a <see cref="Vector2"/>.
312 /// </summary>
313 /// <param name="prop">The parameter to implicitly cast</param>
314 public static implicit operator Vector2(Vector3Parameter prop)
316 return prop.value;
319 /// <summary>
320 /// Implicit conversion between <see cref="Vector3Parameter"/> and a <see cref="Vector4"/>.
321 /// </summary>
322 /// <param name="prop">The parameter to implicitly cast</param>
323 public static implicit operator Vector4(Vector3Parameter prop)
325 return prop.value;
329 /// <summary>
330 /// A <see cref="ParameterOverride{T}"/> that holds a <see cref="Vector4"/> value.
331 /// </summary>
332 /// <remarks>
333 /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>
334 /// for each axis.
335 /// </remarks>
336 [Serializable]
337 public sealed class Vector4Parameter : ParameterOverride<Vector4>
339 /// <inheritdoc />
340 public override void Interp(Vector4 from, Vector4 to, float t)
342 value.x = from.x + (to.x - from.x) * t;
343 value.y = from.y + (to.y - from.y) * t;
344 value.z = from.z + (to.z - from.z) * t;
345 value.w = from.w + (to.w - from.w) * t;
348 /// <summary>
349 /// Implicit conversion between <see cref="Vector4Parameter"/> and a <see cref="Vector2"/>.
350 /// </summary>
351 /// <param name="prop">The parameter to implicitly cast</param>
352 public static implicit operator Vector2(Vector4Parameter prop)
354 return prop.value;
357 /// <summary>
358 /// Implicit conversion between <see cref="Vector4Parameter"/> and a <see cref="Vector3"/>.
359 /// </summary>
360 /// <param name="prop">The parameter to implicitly cast</param>
361 public static implicit operator Vector3(Vector4Parameter prop)
363 return prop.value;
367 /// <summary>
368 /// A <see cref="ParameterOverride{T}"/> that holds a <see cref="Spline"/> value.
369 /// </summary>
370 /// <remarks>
371 /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>
372 /// for each point on the curve.
373 /// </remarks>
374 [Serializable]
375 public sealed class SplineParameter : ParameterOverride<Spline>
377 /// <inheritdoc />
378 protected internal override void OnEnable()
380 if (value != null)
381 value.Cache(int.MinValue);
384 internal override void SetValue(ParameterOverride parameter)
386 base.SetValue(parameter);
388 if (value != null)
389 value.Cache(Time.renderedFrameCount);
392 /// <inheritdoc />
393 public override void Interp(Spline from, Spline to, float t)
395 if (from == null || to == null)
397 base.Interp(from, to, t);
398 return;
401 int frameCount = Time.renderedFrameCount;
402 from.Cache(frameCount);
403 to.Cache(frameCount);
405 for (int i = 0; i < Spline.k_Precision; i++)
407 float a = from.cachedData[i];
408 float b = to.cachedData[i];
409 value.cachedData[i] = a + (b - a) * t;
414 /// <summary>
415 /// A set of default textures to use as default values for <see cref="TextureParameter"/>.
416 /// </summary>
417 public enum TextureParameterDefault
419 /// <summary>
420 /// No texture, or <c>null</c>.
421 /// </summary>
422 None,
424 /// <summary>
425 /// A black texture.
426 /// </summary>
427 Black,
429 /// <summary>
430 /// A white texture.
431 /// </summary>
432 White,
434 /// <summary>
435 /// A transparent texture.
436 /// </summary>
437 Transparent,
439 /// <summary>
440 /// A 2D lookup table in strip format with <c>width = height * height</c>.
441 /// </summary>
442 Lut2D
445 /// <summary>
446 /// A <see cref="ParameterOverride{T}"/> that holds a <see cref="Texture"/> value.
447 /// </summary>
448 /// <remarks>
449 /// Texture interpolation is done using a classic linear interpolation method.
450 /// </remarks>
451 [Serializable]
452 public sealed class TextureParameter : ParameterOverride<Texture>
454 public TextureParameterDefault defaultState = TextureParameterDefault.Black;
456 /// <inheritdoc />
457 public override void Interp(Texture from, Texture to, float t)
459 // Both are null, do nothing
460 if (from == null && to == null)
462 value = null;
463 return;
466 // Both aren't null we're ready to blend
467 if (from != null && to != null)
469 value = TextureLerper.instance.Lerp(from, to, t);
470 return;
473 // One of them is null, blend to/from a default value is applicable
475 if (defaultState == TextureParameterDefault.Lut2D)
477 int size = from != null ? from.height : to.height;
478 Texture defaultTexture = RuntimeUtilities.GetLutStrip(size);
480 if (from == null) from = defaultTexture;
481 if (to == null) to = defaultTexture;
484 Color tgtColor;
486 switch (defaultState)
488 case TextureParameterDefault.Black:
489 tgtColor = Color.black;
490 break;
491 case TextureParameterDefault.White:
492 tgtColor = Color.white;
493 break;
494 case TextureParameterDefault.Transparent:
495 tgtColor = Color.clear;
496 break;
497 case TextureParameterDefault.Lut2D:
499 // Find the current lut size
500 int size = from != null ? from.height : to.height;
501 Texture defaultTexture = RuntimeUtilities.GetLutStrip(size);
502 if (from == null) from = defaultTexture;
503 if (to == null) to = defaultTexture;
505 // Fail safe in case the lut size is incorrect
506 if (from.width != to.width || from.height != to.height)
508 value = null;
509 return;
512 value = TextureLerper.instance.Lerp(from, to, t);
513 // All done, return
514 return;
516 default:
517 // defaultState is none, so just interpolate the base and return
518 base.Interp(from, to, t);
519 return;
521 // If we made it this far, tgtColor contains the color we'll be lerping into (or out of)
522 if (from == null)
524 // color -> texture lerp, invert ratio
525 value = TextureLerper.instance.Lerp(to, tgtColor, 1f - t);
527 else
529 value = TextureLerper.instance.Lerp(from, tgtColor, t);