Transparent Cutout Shader, just render shadow

Is it possible to turn off the visibility for the cutout shader so just the shadows render while still using the alpha cutout in the shadows?

Shader "Transparent/Cutout/Diffuse" {
Properties {
	_Color ("Main Color", Color) = (1,1,1,1)
	_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
	_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}

SubShader {
	Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
	LOD 200
	
CGPROGRAM
#pragma surface surf Lambert alphatest:_Cutoff

sampler2D _MainTex;
fixed4 _Color;

struct Input {
	float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {
	fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
	o.Albedo = c.rgb;
	o.Alpha = c.a;
}
ENDCG
}

Fallback "Transparent/Cutout/VertexLit"
}

This isn’t a decent solution, but if you want you can bake your lightmap with Transparent/Cutout/Diffuse and then swap the material’s shader with the Matte Shadow shader found here:

http://forum.unity3d.com/threads/14438-Matte-Shadow?highlight=matte+shadow

What I ususally do is rendering transparent objects twice, once with a cutout shader that drops shadows and once with a blended shader that does neither drop nor receive shadows. This of course only works if your cutout shader cutoff value is low enough as otherwise your blended shader will be overwritten by the cutout shader, causing ugly cutout artifacts. This technique is also quite useful for trees as that way you can write to the depth buffer with the cutout shader while maintaining smooth edges using the blended shader.

Try this :sunglasses:

Shader "Custom/CutoutShadowOnly" {
	Properties {
		_MainTex ("Alpha (A)", 2D) = "white" {}
		_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
	}
	
	SubShader {
		Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}

		Pass {
			Name "Caster"
			Tags { "LightMode" = "ShadowCaster" }
			Offset 1, 1
			
			Fog {Mode Off}
			ZWrite On ZTest LEqual Cull Off
	
			CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				#pragma multi_compile_shadowcaster
				#pragma fragmentoption ARB_precision_hint_fastest
				#include "UnityCG.cginc"
				
				struct v2f { 
					V2F_SHADOW_CASTER;
					float2  uv : TEXCOORD1;
				};
				
				uniform float4 _MainTex_ST;
				
				v2f vert( appdata_base v ) {
					v2f o;
					TRANSFER_SHADOW_CASTER(o)
					o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
					return o;
				}
				
				uniform sampler2D _MainTex;
				uniform fixed _Cutoff;
				
				float4 frag( v2f i ) : COLOR {
					fixed alpha = tex2D( _MainTex, i.uv ).a;
					// or if you want to use grayscale texture:
					// fixed alpha = tex2D( _MainTex, i.uv ).r;
					clip( alpha - _Cutoff );
					
					SHADOW_CASTER_FRAGMENT(i)
				}
			ENDCG
		}
	}
	Fallback Off
}
2 Likes

@AirShip.
Hi,

Seem you have a solution. For those who are no idea that what you’re talking about can you show a picture. Before and after. Please?

Thanks cician, that’s perfect!

Could you explain a bit about how to use this shader? I’ve placed it on the ground object where the shadow would be cast if I used a diffuse shader. This doesn’t seem to work. I just see a transparent material and the alpha cutoff doesn’t do anything.