_CameraDepthTexture in URP

Hello,
In Built in render pipeline, this fragment shader:

float d = tex2Dproj(_CameraDepthTexture, i.projPos) //i.projPos is calculated via ComputeScreenPos(o.pos) in the vertex shader
return float4(d.rrr/nearPlaneDiv,1);

shows a correct depth texture, with [white/black] values on [near/far] planes, but only if there is a C# script with:

camera.depthTextureMode = DepthTextureMode.Depth;

I have two questions:

  • Why the same shader code in URP doesn’t work? (everything is black, no matter what value I use for nearPlaneDiv
  • is _CameraDepthTexture built-in shader variable is filled even if I don’t use camera.depthTextureMode?

I think I found the solution:

  • URP needs to enable Depth write in Camera settings
  • _CameraDepthTexture built-in shader variable is always present, but in order to be correctly initialized we should use DepthTextureMode.Depth for Builtin render pipeline, and enable Camera Depth writing in URP
1 Like