I'm new to shader coding in Unity (and indeed shaders in general). I've recently been trying to create a basic fog effect by sampling the depth texture and using that to mix between the rendered view and a constant colour, however I'm having trouble getting the information from the depth texture.
From what I understand, the following code should simply display the depth texture in screen space on whatever object has this material applied, however my test object simply shows unshaded flat grey. I've done a couple other tests too and what I think is happening is that when I try to sample from the depth texture, it always returns a fixed4 of 0.5s but I don't understand why.
I know I'm probably making some hilarious noob error here, but I am a noob so any help would be appreciated. Thanks
Shader "Hidden/Atmosphere"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Scale ("Scale", float) = 1
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
GrabPass{"_Post"}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 ScreenPos : TEXCOORD0;
float4 vertex : SV_POSITION;
};
float _Scale;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.ScreenPos = ComputeScreenPos(o.vertex);
return o;
}
sampler2D _Post;
sampler2D _CameraDepthTexture;
fixed4 frag (v2f i) : SV_Target
{
float2 ScreenspaceUV = i.ScreenPos.xy/ i.ScreenPos.w;
//fixed4 col = tex2D(_Post, ScreenspaceUV);
fixed4 col = tex2D(_CameraDepthTexture, ScreenspaceUV);
//col = fixed4(i.ScreenPos.x/i.ScreenPos.w, i.ScreenPos.y/i.ScreenPos.w, 0, 1);
//col.rgb = 1 - col.rgb;
return col;
}
ENDCG
}
}
}