- Home /
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.
Answer by Razziel · Feb 20, 2018 at 09:58 PM
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.
Answer by klemen_unity · Mar 01, 2018 at 05:08 PM
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?
Hey @klemen_unity, I'm going to show you a script attached to the secondary camera:
[ExecuteInEdit$$anonymous$$ode]
public class ReplaceCameraShader : $$anonymous$$onoBehaviour {
public Camera mainCamera; // main camera
private Camera cameraComp; // second camera
public Shader replacedWith;
public static readonly string targetTextureName = "_TransparencyFocus$$anonymous$$ask";
public static readonly string targetDepthName = "_DepthFocus$$anonymous$$ask";
private RenderTexture target;
private RenderTexture targetDepth;
private void OnEnable() {
cameraComp = GetComponent<Camera>();
cameraComp.depthTexture$$anonymous$$ode = DepthTexture$$anonymous$$ode.None;
//cameraComp.SetReplacementShader(replacedWith, "Focused");
target = new RenderTexture(mainCamera.pixelWidth, mainCamera.pixelHeight, 24, RenderTextureFormat.ARGBFloat);
targetDepth = new RenderTexture(mainCamera.pixelWidth, mainCamera.pixelHeight, 24, RenderTextureFormat.Depth);
Shader.SetGlobalTexture(targetTextureName, target);
Shader.SetGlobalTexture(targetDepthName, targetDepth);
cameraComp.SetTargetBuffers(target.colorBuffer, targetDepth.depthBuffer);
}
private void OnDisable() {
cameraComp = GetComponent<Camera>();
//cameraComp .ResetReplacementShader();
if (cameraComp.targetTexture != null) {
RenderTexture temp = cameraComp.targetTexture;
cameraComp.targetTexture = null;
DestroyImmediate(temp);
}
target = null;
targetDepth = null;
Shader.SetGlobalTexture(targetDepthName, targetDepth);
Shader.SetGlobalTexture(targetTextureName, target);
}
}
With this script you can definitely use the secondary's camera depth texture AND color texture (what the camera sees). And you access them, in the shader with uniform sampler2D _DepthFocus$$anonymous$$ask
. You can see that i commented the lines mentioning the shader replacement. That's because i ran in a different problem. (You can see it here: https://answers.unity.com/questions/1472358/problem-with-camera-target-texture-and-replacement.html ) But, as far as getting the secondary's camera depth texture, this method works.
Your answer
Follow this Question
Related Questions
Camera.RenderWithShader not working as of Unity 5.3? 0 Answers
Problem with camera target texture and replacement shader 0 Answers
RenderTexture with image effects baked in 2 Answers
dynamic hole in layer / texture / camera 0 Answers
Rendering the same Render Texture differently to separate cameras 0 Answers