Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit bf47336

Browse files
committed
Added AO color support and fixed more bugs
1 parent d2a7f08 commit bf47336

File tree

6 files changed

+45
-46
lines changed

6 files changed

+45
-46
lines changed

PostProcessing/Editor/PostProcessLayerEditor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ public sealed class PostProcessLayerEditor : BaseEditor<PostProcessLayer>
3131
SerializedProperty m_SAOIntensity;
3232
SerializedProperty m_SAORadius;
3333
SerializedProperty m_SAOQuality;
34+
SerializedProperty m_SAOColor;
3435
SerializedProperty m_MSVOIntensity;
3536
SerializedProperty m_MSVOThicknessModifier;
37+
SerializedProperty m_MSVOColor;
3638
SerializedProperty m_AOAmbientOnly;
3739

3840
SerializedProperty m_FogEnabled;
@@ -86,8 +88,10 @@ void OnEnable()
8688
m_SAOIntensity = FindProperty(x => x.ambientOcclusion.scalableAO.intensity);
8789
m_SAORadius = FindProperty(x => x.ambientOcclusion.scalableAO.radius);
8890
m_SAOQuality = FindProperty(x => x.ambientOcclusion.scalableAO.quality);
91+
m_SAOColor = FindProperty(x => x.ambientOcclusion.scalableAO.color);
8992
m_MSVOIntensity = FindProperty(x => x.ambientOcclusion.multiScaleVO.intensity);
9093
m_MSVOThicknessModifier = FindProperty(x => x.ambientOcclusion.multiScaleVO.thicknessModifier);
94+
m_MSVOColor = FindProperty(x => x.ambientOcclusion.multiScaleVO.color);
9195

9296
m_FogEnabled = FindProperty(x => x.fog.enabled);
9397
m_FogExcludeSkybox = FindProperty(x => x.fog.excludeSkybox);
@@ -259,11 +263,13 @@ void DoAmbientOcclusion(Camera camera)
259263
EditorGUILayout.PropertyField(m_SAOIntensity);
260264
EditorGUILayout.PropertyField(m_SAORadius);
261265
EditorGUILayout.PropertyField(m_SAOQuality);
266+
EditorGUILayout.PropertyField(m_SAOColor);
262267
}
263268
else if (aoMode == (int)AmbientOcclusion.Mode.MSVO)
264269
{
265270
EditorGUILayout.PropertyField(m_MSVOIntensity);
266271
EditorGUILayout.PropertyField(m_MSVOThicknessModifier);
272+
EditorGUILayout.PropertyField(m_MSVOColor);
267273
}
268274

269275
if (camera != null && camera.actualRenderingPath == RenderingPath.DeferredShading && camera.allowHDR)

PostProcessing/Runtime/Effects/MultiScaleVO.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public sealed class MultiScaleVO : IAmbientOcclusionMethod
2222
[Range(0, 2), Tooltip("Degree of darkness added by ambient occlusion.")]
2323
public float intensity = 1;
2424

25+
[ColorUsage(false), Tooltip("Custom color to use for the ambient occlusion.")]
26+
public Color color = Color.black;
27+
2528
internal enum MipLevel { Original, L1, L2, L3, L4, L5, L6 }
2629

2730
internal enum TextureType
@@ -271,6 +274,8 @@ void RebuildCommandBuffers(PostProcessRenderContext context)
271274
context.height
272275
);
273276

277+
m_PropertySheet.properties.SetVector(ShaderIDs.AOColor, Color.white - color);
278+
274279
#if !UNITY_2017_1_OR_NEWER
275280
m_TiledDepth1.AllocateNow();
276281
m_TiledDepth2.AllocateNow();
@@ -280,9 +285,6 @@ void RebuildCommandBuffers(PostProcessRenderContext context)
280285

281286
m_Result.AllocateNow();
282287

283-
// Rebuild the render commands.
284-
cmd.Clear();
285-
286288
PushDownsampleCommands(context, cmd);
287289

288290
m_Occlusion1.PushAllocationCommand(cmd);
@@ -518,8 +520,8 @@ public void RenderAfterOpaque(PostProcessRenderContext context)
518520
cmd.BeginSample("Ambient Occlusion");
519521
DoLazyInitialization(context);
520522
RebuildCommandBuffers(context);
521-
cmd.SetGlobalTexture(ShaderIDs.OcclusionTexture, m_Result.id);
522-
cmd.Blit(null, BuiltinRenderTextureType.CameraTarget, m_PropertySheet.material, 2);
523+
cmd.SetGlobalTexture(ShaderIDs.MSVOcclusionTexture, m_Result.id);
524+
cmd.BlitFullscreenTriangle(context.source, context.destination, m_PropertySheet, 2);
523525
cmd.EndSample("Ambient Occlusion");
524526
}
525527

@@ -537,8 +539,8 @@ public void CompositeAmbientOnly(PostProcessRenderContext context)
537539
var cmd = context.command;
538540
cmd.BeginSample("Ambient Occlusion Composite");
539541
cmd.SetRenderTarget(m_MRT, BuiltinRenderTextureType.CameraTarget);
540-
cmd.SetGlobalTexture(ShaderIDs.OcclusionTexture, m_Result.id);
541-
cmd.DrawProcedural(Matrix4x4.identity, m_PropertySheet.material, 1, MeshTopology.Triangles, 3);
542+
cmd.SetGlobalTexture(ShaderIDs.MSVOcclusionTexture, m_Result.id);
543+
cmd.DrawProcedural(Matrix4x4.identity, m_PropertySheet.material, 1, MeshTopology.Triangles, 3, 1, m_PropertySheet.properties);
542544
cmd.EndSample("Ambient Occlusion Composite");
543545
}
544546

PostProcessing/Runtime/Effects/ScalableAO.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public enum Quality
2626
[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.")]
2727
public Quality quality = Quality.Medium;
2828

29+
[ColorUsage(false), Tooltip("Custom color to use for the ambient occlusion.")]
30+
public Color color = Color.black;
31+
2932
RenderTexture m_Result;
3033
PropertySheet m_PropertySheet;
3134

@@ -105,6 +108,7 @@ void Render(PostProcessRenderContext context, CommandBuffer cmd, int occlusionSo
105108
var sheet = m_PropertySheet;
106109
sheet.ClearKeywords();
107110
sheet.properties.SetVector(ShaderIDs.AOParams, new Vector4(px, py, pz, pw));
111+
sheet.properties.SetVector(ShaderIDs.AOColor, Color.white - color);
108112

109113
// In forward fog is applied at the object level in the grometry pass so we need to
110114
// apply it to AO as well or it'll drawn on top of the fog effect.
@@ -160,6 +164,7 @@ public void RenderAfterOpaque(PostProcessRenderContext context)
160164
var cmd = context.command;
161165
cmd.BeginSample("Ambient Occlusion");
162166
Render(context, cmd, 0);
167+
cmd.SetGlobalTexture(ShaderIDs.SAOcclusionTexture, m_Result);
163168
cmd.BlitFullscreenTriangle(context.source, context.destination, m_PropertySheet, (int)Pass.CompositionForward);
164169
cmd.EndSample("Ambient Occlusion");
165170
}
@@ -176,7 +181,7 @@ public void CompositeAmbientOnly(PostProcessRenderContext context)
176181
{
177182
var cmd = context.command;
178183
cmd.BeginSample("Ambient Occlusion Composite");
179-
cmd.SetGlobalTexture(ShaderIDs.OcclusionTexture, m_Result);
184+
cmd.SetGlobalTexture(ShaderIDs.SAOcclusionTexture, m_Result);
180185
cmd.BlitFullscreenTriangle(BuiltinRenderTextureType.None, m_MRT, BuiltinRenderTextureType.CameraTarget, m_PropertySheet, (int)Pass.CompositionDeferred);
181186
cmd.EndSample("Ambient Occlusion Composite");
182187
}

PostProcessing/Runtime/Utils/ShaderIDs.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ static class ShaderIDs
1616
internal static readonly int SMAA_Flop = Shader.PropertyToID("_SMAA_Flop");
1717

1818
internal static readonly int AOParams = Shader.PropertyToID("_AOParams");
19+
internal static readonly int AOColor = Shader.PropertyToID("_AOColor");
1920
internal static readonly int OcclusionTexture1 = Shader.PropertyToID("_OcclusionTexture1");
2021
internal static readonly int OcclusionTexture2 = Shader.PropertyToID("_OcclusionTexture2");
21-
internal static readonly int OcclusionTexture = Shader.PropertyToID("_OcclusionTexture");
22+
internal static readonly int SAOcclusionTexture = Shader.PropertyToID("_SAOcclusionTexture");
23+
internal static readonly int MSVOcclusionTexture = Shader.PropertyToID("_MSVOcclusionTexture");
2224

2325
internal static readonly int FogColor = Shader.PropertyToID("_FogColor");
2426
internal static readonly int FogParams = Shader.PropertyToID("_FogParams");

PostProcessing/Shaders/Builtins/MultiScaleVO.shader

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Shader "Hidden/PostProcessing/MultiScaleVO"
44

55
#include "../StdLib.hlsl"
66

7+
TEXTURE2D_SAMPLER2D(_MSVOcclusionTexture, sampler_MSVOcclusionTexture);
8+
float3 _AOColor;
9+
710
// Full screen triangle with procedural draw
811
// This can't be used when the destination can be the back buffer because
912
// this doesn't support the situations that requires vertical flipping.
@@ -25,27 +28,6 @@ Shader "Hidden/PostProcessing/MultiScaleVO"
2528
return o;
2629
}
2730

28-
// The standard vertex shader for blit, slightly modified for supporting
29-
// single-pass stereo rendering.
30-
struct AttributesStd
31-
{
32-
float4 vertex : POSITION;
33-
float2 texcoord : TEXCOORD0;
34-
};
35-
36-
VaryingsDefault VertStd(AttributesStd v)
37-
{
38-
VaryingsDefault o;
39-
o.vertex = float4(v.vertex.xy * 2.0 - 1.0, 0.0, 1.0);
40-
o.texcoord = TransformStereoScreenSpaceTex(v.texcoord, 1);
41-
42-
#if UNITY_UV_STARTS_AT_TOP
43-
o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
44-
#endif
45-
46-
return o;
47-
}
48-
4931
ENDHLSL
5032

5133
SubShader
@@ -80,7 +62,6 @@ Shader "Hidden/PostProcessing/MultiScaleVO"
8062
#pragma vertex VertProcedural
8163
#pragma fragment Frag
8264

83-
TEXTURE2D_SAMPLER2D(_OcclusionTexture, sampler_OcclusionTexture);
8465

8566
struct Output
8667
{
@@ -90,31 +71,33 @@ Shader "Hidden/PostProcessing/MultiScaleVO"
9071

9172
Output Frag(VaryingsDefault i)
9273
{
93-
float ao = 1.0 - SAMPLE_TEXTURE2D(_OcclusionTexture, sampler_OcclusionTexture, i.texcoord).r;
74+
float ao = 1.0 - SAMPLE_TEXTURE2D(_MSVOcclusionTexture, sampler_MSVOcclusionTexture, i.texcoord).r;
9475
Output o;
9576
o.gbuffer0 = float4(0.0, 0.0, 0.0, ao);
96-
o.gbuffer3 = float4(ao, ao, ao, 0.0);
77+
o.gbuffer3 = float4(ao * _AOColor, 0.0);
9778
return o;
9879
}
9980

10081
ENDHLSL
10182
}
10283

103-
// 2 - Composite to the frame buffer with the standard blit
84+
// 2 - Composite to the frame buffer
10485
Pass
10586
{
106-
Blend Zero SrcAlpha
107-
10887
HLSLPROGRAM
10988

110-
#pragma vertex VertStd
89+
#pragma vertex VertDefault
11190
#pragma fragment Frag
11291

113-
TEXTURE2D_SAMPLER2D(_OcclusionTexture, sampler_OcclusionTexture);
92+
TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
11493

11594
float4 Frag(VaryingsDefault i) : SV_Target
11695
{
117-
return SAMPLE_TEXTURE2D(_OcclusionTexture, sampler_OcclusionTexture, i.texcoord).rrrr;
96+
float2 texcoord = TransformStereoScreenSpaceTex(i.texcoord, 1);
97+
half ao = 1.0 - SAMPLE_TEXTURE2D(_MSVOcclusionTexture, sampler_MSVOcclusionTexture, texcoord).r;
98+
half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, texcoord);
99+
color.rgb *= 1.0 - ao * _AOColor;
100+
return color;
118101
}
119102

120103
ENDHLSL

PostProcessing/Shaders/Builtins/ScalableAO.hlsl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ TEXTURE2D_SAMPLER2D(_CameraDepthNormalsTexture, sampler_CameraDepthNormalsTextur
5353
float4 _MainTex_TexelSize;
5454

5555
float4 _AOParams;
56+
float3 _AOColor;
5657

5758
// Sample count
5859
#if !defined(SHADER_API_GLES)
@@ -63,8 +64,8 @@ float4 _AOParams;
6364
#endif
6465

6566
// Source texture properties
66-
TEXTURE2D_SAMPLER2D(_OcclusionTexture, sampler_OcclusionTexture);
67-
float4 _OcclusionTexture_TexelSize;
67+
TEXTURE2D_SAMPLER2D(_SAOcclusionTexture, sampler_SAOcclusionTexture);
68+
float4 _SAOcclusionTexture_TexelSize;
6869

6970
// Other parameters
7071
#define INTENSITY _AOParams.x
@@ -383,9 +384,9 @@ half BlurSmall(TEXTURE2D_ARGS(tex, samp), float2 uv, float2 delta)
383384
float4 FragComposition(VaryingsDefault i) : SV_Target
384385
{
385386
float2 delta = _MainTex_TexelSize.xy / DOWNSAMPLE;
386-
half ao = BlurSmall(TEXTURE2D_PARAM(_OcclusionTexture, sampler_OcclusionTexture), i.texcoord, delta);
387+
half ao = BlurSmall(TEXTURE2D_PARAM(_SAOcclusionTexture, sampler_SAOcclusionTexture), i.texcoord, delta);
387388
half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UnityStereoTransformScreenSpaceTex(i.texcoord));
388-
color.rgb *= 1.0 - EncodeAO(ao);
389+
color.rgb *= 1.0 - EncodeAO(ao) * _AOColor;
389390
return color;
390391
}
391392

@@ -399,14 +400,14 @@ struct CompositionOutput
399400

400401
CompositionOutput FragCompositionGBuffer(VaryingsDefault i)
401402
{
402-
// Workaround: _OcclusionTexture_Texelsize hasn't been set properly
403+
// Workaround: _SAOcclusionTexture_Texelsize hasn't been set properly
403404
// for some reasons. Use _ScreenParams instead.
404405
float2 delta = (_ScreenParams.zw - 1.0) / DOWNSAMPLE;
405-
half ao = BlurSmall(TEXTURE2D_PARAM(_OcclusionTexture, sampler_OcclusionTexture), i.texcoord, delta);
406+
half ao = BlurSmall(TEXTURE2D_PARAM(_SAOcclusionTexture, sampler_SAOcclusionTexture), i.texcoord, delta);
406407

407408
CompositionOutput o;
408409
o.gbuffer0 = half4(0.0, 0.0, 0.0, ao);
409-
o.gbuffer3 = half4((half3)EncodeAO(ao), 0.0);
410+
o.gbuffer3 = half4((half3)EncodeAO(ao) * _AOColor, 0.0);
410411
return o;
411412
}
412413

0 commit comments

Comments
 (0)