Search Unity

Trouble about 2D depth blur

Discussion in 'Shaders' started by Payne_Lee, Jan 4, 2019.

  1. Payne_Lee

    Payne_Lee

    Joined:
    Jul 2, 2016
    Posts:
    11
    Working on a 2D effect in LWRP, and the goal is having a depth blur with shader graph and the Post Processing is on the camera.
    Now here's the trouble:

    发帖01.png
    When swithced to “Opaque”, the blur is right on everything, but no alpha cut out;

    发帖02.png
    When swithced to “Transparent”, it's cut out nicly, but everything using this shader is blurred as the farthest one, just like the default unlit shader.

    How can I get both:(:(:(???
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Transparent objects aren't included in the depth texture used by depth of field and post process effects in general. You should be using an alpha tested opaque shader. Set the alpha clip threshold to something not zero, like 0.5.
     
  3. Payne_Lee

    Payne_Lee

    Joined:
    Jul 2, 2016
    Posts:
    11
    OTTWIA1R~9P2KE586OJ1[RL.png
    :)Thanks for helping~
    But the edge looks a little bit ugly even scaled down...
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Alpha tested edges will always be a bit less clean then geometry edges or alpha blending, but that might be as good as you'll get.
     
  5. jackytop

    jackytop

    Joined:
    Jun 13, 2017
    Posts:
    12
    @bgolus
    Hi PayneLee and Bgolus

    I am struggling with depth of field post effect in LWRP 2D.
    I copied sprite-Lit shader and added ZWrite On but it doesn't work.
    What should I charge to the shader so that depth of field starts working?
     
    Last edited: Jan 10, 2020
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Because ZWrite On or Off is irrelevant for depth of field to work. First it needs to be in the opaque queue range (0-2500), and second it needs to write to the depth texture.

    The depth buffer used during rendering (what setting the ZWrite controls) and the depth texture used by various post process effects are separate things. Specifically the depth texture is generated by rendering the scene once with a shader that only writes to the depth buffer, and then Unity copies that depth buffer to a texture that can be sampled from. The camera then renders the scene normally with a totally different depth buffer.

    For the built in rendering paths, this reuses the ShadowCaster pass also used to generate the shadow maps (which are also depth only renders that get converted into textures, just from the point of view of the light rather than the camera). For the LWRP it has a new DepthOnly pass for this. AFAIK none of the built in Sprite shaders have the DepthOnly pass, so none of them will work with the LWRP even if you change the material's queue or make them write to the depth buffer (which, again, is totally separate from the depth texture).

    You'd have to modify the sprite shader to add a proper DepthOnly pass, or use a non-sprite specific shader that is already setup properly.
     
    Lo-renzo and jackytop like this.
  7. jackytop

    jackytop

    Joined:
    Jun 13, 2017
    Posts:
    12
    @bgolus really lots of stuff to learn! I've been reading lots of your post Bgolus. great to learn.
    I will learn more about depthOnly pass and dig into it to make my lwrp 2d sprite gets blur with DOF.

    @Payne_Lee Could you also explain how you made DOF working with your lwrp 2d sprite setup.


    Thank you guys!!
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    @Payne_Lee was using Shader Graph to make the shader rather than using a sprite specific shader.
     
  9. jackytop

    jackytop

    Joined:
    Jun 13, 2017
    Posts:
    12
    @bgolus Oh I see. he's not using lwrp 2d pipe! Thanks
     
  10. jackytop

    jackytop

    Joined:
    Jun 13, 2017
    Posts:
    12
    @bgolus Hi Bgolus. I am back with shader modified
    I changed RenderType to Opaque.

    and added DepthOnly pass ( copied from another post from your answer to someone)

    upload_2020-1-13_23-48-47.png

    And I am using 2d Renderer

    I still can't get the depth of field working. ( everything is just blur even if the sprite is 15 unit away from camera, the distance doesn't count from post effect.

    When I use ForwardRendereer, the depth and everything works. it just doesn't work with 2d Renderer

    what else should I try Bgolus?

    Thank you!


    Code (CSharp):
    1. Shader "JcLwrp2DSprite"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Diffuse", 2D) = "white" {}
    6.         _MaskTex("Mask", 2D) = "white" {}
    7.         _NormalMap("Normal Map", 2D) = "bump" {}
    8.  
    9.         // Legacy properties. They're here so that materials using this shader can gracefully fallback to the legacy sprite shader.
    10.         [HideInInspector] _Color("Tint", Color) = (1,1,1,1)
    11.         [HideInInspector] _RendererColor("RendererColor", Color) = (1,1,1,1)
    12.         [HideInInspector] _Flip("Flip", Vector) = (1,1,1,1)
    13.         [HideInInspector] _AlphaTex("External Alpha", 2D) = "white" {}
    14.         [HideInInspector] _EnableExternalAlpha("Enable External Alpha", Float) = 0
    15.     }
    16.  
    17.     HLSLINCLUDE
    18.     #include "Core.hlsl"
    19.     ENDHLSL
    20.  
    21.     SubShader
    22.     {
    23.         Tags {"Queue" = "Geometry" "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
    24.  
    25.         //Blend SrcAlpha OneMinusSrcAlpha
    26.         //Cull Off
    27.         //ZWrite On
    28.  
    29.         Pass
    30.         {
    31.             Name "DepthOnly"
    32.             Tags{"LightMode" = "DepthOnly"} //DepthOnly
    33.  
    34.             ZWrite On
    35.             ColorMask 0
    36.             Cull Off
    37.  
    38.             HLSLPROGRAM
    39.             // Required to compile gles 2.0 with standard srp library
    40.             #pragma prefer_hlslcc gles
    41.             #pragma exclude_renderers d3d11_9x
    42.             #pragma target 2.0
    43.  
    44.             #pragma vertex DepthOnlyVertex
    45.             #pragma fragment DepthOnlyFragment
    46.  
    47.             // -------------------------------------
    48.             // Material Keywords
    49.             #pragma shader_feature _ALPHATEST_ON
    50.             #pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    51.  
    52.             //--------------------------------------
    53.             // GPU Instancing
    54.             #pragma multi_compile_instancing
    55.  
    56.             #include "LitInput.hlsl"
    57.             #include "DepthOnlyPass.hlsl"
    58.             ENDHLSL
    59.         }
    60.  
    61.         Pass
    62.         {
    63.             Tags { "LightMode" = "Universal2D" }
    64.             HLSLPROGRAM
    65.             #pragma prefer_hlslcc gles
    66.             #pragma vertex CombinedShapeLightVertex
    67.             #pragma fragment CombinedShapeLightFragment
    68.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_0 __
    69.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_1 __
    70.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_2 __
    71.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_3 __
    72.  
    73.             struct Attributes
    74.             {
    75.                 float3 positionOS   : POSITION;
    76.                 float4 color        : COLOR;
    77.                 float2  uv           : TEXCOORD0;
    78.             };
    79.  
    80.             struct Varyings
    81.             {
    82.                 float4  positionCS  : SV_POSITION;
    83.                 float4  color       : COLOR;
    84.                 float2    uv          : TEXCOORD0;
    85.                 float2    lightingUV  : TEXCOORD1;
    86.             };
    87.  
    88.             #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/LightingUtility.hlsl"
    89.  
    90.             TEXTURE2D(_MainTex);
    91.             SAMPLER(sampler_MainTex);
    92.             TEXTURE2D(_MaskTex);
    93.             SAMPLER(sampler_MaskTex);
    94.             TEXTURE2D(_NormalMap);
    95.             SAMPLER(sampler_NormalMap);
    96.             half4 _MainTex_ST;
    97.             half4 _NormalMap_ST;
    98.  
    99.             #if USE_SHAPE_LIGHT_TYPE_0
    100.             SHAPE_LIGHT(0)
    101.             #endif
    102.  
    103.             #if USE_SHAPE_LIGHT_TYPE_1
    104.             SHAPE_LIGHT(1)
    105.             #endif
    106.  
    107.             #if USE_SHAPE_LIGHT_TYPE_2
    108.             SHAPE_LIGHT(2)
    109.             #endif
    110.  
    111.             #if USE_SHAPE_LIGHT_TYPE_3
    112.             SHAPE_LIGHT(3)
    113.             #endif
    114.  
    115.             Varyings CombinedShapeLightVertex(Attributes v)
    116.             {
    117.                 Varyings o = (Varyings)0;
    118.  
    119.                 o.positionCS = TransformObjectToHClip(v.positionOS);
    120.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    121.                 float4 clipVertex = o.positionCS / o.positionCS.w;
    122.                 o.lightingUV = ComputeScreenPos(clipVertex).xy;
    123.                 o.color = v.color;
    124.                 return o;
    125.             }
    126.  
    127.             #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/CombinedShapeLightShared.hlsl"
    128.  
    129.             half4 CombinedShapeLightFragment(Varyings i) : SV_Target
    130.             {
    131.                 half4 main = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    132.                 half4 mask = SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, i.uv);
    133.  
    134.                 return CombinedShapeLightShared(main, mask, i.lightingUV);
    135.             }
    136.             ENDHLSL
    137.         }
    138.  
    139.         Pass
    140.         {
    141.             Tags { "LightMode" = "NormalsRendering"}
    142.             HLSLPROGRAM
    143.             #pragma prefer_hlslcc gles
    144.             #pragma vertex NormalsRenderingVertex
    145.             #pragma fragment NormalsRenderingFragment
    146.  
    147.             struct Attributes
    148.             {
    149.                 float3 positionOS   : POSITION;
    150.                 float4 color        : COLOR;
    151.                 float2 uv            : TEXCOORD0;
    152.                 float4 tangent      : TANGENT;
    153.             };
    154.  
    155.             struct Varyings
    156.             {
    157.                 float4  positionCS        : SV_POSITION;
    158.                 float4  color            : COLOR;
    159.                 float2    uv                : TEXCOORD0;
    160.                 float3  normalWS        : TEXCOORD1;
    161.                 float3  tangentWS        : TEXCOORD2;
    162.                 float3  bitangentWS        : TEXCOORD3;
    163.             };
    164.  
    165.             TEXTURE2D(_MainTex);
    166.             SAMPLER(sampler_MainTex);
    167.             TEXTURE2D(_NormalMap);
    168.             SAMPLER(sampler_NormalMap);
    169.             float4 _NormalMap_ST;  // Is this the right way to do this?
    170.  
    171.             Varyings NormalsRenderingVertex(Attributes attributes)
    172.             {
    173.                 Varyings o = (Varyings)0;
    174.  
    175.                 o.positionCS = TransformObjectToHClip(attributes.positionOS);
    176.                 o.uv = TRANSFORM_TEX(attributes.uv, _NormalMap);
    177.                 o.uv = attributes.uv;
    178.                 o.color = attributes.color;
    179.                 o.normalWS = TransformObjectToWorldDir(float3(0, 0, -1));
    180.                 o.tangentWS = TransformObjectToWorldDir(attributes.tangent.xyz);
    181.                 o.bitangentWS = cross(o.normalWS, o.tangentWS) * attributes.tangent.w;
    182.                 return o;
    183.             }
    184.  
    185.             #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/NormalsRenderingShared.hlsl"
    186.  
    187.             float4 NormalsRenderingFragment(Varyings i) : SV_Target
    188.             {
    189.                 float4 mainTex = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    190.                 float3 normalTS = UnpackNormal(SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, i.uv));
    191.                 return NormalsRenderingShared(mainTex, normalTS, i.tangentWS.xyz, i.bitangentWS.xyz, i.normalWS.xyz);
    192.             }
    193.             ENDHLSL
    194.         }
    195.         Pass
    196.         {
    197.             Tags { "LightMode" = "UniversalForward" "Queue"="Transparent" "RenderType"="Transparent"}
    198.  
    199.             HLSLPROGRAM
    200.             #pragma prefer_hlslcc gles
    201.             #pragma vertex UnlitVertex
    202.             #pragma fragment UnlitFragment
    203.  
    204.             struct Attributes
    205.             {
    206.                 float3 positionOS   : POSITION;
    207.                 float4 color        : COLOR;
    208.                 float2 uv            : TEXCOORD0;
    209.             };
    210.  
    211.             struct Varyings
    212.             {
    213.                 float4  positionCS        : SV_POSITION;
    214.                 float4  color            : COLOR;
    215.                 float2    uv                : TEXCOORD0;
    216.             };
    217.  
    218.             TEXTURE2D(_MainTex);
    219.             SAMPLER(sampler_MainTex);
    220.             float4 _MainTex_ST;
    221.  
    222.             Varyings UnlitVertex(Attributes attributes)
    223.             {
    224.                 Varyings o = (Varyings)0;
    225.  
    226.                 o.positionCS = TransformObjectToHClip(attributes.positionOS);
    227.                 o.uv = TRANSFORM_TEX(attributes.uv, _MainTex);
    228.                 o.uv = attributes.uv;
    229.                 o.color = attributes.color;
    230.                 return o;
    231.             }
    232.  
    233.             float4 UnlitFragment(Varyings i) : SV_Target
    234.             {
    235.                 float4 mainTex = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    236.                 return mainTex;
    237.             }
    238.             ENDHLSL
    239.         }
    240.     }
    241.  
    242.     Fallback "Sprites/Default"
    243. }
     
    Last edited: Jan 14, 2020
  11. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Well, the last question you need to answer is ... does Unity's post processing stack support DoF with an orthographic camera?

    I honestly have no idea. I also don't know if the 2D Renderer generates a depth texture. Try using some default LWRP lit materials on some meshes in your 2D project and see if DoF works on them. If they don't then you might be out of luck either way.
     
  12. jackytop

    jackytop

    Joined:
    Jun 13, 2017
    Posts:
    12
    oh I am using Perspective now.

    Yep I will try with default LWRP lit material with sphere to see if it generates depth texture with 2d renderer.

    it its 00:47 and need to go to bed for tomorrow work! I will let you know tomorrow!

    Thanks @bgolus
     
  13. jackytop

    jackytop

    Joined:
    Jun 13, 2017
    Posts:
    12
    upload_2020-1-14_22-30-25.png upload_2020-1-14_22-31-14.png
    upload_2020-1-14_22-31-37.png

    so when it is URP > ForwardRenderer with URP/Lit material. it works
    and Change to 2D renderer, with 3d sphere with URP/Lit, doesn't seem to generate depth texture.
    upload_2020-1-14_22-33-5.png

    upload_2020-1-14_22-33-26.png

    I will bring this up in 2D forum to ask and if 2D renderer can be updated with depth texture feature.

    Thank you @bgolus
     
  14. zfh2773

    zfh2773

    Joined:
    Nov 16, 2019
    Posts:
    27
    I want to ask this question, too! This error belongs to unity itself!
     
  15. zfh2773

    zfh2773

    Joined:
    Nov 16, 2019
    Posts:
    27
    I am also worried about this problem, if you find a solution, please tell me! Thank you very much!