Is it possible to have a material that is transparent with alpha (like Unlit/Transparent), but casts shadows like a cutout shader?
I have a shader that uses two passes, one for how it looks to the camera, and one for how it casts shadows. Objects rendered with it look like a cutout, with sharp edges.
Can I have the first one to allow semi transparent textures, while keeping the shadows of a cutout shape?
I figured that the shadow needs to be rendered when geometry is being rendered and the object with alpha needs to be rendered when transparent stuff is being rendered (After everything else).
From what I can tell, the only way to achieve this is to use 2 materials on the same MeshRenderer
, one with a simple unlit transparent pass (you can use “Unlit/Transparent” shader), and one with only the shadow pass.
Here’s the shadow only shader:
Shader "OnlyShadows" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags {"Queue" = "AlphaTest" "RenderType" = "TransparentCutout"}
// Pass to render object as a shadow caster
Pass {
Name "Caster"
Tags { "LightMode" = "ShadowCaster" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#pragma multi_compile_shadowcaster
#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_NORMALOFFSET(o)
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
uniform sampler2D _MainTex;
uniform fixed4 _Color;
float4 frag( v2f i ) : SV_Target{
fixed4 texcol = tex2D( _MainTex, i.uv );
clip(texcol.a * _Color.a - 0.9);
SHADOW_CASTER_FRAGMENT(i)
}
ENDCG
}
}
}