Camera Depth Texture from a second camera with replacement shader

I’m trying to access another camera’s depth texture inside a shader.

The setup is like this:

The main camera has no script attached that renders to depth buffer or something. It’s a simple camera.
The second camera has a replacement shader, replaced by a script “ReplaceAndUseDepth.cs”, that renders only some objects from the scene (those who have a shader tagged with “Focused”).

The problem that I can’t solve:

I want this replacement shader to make use of the second camera’s depth buffer. But accessing it with uniform sampler2D _CameraDepthTexture will not get me anything.

I managed to solve this. My second’s camera target texture was set to something different than null. That seemed to cause the problem. Also, I used custom target buffers to make sure that I get the second’s camera depth buffer, using :

target = new RenderTexture(mainCamera.pixelWidth, mainCamera.pixelHeight, 16, RenderTextureFormat.Default);
        targetDepth = new RenderTexture(mainCamera.pixelWidth, mainCamera.pixelHeight, 24, RenderTextureFormat.Depth);

        secondCamera.SetTargetBuffers(target.colorBuffer, targetDepth.depthBuffer);

        //secondCamera.targetTexture = target; **COMMENTED LINE**

        Shader.SetGlobalTexture(targetColorName, target);
        Shader.SetGlobalTexture(targetDepthName, targetDepth);

With this code, using the uniform sampler2D [targetDepthName]will get me exactly what _CameraDepthTexture would normally get me if I had a simple setup with only one camera.

Hey @Razziel, I’m trying to do the same thing, use a secondary camera’s depth in a shader. _LastCameraDepthTexture always looks the same as _CameraDepthTexture for me. Are you able to share a more detailed breakdown of your solution?