Search Unity

Question why are shadows in custom cutout shader slightly offset?

Discussion in 'Shaders' started by JoeStrout, May 24, 2021.

  1. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I've got a custom cutout shader that uses a texture array. It is otherwise a pretty ordinary vertex shader. But now I'm trying to add shadow support, and it's coming out like this:

    upload_2021-5-24_12-33-2.png

    As you can see there is a weird gap next to each of the cutout areas, where the shadows falling on the plane behind are not there (i.e. the plane is unshadowed in the gaps). And I can't figure out what I'm doing wrong.

    As you might guess, this was originally a vertex displacement shader, but for now I've commented all that stuff out of both the main pass and the shadow pass. So both passes are now just doing

    Code (CSharp):
    1.         v2f o;
    2.                 o.pos = UnityObjectToClipPos(v.vertex);
    3.                 o.uv = v.texcoord;
    The main pass then has a TRANSFER_SHADOW(o) macro call, while the shadow pass does TRANSFER_SHADOW_CASTER_NORMALOFFSET(o). Finally the shadow pass fragment function is

    Code (CSharp):
    1.                 float4 frag( v2f i ) : COLOR {
    2.                     fixed4 col = UNITY_SAMPLE_TEX2DARRAY(_MainTex, i.uv);
    3.                     clip(col.a - _Cutoff);
    4.                  
    5.                     SHADOW_CASTER_FRAGMENT(i)
    6.                 }
    (The first two lines, checking the color and calling clip, are the same in the main pass too.) All seems pretty straightforward. Yet I have this weird gap around the leaves. What's going on?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Are you sure? Make sure there are no errors and one of the passes isn't stuck using a still displaced version. If there's an error in the shader and modifications you make to it doesn't change what the error was last time it compiled, Unity will sometimes not show you the error message in the console. Select the shader in the project view and see if any error messages are showing in the inspector. You might also try reimporting it or clicking on the compile button which can force errors to show that are being suppressed.

    That's the only way I can imagine what you're seeing happening.
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It turned out to be that my shadow pass had Cull Off on it. Got what I deserved for copying & pasting code, I guess! Taking that out (or changing it to Cull Back) to match the main pass got rid of the phantom shadow-blockers.
     
    bgolus likes this.