Hi
I have a shader based on the projector shader from the Standard Assets. I use the shader to project an image onto the ground plane - like a decal. The image consists of a circle with transparent background. The projection works fine but the decal doesn’t receive any shadows. See image below.
I have tried examples to get the shader to receive shadows (mainly from here: Adding Shadows to a Unity Vertex/Fragment Shader in 7 Easy Steps | Alastair Aitchison) - but it doesn’t do anything. Is it possible to do? Blending options almost works, but then I get the original red texture blended with the decal, which I don’t want. I removed transparency effects from the shader when I read that shadows can’t be rendered on transparent objects, so I moved it to the geometry queue and made the shader clip the transparent part of the image instead. I’ve tried adding the light attenuation (in code example) from the link above as well as shadow attenuation from other examples. It might be that the projected texture doesn’t work with what I’m trying to do but my knowledge is limited here.
Thanks!
Shader "Custom/ProjectorShadow" {
Properties{
_MainTex("Image", 2D) = "white" {}
}
Subshader{
Tags{ "Queue" = "Geometry"
"RenderType"="Opaque"
}
Pass{
Tags { "LightMode"="ForwardBase"}
//Blend Off
//ColorMask RGB
//Offset -1, -1
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"
struct v2f {
float4 uvShadow : TEXCOORD0;
float4 pos : SV_POSITION;
LIGHTING_COORDS(1, 2)
};
float4x4 unity_Projector;
float4x4 unity_ProjectorClip;
v2f vert(float4 vertex : POSITION)
{
v2f o;
o.pos = UnityObjectToClipPos(vertex);
o.uvShadow = mul(unity_Projector, vertex);
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
sampler2D _MainTex;
fixed4 frag(v2f i) : SV_Target
{
fixed4 texS = tex2Dproj(_MainTex, UNITY_PROJ_COORD(i.uvShadow));
if (texS.a < 0.5)
discard;
float attenuation = LIGHT_ATTENUATION(i);
texS.rgb *= attenuation;
return texS;
}
ENDCG
}
}
FallBack "Diffuse"
}