Hi,
I have another problem with shadows and clipping. This time the effect I am trying to achieve is a flashlight (spot light) that illuminates the scene, showing hidden objects and clipping the parts of the objects that are not currently in direct light of the spotlight.
Right now I am using a custom fragment / vertex shader and use LIGHT_ATTENUATION to compute, whether the fragment is in shadow or not. Doing so clips the fragments as intendet, but the object still blocks shadows behind it, in its original shape.
I know this might be solved, using a surface shader and #pragma addshadow, but I could not get the LIGHT_ATTENUATION to work in the surface function, so I was not able clip the fragments.
Here is the code I am currently using:
Shader "Unlit/Shadow_Test"
{
Properties {}
CGINCLUDE
#include "UnityCG.cginc"
#include "AutoLight.cginc"
struct v2f_shadow {
float4 pos : SV_POSITION;
LIGHTING_COORDS(0, 1)
};
half4 _Color;
v2f_shadow vert_shadow(appdata_full v)
{
v2f_shadow o;
o.pos = UnityObjectToClipPos(v.vertex);
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
half4 frag_shadow(v2f_shadow IN) : SV_Target
{
half atten = LIGHT_ATTENUATION(IN);
if (atten == 0.0) {
clip(-1.0);
}
return half4(atten, atten, atten, 1.0);
}
ENDCG
SubShader
{
Tags { "Queue"="Geometry" }
// Forward add pass
Pass
{
Tags { "LightMode" = "ForwardAdd" }
Blend SrcAlpha OneMinusSrcAlpha
Cull Off
CGPROGRAM
#pragma vertex vert_shadow
#pragma fragment frag_shadow
#pragma multi_compile_fwdadd_fullshadows
ENDCG
}
}
FallBack "Diffuse"
}
Regards
Peter

