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

Commit 6619875

Browse files
authored
Merge pull request #101 from Unity-Technologies/simple-fog
Simple fog
2 parents 8b303cb + 12198a2 commit 6619875

File tree

11 files changed

+357
-1
lines changed

11 files changed

+357
-1
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using UnityEngine;
2+
using UnityEngine.PostProcessing;
3+
4+
namespace UnityEditor.PostProcessing
5+
{
6+
using Settings = FogModel.Settings;
7+
8+
[PostProcessingModelEditor(typeof(FogModel))]
9+
public class FogModelEditor : PostProcessingModelEditor
10+
{
11+
SerializedProperty m_Color;
12+
SerializedProperty m_Mode;
13+
SerializedProperty m_Density;
14+
SerializedProperty m_Start;
15+
SerializedProperty m_End;
16+
SerializedProperty m_ExcludeSkybox;
17+
18+
public override void OnEnable()
19+
{
20+
m_Color = FindSetting((Settings x) => x.color);
21+
m_Mode = FindSetting((Settings x) => x.mode);
22+
m_Density = FindSetting((Settings x) => x.density);
23+
m_Start = FindSetting((Settings x) => x.start);
24+
m_End = FindSetting((Settings x) => x.end);
25+
m_ExcludeSkybox = FindSetting((Settings x) => x.excludeSkybox);
26+
}
27+
28+
public override void OnInspectorGUI()
29+
{
30+
EditorGUILayout.PropertyField(m_Color);
31+
EditorGUILayout.PropertyField(m_ExcludeSkybox);
32+
EditorGUILayout.PropertyField(m_Mode);
33+
34+
EditorGUI.indentLevel++;
35+
if (m_Mode.intValue == (int)FogMode.Linear)
36+
{
37+
EditorGUILayout.PropertyField(m_Start);
38+
EditorGUILayout.PropertyField(m_End);
39+
}
40+
else
41+
{
42+
EditorGUILayout.PropertyField(m_Density);
43+
}
44+
EditorGUI.indentLevel--;
45+
}
46+
}
47+
}

PostProcessing/Editor/Models/FogModelEditor.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
Shader "Hidden/Post FX/Fog"
2+
{
3+
Properties
4+
{
5+
_MainTex("Main Texture", 2D) = "white" {}
6+
}
7+
8+
CGINCLUDE
9+
10+
#pragma multi_compile __ FOG_LINEAR FOG_EXP FOG_EXP2
11+
#include "UnityCG.cginc"
12+
#include "Common.cginc"
13+
14+
#define SKYBOX_THREASHOLD_VALUE 0.9999
15+
16+
struct Varyings
17+
{
18+
float2 uv : TEXCOORD0;
19+
float4 vertex : SV_POSITION;
20+
};
21+
22+
Varyings VertFog(AttributesDefault v)
23+
{
24+
Varyings o;
25+
o.vertex = UnityObjectToClipPos(v.vertex);
26+
o.uv = UnityStereoScreenSpaceUVAdjust(v.texcoord, _MainTex_ST);
27+
return o;
28+
}
29+
30+
sampler2D _CameraDepthTexture;
31+
32+
half4 _FogColor;
33+
float _Density;
34+
float _Start;
35+
float _End;
36+
37+
half ComputeFog(float z)
38+
{
39+
half fog = 0.0;
40+
#if FOG_LINEAR
41+
fog = (_End - z) / (_End - _Start);
42+
#elif FOG_EXP
43+
fog = exp2(-_Density * z);
44+
#else // FOG_EXP2
45+
fog = _Density * z;
46+
fog = exp2(-fog * fog);
47+
#endif
48+
return saturate(fog);
49+
}
50+
51+
float ComputeDistance(float depth)
52+
{
53+
float dist = depth * _ProjectionParams.z;
54+
dist -= _ProjectionParams.y;
55+
return dist;
56+
}
57+
58+
half4 FragFog(Varyings i) : SV_Target
59+
{
60+
half4 color = tex2D(_MainTex, i.uv);
61+
62+
float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
63+
depth = Linear01Depth(depth);
64+
float dist = ComputeDistance(depth) - _Start;
65+
half fog = 1.0 - ComputeFog(dist);
66+
67+
return lerp(color, _FogColor, fog);
68+
}
69+
70+
half4 FragFogExcludeSkybox(Varyings i) : SV_Target
71+
{
72+
half4 color = tex2D(_MainTex, i.uv);
73+
74+
float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
75+
depth = Linear01Depth(depth);
76+
float skybox = depth < SKYBOX_THREASHOLD_VALUE;
77+
float dist = ComputeDistance(depth) - _Start;
78+
half fog = 1.0 - ComputeFog(dist);
79+
80+
return lerp(color, _FogColor, fog * skybox);
81+
}
82+
83+
ENDCG
84+
85+
SubShader
86+
{
87+
Cull Off ZWrite Off ZTest Always
88+
89+
Pass
90+
{
91+
CGPROGRAM
92+
93+
#pragma vertex VertFog
94+
#pragma fragment FragFog
95+
96+
ENDCG
97+
}
98+
99+
Pass
100+
{
101+
CGPROGRAM
102+
103+
#pragma vertex VertFog
104+
#pragma fragment FragFogExcludeSkybox
105+
106+
ENDCG
107+
}
108+
}
109+
}

PostProcessing/Resources/Shaders/Fog.shader.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System;
2+
using UnityEngine.Rendering;
3+
4+
namespace UnityEngine.PostProcessing
5+
{
6+
public sealed class FogComponent : PostProcessingComponentCommandBuffer<FogModel>
7+
{
8+
static class Uniforms
9+
{
10+
internal static readonly int _FogColor = Shader.PropertyToID("_FogColor");
11+
internal static readonly int _Density = Shader.PropertyToID("_Density");
12+
internal static readonly int _Start = Shader.PropertyToID("_Start");
13+
internal static readonly int _End = Shader.PropertyToID("_End");
14+
internal static readonly int _TempRT = Shader.PropertyToID("_TempRT");
15+
}
16+
17+
const string k_ShaderString = "Hidden/Post FX/Fog";
18+
19+
public override bool active
20+
{
21+
get
22+
{
23+
return model.enabled
24+
&& context.isGBufferAvailable // In forward fog is already done at shader level
25+
&& !context.interrupted;
26+
}
27+
}
28+
29+
public override string GetName()
30+
{
31+
return "Fog";
32+
}
33+
34+
public override DepthTextureMode GetCameraFlags()
35+
{
36+
return DepthTextureMode.Depth;
37+
}
38+
39+
public override CameraEvent GetCameraEvent()
40+
{
41+
return CameraEvent.BeforeImageEffectsOpaque;
42+
}
43+
44+
public override void Init(PostProcessingContext pcontext, FogModel pmodel)
45+
{
46+
base.Init(pcontext, pmodel);
47+
48+
var settings = model.settings;
49+
RenderSettings.fog = model.enabled;
50+
RenderSettings.fogColor = settings.color;
51+
RenderSettings.fogMode = settings.mode;
52+
RenderSettings.fogDensity = settings.density;
53+
RenderSettings.fogStartDistance = settings.start;
54+
RenderSettings.fogEndDistance = settings.end;
55+
}
56+
57+
public override void PopulateCommandBuffer(CommandBuffer cb)
58+
{
59+
var settings = model.settings;
60+
61+
var material = context.materialFactory.Get(k_ShaderString);
62+
material.shaderKeywords = null;
63+
material.SetColor(Uniforms._FogColor, settings.color);
64+
material.SetFloat(Uniforms._Density, settings.density);
65+
material.SetFloat(Uniforms._Start, settings.start);
66+
material.SetFloat(Uniforms._End, settings.end);
67+
68+
switch (settings.mode)
69+
{
70+
case FogMode.Linear:
71+
material.EnableKeyword("FOG_LINEAR");
72+
break;
73+
case FogMode.Exponential:
74+
material.EnableKeyword("FOG_EXP");
75+
break;
76+
case FogMode.ExponentialSquared:
77+
material.EnableKeyword("FOG_EXP2");
78+
break;
79+
}
80+
81+
var fbFormat = context.isHdr
82+
? RenderTextureFormat.DefaultHDR
83+
: RenderTextureFormat.Default;
84+
85+
cb.GetTemporaryRT(Uniforms._TempRT, context.width, context.height, 24, FilterMode.Bilinear, fbFormat);
86+
cb.Blit(BuiltinRenderTextureType.CameraTarget, Uniforms._TempRT);
87+
cb.Blit(Uniforms._TempRT, BuiltinRenderTextureType.CameraTarget, material, settings.excludeSkybox ? 1 : 0);
88+
cb.ReleaseTemporaryRT(Uniforms._TempRT);
89+
}
90+
}
91+
}

PostProcessing/Runtime/Components/FogComponent.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
3+
namespace UnityEngine.PostProcessing
4+
{
5+
[Serializable]
6+
public class FogModel : PostProcessingModel
7+
{
8+
[Serializable]
9+
public struct Settings
10+
{
11+
[Tooltip("Controls the color of that fog drawn in the scene.")]
12+
public Color color;
13+
14+
[Tooltip("Controls the mathematical function determining the way fog accumulates with distance from the camera. Options are Linear, Exponential and Exponential Squared.")]
15+
public FogMode mode;
16+
17+
[Tooltip("Controls the density of the fog effect in the Scene when using Exponential or Exponential Squared modes.")]
18+
public float density;
19+
20+
[Tooltip("Controls the distance from the camera where the fog will start in the scene.")]
21+
public float start;
22+
23+
[Tooltip("Controls the distance from the camera where the fog will completely obscure objects in the Scene.")]
24+
public float end;
25+
26+
[Tooltip("Should the fog affect the skybox?")]
27+
public bool excludeSkybox;
28+
29+
public static Settings defaultSettings
30+
{
31+
get
32+
{
33+
return new Settings
34+
{
35+
color = new Color32(102, 108, 113, 154),
36+
mode = FogMode.Exponential,
37+
density = 0.001f,
38+
start = 0f,
39+
end = 600f,
40+
excludeSkybox = true
41+
};
42+
}
43+
}
44+
}
45+
46+
[SerializeField]
47+
Settings m_Settings = Settings.defaultSettings;
48+
public Settings settings
49+
{
50+
get { return m_Settings; }
51+
set { m_Settings = value; }
52+
}
53+
54+
public override void Reset()
55+
{
56+
m_Settings = Settings.defaultSettings;
57+
}
58+
}
59+
}

PostProcessing/Runtime/Models/FogModel.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PostProcessing/Runtime/PostProcessingBehaviour.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class PostProcessingBehaviour : MonoBehaviour
3636
BuiltinDebugViewsComponent m_DebugViews;
3737
AmbientOcclusionComponent m_AmbientOcclusion;
3838
ScreenSpaceReflectionComponent m_ScreenSpaceReflection;
39+
FogComponent m_FogComponent;
3940
MotionBlurComponent m_MotionBlur;
4041
TaaComponent m_Taa;
4142
EyeAdaptationComponent m_EyeAdaptation;
@@ -63,6 +64,7 @@ void OnEnable()
6364
m_DebugViews = AddComponent(new BuiltinDebugViewsComponent());
6465
m_AmbientOcclusion = AddComponent(new AmbientOcclusionComponent());
6566
m_ScreenSpaceReflection = AddComponent(new ScreenSpaceReflectionComponent());
67+
m_FogComponent = AddComponent(new FogComponent());
6668
m_MotionBlur = AddComponent(new MotionBlurComponent());
6769
m_Taa = AddComponent(new TaaComponent());
6870
m_EyeAdaptation = AddComponent(new EyeAdaptationComponent());
@@ -117,6 +119,7 @@ void OnPreCull()
117119
m_DebugViews.Init(context, profile.debugViews);
118120
m_AmbientOcclusion.Init(context, profile.ambientOcclusion);
119121
m_ScreenSpaceReflection.Init(context, profile.screenSpaceReflection);
122+
m_FogComponent.Init(context, profile.fog);
120123
m_MotionBlur.Init(context, profile.motionBlur);
121124
m_Taa.Init(context, profile.antialiasing);
122125
m_EyeAdaptation.Init(context, profile.eyeAdaptation);
@@ -167,6 +170,7 @@ void OnPreRender()
167170
TryExecuteCommandBuffer(m_DebugViews);
168171
TryExecuteCommandBuffer(m_AmbientOcclusion);
169172
TryExecuteCommandBuffer(m_ScreenSpaceReflection);
173+
TryExecuteCommandBuffer(m_FogComponent);
170174

171175
if (!m_RenderingInSceneView)
172176
TryExecuteCommandBuffer(m_MotionBlur);

PostProcessing/Runtime/PostProcessingComponent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public abstract class PostProcessingComponent<T> : PostProcessingComponentBase
2727
{
2828
public T model { get; internal set; }
2929

30-
public void Init(PostProcessingContext pcontext, T pmodel)
30+
public virtual void Init(PostProcessingContext pcontext, T pmodel)
3131
{
3232
context = pcontext;
3333
model = pmodel;

0 commit comments

Comments
 (0)