3 namespace UnityEngine
.Rendering
.PostProcessing
6 /// Ambient occlusion modes.
8 public enum AmbientOcclusionMode
11 /// A standard implementation of ambient obscurance that works on non modern platforms. If
12 /// you target a compute-enabled platform we recommend that you use
13 /// <see cref="MultiScaleVolumetricObscurance"/> instead.
15 ScalableAmbientObscurance
,
18 /// A modern version of ambient occlusion heavily optimized for consoles and desktop
21 MultiScaleVolumetricObscurance
25 /// Quality settings for <see cref="AmbientOcclusionMode.ScalableAmbientObscurance"/>.
27 public enum AmbientOcclusionQuality
30 /// 4 samples + downsampling.
35 /// 6 samples + downsampling.
40 /// 10 samples + downsampling.
56 /// A volume parameter holding a <see cref="AmbientOcclusionMode"/> value.
59 public sealed class AmbientOcclusionModeParameter
: ParameterOverride
<AmbientOcclusionMode
> {}
62 /// A volume parameter holding a <see cref="AmbientOcclusionQuality"/> value.
65 public sealed class AmbientOcclusionQualityParameter
: ParameterOverride
<AmbientOcclusionQuality
> {}
68 /// This class holds settings for the Ambient Occlusion effect.
71 [PostProcess(typeof(AmbientOcclusionRenderer
), "Unity/Ambient Occlusion")]
72 public sealed class AmbientOcclusion
: PostProcessEffectSettings
77 /// The ambient occlusion method to use.
79 [Tooltip("The ambient occlusion method to use. \"Multi Scale Volumetric Obscurance\" is higher quality and faster on desktop & console platforms but requires compute shader support.")]
81 public AmbientOcclusionModeParameter mode
= new AmbientOcclusionModeParameter { value = AmbientOcclusionMode.MultiScaleVolumetricObscurance }
;
84 /// The degree of darkness added by ambient occlusion.
86 [Range(0f
, 4f
), Tooltip("The degree of darkness added by ambient occlusion. Higher values produce darker areas.")]
87 public FloatParameter intensity
= new FloatParameter { value = 0f }
;
90 /// A custom color to use for the ambient occlusion.
92 [ColorUsage(false), Tooltip("The custom color to use for the ambient occlusion. The default is black.")]
94 public ColorParameter color
= new ColorParameter { value = Color.black }
;
97 /// Only affects ambient lighting. This mode is only available with the Deferred rendering
98 /// path and HDR rendering. Objects rendered with the Forward rendering path won't get any
99 /// ambient occlusion.
101 [Tooltip("Check this box to mark this Volume as to only affect ambient lighting. This mode is only available with the Deferred rendering path and HDR rendering. Objects rendered with the Forward rendering path won't get any ambient occlusion.")]
102 public BoolParameter ambientOnly
= new BoolParameter { value = true }
;
104 // MSVO-only parameters
107 /// The tolerance of the noise filter to changes in the depth pyramid.
110 public FloatParameter noiseFilterTolerance
= new FloatParameter { value = 0f }
; // Hidden
113 /// The tolerance of the bilateral blur filter to depth changes.
116 public FloatParameter blurTolerance
= new FloatParameter { value = -4.6f }
; // Hidden
119 /// The tolerance of the upsampling pass to depth changes.
122 public FloatParameter upsampleTolerance
= new FloatParameter { value = -12f }
; // Hidden
125 /// Modifies the thickness of occluders. This increases dark areas but also introduces dark
126 /// halo around objects.
128 [Range(1f
, 10f
), Tooltip("This modifies the thickness of occluders. It increases the size of dark areas and also introduces a dark halo around objects.")]
129 public FloatParameter thicknessModifier
= new FloatParameter { value = 1f }
;
131 // HDRP-only parameters
134 /// Modifies he influence of direct lighting on ambient occlusion. This is only used in the
135 /// HD Render Pipeline currently.
137 [Range(0f
, 1f
), Tooltip("Modifies the influence of direct lighting on ambient occlusion.")]
138 public FloatParameter directLightingStrength
= new FloatParameter { value = 0f }
;
140 // SAO-only parameters
142 /// Radius of sample points, which affects extent of darkened areas.
144 [Tooltip("The radius of sample points. This affects the size of darkened areas.")]
145 public FloatParameter radius
= new FloatParameter { value = 0.25f }
;
148 /// The number of sample points, which affects quality and performance. Lowest, Low & Medium
149 /// passes are downsampled. High and Ultra are not and should only be used on high-end
152 [Tooltip("The number of sample points. This affects both quality and performance. For \"Lowest\", \"Low\", and \"Medium\", passes are downsampled. For \"High\" and \"Ultra\", they are not and therefore you should only \"High\" and \"Ultra\" on high-end hardware.")]
153 public AmbientOcclusionQualityParameter quality
= new AmbientOcclusionQualityParameter { value = AmbientOcclusionQuality.Medium }
;
155 // SRPs can call this method without a context set (see HDRP).
156 // We need a better way to handle this than checking for a null context, context should
159 public override bool IsEnabledAndSupported(PostProcessRenderContext context
)
161 bool state
= enabled
.value
162 && intensity
.value > 0f
;
164 if (mode
.value == AmbientOcclusionMode
.ScalableAmbientObscurance
)
166 state
&= !RuntimeUtilities
.scriptableRenderPipelineActive
;
170 state
&= context
.resources
.shaders
.scalableAO
171 && context
.resources
.shaders
.scalableAO
.isSupported
;
174 else if (mode
.value == AmbientOcclusionMode
.MultiScaleVolumetricObscurance
)
176 #if UNITY_2017_1_OR_NEWER
179 state
&= context
.resources
.shaders
.multiScaleAO
180 && context
.resources
.shaders
.multiScaleAO
.isSupported
181 && context
.resources
.computeShaders
.multiScaleAODownsample1
182 && context
.resources
.computeShaders
.multiScaleAODownsample2
183 && context
.resources
.computeShaders
.multiScaleAORender
184 && context
.resources
.computeShaders
.multiScaleAOUpsample
;
187 state
&= SystemInfo
.supportsComputeShaders
188 && !RuntimeUtilities
.isAndroidOpenGL
189 && RenderTextureFormat
.RFloat
.IsSupported()
190 && RenderTextureFormat
.RHalf
.IsSupported()
191 && RenderTextureFormat
.R8
.IsSupported();
201 internal interface IAmbientOcclusionMethod
203 DepthTextureMode
GetCameraFlags();
204 void RenderAfterOpaque(PostProcessRenderContext context
);
205 void RenderAmbientOnly(PostProcessRenderContext context
);
206 void CompositeAmbientOnly(PostProcessRenderContext context
);
210 #if UNITY_2017_1_OR_NEWER
211 [UnityEngine
.Scripting
.Preserve
]
213 internal sealed class AmbientOcclusionRenderer
: PostProcessEffectRenderer
<AmbientOcclusion
>
215 IAmbientOcclusionMethod
[] m_Methods
;
217 public override void Init()
219 if (m_Methods
== null)
221 m_Methods
= new IAmbientOcclusionMethod
[]
223 new ScalableAO(settings
),
224 new MultiScaleVO(settings
),
229 public bool IsAmbientOnly(PostProcessRenderContext context
)
231 var camera
= context
.camera
;
232 return settings
.ambientOnly
.value
233 && camera
.actualRenderingPath
== RenderingPath
.DeferredShading
237 public IAmbientOcclusionMethod
Get()
239 return m_Methods
[(int)settings
.mode
.value];
242 public override DepthTextureMode
GetCameraFlags()
244 return Get().GetCameraFlags();
247 public override void Release()
249 foreach (var m
in m_Methods
)
253 public ScalableAO
GetScalableAO()
255 return (ScalableAO
)m_Methods
[(int)AmbientOcclusionMode
.ScalableAmbientObscurance
];
258 public MultiScaleVO
GetMultiScaleVO()
260 return (MultiScaleVO
)m_Methods
[(int)AmbientOcclusionMode
.MultiScaleVolumetricObscurance
];
264 public override void Render(PostProcessRenderContext context
)