Copy depth texture using command buffer

Im trying to add a command buffer to a camera which should copy the depth texture into a render texture but I am getting a warning come up and nothing gets copied.

The warning is ‘built in render type 3 not found while executing’

It occurs in dx9/dx11 and forward/deferred.

The code that adds the command to the camera is…

            //Copy depths into temporary RT.
            int depthID = Shader.PropertyToID("_DepthCopyTexture"); 
            cmd.GetTemporaryRT(depthID, cam.pixelWidth, cam.pixelHeight, 0, FilterMode.Point, RenderTextureFormat.RFloat, RenderTextureReadWrite.Linear);
            cmd.Blit(BuiltinRenderTextureType.Depth, depthID);
            cmd.SetGlobalTexture("_DepthBuffer", depthID);

            cam.AddCommandBuffer(CameraEvent.AfterSkybox, cmd);

Ive tried a few different formats and camera events but nothing seems to work.

The closest Ive got is the ‘CameraEvent.AfterDepthTexture’ event but the contents of the texture does not look correct.

The only conclusion is that the depth texture does not exist when when the command gets executed. I need the command to run after opaque objects are drawn but before transparent one.

Any Ideas?

Once again Ive answered my own question…

Got this to work today. I could not get the BuiltinRenderTextureType.Depth to copy into a texture. Maybe theres a way but damned if I know. Instead I just used a blit command with a custom material that reads from _CameraDepthTexture into my render texture. This works in the AfterSkyBox event where I needed it.

1 Like

For anyone who finds this thread and still can’t get this to work, here’s my solution: Has anyone ever gotten a stencil buffer to copy with CommandBuffer.Blit? - Unity Engine - Unity Discussions

1 Like