Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

How to access depth render target used by zwrite from transparent when ImageEffectOpaque present?

Discussion in 'Image Effects' started by chrismarch, Apr 13, 2016.

  1. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    441
    How can I access the depth information my transparent shader writes to a Depth render target (see picture) when an ImageEffectOpaque is present? It is not in _CameraDepthTexture, whether there are ImageEffectOpaque present or not, but ztesting works fine against the depth written by my transparent shader until I turn on an ImageEffectOpaque.

    ImageEffectsTemp_DepthRT.png
     
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,163
    I'm a little confused by what you say, are these things correct? (just want to validate what you are seeing).

    1. Your object NEVER appears in _CameraDepthTexture
    2. normal standard ztesting works fine when there is NO ImageEffectOpaque
    3. normal standard ztesting breaks when there is ImageEffectOpaque present

    So to answer #1
    _CameraDepthTexture is created before transparent objects are rendered (so it can be used in ImageEffectOpaque), it's not recreated before transparent image effects. This is why your object is not there.

    #2 Seems correct

    #3 Seems weird, are any of your effects / other clearing the z-buffer?


    Workaround for getting a _CameraDepthTexture with your 'transparent' object.

    Camera 1 - Do normal rendering as you do now, don't do your transparent effects
    Camera 2 - Set to don't clear, have your image effects. The depth buffer on this one will have the _CameraDepthTexture with your transparent ones set also.
     
    chrismarch likes this.
  3. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    441
    1. Yes
    2. Yes
    3. Yes it breaks and there is no clear, unless it is hidden from the Frame Debugger (see screenshot below). There is no clear after forward alpha and before the ztesting that fails in PopcornFX Rendering/Plugin Event afterwords (that ztesting succeeds if there is no ImageEffectOpaque present. I have Edge Detection on in the screenshot, to show the state when it fails to ztest against the marine mesh displayed in the depth render target)
    4. Workaround: Camera 1 would only render opaque objects, and no ImageEffectOpaque or transparent zwrite objects? Camera 2 would do ImageEffectOpaque and transparent zwrite (on top of the results from Camera 1)? I feel like I am missing something here.
    ImageEffectsTemp_DepthRT_NoClear.png
     
  4. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,163
    Not quite.

    Camera 1 renders all objects (including transparent).
    Camera 2 renders NO objects, has clear mode set to 'don't clear' (keeps the depth buffer), and has the image effects you need.

    When camera 2 converts the depth buffer into a usable texture it will convert the depth buffer that has both your transparent / opaque in it.
     
    chrismarch and PeteUnity3D like this.
  5. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    441
    The works, thanks, but it breaks HDR rendering (Bloom on the second camera does not do HDR bloom)
     
  6. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,163
    :S Damn, why is that? Have you tried using the frame debugger to see what format the render targets are in and when it changes from an HDR target to a LDR target?
     
  7. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,163
    Could you raise a bug with small reproduction project for the very initial issue. I would like to take a closer look.
     
  8. cpetry

    cpetry

    Joined:
    Jun 30, 2014
    Posts:
    29
    I'm trying to do actually the exact same thing.
    Instead my ImageEffect is ScreenSpaceAmbientObscurance.
    • If I use forward rendering, the second camera doesn't write depth and therefor is not able to use it for SSAO.
    • Using deferred rendering, the second camera creates depth but adds it per frame (getting more and more white)
    Also, what do you mean by "camera 2 renders NO objects"? Should I set the culling mask to Nothing?
     
  9. cpetry

    cpetry

    Joined:
    Jun 30, 2014
    Posts:
    29
    I managed to solve that problem using two cameras.
    In case someone else has this problem:
    Create a depth RenderTexture and set it as renderTarget for the second camera.

    Still I would like a solution without using a second camera.
     
  10. daxiongmao

    daxiongmao

    Joined:
    Feb 2, 2016
    Posts:
    344
    I have run into something similar I think. I think there are issues in the graphics.blt.
    It is making changes internally.

    I was able to kind of work around it using command buffers but was still to convoluted.
    I was trying to accomplish this.
    Camera 1 render background environment. Apply image effect blur. Clear all
    Camera 2 render characters, no image effect, clear none

    I expected to see the blurred background with the character properly depth tested in the scene
    During the image effect the graphics.blt was changing or destroying the screens bound depth buffer.
    I tried modifying the image effect to restore the depth buffer. The frame debugger helped show the buffer changed during the blit.

    I was able to put together a cmd buffer version but even that took too much trial and error.
    I still had to use two cameras.

    Reading other threads there is just too much magic I think related to the Api.
    This was on dev pc, I never even bothered testing this on iOS.

    Unity 5.5.1f1
     
unityunity