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

Commit b153d2d

Browse files
committed
Added a temporary, simple fog effect
1 parent a87de09 commit b153d2d

File tree

11 files changed

+328
-1
lines changed

11 files changed

+328
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
17+
public override void OnEnable()
18+
{
19+
m_Color = FindSetting((Settings x) => x.color);
20+
m_Mode = FindSetting((Settings x) => x.mode);
21+
m_Density = FindSetting((Settings x) => x.density);
22+
m_Start = FindSetting((Settings x) => x.start);
23+
m_End = FindSetting((Settings x) => x.end);
24+
}
25+
26+
public override void OnInspectorGUI()
27+
{
28+
EditorGUILayout.PropertyField(m_Color);
29+
EditorGUILayout.PropertyField(m_Mode);
30+
31+
EditorGUI.indentLevel++;
32+
if (m_Mode.intValue == (int)FogMode.Linear)
33+
{
34+
EditorGUILayout.PropertyField(m_Start);
35+
EditorGUILayout.PropertyField(m_End);
36+
}
37+
else
38+
{
39+
EditorGUILayout.PropertyField(m_Density);
40+
}
41+
EditorGUI.indentLevel--;
42+
}
43+
}
44+
}

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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 skybox = depth < SKYBOX_THREASHOLD_VALUE;
65+
float dist = ComputeDistance(depth) - _Start;
66+
half fog = 1.0 - ComputeFog(dist);
67+
68+
return lerp(color, _FogColor, fog * skybox);
69+
}
70+
71+
ENDCG
72+
73+
SubShader
74+
{
75+
Cull Off ZWrite Off ZTest Always
76+
77+
Pass
78+
{
79+
CGPROGRAM
80+
81+
#pragma vertex VertFog
82+
#pragma fragment FragFog
83+
84+
ENDCG
85+
}
86+
}
87+
}

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);
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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
public static Settings defaultSettings
27+
{
28+
get
29+
{
30+
return new Settings
31+
{
32+
color = new Color32(102, 108, 113, 154),
33+
mode = FogMode.Exponential,
34+
density = 0.001f,
35+
start = 0f,
36+
end = 600f
37+
};
38+
}
39+
}
40+
}
41+
42+
[SerializeField]
43+
Settings m_Settings = Settings.defaultSettings;
44+
public Settings settings
45+
{
46+
get { return m_Settings; }
47+
set { m_Settings = value; }
48+
}
49+
50+
public override void Reset()
51+
{
52+
m_Settings = Settings.defaultSettings;
53+
}
54+
}
55+
}

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
@@ -33,6 +33,7 @@ public class PostProcessingBehaviour : MonoBehaviour
3333
BuiltinDebugViewsComponent m_DebugViews;
3434
AmbientOcclusionComponent m_AmbientOcclusion;
3535
ScreenSpaceReflectionComponent m_ScreenSpaceReflection;
36+
FogComponent m_FogComponent;
3637
MotionBlurComponent m_MotionBlur;
3738
TaaComponent m_Taa;
3839
EyeAdaptationComponent m_EyeAdaptation;
@@ -60,6 +61,7 @@ void OnEnable()
6061
m_DebugViews = AddComponent(new BuiltinDebugViewsComponent());
6162
m_AmbientOcclusion = AddComponent(new AmbientOcclusionComponent());
6263
m_ScreenSpaceReflection = AddComponent(new ScreenSpaceReflectionComponent());
64+
m_FogComponent = AddComponent(new FogComponent());
6365
m_MotionBlur = AddComponent(new MotionBlurComponent());
6466
m_Taa = AddComponent(new TaaComponent());
6567
m_EyeAdaptation = AddComponent(new EyeAdaptationComponent());
@@ -114,6 +116,7 @@ void OnPreCull()
114116
m_DebugViews.Init(context, profile.debugViews);
115117
m_AmbientOcclusion.Init(context, profile.ambientOcclusion);
116118
m_ScreenSpaceReflection.Init(context, profile.screenSpaceReflection);
119+
m_FogComponent.Init(context, profile.fog);
117120
m_MotionBlur.Init(context, profile.motionBlur);
118121
m_Taa.Init(context, profile.antialiasing);
119122
m_EyeAdaptation.Init(context, profile.eyeAdaptation);
@@ -161,6 +164,7 @@ void OnPreRender()
161164
TryExecuteCommandBuffer(m_DebugViews);
162165
TryExecuteCommandBuffer(m_AmbientOcclusion);
163166
TryExecuteCommandBuffer(m_ScreenSpaceReflection);
167+
TryExecuteCommandBuffer(m_FogComponent);
164168

165169
if (!m_RenderingInSceneView)
166170
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)