Adding cutout to shadows?

Is it possible to add cutout to this shader? I would need only a few specific parts of the mesh to receive shadows based on alpha/mask while keeping it transparent.

    Shader "Custom/TransparentShadowCollector"
    {
        Properties
        {
            _ShadowIntensity ("Shadow Intensity", Range (0, 1)) = 0.6
        }
    
    
        SubShader
        {
    
            Tags {"Queue"="AlphaTest" }
    
            Pass
            {
                Tags {"LightMode" = "ForwardBase" }
                Cull Back
                Blend SrcAlpha OneMinusSrcAlpha
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #pragma multi_compile_fwdbase
    
                #include "UnityCG.cginc"
                #include "AutoLight.cginc"
                uniform float _ShadowIntensity;

                  struct v2f
                {
                    float4 pos : SV_POSITION;
                    LIGHTING_COORDS(0,1)
                };
                v2f vert(appdata_base v)
                {
                    v2f o;
                    o.pos = UnityObjectToClipPos (v.vertex);
                    TRANSFER_VERTEX_TO_FRAGMENT(o);
                  
                    return o;
                }
                fixed4 frag(v2f i) : COLOR
                {
                    float attenuation = LIGHT_ATTENUATION(i);
                    return fixed4(0,0,0,(1-attenuation)*_ShadowIntensity);
                }
                ENDCG
            }
    
        }
        Fallback "VertexLit"
    }

Add this line to your properties:
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5

And replace your Fallback line with:
Fallback “Transparent/Cutout/VertexLit”

That’ll get it setup to use an alpha tested shadow. After that you’ll need to add back in a _MainTex property, the alpha of which you can use to modify the areas that receive shadows.

Note that this also controls which areas cast shadows, and transparent objects that receive shadows will prevent areas behind it from receiving shadows.

2 Likes

doesnt seem to work XD

That does exactly what you asked for, but what you’re asking for might not be what you want.

Can you further describe or show some examples of what you’re trying to accomplish.

I want shadows on transparent meshes. Based on my research it is very hard to achieve so I plan on adding a second mesh in front of it which receives the shadow. It kind of worked except the shadow is visible on the whole shadow receiver mesh instead of just a few spots. The cutout of booth the transparent mesh and the shadow receiver have to be the same otherwise there will be floating shadows

So, if I understand what you’re trying to do, you’ve got an existing transparent material using either alpha blending or alpha test to only show in certain areas. Then you also this additional material that you’re using just to show shadows, presumably using a copy of the transparent mesh?

I’m assuming the transparent material is still rendering during the transparent queue, and this material is set to run during the alpha test queue to ensure it receives shadows. Yes? The result would be your transparent object is going to render “on top” of the shadows rather than the shadows being part of the transparency, so maybe you move the transparent material to be on a queue less than the shadow collector?

If that’s the case, I’m not sure why you wouldn’t just change your transparent material to use shadows? The only issue is materials with a transparent queue can’t receive the data about shadows, but a transparent material with an alpha test queue (really < 2500) will still get information about shadows, as long as it still has a shadow caster pass.