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

Commit a49e546

Browse files
authored
Merge pull request #158 from Unity-Technologies/fog-fixes
Fog fixes
2 parents 3afb425 + 0307923 commit a49e546

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

PostProcessing/Resources/Shaders/AmbientOcclusion.cginc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ float4 _OcclusionTexture_TexelSize;
6969
half _Intensity;
7070
float _Radius;
7171
float _Downsample;
72+
float3 _FogParams; // x: density, y: start, z: end
7273

7374
// Accessors for packed AO/normal buffer
7475
fixed4 PackAONormal(fixed ao, fixed3 n)
@@ -220,6 +221,28 @@ float3 PickSamplePoint(float2 uv, float index)
220221
return v * l;
221222
}
222223

224+
// Fog handling in forward
225+
half ComputeFog(float z)
226+
{
227+
half fog = 0.0;
228+
#if FOG_LINEAR
229+
fog = (_FogParams.z - z) / (_FogParams.z - _FogParams.y);
230+
#elif FOG_EXP
231+
fog = exp2(-_FogParams.x * z);
232+
#else // FOG_EXP2
233+
fog = _FogParams.x * z;
234+
fog = exp2(-fog * fog);
235+
#endif
236+
return saturate(fog);
237+
}
238+
239+
float ComputeDistance(float depth)
240+
{
241+
float dist = depth * _ProjectionParams.z;
242+
dist -= _ProjectionParams.y;
243+
return dist;
244+
}
245+
223246
//
224247
// Distance-based AO estimator based on Morgan 2011 http://goo.gl/2iz3P
225248
//
@@ -282,6 +305,13 @@ half4 FragAO(VaryingsMultitex i) : SV_Target
282305
// Apply other parameters.
283306
ao = pow(ao * _Intensity / _SampleCount, kContrast);
284307

308+
// Apply fog when enabled (forward-only)
309+
#if !FOG_OFF
310+
float d = Linear01Depth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, uv));
311+
d = ComputeDistance(d);
312+
ao *= ComputeFog(d);
313+
#endif
314+
285315
return PackAONormal(ao, norm_o);
286316
}
287317

PostProcessing/Resources/Shaders/AmbientOcclusion.shader

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Shader "Hidden/Post FX/Ambient Occlusion"
1616
CGPROGRAM
1717
#pragma vertex VertMultitex
1818
#pragma fragment FragAO
19+
#pragma multi_compile FOG_OFF FOG_LINEAR FOG_EXP FOG_EXP2
1920
#define SOURCE_DEPTH
2021
#include "AmbientOcclusion.cginc"
2122
ENDCG
@@ -27,6 +28,7 @@ Shader "Hidden/Post FX/Ambient Occlusion"
2728
CGPROGRAM
2829
#pragma vertex VertMultitex
2930
#pragma fragment FragAO
31+
#pragma multi_compile FOG_OFF FOG_LINEAR FOG_EXP FOG_EXP2
3032
#define SOURCE_DEPTHNORMALS
3133
#include "AmbientOcclusion.cginc"
3234
ENDCG
@@ -38,6 +40,7 @@ Shader "Hidden/Post FX/Ambient Occlusion"
3840
CGPROGRAM
3941
#pragma vertex VertMultitex
4042
#pragma fragment FragAO
43+
#pragma multi_compile FOG_OFF FOG_LINEAR FOG_EXP FOG_EXP2
4144
#define SOURCE_GBUFFER
4245
#include "AmbientOcclusion.cginc"
4346
ENDCG

PostProcessing/Resources/Shaders/Fog.shader

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Shader "Hidden/Post FX/Fog"
6161

6262
float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
6363
depth = Linear01Depth(depth);
64-
float dist = ComputeDistance(depth) - _Start;
64+
float dist = ComputeDistance(depth);
6565
half fog = 1.0 - ComputeFog(dist);
6666

6767
return lerp(color, _FogColor, fog);
@@ -74,7 +74,7 @@ Shader "Hidden/Post FX/Fog"
7474
float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
7575
depth = Linear01Depth(depth);
7676
float skybox = depth < SKYBOX_THREASHOLD_VALUE;
77-
float dist = ComputeDistance(depth) - _Start;
77+
float dist = ComputeDistance(depth);
7878
half fog = 1.0 - ComputeFog(dist);
7979

8080
return lerp(color, _FogColor, fog * skybox);

PostProcessing/Runtime/Components/AmbientOcclusionComponent.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ static class Uniforms
1010
{
1111
internal static readonly int _Intensity = Shader.PropertyToID("_Intensity");
1212
internal static readonly int _Radius = Shader.PropertyToID("_Radius");
13+
internal static readonly int _FogParams = Shader.PropertyToID("_FogParams");
1314
internal static readonly int _Downsample = Shader.PropertyToID("_Downsample");
1415
internal static readonly int _SampleCount = Shader.PropertyToID("_SampleCount");
1516
internal static readonly int _OcclusionTexture1 = Shader.PropertyToID("_OcclusionTexture1");
@@ -103,6 +104,28 @@ public override void PopulateCommandBuffer(CommandBuffer cb)
103104
material.SetFloat(Uniforms._Downsample, settings.downsampling ? 0.5f : 1f);
104105
material.SetInt(Uniforms._SampleCount, (int)settings.sampleCount);
105106

107+
if (!context.isGBufferAvailable && RenderSettings.fog)
108+
{
109+
material.SetVector(Uniforms._FogParams, new Vector3(RenderSettings.fogDensity, RenderSettings.fogStartDistance, RenderSettings.fogEndDistance));
110+
111+
switch (RenderSettings.fogMode)
112+
{
113+
case FogMode.Linear:
114+
material.EnableKeyword("FOG_LINEAR");
115+
break;
116+
case FogMode.Exponential:
117+
material.EnableKeyword("FOG_EXP");
118+
break;
119+
case FogMode.ExponentialSquared:
120+
material.EnableKeyword("FOG_EXP2");
121+
break;
122+
}
123+
}
124+
else
125+
{
126+
material.EnableKeyword("FOG_OFF");
127+
}
128+
106129
int tw = context.width;
107130
int th = context.height;
108131
int ts = settings.downsampling ? 2 : 1;

PostProcessing/Runtime/Components/FogComponent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using UnityEngine.Rendering;
32

43
namespace UnityEngine.PostProcessing
@@ -39,7 +38,7 @@ public override DepthTextureMode GetCameraFlags()
3938

4039
public override CameraEvent GetCameraEvent()
4140
{
42-
return CameraEvent.BeforeImageEffectsOpaque;
41+
return CameraEvent.AfterImageEffectsOpaque;
4342
}
4443

4544
public override void PopulateCommandBuffer(CommandBuffer cb)
@@ -48,7 +47,8 @@ public override void PopulateCommandBuffer(CommandBuffer cb)
4847

4948
var material = context.materialFactory.Get(k_ShaderString);
5049
material.shaderKeywords = null;
51-
material.SetColor(Uniforms._FogColor, RenderSettings.fogColor);
50+
var fogColor = GraphicsUtils.isLinearColorSpace ? RenderSettings.fogColor.linear : RenderSettings.fogColor;
51+
material.SetColor(Uniforms._FogColor, fogColor);
5252
material.SetFloat(Uniforms._Density, RenderSettings.fogDensity);
5353
material.SetFloat(Uniforms._Start, RenderSettings.fogStartDistance);
5454
material.SetFloat(Uniforms._End, RenderSettings.fogEndDistance);

0 commit comments

Comments
 (0)