using System;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Linq;
namespace UnityEngine.Rendering.PostProcessing
{
///
/// The base class for all post-processing effect settings. Any
/// members found in this class will be automatically handled and interpolated by the volume
/// framework.
///
///
///
/// [Serializable]
/// [PostProcess(typeof(ExampleRenderer), "Custom/ExampleEffect")]
/// public sealed class ExampleEffect : PostProcessEffectSettings
/// {
/// [Range(0f, 1f), Tooltip("Effect intensity.")]
/// public FloatParameter intensity = new FloatParameter { value = 0f };
///
/// public override bool IsEnabledAndSupported(PostProcessRenderContext context)
/// {
/// return enabled.value
/// && intensity.value > 0f; // Only render the effect if intensity is greater than 0
/// }
/// }
///
///
[Serializable]
public class PostProcessEffectSettings : ScriptableObject
{
///
/// The active state of the set of parameter defined in this class.
///
///
public bool active = true;
///
/// The true state of the effect override in the stack. Setting this to false will
/// disable rendering for this effect assuming a volume with a higher priority doesn't
/// override it to true.
///
public BoolParameter enabled = new BoolParameter { overrideState = true, value = false };
internal ReadOnlyCollection parameters;
void OnEnable()
{
// Automatically grab all fields of type ParameterOverride for this instance
parameters = GetType()
.GetFields(BindingFlags.Public | BindingFlags.Instance)
.Where(t => t.FieldType.IsSubclassOf(typeof(ParameterOverride)))
.OrderBy(t => t.MetadataToken) // Guaranteed order
.Select(t => (ParameterOverride)t.GetValue(this))
.ToList()
.AsReadOnly();
foreach (var parameter in parameters)
parameter.OnEnable();
}
void OnDisable()
{
if (parameters == null)
return;
foreach (var parameter in parameters)
parameter.OnDisable();
}
///
/// Sets all the overrides for this effect to a given value.
///
/// The value to set the override states to
/// If false, the field will also
/// be set to the given value.
public void SetAllOverridesTo(bool state, bool excludeEnabled = true)
{
foreach (var prop in parameters)
{
if (excludeEnabled && prop == enabled)
continue;
prop.overrideState = state;
}
}
///
/// Returns true if the effect is currently enabled and supported.
///
/// The current post-processing render context
/// true if the effect is currently enabled and supported
public virtual bool IsEnabledAndSupported(PostProcessRenderContext context)
{
return enabled.value;
}
///
/// Returns the computed hash code for this parameter.
///
/// A computed hash code
public int GetHash()
{
// Custom hashing function used to compare the state of settings (it's not meant to be
// unique but to be a quick way to check if two setting sets have the same state or not).
// Hash collision rate should be pretty low.
unchecked
{
//return parameters.Aggregate(17, (i, p) => i * 23 + p.GetHash());
int hash = 17;
foreach (var p in parameters)
hash = hash * 23 + p.GetHash();
return hash;
}
}
}
}