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