I am trying to sample _LastCameraDepthTexture in an image effect shader, but the sample appears to be zero all the time. Anyone knows anything about this builtin? (_CameraDepthTexture behaves the same)
Shader "Hidden/SalzFog"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
sampler2D_float _LastCameraDepthTexture;
fixed4 frag (v2f_img i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
float depth = SAMPLE_DEPTH_TEXTURE(_LastCameraDepthTexture, i.uv);
return col * depth; // it's just black
}
ENDCG
}
}
// Fallback "Diffuse"
}
Well, I’m silly. It did work, but near plane distance is white (1.0) and far plane distance is black (0.0). Because of the non-linearity of the depth value only being very close to an object would yield a non-black pixel.
Now I want to convert this into a distance value. If the value was linear, the distance would be near + (1 - depth) * (far - near), but it is not linear. Is there documentation on how exactly the depth value is encoded?
fixed4 frag (v2f_img i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
float near = 0.1;
float far = 100;
float dist = near + (1 - depth) * (far - near);
return col * (far - dist) / far; // already pitch-black after a distance of 1 unit
}
There is a helper function for that, but documentation doesn’t really exist for that. You have to either dig through the included shaders or search on the forum:
Hi Im using 2 cam in scene and the _LastCameraDepthTexture turn out to be black, but the _CameraDepthTexture woeks, can you share you settings or do you know why?
using _LastCameraDepthTexture and _CameraDepthTexture: