Search Unity

DepthPass draw call do not batch correctly

Discussion in 'General Graphics' started by Patrascu, Jun 29, 2018.

  1. Patrascu

    Patrascu

    Joined:
    Jan 20, 2016
    Posts:
    59
    I'm having a Sprite shader with a ShadowCaster pass which causes the DepthPass draw call to not batch correctly.

    I've created a custom shader based on the Sprite-Default shader and added a ShadowCaster pass to it and changed the render queue to Geometry so that it can write to the depth buffer and use the depth texture in post effects such as Depth of Field.
    The depth pass runs, however, the order in which the draw calls are performed is not the same as the drawing order of the renderers in the scene, and therefore dynamic batching is very prone to break on a lot of cases.

    Any ideas on what is causing this behavior? I'll post the shader I'm using here:

    Code (CSharp):
    1. Shader "Sprites/Default-ShadowCaster"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _Color ("Tint", Color) = (1,1,1,1)
    7.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    8.         [HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1)
    9.         [HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1)
    10.         [PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {}
    11.         [PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0
    12.  
    13.         [PerRendererData]_Opacity("Opacity", Range(0,1)) = 1
    14.         [PerRendererData]_Cutoff("Depth Alpha", Range(0,1)) = 0.5
    15.     }
    16.  
    17.     SubShader
    18.     {
    19.         Tags
    20.         {
    21.             "Queue"="Transparent"
    22.             "IgnoreProjector"="True"
    23.             "RenderType"="Transparent"
    24.             "PreviewType"="Plane"
    25.             "CanUseSpriteAtlas"="True"
    26.         }
    27.  
    28.         Cull Off
    29.         Lighting Off
    30.         ZWrite Off
    31.         Blend One OneMinusSrcAlpha
    32.  
    33.         Pass
    34.         {
    35.         CGPROGRAM
    36.             #pragma vertex SpriteVert
    37.             #pragma fragment SpriteFrag
    38.             #pragma target 2.0
    39.             #pragma multi_compile_instancing
    40.             #pragma multi_compile _ PIXELSNAP_ON
    41.             #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
    42.             #include "UnitySprites.cginc"
    43.         ENDCG
    44.         }
    45.  
    46.         Pass
    47.         {
    48.             Name "DEPTHCASTER"
    49.             Tags{ "LightMode" = "ShadowCaster" }
    50.             Offset 1, 1
    51.  
    52.             Fog{ Mode Off }
    53.             ZWrite On ZTest LEqual Cull Off
    54.             Blend One Zero
    55.  
    56.             CGPROGRAM
    57.             #pragma vertex vert
    58.             #pragma fragment frag
    59.             #pragma multi_compile_shadowcaster
    60.             #include "UnityCG.cginc"
    61.  
    62.             uniform sampler2D _MainTex;
    63.             uniform half4 _MainTex_ST;
    64.             uniform fixed _Cutoff;
    65.             uniform float _Opacity;
    66.  
    67.             struct v2f {
    68.                 V2F_SHADOW_CASTER;
    69.                 float4  uv : TEXCOORD1;
    70.             };
    71.  
    72.             v2f vert(appdata_base v)
    73.             {
    74.                 v2f o;
    75.                 TRANSFER_SHADOW_CASTER(o)
    76.                 o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
    77.                 o.uv.zw = v.texcoord;
    78.                 return o;
    79.             }
    80.  
    81.             float4 frag(v2f i) : SV_Target
    82.             {
    83.                 float a = floor(0.99 + _Opacity);
    84.                 fixed4 texcol = tex2D(_MainTex, i.uv.xy);
    85.  
    86.                 clip(texcol.a * a - _Cutoff);
    87.  
    88.                 SHADOW_CASTER_FRAGMENT(i)
    89.             }
    90.             ENDCG
    91.         }
    92.     }
    93. }
    94.