I'm Trying to sample the camera depth texture inside a compute shader for occlusion culling.
I've been fetching the texture via Shader.GetGlobalTexture("_CameraDepthTexture")
and assigning it simply with computeShader.SetTexture()
.
My code worked with Unity 2021
and prior but after the update to Unity 6
, it didn't anymore.
After debugging the texture with a raw image
I can see that the depth texture doesn't seem to be generated. The texture name is UnityBlack
and has a size of 4px
x 4px
and is, as the name suggests, completley black.
What I've already setup:
Depth Texture
mode to On
on the camera.Depth Texture
on the Render pipeline asset
_cam.depthTextureMode = DepthTextureMode.Depth;
via scriptWhat else am I missing?
I've also used the frame debugger
and I can see that the CopyDepth
pass seems to have a correct depth texture.
Turns out Unity has changed how global textures are handled with the new version. Please refer to this answer I've got from the Unity staff.
In the end I found a much simpler solution than writing my own rendering feature.
I found out that the camera depth texture is available in the OnRenderObject
call. So I'm just fetching it there and doing my logic etc. in Update
.
Just make sure to add a null check before you start rendering.
Previously in 2021 and prior I also had to wait 1 frame until the depth texture was generated but this was still possible using only the Update
loop.