PostProcessEffectSettings.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Reflection;
  4. using System.Linq;
  5. namespace UnityEngine.Rendering.PostProcessing
  6. {
  7. /// <summary>
  8. /// The base class for all post-processing effect settings. Any <see cref="ParameterOverride"/>
  9. /// members found in this class will be automatically handled and interpolated by the volume
  10. /// framework.
  11. /// </summary>
  12. /// <example>
  13. /// <code>
  14. /// [Serializable]
  15. /// [PostProcess(typeof(ExampleRenderer), "Custom/ExampleEffect")]
  16. /// public sealed class ExampleEffect : PostProcessEffectSettings
  17. /// {
  18. /// [Range(0f, 1f), Tooltip("Effect intensity.")]
  19. /// public FloatParameter intensity = new FloatParameter { value = 0f };
  20. ///
  21. /// public override bool IsEnabledAndSupported(PostProcessRenderContext context)
  22. /// {
  23. /// return enabled.value
  24. /// &amp;&amp; intensity.value > 0f; // Only render the effect if intensity is greater than 0
  25. /// }
  26. /// }
  27. /// </code>
  28. /// </example>
  29. [Serializable]
  30. public class PostProcessEffectSettings : ScriptableObject
  31. {
  32. /// <summary>
  33. /// The active state of the set of parameter defined in this class.
  34. /// </summary>
  35. /// <seealso cref="enabled"/>
  36. public bool active = true;
  37. /// <summary>
  38. /// The true state of the effect override in the stack. Setting this to <c>false</c> will
  39. /// disable rendering for this effect assuming a volume with a higher priority doesn't
  40. /// override it to <c>true</c>.
  41. /// </summary>
  42. public BoolParameter enabled = new BoolParameter { overrideState = true, value = false };
  43. internal ReadOnlyCollection<ParameterOverride> parameters;
  44. void OnEnable()
  45. {
  46. // Automatically grab all fields of type ParameterOverride for this instance
  47. parameters = GetType()
  48. .GetFields(BindingFlags.Public | BindingFlags.Instance)
  49. .Where(t => t.FieldType.IsSubclassOf(typeof(ParameterOverride)))
  50. .OrderBy(t => t.MetadataToken) // Guaranteed order
  51. .Select(t => (ParameterOverride)t.GetValue(this))
  52. .ToList()
  53. .AsReadOnly();
  54. foreach (var parameter in parameters)
  55. parameter.OnEnable();
  56. }
  57. void OnDisable()
  58. {
  59. if (parameters == null)
  60. return;
  61. foreach (var parameter in parameters)
  62. parameter.OnDisable();
  63. }
  64. /// <summary>
  65. /// Sets all the overrides for this effect to a given value.
  66. /// </summary>
  67. /// <param name="state">The value to set the override states to</param>
  68. /// <param name="excludeEnabled">If <c>false</c>, the <see cref="enabled"/> field will also
  69. /// be set to the given <see cref="state"/> value.</param>
  70. public void SetAllOverridesTo(bool state, bool excludeEnabled = true)
  71. {
  72. foreach (var prop in parameters)
  73. {
  74. if (excludeEnabled && prop == enabled)
  75. continue;
  76. prop.overrideState = state;
  77. }
  78. }
  79. /// <summary>
  80. /// Returns <c>true</c> if the effect is currently enabled and supported.
  81. /// </summary>
  82. /// <param name="context">The current post-processing render context</param>
  83. /// <returns><c>true</c> if the effect is currently enabled and supported</returns>
  84. public virtual bool IsEnabledAndSupported(PostProcessRenderContext context)
  85. {
  86. return enabled.value;
  87. }
  88. /// <summary>
  89. /// Returns the computed hash code for this parameter.
  90. /// </summary>
  91. /// <returns>A computed hash code</returns>
  92. public int GetHash()
  93. {
  94. // Custom hashing function used to compare the state of settings (it's not meant to be
  95. // unique but to be a quick way to check if two setting sets have the same state or not).
  96. // Hash collision rate should be pretty low.
  97. unchecked
  98. {
  99. //return parameters.Aggregate(17, (i, p) => i * 23 + p.GetHash());
  100. int hash = 17;
  101. foreach (var p in parameters)
  102. hash = hash * 23 + p.GetHash();
  103. return hash;
  104. }
  105. }
  106. }
  107. }