This is also straightforward to incorporate into a Surface shader.:
Then modify our inputInput struct to include the screenPosscreenPos parameter, which signals to Unity that it should pass the screenspace position of the fragment to us:
Now we use the z value of this screenspace coordinate to blend between our texture colour and the fog colour, z being the depth or distance of the fragment from the camera:
Shader "Unlit/UnlitManualFog"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_FogColor ("Fog Color", Color) = (0.5, 0.5, 0.5, 1.0)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
// Add a variable to carry depth information
float depth : TEXCOORD1;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _FogColor;
v2f vert (appdata v)
{
v2f o;
// Transform vertex into view space
o.vertex = mul(UNITY_MATRIX_MV, v.vertex);
// Copy the viewspace z coordinate and call it depth
o.depth = o.vertex.z;
// Finish transforming the vetex by applying the projection matrix
o.vertex = mul(UNITY_MATRIX_P, o.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
// Blend colour using depth parameter we calculated earlier
return lerp(col, _FogColor, saturate(-0.05f * i.depth));
}
ENDCG
}
}
}


