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

LWRP - Reading/Writing depth

Discussion in 'Graphics Experimental Previews' started by Laex410, Sep 5, 2018.

  1. Laex410

    Laex410

    Joined:
    Mar 18, 2015
    Posts:
    51
    Hello everyone,

    I am currently working with the LWRP and I'm experiencing some issues when working with depth.
    I added a lightweight render pipeline asset to the graphic settings, enabled the depth texture and added the "Lightweight additional camera data" script to the camera.

    Issue3.PNG Issue4.PNG

    I checked "Requires depth texture" there and added a rendertexture to the camera. As you can see I can display color without any issues, however when it comes to depth there appears to be no data.

    Issue1.PNG Issue2.PNG

    I tried adding a depth pass to a custom shader but it doesn't seem to work there either.

    Code (CSharp):
    1. Pass
    2.         {
    3.             Name "DepthOnly"
    4.             Tags{"LightMode" = "DepthOnly"}
    5.  
    6.             ZWrite On
    7.             ColorMask 0
    8.         }
    I am working with the LWRP version 3.0.0 - preview.
    Do you guys experience similar issues? Is there a Unity dev around who can confirm whether this is a bug?
    I'm grateful for every help you can provide.

    Regards,
    Alex

    This post is related to a shader post I made here: https://forum.unity.com/threads/transparency-using-the-lwrp.550711/, however I decided to post this question here because the issue is quite specific by now to the LWRP.
     

    Attached Files:

    Desoxi likes this.
  2. Desoxi

    Desoxi

    Joined:
    Apr 12, 2015
    Posts:
    195
    I'm trying to get a hold on the depth texture on a second camera as well and posted a related thread. I tried to use OnRenderImage but it seems like this is not called in the new render pipeline anymore. The new way seems to be to use the beginframeRendering or beginCameraRendering events in the renderpipeline class. Though I didn't find an example on how to do this and I didn't have the time to try a few things out.

    I think in those events you can use a blit into a rendertexture with a custom material which reads the camera depth. But again.. Maybe there is another undocumented renderpipeline thing which makes this a bit easier. I hope someone who knows how to do this can reply to this thread .
     
  3. Laex410

    Laex410

    Joined:
    Mar 18, 2015
    Posts:
    51
    Hey Desoxi,

    could you explain to me how you got the rendertexture at the end? As described above I added a depth only pass to my shader and want to check whether it is actually writing to the depth buffer or not. It would help me a lot!
     
  4. Desoxi

    Desoxi

    Joined:
    Apr 12, 2015
    Posts:
    195
    Hey, i just wrote the solution to this in another thread (with the whole class and shader im using).
    Im using a custom post process effect to write the shader into a render texture. The only thing you need to do then is to add the "Depth Exporter" effect to the post process volume script on your camera through "Add effect -> custom-> Depth exporter".
    Tell me if you have problems with it.
     
    Last edited: Sep 12, 2018
  5. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    584
    Hi Laex. I've just tested LWRP and it's working as expected. I think the issue comes from your custom shader. In the image you posted your depth pass doesn't define any vertex/fragment shaders.
     
  6. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    584
    LWRP only supports using the depth texture of a camera in the current camera rendering. You can inject custom render passes in LWRP with the ScriptableRenderPass architecture (docs and examples are coming) instead of using a camera.

    Otherwise you'll have to do something similar to @Laex410, create a render texture and render to it with a camera.
     
  7. Desoxi

    Desoxi

    Joined:
    Apr 12, 2015
    Posts:
    195
    Jep i successfully did you through a custom post process effect, but the depth texture seems to be jittering and i dont know how to stabilize it. Nothing is moving, but the displacement which is driven by this depth rendertexture is jittering:
    https://forum.unity.com/threads/doe...-although-the-whole-scene-stays-still.552787/
    Maybe you have an idea on how to solve this..?
     
    Last edited: Sep 11, 2018
  8. Laex410

    Laex410

    Joined:
    Mar 18, 2015
    Posts:
    51
    Yeah I realized that yesterday as well, not sure what I did there. Here is the current version which produces the attached image. Cube hast the LWRP Standard shader. The Hair consists of one mesh with multiple materials.

    Code (CSharp):
    1. Shader "LWRP/Unlit/Hair"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _Color("Color", Color) = (1, 1, 1, 1)
    7.         _Cutoff("AlphaCutout", Range(0.0, 1.0)) = 0.0
    8.     }
    9.     SubShader
    10.     {  
    11.         Tags {"RenderType" = "TransparentCutout" "IgnoreProjector"="True" "Queue"="Transparent" "RenderPipeline" = "LightweightPipeline"}  
    12.  
    13.         //Depth only prepass
    14.         Pass
    15.         {
    16.             Name "DepthOnly"
    17.             Tags{"LightMode" = "DepthOnly"}
    18.             ZWrite On
    19.             ColorMask 0
    20.  
    21.             HLSLPROGRAM
    22.  
    23.             #pragma prefer_hlslcc gles
    24.             #pragma exclude_renderers d3d11_9x
    25.             #pragma target 2.0
    26.            
    27.             //#pragma shader_feature _ALPHATEST_ON
    28.  
    29.             #pragma vertex DepthOnlyVertex
    30.             #pragma fragment DepthOnlyFragment
    31.            
    32.             #include "LWRP/ShaderLibrary/InputSurfaceUnlit.hlsl"
    33.             #include "LWRP/ShaderLibrary/LightweightPassDepthOnly.hlsl"
    34.  
    35.             ENDHLSL
    36.         }
    37.  
    38.         //Main pass
    39.         Pass
    40.         {
    41.             Name "StandardUnlit"
    42.  
    43.             ColorMask RGB
    44.             Blend SrcAlpha OneMinusSrcAlpha
    45.             ZWrite Off
    46.             Cull Off
    47.  
    48.             HLSLPROGRAM
    49.  
    50.             #pragma prefer_hlslcc gles
    51.             #pragma exclude_renderers d3d11_9x
    52.             #pragma target 2.0
    53.  
    54.             //Define vertex and fragment function
    55.             #pragma vertex vert
    56.             #pragma fragment frag
    57.             #include "UnityCG.cginc"
    58.  
    59.             //Basic input
    60.             struct appdata
    61.             {
    62.                 float4 vertex : POSITION;
    63.                 float2 uv : TEXCOORD0;
    64.             };
    65.  
    66.             //Basic fragment input
    67.             struct v2f
    68.             {
    69.                 float2 uv : TEXCOORD0;
    70.                 float4 vertex : SV_POSITION;
    71.             };
    72.  
    73.             //Vars
    74.             sampler2D _MainTex;
    75.             float4 _MainTex_ST;
    76.             float4 _Color;
    77.             float _Cutoff;
    78.            
    79.             //Basic vertex function
    80.             v2f vert (appdata v)
    81.             {
    82.                 v2f o;
    83.                 o.vertex = UnityObjectToClipPos(v.vertex);
    84.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    85.                 return o;
    86.             }
    87.  
    88.             //Alpha cutoff function
    89.             void AlphaDiscard(half alpha, half cutoff, half offset = 0.0001h)
    90.             {
    91.                 clip(alpha - cutoff + offset);
    92.             }
    93.            
    94.             //Basic fragment function with alpha support
    95.             fixed4 frag (v2f i) : SV_Target
    96.             {
    97.                 half4 texColor = tex2D(_MainTex, i.uv);
    98.                 half3 color = texColor.rgb * _Color.rgb;
    99.                 AlphaDiscard(texColor.a, _Cutoff);
    100.                 return half4(color, texColor.a);
    101.             }
    102.            
    103.             ENDHLSL
    104.         }
    105.     }
    106.     FallBack "Hidden/InternalErrorShader"
    107. }
     

    Attached Files:

  9. jjxtra

    jjxtra

    Joined:
    Aug 30, 2013
    Posts:
    1,464
    This does not show up in the frame debugger as of latest Unity 2019 May 28 2019, how do we get a custom shader to write to lwrp depth buffer?
     
  10. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    I think I figured it out... Change the depth pass #includes to:

    #include "Packages/com.unity.render-pipelines.lightweight/Shaders/UnlitInput.hlsl"
    #include "Packages/com.unity.render-pipelines.lightweight/Shaders/DepthOnlyPass.hlsl"
     
    jackytop likes this.
  11. cyuxi

    cyuxi

    Joined:
    Apr 21, 2014
    Posts:
    49
    Hi,
    Sorry to bump up the old thread, but it's too hard to find learning information around ...
    My project is using Unity2018.4.0f1 LWRP.
    How can I access and manipulate the LWRP's _CameraDepthTexture via a ScriptableRenderPass? such as Blitting the depth to a custom RT,
    such as:

    buf.GetTemporaryRT(customRT, size, size, 16, FilterMode.Point, RenderTextureFormat.Depth);
    buf.Bilt(BuiltinRenderTextureType.Depth, customRT, blitMat) ;

    the customRT is a small rect area, I want it to sample from the _CameraDepthTexture, and the purpose is to get a median value from the rect area(the customRT).

    I also followed the demo LWRPScriptableRenderPass_ExampleLibrary_Project from git, but not getting lucky. The depth texture is not blitted at all, only get a 16_16 gray block of _MainTex named UnityDefault2D...

    Can you please give any simple code samples for this issue? Thank you!
     
    Last edited: Jun 4, 2020
  12. Januaryz

    Januaryz

    Joined:
    Apr 18, 2015
    Posts:
    4
    Hi, @cyuxi
    I came into the same problem in my Custom SRP. Do you find any solution?
    I manually set the _CameraDepthTexture
    upload_2021-2-2_19-12-6.png

    but it not working (the _CameraDepthTexture is 16x16 gray texture)
    upload_2021-2-2_19-17-34.png
    and unity gives me a warning
    upload_2021-2-2_19-13-33.png
     

    Attached Files:

  13. Januaryz

    Januaryz

    Joined:
    Apr 18, 2015
    Posts:
    4
    I solved this problem by manually get temporary RT as depth texture and set render target with this RT.
    depthTexId is the global texture _CameraDepthTexture
    upload_2021-2-2_21-59-3.png

    and set the render target in Render method
    upload_2021-2-2_21-59-31.png