Search Unity

Blit RenderTexture into Camera's depth texture?

Discussion in 'General Graphics' started by JM_CG, Jun 27, 2019.

  1. JM_CG

    JM_CG

    Joined:
    Apr 20, 2017
    Posts:
    35
    Is there a way I can blit my own depth RenderTexture into the Camera's depth texture?

    I'm doing all my scene rendering in OnRenderImage (as my scene is mostly procedural), and then have a stack of post processing effects after it that I would like to work appropriately (e.g depth of field, SSAO) that rely on the depth buffer having the right information.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    From OnRenderImage? No.

    Using command buffers? Yes.

    You can either render your geometry during the AfterDepthTexture camera event and using your shader’s shadow caster pass, or generate your own custom depth texture and copy it in either during that same event using the Hidden/DepthCopy shader, or in a later camera event like BeforeImageEffects and Blit or CopyTexture it to the color value of the BuiltinRenderTextureType.Depth texture. If you need the normals from the camera depth normals texture, there’s no specific event for that, so you have to render to it later.


    The other option is to use the deferred rendering path and render your objects during the AfterGBuffer camera event with a suitable deferred shader. Then those objects will automatically get pushed into both the depth texture and depth normals texture as they get extracted from the gbuffers.
     
    AlejMC and JM_CG like this.
  3. AlejMC

    AlejMC

    Joined:
    Oct 15, 2013
    Posts:
    149
    @bgolus just wanted to mention that I have used the CommandBuffer AfterDepthTexture to render some transparent objects to the depth buffer (2D-like characters on a 3D environment) and it works amazingly well.

    Luckily when the AfterDepthTexture draws, the render targets are still intact to be able to draw new stuff onto it (i.e. not cleared or swapped).

    I just make sure to collect the renderers to a global list, each character adds/remove itself to it (either via OnEnable/OnDisable or via BecameVisible/BecameInvisible monobehaviour callbacks). The renderer's materials need to have said ShadowCaster pass which is used when the command buffer draw renderer needs to call a specific material's pass.

    Thanks for the pointers, I was doing some simplified post processed (deferred style) point lighting and was using the depth buffer to cast shadows (to avoid the shadow maps per light), needed to have the 2D transparent characters in there.