Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

access _CameraDepthTexture with LWRP

Discussion in 'Graphics Experimental Previews' started by stephero, Aug 21, 2018.

  1. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    123
    Hi guys,
    Has anyone managed to access data from the _CameraDepthTexture when using LWRP?
    I have a custom shader which use the depth texture to render some effect. It works fine with the legacy RP and with the HDRP. However, it doesn't work when using the LWRP, even if I properly enabled the Depth Texture property in the LW asset.
    Screenshot_6.png

    What is even weirder, is when I press play, I can see the depth texture but from the editor scene camera POV (instead of the main game camera POV): when I move the editor camera (left), you can see the depth texture moving on the game window (right). But not when I move the game camera.
    gif3.gif

    So do I need to do something special to get access to the depth texture with LWRP, or is it not supported yet even if the documentation says it is?
    Thanks
     
  2. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    You have to add the additional rendered data component and enable the depth texture there too.
     
  3. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    123
    Thank you for your answer but I don't understand. What is this additional rendered data component?
    I couldn't find anything related to it. The only data I use is the Scriptable Render Pipeline Asset you set via the GraphicsSettings.
    Screenshot_7.png

    Could you be more specific please?
    Thanks a lot!
     
  4. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    The component is called 'LightweightAdditionalCameraData'. Just add that to the camera's GameObject and enable 'Requires Depth Texture' on it.

    By default LWRP enables a depth texture when it is activated in the capabilities and
    * LightweightAdditionalCameraData exists and RequiresDepthTexture is enabled
    * OR a PostProcessLayer is attached to the camera
     
  5. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    123
    Thank you very much. I didn't know this new component.
    So indeed in works... until I set a Target Render Texture:
    Screenshot_8.png

    If I render my camera to a RenderTexture, the data from _CameraDepthTexture is broken again, even if my RT has properly a depth buffer.
    Screenshot_9.png

    Do you know if it's possible to make it work while rendering to a RT?
    Many thanks!
     
  6. z_space

    z_space

    Joined:
    Jul 17, 2016
    Posts:
    10
    I'm actually trying to get _CameraDepthTexture to work with HDRP, mine looks very strange (despite working in the normal rendering pipeline). Could you post what you got working with it?
     
  7. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    123
    I couldn't make it work with a RenderTexture :(
     
  8. z_space

    z_space

    Joined:
    Jul 17, 2016
    Posts:
    10
    I mean the HDRP, not the LWRP. You said you successfully made a shader that used camera depth with the HDRP, I can't seem to get that to work. Can you post the custom shader that worked with the HDRP?
     
  9. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    584
    Hey guys. I recommend updating to 4.3.0-preview (2018.3) or 5.2.1 (2019.1). In these versions you just need to enable the depth texture in the asset. In the camera you will have the option to override the asset value so you can also decide on a camera basis if that generated the depth texture or not.

    While in previous versions it was confusing. You had to add this additional camera data component to make it work.
     
  10. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    123
    Well, it worked with HDRP 3.0.0 (without RenderTexture). But I tried again and it's broken again with HDRP 4.3.0. It's really annoying, I have to investigate.
     
  11. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    13,362
    Any news on this ? I try to grab depth from a 2ond non main camera and cant seem to find a clear way to do it.
     
    carlotta89 likes this.
  12. carlotta89

    carlotta89

    Joined:
    Dec 19, 2017
    Posts:
    4
    Has anyone figured out a way to add depwth texture (z-depth) to the LWRP? I am loosing my patienve because it does not seem to be working
     
  13. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    123
    Yes I think I finally found a way.

    Here is how I get access to the depth texture with the legacy render pipeline:
    Code (CSharp):
    1. UNITY_DECLARE_DEPTH_TEXTURE(_CameraDepthTexture);
    2. #define SampleDepthTexture(uv) (SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, uv/uv.w))
    And here is how I do with LWRP and HDRP:
    Code (CSharp):
    1. TEXTURE2D(_CameraDepthTexture);
    2. inline float SampleDepthTexture(float4 uv)
    3. {
    4.     float2 screenParams = _ScreenParams.xy;
    5.     uint2 pixelCoords = uint2( (uv.xy/uv.w) * screenParams );
    6.     return LOAD_TEXTURE2D_LOD(_CameraDepthTexture, pixelCoords, 0).r;
    7. }
    Hope it helps!
     
    clickbecause and a436t4ataf like this.