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

Commit 36eac35

Browse files
committed
Fixed see-through AO with fog in Forward
1 parent 5e6aea3 commit 36eac35

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

PostProcessing/Runtime/Effects/MultiScaleVO.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ void RebuildCommandBuffers(PostProcessRenderContext context)
281281
context.height
282282
);
283283

284+
m_PropertySheet.ClearKeywords();
284285
m_PropertySheet.properties.SetVector(ShaderIDs.AOColor, Color.white - color);
285286

286287
#if !UNITY_2017_1_OR_NEWER
@@ -526,9 +527,36 @@ public void RenderAfterOpaque(PostProcessRenderContext context)
526527
var cmd = context.command;
527528
cmd.BeginSample("Ambient Occlusion");
528529
DoLazyInitialization(context);
530+
531+
var sheet = m_PropertySheet;
532+
533+
// In forward fog is applied at the object level in the grometry pass so we need to
534+
// apply it to AO as well or it'll drawn on top of the fog effect.
535+
// Not needed in Deferred.
536+
if (context.camera.actualRenderingPath == RenderingPath.Forward && RenderSettings.fog)
537+
{
538+
sheet.properties.SetVector(
539+
ShaderIDs.FogParams,
540+
new Vector3(RenderSettings.fogDensity, RenderSettings.fogStartDistance, RenderSettings.fogEndDistance)
541+
);
542+
543+
switch (RenderSettings.fogMode)
544+
{
545+
case FogMode.Linear:
546+
sheet.EnableKeyword("FOG_LINEAR");
547+
break;
548+
case FogMode.Exponential:
549+
sheet.EnableKeyword("FOG_EXP");
550+
break;
551+
case FogMode.ExponentialSquared:
552+
sheet.EnableKeyword("FOG_EXP2");
553+
break;
554+
}
555+
}
556+
529557
RebuildCommandBuffers(context);
530558
cmd.SetGlobalTexture(ShaderIDs.MSVOcclusionTexture, m_Result.id);
531-
cmd.BlitFullscreenTriangle(context.source, context.destination, m_PropertySheet, (int)Pass.CompositeForward);
559+
cmd.BlitFullscreenTriangle(context.source, context.destination, sheet, (int)Pass.CompositeForward);
532560
cmd.EndSample("Ambient Occlusion");
533561
}
534562

PostProcessing/Runtime/Effects/ScalableAO.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ void Render(PostProcessRenderContext context, CommandBuffer cmd, int occlusionSo
115115
// Not needed in Deferred.
116116
if (context.camera.actualRenderingPath == RenderingPath.Forward && RenderSettings.fog)
117117
{
118-
sheet.properties.SetVector(ShaderIDs.FogParams, new Vector3(RenderSettings.fogDensity, RenderSettings.fogStartDistance, RenderSettings.fogEndDistance));
118+
sheet.properties.SetVector(
119+
ShaderIDs.FogParams,
120+
new Vector3(RenderSettings.fogDensity, RenderSettings.fogStartDistance, RenderSettings.fogEndDistance)
121+
);
119122

120123
switch (RenderSettings.fogMode)
121124
{

PostProcessing/Shaders/Builtins/MultiScaleVO.shader

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Shader "Hidden/PostProcessing/MultiScaleVO"
33
HLSLINCLUDE
44

55
#include "../StdLib.hlsl"
6+
#include "Fog.hlsl"
67

78
TEXTURE2D_SAMPLER2D(_MSVOcclusionTexture, sampler_MSVOcclusionTexture);
89
float3 _AOColor;
@@ -86,15 +87,25 @@ Shader "Hidden/PostProcessing/MultiScaleVO"
8687
{
8788
HLSLPROGRAM
8889

90+
#pragma multi_compile _ FOG_LINEAR FOG_EXP FOG_EXP2
8991
#pragma vertex VertDefault
9092
#pragma fragment Frag
9193

9294
TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
95+
TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
9396

9497
float4 Frag(VaryingsDefault i) : SV_Target
9598
{
9699
float2 texcoord = TransformStereoScreenSpaceTex(i.texcoord, 1);
97100
half ao = 1.0 - SAMPLE_TEXTURE2D(_MSVOcclusionTexture, sampler_MSVOcclusionTexture, texcoord).r;
101+
102+
// Apply fog when enabled (forward-only)
103+
#if (FOG_LINEAR || FOG_EXP || FOG_EXP2)
104+
float d = Linear01Depth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, texcoord));
105+
d = ComputeFogDistance(d);
106+
ao *= ComputeFog(d);
107+
#endif
108+
98109
half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, texcoord);
99110
color.rgb *= 1.0 - ao * _AOColor;
100111
return color;

0 commit comments

Comments
 (0)