|
2 | 2 |
|
3 | 3 | namespace UnityEngine.Rendering.PostProcessing |
4 | 4 | { |
5 | | - public interface IAmbientOcclusionMethod |
| 5 | + public enum AmbientOcclusionMode |
6 | 6 | { |
7 | | - DepthTextureMode GetCameraFlags(); |
8 | | - bool IsSupported(PostProcessRenderContext context); |
9 | | - void RenderAfterOpaque(PostProcessRenderContext context); |
10 | | - void RenderAmbientOnly(PostProcessRenderContext context); |
11 | | - void CompositeAmbientOnly(PostProcessRenderContext context); |
12 | | - void Release(); |
| 7 | + ScalableAmbientObscurance, |
| 8 | + MultiScaleVolumetricObscurance |
13 | 9 | } |
14 | 10 |
|
| 11 | + public enum AmbientOcclusionQuality |
| 12 | + { |
| 13 | + Lowest, |
| 14 | + Low, |
| 15 | + Medium, |
| 16 | + High, |
| 17 | + Ultra |
| 18 | + } |
| 19 | + |
| 20 | + [Serializable] |
| 21 | + public sealed class AmbientOcclusionModeParameter : ParameterOverride<AmbientOcclusionMode> {} |
| 22 | + |
| 23 | + [Serializable] |
| 24 | + public sealed class AmbientOcclusionQualityParameter : ParameterOverride<AmbientOcclusionQuality> {} |
| 25 | + |
15 | 26 | [Serializable] |
16 | | - public sealed class AmbientOcclusion |
| 27 | + [PostProcess(typeof(AmbientOcclusionRenderer), "Unity/Ambient Occlusion")] |
| 28 | + public sealed class AmbientOcclusion : PostProcessEffectSettings |
17 | 29 | { |
18 | | - public enum Mode |
19 | | - { |
20 | | - SAO, |
21 | | - MSVO |
22 | | - } |
| 30 | + // Shared parameters |
| 31 | + [Tooltip("The ambient occlusion method to use. \"Modern\" is higher quality and faster on desktop & console platforms but requires compute shader support.")] |
| 32 | + public AmbientOcclusionModeParameter mode = new AmbientOcclusionModeParameter { value = AmbientOcclusionMode.MultiScaleVolumetricObscurance }; |
23 | 33 |
|
24 | | - [Tooltip("Enables ambient occlusion.")] |
25 | | - public bool enabled = false; |
| 34 | + [Range(0f, 4f), Tooltip("Degree of darkness added by ambient occlusion.")] |
| 35 | + public FloatParameter intensity = new FloatParameter { value = 0f }; |
26 | 36 |
|
27 | | - public Mode mode = Mode.MSVO; |
| 37 | + [ColorUsage(false), Tooltip("Custom color to use for the ambient occlusion.")] |
| 38 | + public ColorParameter color = new ColorParameter { value = Color.black }; |
28 | 39 |
|
29 | 40 | [Tooltip("Only affects 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.")] |
30 | | - public bool ambientOnly = false; |
| 41 | + public BoolParameter ambientOnly = new BoolParameter { value = true }; |
31 | 42 |
|
32 | | - // Polymorphism doesn't play well with serialization in Unity so we have to keep explicit |
33 | | - // references... Would be nice to have this more dynamic to allow user-custom AO methods. |
34 | | - public ScalableAO scalableAO; |
35 | | - public MultiScaleVO multiScaleVO; |
| 43 | + // MSVO-only parameters |
| 44 | + [Range(-8f, 0f)] |
| 45 | + public FloatParameter noiseFilterTolerance = new FloatParameter { value = 0f }; // Hidden |
36 | 46 |
|
37 | | - IAmbientOcclusionMethod[] m_Methods; |
| 47 | + [Range(-8f, -1f)] |
| 48 | + public FloatParameter blurTolerance = new FloatParameter { value = -4.6f }; // Hidden |
| 49 | + |
| 50 | + [Range(-12f, -1f)] |
| 51 | + public FloatParameter upsampleTolerance = new FloatParameter { value = -12f }; // Hidden |
| 52 | + |
| 53 | + [Range(1f, 10f), Tooltip("Modifies thickness of occluders. This increases dark areas but also introduces dark halo around objects.")] |
| 54 | + public FloatParameter thicknessModifier = new FloatParameter { value = 1f }; |
| 55 | + |
| 56 | + // SAO-only parameters |
| 57 | + [Tooltip("Radius of sample points, which affects extent of darkened areas.")] |
| 58 | + public FloatParameter radius = new FloatParameter { value = 0.25f }; |
| 59 | + |
| 60 | + [Tooltip("Number of sample points, which affects quality and performance. Lowest, Low & Medium passes are downsampled. High and Ultra are not and should only be used on high-end hardware.")] |
| 61 | + public AmbientOcclusionQualityParameter quality = new AmbientOcclusionQualityParameter { value = AmbientOcclusionQuality.Medium }; |
38 | 62 |
|
39 | | - public AmbientOcclusion() |
| 63 | + public override bool IsEnabledAndSupported(PostProcessRenderContext context) |
40 | 64 | { |
41 | | - if (scalableAO == null) scalableAO = new ScalableAO(); |
42 | | - if (multiScaleVO == null) multiScaleVO = new MultiScaleVO(); |
| 65 | + bool state = enabled.value |
| 66 | + && intensity.value > 0f |
| 67 | + && !RuntimeUtilities.scriptableRenderPipelineActive; |
43 | 68 |
|
44 | | - m_Methods = new IAmbientOcclusionMethod[] { scalableAO, multiScaleVO }; |
| 69 | + if (mode.value == AmbientOcclusionMode.MultiScaleVolumetricObscurance) |
| 70 | + state &= SystemInfo.supportsComputeShaders && SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.RFloat); |
| 71 | + |
| 72 | + return state; |
45 | 73 | } |
| 74 | + } |
| 75 | + |
| 76 | + public interface IAmbientOcclusionMethod |
| 77 | + { |
| 78 | + DepthTextureMode GetCameraFlags(); |
| 79 | + void RenderAfterOpaque(PostProcessRenderContext context); |
| 80 | + void RenderAmbientOnly(PostProcessRenderContext context); |
| 81 | + void CompositeAmbientOnly(PostProcessRenderContext context); |
| 82 | + void Release(); |
| 83 | + } |
46 | 84 |
|
47 | | - public bool IsEnabledAndSupported(PostProcessRenderContext context) |
| 85 | + public sealed class AmbientOcclusionRenderer : PostProcessEffectRenderer<AmbientOcclusion> |
| 86 | + { |
| 87 | + IAmbientOcclusionMethod[] m_Methods; |
| 88 | + |
| 89 | + public override void Init() |
48 | 90 | { |
49 | | - return enabled && Get().IsSupported(context); |
| 91 | + if (m_Methods == null) |
| 92 | + { |
| 93 | + m_Methods = new IAmbientOcclusionMethod[] |
| 94 | + { |
| 95 | + new ScalableAO(settings), |
| 96 | + new MultiScaleVO(settings), |
| 97 | + }; |
| 98 | + } |
50 | 99 | } |
51 | 100 |
|
52 | 101 | public bool IsAmbientOnly(PostProcessRenderContext context) |
53 | 102 | { |
54 | 103 | var camera = context.camera; |
55 | | - return ambientOnly |
| 104 | + return settings.ambientOnly.value |
56 | 105 | && camera.actualRenderingPath == RenderingPath.DeferredShading |
57 | 106 | && camera.allowHDR; |
58 | 107 | } |
59 | 108 |
|
60 | 109 | public IAmbientOcclusionMethod Get() |
61 | 110 | { |
62 | | - return m_Methods[(int)mode]; |
| 111 | + return m_Methods[(int)settings.mode.value]; |
| 112 | + } |
| 113 | + |
| 114 | + public override DepthTextureMode GetCameraFlags() |
| 115 | + { |
| 116 | + return Get().GetCameraFlags(); |
63 | 117 | } |
64 | 118 |
|
65 | | - public void Release() |
| 119 | + public override void Release() |
66 | 120 | { |
67 | 121 | foreach (var m in m_Methods) |
68 | 122 | m.Release(); |
69 | 123 | } |
| 124 | + |
| 125 | + // Unused |
| 126 | + public override void Render(PostProcessRenderContext context) |
| 127 | + { |
| 128 | + } |
70 | 129 | } |
71 | 130 | } |
0 commit comments