Search Unity

Depth buffer injection?

Discussion in 'Shaders' started by chingwa, Sep 17, 2017.

  1. chingwa

    chingwa

    Joined:
    Dec 4, 2009
    Posts:
    3,790
    Hello all,

    I've been wracking my head around this issue for a while and I thought I would finally come onto the forums and ask all you shader wizards for help...

    The goal: To alter the camera depth buffer in between 2 separate image effects on the same camera.

    I have looked at command buffer examples till my face turned blue and all my hair fell out, and have yet to find an adequate tutorial on how they work. I have been doing tests but have not been able to determine if the above is even possible or not.

    The main difficulty I have is that while the first camera effect is a custom effect, the second/later effect will not be... so I need to be able to alter the depth buffer between these two effects, not just using a custom RT buffer for depth in the second effect.

    So my question is a) do you think this is possible, and if so b) are there any examples or tutorials about how to go about this? Using command buffers or otherwise?
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    The first camera effect could adjust the depth buffer or you could add a third effect that does it in between.

    Any shader can output a modified depth value to SV_Depth:
    https://docs.unity3d.com/Manual/SL-ShaderSemantics.html

    This line actually makes no sense to me, though it doesn't apply to your case:
    "Render shaders that modify depth after all regular opaque shaders"
    I would recommend doing the reverse. When modifying depth, the gpu disables early z rejection, which means the z-testing is done after the shader is executed instead of before. This means that pixels with modified depth are always calculated, even if they are occluded. So always do those first, because they might occlude other pixels that can be skipped because of z-testing.
    (There are some other alternatives, but plainly rendering them last doesn't help.)
     
    CyrilGhys likes this.
  3. chingwa

    chingwa

    Joined:
    Dec 4, 2009
    Posts:
    3,790
    Thanks for the reply @jvo3dc I spent most of yesterday trying to get this working, this time using SV_Depth in various shaders, all with no luck. I've tried using Command Buffers but I don't see any way of targeting in between specific effects so don't think they will work for this case. I then tried using Graphics.Blit directly from the first image effect and did not have any luck with that either, even blitting with a custom material/shader using SV_Depth. I tried Graphics.DrawTexture as well, which draws to the screen fine but does not draw into the depth texture no matter what I try.

    One option I haven't explored is using Graphics.DrawMesh and re-drawing the geometry again, which is something I was trying to avoid since I already have a RenderTexture containing the depth information of the mesh I want to inject into the _CameraDepthTexture.

    I assumed since I already have the original scene depth in _CameraDepthTexture, and the depth of custom objects captured into a custom texture, I could simply combine them together and re-export the new depth to _CameraDepthTexture. Nothing I do seems to effect it though. Any ideas here? As far as I can tell editing _CameraDepthTexture or _CameraDepthNormalsTexture directly seems verboten.