Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Reuse Depth buffer

Discussion in 'Shaders' started by jura_wd, Feb 16, 2014.

  1. jura_wd

    jura_wd

    Joined:
    Jan 28, 2014
    Posts:
    32
    Hey guys!

    Trying to implement a heat haze effect as a post-process.

    Rendering the scene as usual, without heating objects.
    Then rendering some heating objects using temporary camera... But wait, I must use depth for depth test from previous rendering pass!

    How I can do that? Maybe just MRT, but I cannot find out how to do that (yeah, I know that all color render buffers should be same resolution. but one of color RT should be.. null, or what?).
    Even then, MRT is not the best way, I want to just share depth.

    How can I do that in Unity?
     
    Last edited: Feb 16, 2014
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
  3. jura_wd

    jura_wd

    Joined:
    Jan 28, 2014
    Posts:
    32
    Thanks, I know about Graphics.SetRenderTarget(). Can you be more specific?

    Do you mean using MRT by passing RenderBuffer[] colorBuffers, something like passing first as my custom color buffer and second one for 'screen' (like null)?

    But.. MRT is an overkill for heat haze. It must be something simple, with wide hardware support. For example iOS doesn't support GL_COLOR_ATTACHMENT more that one (so MRT is not an option).
     
  4. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I think you can't use only part of the frame buffer. You might have to render your scene to one RenderTexture, and then do your haze render using the colour buffer of a second RenderTexture, but the depth buffer of the first.
     
  5. jura_wd

    jura_wd

    Joined:
    Jan 28, 2014
    Posts:
    32
    So, you mean:

    0) make sure that main camera is not rendering stuff
    1) render temp camera to custom render target #1 [color + depth] (which is same size as screen)
    2) render temp camera with heat objects to custom render target #2 [color + prev.depth] (which is same size as screen)
    3) combine that #1 and #2 in main camera with heat haze

    That's a dirty hack with memory overhead. I'm using deferred shading, so I guess step #1 is really complicated. I should modify internal PrePass stuff to make it works. And I really not sure that other stuff will work.
    And that's for heat haze?.. No, thanks.

    It's better to use surface shader + grabpass. It's not optimal at all, but I cannot see other options...

    Hey, Unity guys! Any thoughts?
     
  6. jura_wd

    jura_wd

    Joined:
    Jan 28, 2014
    Posts:
    32
    Well, ok.
    I can re-render all scene with z-only shader to downscaled RT. And then render heat objects.

    That's better. But still hack. And still not so optimal as it can be done in other engines/pure OpenGL/pure D3D.
     
  7. jura_wd

    jura_wd

    Joined:
    Jan 28, 2014
    Posts:
    32
    Ok, implemented that in separate camera + re-rendering all stuff with simple shader.
    And you know what? It is not working with d3d, only opengl... Have no idea why.

    How to check / debug that?
     
  8. HumphreyZhu

    HumphreyZhu

    Joined:
    Sep 19, 2015
    Posts:
    2
    Did you find a better way? I'm facing the same question now. I just want to change the color buffer not the depth buffer so I can draw glowing objects to another color buffer with the right depth culling.
     
  9. jura_wd

    jura_wd

    Joined:
    Jan 28, 2014
    Posts:
    32
    Nope, there is no way, as far as I know. Such easy thing impossible in Unity. wtf :(
     
  10. Maverick

    Maverick

    Joined:
    Dec 18, 2009
    Posts:
    239
    "...using the colour buffer of a second RenderTexture, but the depth buffer of the first." How you do that? Could you please elaborate? Especially using depth buffer of the prev camera part.

    Thanks.
     
    Boris-Evtifeev likes this.
  11. Boris-Evtifeev

    Boris-Evtifeev

    Joined:
    Sep 8, 2014
    Posts:
    2
    I'm trying to use command buffers for rendering some meshes in additional RT:
    CommandBuffer.SetRenderTarget(colorRT, BuiltinRenderTextureType.CameraTarget);
    but getting errors:
    "Assertion failed on expression: 'rcolorZero->backBuffer == rdepth->backBuffer'"
    in Unity 5.6.3p1 and 2017.1p4.

    How can I use this method properly -
    public void SetRenderTarget(Rendering.RenderTargetIdentifier color, Rendering.RenderTargetIdentifier depth);
    https://docs.unity3d.com/ScriptReference/Rendering.CommandBuffer.SetRenderTarget.html
    ? (if backbuffers of color and depth RTs must be the same)