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

Commit

Permalink
Merge pull request #158 from Unity-Technologies/fog-fixes
Browse files Browse the repository at this point in the history
Fog fixes
  • Loading branch information
Chman authored Apr 20, 2017
2 parents 3afb425 + 0307923 commit a49e546
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
30 changes: 30 additions & 0 deletions PostProcessing/Resources/Shaders/AmbientOcclusion.cginc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ float4 _OcclusionTexture_TexelSize;
half _Intensity;
float _Radius;
float _Downsample;
float3 _FogParams; // x: density, y: start, z: end

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

// Fog handling in forward
half ComputeFog(float z)
{
half fog = 0.0;
#if FOG_LINEAR
fog = (_FogParams.z - z) / (_FogParams.z - _FogParams.y);
#elif FOG_EXP
fog = exp2(-_FogParams.x * z);
#else // FOG_EXP2
fog = _FogParams.x * z;
fog = exp2(-fog * fog);
#endif
return saturate(fog);
}

float ComputeDistance(float depth)
{
float dist = depth * _ProjectionParams.z;
dist -= _ProjectionParams.y;
return dist;
}

//
// Distance-based AO estimator based on Morgan 2011 http://goo.gl/2iz3P
//
Expand Down Expand Up @@ -282,6 +305,13 @@ half4 FragAO(VaryingsMultitex i) : SV_Target
// Apply other parameters.
ao = pow(ao * _Intensity / _SampleCount, kContrast);

// Apply fog when enabled (forward-only)
#if !FOG_OFF
float d = Linear01Depth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, uv));
d = ComputeDistance(d);
ao *= ComputeFog(d);
#endif

return PackAONormal(ao, norm_o);
}

Expand Down
3 changes: 3 additions & 0 deletions PostProcessing/Resources/Shaders/AmbientOcclusion.shader
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Shader "Hidden/Post FX/Ambient Occlusion"
CGPROGRAM
#pragma vertex VertMultitex
#pragma fragment FragAO
#pragma multi_compile FOG_OFF FOG_LINEAR FOG_EXP FOG_EXP2
#define SOURCE_DEPTH
#include "AmbientOcclusion.cginc"
ENDCG
Expand All @@ -27,6 +28,7 @@ Shader "Hidden/Post FX/Ambient Occlusion"
CGPROGRAM
#pragma vertex VertMultitex
#pragma fragment FragAO
#pragma multi_compile FOG_OFF FOG_LINEAR FOG_EXP FOG_EXP2
#define SOURCE_DEPTHNORMALS
#include "AmbientOcclusion.cginc"
ENDCG
Expand All @@ -38,6 +40,7 @@ Shader "Hidden/Post FX/Ambient Occlusion"
CGPROGRAM
#pragma vertex VertMultitex
#pragma fragment FragAO
#pragma multi_compile FOG_OFF FOG_LINEAR FOG_EXP FOG_EXP2
#define SOURCE_GBUFFER
#include "AmbientOcclusion.cginc"
ENDCG
Expand Down
4 changes: 2 additions & 2 deletions PostProcessing/Resources/Shaders/Fog.shader
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Shader "Hidden/Post FX/Fog"

float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
depth = Linear01Depth(depth);
float dist = ComputeDistance(depth) - _Start;
float dist = ComputeDistance(depth);
half fog = 1.0 - ComputeFog(dist);

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

return lerp(color, _FogColor, fog * skybox);
Expand Down
23 changes: 23 additions & 0 deletions PostProcessing/Runtime/Components/AmbientOcclusionComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ static class Uniforms
{
internal static readonly int _Intensity = Shader.PropertyToID("_Intensity");
internal static readonly int _Radius = Shader.PropertyToID("_Radius");
internal static readonly int _FogParams = Shader.PropertyToID("_FogParams");
internal static readonly int _Downsample = Shader.PropertyToID("_Downsample");
internal static readonly int _SampleCount = Shader.PropertyToID("_SampleCount");
internal static readonly int _OcclusionTexture1 = Shader.PropertyToID("_OcclusionTexture1");
Expand Down Expand Up @@ -103,6 +104,28 @@ public override void PopulateCommandBuffer(CommandBuffer cb)
material.SetFloat(Uniforms._Downsample, settings.downsampling ? 0.5f : 1f);
material.SetInt(Uniforms._SampleCount, (int)settings.sampleCount);

if (!context.isGBufferAvailable && RenderSettings.fog)
{
material.SetVector(Uniforms._FogParams, new Vector3(RenderSettings.fogDensity, RenderSettings.fogStartDistance, RenderSettings.fogEndDistance));

switch (RenderSettings.fogMode)
{
case FogMode.Linear:
material.EnableKeyword("FOG_LINEAR");
break;
case FogMode.Exponential:
material.EnableKeyword("FOG_EXP");
break;
case FogMode.ExponentialSquared:
material.EnableKeyword("FOG_EXP2");
break;
}
}
else
{
material.EnableKeyword("FOG_OFF");
}

int tw = context.width;
int th = context.height;
int ts = settings.downsampling ? 2 : 1;
Expand Down
6 changes: 3 additions & 3 deletions PostProcessing/Runtime/Components/FogComponent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using UnityEngine.Rendering;

namespace UnityEngine.PostProcessing
Expand Down Expand Up @@ -39,7 +38,7 @@ public override DepthTextureMode GetCameraFlags()

public override CameraEvent GetCameraEvent()
{
return CameraEvent.BeforeImageEffectsOpaque;
return CameraEvent.AfterImageEffectsOpaque;
}

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

var material = context.materialFactory.Get(k_ShaderString);
material.shaderKeywords = null;
material.SetColor(Uniforms._FogColor, RenderSettings.fogColor);
var fogColor = GraphicsUtils.isLinearColorSpace ? RenderSettings.fogColor.linear : RenderSettings.fogColor;
material.SetColor(Uniforms._FogColor, fogColor);
material.SetFloat(Uniforms._Density, RenderSettings.fogDensity);
material.SetFloat(Uniforms._Start, RenderSettings.fogStartDistance);
material.SetFloat(Uniforms._End, RenderSettings.fogEndDistance);
Expand Down

0 comments on commit a49e546

Please sign in to comment.