Receiving shadows in a custom alpha-cutout shader

Hi,

I wrote an alpha cutout shader similar to the cutout diffuse and want to receive shadows with that shader. I tried out the shadow receive pass used in the built-in shader. The Problem is, that I can now see the received shadow but it only appears on the cutout parts (where the alpha is 0) of the texture. Where the texture is fully visible, no shadow appears. Maybe it’s a blending problem.

The other problem is that the deformations done in the first pass are ignored in the shadow receiving pass. Is there a way to integrate the shadows in the first pass?

Here’s the shader code:

Shader “TreeShader” {
Properties {
_MainTex (“Base (RGB) Trans (A)”, 2D) = “white” {}
_Cutoff (“Alpha cutoff”, Range(0,1)) = 0.5
_WindStrength (“Wind Strength”, Vector) = (0, 0, 0, 0)
_LightDir (“Light Direction”, Vector) = (0, 0, 0, 0)
_LightColor (“Light Color”, Color) = (1, 1, 1, 1)
_LightIntensity (“Light Intensity”, Range(0,1)) = 0.5
_SelfShadow (“Self-Shadow Max. Distance”, Range(0,100)) = 50
}

Category {

AlphaToMask True
ColorMask RGB
Fog { Color [_AddFog] }

SubShader {

Pass {
Name “BASE”
Tags { “LightMode” = “Pixel” }
Alphatest Greater [_Cutoff]

CGPROGRAM

#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_builtin
#include “UnityCG.cginc”
#include “AutoLight.cginc”

struct appdata {
float4 vertex;
float4 color;
float4 normal;
float2 texcoord;
};

struct v2f {
V2F_POS_FOG;
float2 uv;
float selfShadow;
float diffuse;
};

uniform float4 _MainTex_ST;
uniform sampler2D _MainTex;
uniform float4 _Color;
uniform float _Offset;
uniform float _Speed;
uniform float4 _WindStrength;
uniform float4 _LightDir;
uniform float4 _LightColor;
uniform float _LightIntensity;
uniform float _SelfShadow;

v2f vert (appdata v)
{
v2f o;

float4 lightDir = normalize(_LightDir);

// Tree Deformation

PositionFog( v.vertex, o.pos, o.fog );
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);

o.diffuse = saturate(dot(-lightDir.xyz, v.normal.xyz));
o.diffuse *= _LightIntensity;

float distance = length(float3(v.vertex.x, 0, v.vertex.z));

o.selfShadow = saturate(distance / _SelfShadow);

return o;
}

half4 frag (v2f i) : COLOR
{
half4 texcol = tex2D(_MainTex, i.uv);
half4 c = texcol * 2* i.diffuse * _LightColor * i.selfShadow;
c.a = texcol.a;
return c;
}

ENDCG

}

// Pass to render object as a shadow collector
Pass {
Name “ShadowCollector”
Tags { “LightMode” = “ShadowCollector” }

Fog {Mode Off}
ZWrite On
ZTest Less

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest

#define SHADOW_COLLECTOR_PASS
#include “UnityCG.cginc”

struct appdata {
float4 vertex;
};

struct v2f {
V2F_SHADOW_COLLECTOR;
};

v2f vert (appdata v)
{
v2f o;

TRANSFER_SHADOW_COLLECTOR(o)
return o;
}

half4 frag (v2f i) : COLOR
{
SHADOW_COLLECTOR_FRAGMENT(i)
}
ENDCG

}
}
}
}

Thanks for helping

Daniel

A very sensible question and nobody deigned to answer it? For 3 years?

I ran exactly into same problem: shadow is visible on alphacut parts and it seems that only when triangle is backfaced. I’m using “Cull Off”.
Pretty please Unity Staff, explain this to us smallfolk. We waited long enough!

Ok, it seems that this behaviour has something to do with:

FallBack “Diffuse”
when I changed it to:
FallBack “Transparent/Cutout/Diffuse”

bad shadows dissapeared, so I will investigate further this default cutout.