unity3d: Use main camera's depth buffer for rendering another camera view
After my main camera renders, I'd like to use (or copy) its depth buffer to a (disabled) camera's depth buffer.
My goal is to draw particles onto a smaller render target (using a separate camera) while using the depth buffer after opaque objects are drawn.
I can't do this in a single camera, since the goal is to use a smaller render target for the particles for performance reasons.
Replacement shaders in Unity aren't an option either: I want my particles to use their existing shaders - i just want the depth buffer of the particle camera to be overwritten with a subsampled version of the main camera's depth buffer before the particles are drawn.
I didn't get any reply to my earlier question; hence, the repost.
Here's the script attached to my main camera. It renders all the non-particle layers and I use OnRenderImage to invoke the particle camera.
public class MagicRenderer : MonoBehaviour
public Shader particleShader; // shader that uses the main camera's depth buffer to depth test particle Z
public Material blendMat; // material that uses a simple blend shader
public int downSampleFactor = 1;
private RenderTexture particleRT;
private static GameObject pCam;
void Awake ()
// make the main cameras depth buffer available to the shaders via _CameraDepthTexture
camera.depthTextureMode = DepthTextureMode.Depth;
// Update is called once per frame
void Update ()
void OnRenderImage(RenderTexture src, RenderTexture dest)
// create tmp RT
particleRT = RenderTexture.GetTemporary (Screen.width / downSampleFactor, Screen.height / downSampleFactor, 0);
particleRT.antiAliasing = 1;
// create particle cam
Camera pCam = GetPCam ();
pCam.CopyFrom (camera);
pCam.clearFlags = CameraClearFlags.SolidColor;
pCam.backgroundColor = new Color (0.0f, 0.0f, 0.0f, 0.0f);
pCam.cullingMask = 1 << LayerMask.NameToLayer ("Particles");
pCam.useOcclusionCulling = false;
pCam.targetTexture = particleRT;
pCam.depth = 0;
// Draw to particleRT's colorBuffer using mainCam's depth buffer
// ?? - how do i transfer this camera's depth buffer to pCam?
pCam.Render ();
// pCam.RenderWithShader (particleShader, "Transparent"); // I don't want to replace the shaders my particles use; os shader replacement isnt an option.
// blend mainCam's colorBuffer with particleRT's colorBuffer
// Graphics.Blit(pCam.targetTexture, src, blendMat);
// copy resulting buffer to destination
Graphics.Blit (pCam.targetTexture, dest);
// clean up
RenderTexture.ReleaseTemporary(particleRT);
static public Camera GetPCam()
if (!pCam)
GameObject oldpcam = GameObject.Find("pCam");
Debug.Log (oldpcam);
if (oldpcam) Destroy(oldpcam);
pCam = new GameObject("pCam");
pCam.AddComponent<Camera>();
pCam.camera.enabled = false;
pCam.hideFlags = HideFlags.DontSave;
return pCam.camera;
I've a few additional questions:
1) Why does camera.depthTextureMode = DepthTextureMode.Depth; end up drawing all the objects in the scene just to write to the Z-buffer? Using Intel GPA, I see two passes before OnRenderImage gets called:
(i) Z-PrePass, that only writes to the depth buffer
(ii) Color pass, that writes to both the color and depth buffer.
2) I re-rendered the opaque objects to pCam's RT using a replacement shader that writes (0,0,0,0) to the colorBuffer with ZWrite On (to overcome the depth buffer transfer problem). After that, I reset the layers and clear mask as follows:
pCam.cullingMask = 1 << LayerMask.NameToLayer ("Particles");
pCam.clearFlags = CameraClearFlags.Nothing;
and rendered them using pCam.Render().
I thought this would render the particles using their existing shaders with the ZTest.
Unfortunately, what I notice is that the depth-stencil buffer is cleared before the particles are drawn (inspite me not clearing anything..).
Why does this happen?
graphics unity3d rendering
add a comment |
After my main camera renders, I'd like to use (or copy) its depth buffer to a (disabled) camera's depth buffer.
My goal is to draw particles onto a smaller render target (using a separate camera) while using the depth buffer after opaque objects are drawn.
I can't do this in a single camera, since the goal is to use a smaller render target for the particles for performance reasons.
Replacement shaders in Unity aren't an option either: I want my particles to use their existing shaders - i just want the depth buffer of the particle camera to be overwritten with a subsampled version of the main camera's depth buffer before the particles are drawn.
I didn't get any reply to my earlier question; hence, the repost.
Here's the script attached to my main camera. It renders all the non-particle layers and I use OnRenderImage to invoke the particle camera.
public class MagicRenderer : MonoBehaviour
public Shader particleShader; // shader that uses the main camera's depth buffer to depth test particle Z
public Material blendMat; // material that uses a simple blend shader
public int downSampleFactor = 1;
private RenderTexture particleRT;
private static GameObject pCam;
void Awake ()
// make the main cameras depth buffer available to the shaders via _CameraDepthTexture
camera.depthTextureMode = DepthTextureMode.Depth;
// Update is called once per frame
void Update ()
void OnRenderImage(RenderTexture src, RenderTexture dest)
// create tmp RT
particleRT = RenderTexture.GetTemporary (Screen.width / downSampleFactor, Screen.height / downSampleFactor, 0);
particleRT.antiAliasing = 1;
// create particle cam
Camera pCam = GetPCam ();
pCam.CopyFrom (camera);
pCam.clearFlags = CameraClearFlags.SolidColor;
pCam.backgroundColor = new Color (0.0f, 0.0f, 0.0f, 0.0f);
pCam.cullingMask = 1 << LayerMask.NameToLayer ("Particles");
pCam.useOcclusionCulling = false;
pCam.targetTexture = particleRT;
pCam.depth = 0;
// Draw to particleRT's colorBuffer using mainCam's depth buffer
// ?? - how do i transfer this camera's depth buffer to pCam?
pCam.Render ();
// pCam.RenderWithShader (particleShader, "Transparent"); // I don't want to replace the shaders my particles use; os shader replacement isnt an option.
// blend mainCam's colorBuffer with particleRT's colorBuffer
// Graphics.Blit(pCam.targetTexture, src, blendMat);
// copy resulting buffer to destination
Graphics.Blit (pCam.targetTexture, dest);
// clean up
RenderTexture.ReleaseTemporary(particleRT);
static public Camera GetPCam()
if (!pCam)
GameObject oldpcam = GameObject.Find("pCam");
Debug.Log (oldpcam);
if (oldpcam) Destroy(oldpcam);
pCam = new GameObject("pCam");
pCam.AddComponent<Camera>();
pCam.camera.enabled = false;
pCam.hideFlags = HideFlags.DontSave;
return pCam.camera;
I've a few additional questions:
1) Why does camera.depthTextureMode = DepthTextureMode.Depth; end up drawing all the objects in the scene just to write to the Z-buffer? Using Intel GPA, I see two passes before OnRenderImage gets called:
(i) Z-PrePass, that only writes to the depth buffer
(ii) Color pass, that writes to both the color and depth buffer.
2) I re-rendered the opaque objects to pCam's RT using a replacement shader that writes (0,0,0,0) to the colorBuffer with ZWrite On (to overcome the depth buffer transfer problem). After that, I reset the layers and clear mask as follows:
pCam.cullingMask = 1 << LayerMask.NameToLayer ("Particles");
pCam.clearFlags = CameraClearFlags.Nothing;
and rendered them using pCam.Render().
I thought this would render the particles using their existing shaders with the ZTest.
Unfortunately, what I notice is that the depth-stencil buffer is cleared before the particles are drawn (inspite me not clearing anything..).
Why does this happen?
graphics unity3d rendering
Sorry I don't know how to copy the depth buffer as you describe. Just to clarify, why not use render texture for it's normal use case? From Unity docs: -Create a new Render Texture asset using Assets->Create->Render Texture. - Create a new Camera using GameObject->Create Other->Camera. - Assign the Render Texture to the Target Texture of the new Camera. - Create a wide, tall and thin box - Drag the Render Texture onto it to create a Material that uses the render texture. - Enter Play Mode, and observe that the box’s texture is updated in real-time based on the new Camera’s output.
– Alex Rice
Apr 6 '14 at 16:09
Thanks for the reply. MSAA for alpha-blended particles is too costly and I want to cut down on it. I can't just render the particles to a separate small RT without a depth test since I need to ensure that particles behind opaque objects are Z-culled.
– Raja
Apr 7 '14 at 18:51
have you tried setting the clear flags to Don't clear, render like this, then setting the flags to clear color and render again, just to wipe out the color?
– Radu Diță
Apr 8 '14 at 7:06
yeah. before i render the particles, i need to transfer the downsampled depth buffer (of the first camera's RT) to the second RT. that's where i'm struggling.
– Raja
Apr 8 '14 at 16:29
add a comment |
After my main camera renders, I'd like to use (or copy) its depth buffer to a (disabled) camera's depth buffer.
My goal is to draw particles onto a smaller render target (using a separate camera) while using the depth buffer after opaque objects are drawn.
I can't do this in a single camera, since the goal is to use a smaller render target for the particles for performance reasons.
Replacement shaders in Unity aren't an option either: I want my particles to use their existing shaders - i just want the depth buffer of the particle camera to be overwritten with a subsampled version of the main camera's depth buffer before the particles are drawn.
I didn't get any reply to my earlier question; hence, the repost.
Here's the script attached to my main camera. It renders all the non-particle layers and I use OnRenderImage to invoke the particle camera.
public class MagicRenderer : MonoBehaviour
public Shader particleShader; // shader that uses the main camera's depth buffer to depth test particle Z
public Material blendMat; // material that uses a simple blend shader
public int downSampleFactor = 1;
private RenderTexture particleRT;
private static GameObject pCam;
void Awake ()
// make the main cameras depth buffer available to the shaders via _CameraDepthTexture
camera.depthTextureMode = DepthTextureMode.Depth;
// Update is called once per frame
void Update ()
void OnRenderImage(RenderTexture src, RenderTexture dest)
// create tmp RT
particleRT = RenderTexture.GetTemporary (Screen.width / downSampleFactor, Screen.height / downSampleFactor, 0);
particleRT.antiAliasing = 1;
// create particle cam
Camera pCam = GetPCam ();
pCam.CopyFrom (camera);
pCam.clearFlags = CameraClearFlags.SolidColor;
pCam.backgroundColor = new Color (0.0f, 0.0f, 0.0f, 0.0f);
pCam.cullingMask = 1 << LayerMask.NameToLayer ("Particles");
pCam.useOcclusionCulling = false;
pCam.targetTexture = particleRT;
pCam.depth = 0;
// Draw to particleRT's colorBuffer using mainCam's depth buffer
// ?? - how do i transfer this camera's depth buffer to pCam?
pCam.Render ();
// pCam.RenderWithShader (particleShader, "Transparent"); // I don't want to replace the shaders my particles use; os shader replacement isnt an option.
// blend mainCam's colorBuffer with particleRT's colorBuffer
// Graphics.Blit(pCam.targetTexture, src, blendMat);
// copy resulting buffer to destination
Graphics.Blit (pCam.targetTexture, dest);
// clean up
RenderTexture.ReleaseTemporary(particleRT);
static public Camera GetPCam()
if (!pCam)
GameObject oldpcam = GameObject.Find("pCam");
Debug.Log (oldpcam);
if (oldpcam) Destroy(oldpcam);
pCam = new GameObject("pCam");
pCam.AddComponent<Camera>();
pCam.camera.enabled = false;
pCam.hideFlags = HideFlags.DontSave;
return pCam.camera;
I've a few additional questions:
1) Why does camera.depthTextureMode = DepthTextureMode.Depth; end up drawing all the objects in the scene just to write to the Z-buffer? Using Intel GPA, I see two passes before OnRenderImage gets called:
(i) Z-PrePass, that only writes to the depth buffer
(ii) Color pass, that writes to both the color and depth buffer.
2) I re-rendered the opaque objects to pCam's RT using a replacement shader that writes (0,0,0,0) to the colorBuffer with ZWrite On (to overcome the depth buffer transfer problem). After that, I reset the layers and clear mask as follows:
pCam.cullingMask = 1 << LayerMask.NameToLayer ("Particles");
pCam.clearFlags = CameraClearFlags.Nothing;
and rendered them using pCam.Render().
I thought this would render the particles using their existing shaders with the ZTest.
Unfortunately, what I notice is that the depth-stencil buffer is cleared before the particles are drawn (inspite me not clearing anything..).
Why does this happen?
graphics unity3d rendering
After my main camera renders, I'd like to use (or copy) its depth buffer to a (disabled) camera's depth buffer.
My goal is to draw particles onto a smaller render target (using a separate camera) while using the depth buffer after opaque objects are drawn.
I can't do this in a single camera, since the goal is to use a smaller render target for the particles for performance reasons.
Replacement shaders in Unity aren't an option either: I want my particles to use their existing shaders - i just want the depth buffer of the particle camera to be overwritten with a subsampled version of the main camera's depth buffer before the particles are drawn.
I didn't get any reply to my earlier question; hence, the repost.
Here's the script attached to my main camera. It renders all the non-particle layers and I use OnRenderImage to invoke the particle camera.
public class MagicRenderer : MonoBehaviour
public Shader particleShader; // shader that uses the main camera's depth buffer to depth test particle Z
public Material blendMat; // material that uses a simple blend shader
public int downSampleFactor = 1;
private RenderTexture particleRT;
private static GameObject pCam;
void Awake ()
// make the main cameras depth buffer available to the shaders via _CameraDepthTexture
camera.depthTextureMode = DepthTextureMode.Depth;
// Update is called once per frame
void Update ()
void OnRenderImage(RenderTexture src, RenderTexture dest)
// create tmp RT
particleRT = RenderTexture.GetTemporary (Screen.width / downSampleFactor, Screen.height / downSampleFactor, 0);
particleRT.antiAliasing = 1;
// create particle cam
Camera pCam = GetPCam ();
pCam.CopyFrom (camera);
pCam.clearFlags = CameraClearFlags.SolidColor;
pCam.backgroundColor = new Color (0.0f, 0.0f, 0.0f, 0.0f);
pCam.cullingMask = 1 << LayerMask.NameToLayer ("Particles");
pCam.useOcclusionCulling = false;
pCam.targetTexture = particleRT;
pCam.depth = 0;
// Draw to particleRT's colorBuffer using mainCam's depth buffer
// ?? - how do i transfer this camera's depth buffer to pCam?
pCam.Render ();
// pCam.RenderWithShader (particleShader, "Transparent"); // I don't want to replace the shaders my particles use; os shader replacement isnt an option.
// blend mainCam's colorBuffer with particleRT's colorBuffer
// Graphics.Blit(pCam.targetTexture, src, blendMat);
// copy resulting buffer to destination
Graphics.Blit (pCam.targetTexture, dest);
// clean up
RenderTexture.ReleaseTemporary(particleRT);
static public Camera GetPCam()
if (!pCam)
GameObject oldpcam = GameObject.Find("pCam");
Debug.Log (oldpcam);
if (oldpcam) Destroy(oldpcam);
pCam = new GameObject("pCam");
pCam.AddComponent<Camera>();
pCam.camera.enabled = false;
pCam.hideFlags = HideFlags.DontSave;
return pCam.camera;
I've a few additional questions:
1) Why does camera.depthTextureMode = DepthTextureMode.Depth; end up drawing all the objects in the scene just to write to the Z-buffer? Using Intel GPA, I see two passes before OnRenderImage gets called:
(i) Z-PrePass, that only writes to the depth buffer
(ii) Color pass, that writes to both the color and depth buffer.
2) I re-rendered the opaque objects to pCam's RT using a replacement shader that writes (0,0,0,0) to the colorBuffer with ZWrite On (to overcome the depth buffer transfer problem). After that, I reset the layers and clear mask as follows:
pCam.cullingMask = 1 << LayerMask.NameToLayer ("Particles");
pCam.clearFlags = CameraClearFlags.Nothing;
and rendered them using pCam.Render().
I thought this would render the particles using their existing shaders with the ZTest.
Unfortunately, what I notice is that the depth-stencil buffer is cleared before the particles are drawn (inspite me not clearing anything..).
Why does this happen?
graphics unity3d rendering
graphics unity3d rendering
edited May 23 '17 at 12:10
Community♦
11
11
asked Mar 27 '14 at 18:24
Raja
1,03831223
1,03831223
Sorry I don't know how to copy the depth buffer as you describe. Just to clarify, why not use render texture for it's normal use case? From Unity docs: -Create a new Render Texture asset using Assets->Create->Render Texture. - Create a new Camera using GameObject->Create Other->Camera. - Assign the Render Texture to the Target Texture of the new Camera. - Create a wide, tall and thin box - Drag the Render Texture onto it to create a Material that uses the render texture. - Enter Play Mode, and observe that the box’s texture is updated in real-time based on the new Camera’s output.
– Alex Rice
Apr 6 '14 at 16:09
Thanks for the reply. MSAA for alpha-blended particles is too costly and I want to cut down on it. I can't just render the particles to a separate small RT without a depth test since I need to ensure that particles behind opaque objects are Z-culled.
– Raja
Apr 7 '14 at 18:51
have you tried setting the clear flags to Don't clear, render like this, then setting the flags to clear color and render again, just to wipe out the color?
– Radu Diță
Apr 8 '14 at 7:06
yeah. before i render the particles, i need to transfer the downsampled depth buffer (of the first camera's RT) to the second RT. that's where i'm struggling.
– Raja
Apr 8 '14 at 16:29
add a comment |
Sorry I don't know how to copy the depth buffer as you describe. Just to clarify, why not use render texture for it's normal use case? From Unity docs: -Create a new Render Texture asset using Assets->Create->Render Texture. - Create a new Camera using GameObject->Create Other->Camera. - Assign the Render Texture to the Target Texture of the new Camera. - Create a wide, tall and thin box - Drag the Render Texture onto it to create a Material that uses the render texture. - Enter Play Mode, and observe that the box’s texture is updated in real-time based on the new Camera’s output.
– Alex Rice
Apr 6 '14 at 16:09
Thanks for the reply. MSAA for alpha-blended particles is too costly and I want to cut down on it. I can't just render the particles to a separate small RT without a depth test since I need to ensure that particles behind opaque objects are Z-culled.
– Raja
Apr 7 '14 at 18:51
have you tried setting the clear flags to Don't clear, render like this, then setting the flags to clear color and render again, just to wipe out the color?
– Radu Diță
Apr 8 '14 at 7:06
yeah. before i render the particles, i need to transfer the downsampled depth buffer (of the first camera's RT) to the second RT. that's where i'm struggling.
– Raja
Apr 8 '14 at 16:29
Sorry I don't know how to copy the depth buffer as you describe. Just to clarify, why not use render texture for it's normal use case? From Unity docs: -Create a new Render Texture asset using Assets->Create->Render Texture. - Create a new Camera using GameObject->Create Other->Camera. - Assign the Render Texture to the Target Texture of the new Camera. - Create a wide, tall and thin box - Drag the Render Texture onto it to create a Material that uses the render texture. - Enter Play Mode, and observe that the box’s texture is updated in real-time based on the new Camera’s output.
– Alex Rice
Apr 6 '14 at 16:09
Sorry I don't know how to copy the depth buffer as you describe. Just to clarify, why not use render texture for it's normal use case? From Unity docs: -Create a new Render Texture asset using Assets->Create->Render Texture. - Create a new Camera using GameObject->Create Other->Camera. - Assign the Render Texture to the Target Texture of the new Camera. - Create a wide, tall and thin box - Drag the Render Texture onto it to create a Material that uses the render texture. - Enter Play Mode, and observe that the box’s texture is updated in real-time based on the new Camera’s output.
– Alex Rice
Apr 6 '14 at 16:09
Thanks for the reply. MSAA for alpha-blended particles is too costly and I want to cut down on it. I can't just render the particles to a separate small RT without a depth test since I need to ensure that particles behind opaque objects are Z-culled.
– Raja
Apr 7 '14 at 18:51
Thanks for the reply. MSAA for alpha-blended particles is too costly and I want to cut down on it. I can't just render the particles to a separate small RT without a depth test since I need to ensure that particles behind opaque objects are Z-culled.
– Raja
Apr 7 '14 at 18:51
have you tried setting the clear flags to Don't clear, render like this, then setting the flags to clear color and render again, just to wipe out the color?
– Radu Diță
Apr 8 '14 at 7:06
have you tried setting the clear flags to Don't clear, render like this, then setting the flags to clear color and render again, just to wipe out the color?
– Radu Diță
Apr 8 '14 at 7:06
yeah. before i render the particles, i need to transfer the downsampled depth buffer (of the first camera's RT) to the second RT. that's where i'm struggling.
– Raja
Apr 8 '14 at 16:29
yeah. before i render the particles, i need to transfer the downsampled depth buffer (of the first camera's RT) to the second RT. that's where i'm struggling.
– Raja
Apr 8 '14 at 16:29
add a comment |
1 Answer
1
active
oldest
votes
I managed to reuse camera Z-buffer "manually" in the shader used for rendering. See http://forum.unity3d.com/threads/reuse-depth-buffer-of-main-camera.280460/ for more.
Just alter the particle shader you use already for particle rendering.
Ah, i wanted to avoid doing the depth test in the pixel shader. What do you need to do in script to pass the depth texture though? Is there aMaterial.SetTexture("_CameraDepthTexture", Camera.main.<something>line somewhere?
– Raja
Nov 19 '14 at 1:20
Simply docs.unity3d.com/ScriptReference/Camera-depthTextureMode.html
– Geri
Nov 19 '14 at 16:05
Once you added that line, the depth texture gets "advertised" globally all over the shaders, accessible through_CameraDepthTexture.
– Geri
Nov 19 '14 at 16:06
You may create a pass that writes the Z-buffer of theRenderTexturebefore actual rendering, then ZTests will probably done automatically.
– Geri
Nov 19 '14 at 16:08
Interesting. I'm gonna check it out. When i last profiled my scene wherein i setcamera.depthTextureModein my script, I noticed that Unity does a Z-prepass with the entire scene (as seen by that camera). That really sucks from a perf point of view. Submitting all the geometry twice instead of just reusing the Z-buffer is a huge hit.
– Raja
Nov 19 '14 at 21:40
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22696123%2funity3d-use-main-cameras-depth-buffer-for-rendering-another-camera-view%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I managed to reuse camera Z-buffer "manually" in the shader used for rendering. See http://forum.unity3d.com/threads/reuse-depth-buffer-of-main-camera.280460/ for more.
Just alter the particle shader you use already for particle rendering.
Ah, i wanted to avoid doing the depth test in the pixel shader. What do you need to do in script to pass the depth texture though? Is there aMaterial.SetTexture("_CameraDepthTexture", Camera.main.<something>line somewhere?
– Raja
Nov 19 '14 at 1:20
Simply docs.unity3d.com/ScriptReference/Camera-depthTextureMode.html
– Geri
Nov 19 '14 at 16:05
Once you added that line, the depth texture gets "advertised" globally all over the shaders, accessible through_CameraDepthTexture.
– Geri
Nov 19 '14 at 16:06
You may create a pass that writes the Z-buffer of theRenderTexturebefore actual rendering, then ZTests will probably done automatically.
– Geri
Nov 19 '14 at 16:08
Interesting. I'm gonna check it out. When i last profiled my scene wherein i setcamera.depthTextureModein my script, I noticed that Unity does a Z-prepass with the entire scene (as seen by that camera). That really sucks from a perf point of view. Submitting all the geometry twice instead of just reusing the Z-buffer is a huge hit.
– Raja
Nov 19 '14 at 21:40
add a comment |
I managed to reuse camera Z-buffer "manually" in the shader used for rendering. See http://forum.unity3d.com/threads/reuse-depth-buffer-of-main-camera.280460/ for more.
Just alter the particle shader you use already for particle rendering.
Ah, i wanted to avoid doing the depth test in the pixel shader. What do you need to do in script to pass the depth texture though? Is there aMaterial.SetTexture("_CameraDepthTexture", Camera.main.<something>line somewhere?
– Raja
Nov 19 '14 at 1:20
Simply docs.unity3d.com/ScriptReference/Camera-depthTextureMode.html
– Geri
Nov 19 '14 at 16:05
Once you added that line, the depth texture gets "advertised" globally all over the shaders, accessible through_CameraDepthTexture.
– Geri
Nov 19 '14 at 16:06
You may create a pass that writes the Z-buffer of theRenderTexturebefore actual rendering, then ZTests will probably done automatically.
– Geri
Nov 19 '14 at 16:08
Interesting. I'm gonna check it out. When i last profiled my scene wherein i setcamera.depthTextureModein my script, I noticed that Unity does a Z-prepass with the entire scene (as seen by that camera). That really sucks from a perf point of view. Submitting all the geometry twice instead of just reusing the Z-buffer is a huge hit.
– Raja
Nov 19 '14 at 21:40
add a comment |
I managed to reuse camera Z-buffer "manually" in the shader used for rendering. See http://forum.unity3d.com/threads/reuse-depth-buffer-of-main-camera.280460/ for more.
Just alter the particle shader you use already for particle rendering.
I managed to reuse camera Z-buffer "manually" in the shader used for rendering. See http://forum.unity3d.com/threads/reuse-depth-buffer-of-main-camera.280460/ for more.
Just alter the particle shader you use already for particle rendering.
answered Nov 18 '14 at 20:13
Geri
8,7391177133
8,7391177133
Ah, i wanted to avoid doing the depth test in the pixel shader. What do you need to do in script to pass the depth texture though? Is there aMaterial.SetTexture("_CameraDepthTexture", Camera.main.<something>line somewhere?
– Raja
Nov 19 '14 at 1:20
Simply docs.unity3d.com/ScriptReference/Camera-depthTextureMode.html
– Geri
Nov 19 '14 at 16:05
Once you added that line, the depth texture gets "advertised" globally all over the shaders, accessible through_CameraDepthTexture.
– Geri
Nov 19 '14 at 16:06
You may create a pass that writes the Z-buffer of theRenderTexturebefore actual rendering, then ZTests will probably done automatically.
– Geri
Nov 19 '14 at 16:08
Interesting. I'm gonna check it out. When i last profiled my scene wherein i setcamera.depthTextureModein my script, I noticed that Unity does a Z-prepass with the entire scene (as seen by that camera). That really sucks from a perf point of view. Submitting all the geometry twice instead of just reusing the Z-buffer is a huge hit.
– Raja
Nov 19 '14 at 21:40
add a comment |
Ah, i wanted to avoid doing the depth test in the pixel shader. What do you need to do in script to pass the depth texture though? Is there aMaterial.SetTexture("_CameraDepthTexture", Camera.main.<something>line somewhere?
– Raja
Nov 19 '14 at 1:20
Simply docs.unity3d.com/ScriptReference/Camera-depthTextureMode.html
– Geri
Nov 19 '14 at 16:05
Once you added that line, the depth texture gets "advertised" globally all over the shaders, accessible through_CameraDepthTexture.
– Geri
Nov 19 '14 at 16:06
You may create a pass that writes the Z-buffer of theRenderTexturebefore actual rendering, then ZTests will probably done automatically.
– Geri
Nov 19 '14 at 16:08
Interesting. I'm gonna check it out. When i last profiled my scene wherein i setcamera.depthTextureModein my script, I noticed that Unity does a Z-prepass with the entire scene (as seen by that camera). That really sucks from a perf point of view. Submitting all the geometry twice instead of just reusing the Z-buffer is a huge hit.
– Raja
Nov 19 '14 at 21:40
Ah, i wanted to avoid doing the depth test in the pixel shader. What do you need to do in script to pass the depth texture though? Is there a
Material.SetTexture("_CameraDepthTexture", Camera.main.<something> line somewhere?– Raja
Nov 19 '14 at 1:20
Ah, i wanted to avoid doing the depth test in the pixel shader. What do you need to do in script to pass the depth texture though? Is there a
Material.SetTexture("_CameraDepthTexture", Camera.main.<something> line somewhere?– Raja
Nov 19 '14 at 1:20
Simply docs.unity3d.com/ScriptReference/Camera-depthTextureMode.html
– Geri
Nov 19 '14 at 16:05
Simply docs.unity3d.com/ScriptReference/Camera-depthTextureMode.html
– Geri
Nov 19 '14 at 16:05
Once you added that line, the depth texture gets "advertised" globally all over the shaders, accessible through
_CameraDepthTexture.– Geri
Nov 19 '14 at 16:06
Once you added that line, the depth texture gets "advertised" globally all over the shaders, accessible through
_CameraDepthTexture.– Geri
Nov 19 '14 at 16:06
You may create a pass that writes the Z-buffer of the
RenderTexture before actual rendering, then ZTests will probably done automatically.– Geri
Nov 19 '14 at 16:08
You may create a pass that writes the Z-buffer of the
RenderTexture before actual rendering, then ZTests will probably done automatically.– Geri
Nov 19 '14 at 16:08
Interesting. I'm gonna check it out. When i last profiled my scene wherein i set
camera.depthTextureMode in my script, I noticed that Unity does a Z-prepass with the entire scene (as seen by that camera). That really sucks from a perf point of view. Submitting all the geometry twice instead of just reusing the Z-buffer is a huge hit.– Raja
Nov 19 '14 at 21:40
Interesting. I'm gonna check it out. When i last profiled my scene wherein i set
camera.depthTextureMode in my script, I noticed that Unity does a Z-prepass with the entire scene (as seen by that camera). That really sucks from a perf point of view. Submitting all the geometry twice instead of just reusing the Z-buffer is a huge hit.– Raja
Nov 19 '14 at 21:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22696123%2funity3d-use-main-cameras-depth-buffer-for-rendering-another-camera-view%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Sorry I don't know how to copy the depth buffer as you describe. Just to clarify, why not use render texture for it's normal use case? From Unity docs: -Create a new Render Texture asset using Assets->Create->Render Texture. - Create a new Camera using GameObject->Create Other->Camera. - Assign the Render Texture to the Target Texture of the new Camera. - Create a wide, tall and thin box - Drag the Render Texture onto it to create a Material that uses the render texture. - Enter Play Mode, and observe that the box’s texture is updated in real-time based on the new Camera’s output.
– Alex Rice
Apr 6 '14 at 16:09
Thanks for the reply. MSAA for alpha-blended particles is too costly and I want to cut down on it. I can't just render the particles to a separate small RT without a depth test since I need to ensure that particles behind opaque objects are Z-culled.
– Raja
Apr 7 '14 at 18:51
have you tried setting the clear flags to Don't clear, render like this, then setting the flags to clear color and render again, just to wipe out the color?
– Radu Diță
Apr 8 '14 at 7:06
yeah. before i render the particles, i need to transfer the downsampled depth buffer (of the first camera's RT) to the second RT. that's where i'm struggling.
– Raja
Apr 8 '14 at 16:29