3 namespace UnityEngine
.Rendering
.PostProcessing
6 /// The base abstract class for all parameter override types.
8 /// <seealso cref="ParameterOverride{T}"/>
9 public abstract class ParameterOverride
12 /// The override state of this parameter.
14 public bool overrideState
;
16 internal abstract void Interp(ParameterOverride
from, ParameterOverride to
, float t
);
19 /// Returns the computed hash code for this parameter.
21 /// <returns>A computed hash code</returns>
22 public abstract int GetHash();
25 /// Casts and returns the value stored in this parameter.
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;
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"/>).
41 /// <seealso cref="OnDisable"/>
42 protected internal virtual void OnEnable()
47 /// This method is called right before the parent <see cref="PostProcessEffectSettings"/>
48 /// gets de-initialized.
50 /// <seealso cref="OnEnable"/>
51 protected internal virtual void OnDisable()
55 internal abstract void SetValue(ParameterOverride parameter
);
59 /// The base typed class for all parameter override types.
61 /// <typeparam name="T">The type of value to store in this <c>ParameterOverride</c></typeparam>
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.
68 /// This sample code shows how to make a custom parameter holding a <c>float</c>.
71 /// public sealed class FloatParameter : ParameterOverride<float>
73 /// public override void Interp(float from, float to, float t)
75 /// value = from + (to - from) * t;
81 public class ParameterOverride
<T
> : ParameterOverride
84 /// The value stored in this parameter.
89 /// Creates a <c>ParameterOverride</c> with a default <see cref="value"/> and
90 /// <see cref="ParameterOverride.overrideState"/> set to <c>false</c>.
92 public ParameterOverride()
93 : this(default(T
), false)
98 /// Creates a <c>ParameterOverride</c> with a given value and
99 /// <see cref="ParameterOverride.overrideState"/> set to <c>false</c>.
101 /// <param name="value">The value to set this parameter to</param>
102 public ParameterOverride(T
value)
108 /// Creates a <c>ParameterOverride</c> with a given value and override state.
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
)
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
);
125 /// Interpolates between two values given an interpolation factor <paramref name="t"/>.
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>
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"/>
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
139 value = t
> 0f
? to
: from;
143 /// Sets the value for this parameter to <paramref name="x"/> and mark the override state
146 /// <param name="x"></param>
147 public void Override(T x
)
149 overrideState
= true;
153 internal override void SetValue(ParameterOverride parameter
)
155 value = parameter
.GetValue
<T
>();
159 public override int GetHash()
164 hash
= hash
* 23 + overrideState
.GetHashCode();
165 hash
= hash
* 23 + value.GetHashCode();
171 /// Implicit conversion between <see cref="ParameterOverride{T}"/> and its value type.
173 /// <param name="prop">The parameter to implicitly cast</param>
174 public static implicit operator T(ParameterOverride
<T
> prop
)
180 // Bypassing the limited unity serialization system...
183 /// A <see cref="ParameterOverride{T}"/> that holds a <c>float</c> value.
186 /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>.
189 public sealed class FloatParameter
: ParameterOverride
<float>
192 public override void Interp(float from, float to
, float t
)
194 value = from + (to
- from) * t
;
199 /// A <see cref="ParameterOverride{T}"/> that holds a <c>int</c> value.
202 /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>
203 /// casted to <c>int</c>.
206 public sealed class IntParameter
: ParameterOverride
<int>
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
);
218 /// A <see cref="ParameterOverride{T}"/> that holds a <c>bool</c> value.
221 public sealed class BoolParameter
: ParameterOverride
<bool> {}
224 /// A <see cref="ParameterOverride{T}"/> that holds a <see cref="Color"/> value.
227 /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>
228 /// for each channel.
231 public sealed class ColorParameter
: ParameterOverride
<Color
>
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
;
247 /// Implicit conversion between <see cref="ColorParameter"/> and a <see cref="Vector4"/>.
249 /// <param name="prop">The parameter to implicitly cast</param>
250 public static implicit operator Vector4(ColorParameter prop
)
257 /// A <see cref="ParameterOverride{T}"/> that holds a <see cref="Vector2"/> value.
260 /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>
264 public sealed class Vector2Parameter
: ParameterOverride
<Vector2
>
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
;
274 /// Implicit conversion between <see cref="Vector2Parameter"/> and a <see cref="Vector3"/>.
276 /// <param name="prop">The parameter to implicitly cast</param>
277 public static implicit operator Vector3(Vector2Parameter prop
)
283 /// Implicit conversion between <see cref="Vector2Parameter"/> and a <see cref="Vector4"/>.
285 /// <param name="prop">The parameter to implicitly cast</param>
286 public static implicit operator Vector4(Vector2Parameter prop
)
293 /// A <see cref="ParameterOverride{T}"/> that holds a <see cref="Vector3"/> value.
296 /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>
300 public sealed class Vector3Parameter
: ParameterOverride
<Vector3
>
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
;
311 /// Implicit conversion between <see cref="Vector3Parameter"/> and a <see cref="Vector2"/>.
313 /// <param name="prop">The parameter to implicitly cast</param>
314 public static implicit operator Vector2(Vector3Parameter prop
)
320 /// Implicit conversion between <see cref="Vector3Parameter"/> and a <see cref="Vector4"/>.
322 /// <param name="prop">The parameter to implicitly cast</param>
323 public static implicit operator Vector4(Vector3Parameter prop
)
330 /// A <see cref="ParameterOverride{T}"/> that holds a <see cref="Vector4"/> value.
333 /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>
337 public sealed class Vector4Parameter
: ParameterOverride
<Vector4
>
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
;
349 /// Implicit conversion between <see cref="Vector4Parameter"/> and a <see cref="Vector2"/>.
351 /// <param name="prop">The parameter to implicitly cast</param>
352 public static implicit operator Vector2(Vector4Parameter prop
)
358 /// Implicit conversion between <see cref="Vector4Parameter"/> and a <see cref="Vector3"/>.
360 /// <param name="prop">The parameter to implicitly cast</param>
361 public static implicit operator Vector3(Vector4Parameter prop
)
368 /// A <see cref="ParameterOverride{T}"/> that holds a <see cref="Spline"/> value.
371 /// The interpolation method for this parameter is the same as <see cref="Mathf.LerpUnclamped"/>
372 /// for each point on the curve.
375 public sealed class SplineParameter
: ParameterOverride
<Spline
>
378 protected internal override void OnEnable()
381 value.Cache(int.MinValue
);
384 internal override void SetValue(ParameterOverride parameter
)
386 base.SetValue(parameter
);
389 value.Cache(Time
.renderedFrameCount
);
393 public override void Interp(Spline
from, Spline to
, float t
)
395 if (from == null || to
== null)
397 base.Interp(from, to
, t
);
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
;
415 /// A set of default textures to use as default values for <see cref="TextureParameter"/>.
417 public enum TextureParameterDefault
420 /// No texture, or <c>null</c>.
435 /// A transparent texture.
440 /// A 2D lookup table in strip format with <c>width = height * height</c>.
446 /// A <see cref="ParameterOverride{T}"/> that holds a <see cref="Texture"/> value.
449 /// Texture interpolation is done using a classic linear interpolation method.
452 public sealed class TextureParameter
: ParameterOverride
<Texture
>
454 public TextureParameterDefault defaultState
= TextureParameterDefault
.Black
;
457 public override void Interp(Texture
from, Texture to
, float t
)
459 // Both are null, do nothing
460 if (from == null && to
== null)
466 // Both aren't null we're ready to blend
467 if (from != null && to
!= null)
469 value = TextureLerper
.instance
.Lerp(from, to
, t
);
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
;
486 switch (defaultState
)
488 case TextureParameterDefault
.Black
:
489 tgtColor
= Color
.black
;
491 case TextureParameterDefault
.White
:
492 tgtColor
= Color
.white
;
494 case TextureParameterDefault
.Transparent
:
495 tgtColor
= Color
.clear
;
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
)
512 value = TextureLerper
.instance
.Lerp(from, to
, t
);
517 // defaultState is none, so just interpolate the base and return
518 base.Interp(from, to
, t
);
521 // If we made it this far, tgtColor contains the color we'll be lerping into (or out of)
524 // color -> texture lerp, invert ratio
525 value = TextureLerper
.instance
.Lerp(to
, tgtColor
, 1f
- t
);
529 value = TextureLerper
.instance
.Lerp(from, tgtColor
, t
);