- Home /
Unlit Shader with Shadows from point lights.
Hi guys, I'm really a noob at shader coding, I found this shader that does almost what I need but has a problem, I need an object to be unlit, has a texture, and receives shadows form directional lights and point lights, specially the last one. The idea is that I will have a quad or plane (facing up) in which I'll be loading an image or movie texture, and the objects on top of it should cast shadows on this quad or plane. I need ir to receive shadows caused by a point light because thats what my client needs.
Here is the shader that receive shadows only from a directional light, I would need someone to add the point light support.
Shader "UnlitShadows/UnlitShadowReceive" { Properties{ _MainTex("Base (RGB)", 2D) = "white" {} } SubShader{ Pass{ SetTexture[_MainTex] } Pass { Blend DstColor Zero Tags{ "LightMode" = "ForwardBase" } CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" #pragma multi_compile_fwdbase #include "AutoLight.cginc" struct v2f { float4 pos : SV_POSITION; LIGHTING_COORDS(0,1) }; v2f vert(appdata_base v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); TRANSFER_VERTEX_TO_FRAGMENT(o); return o; } fixed4 frag(v2f i) : COLOR{ float attenuation = LIGHT_ATTENUATION(i); return attenuation; } ENDCG } } Fallback "VertexLit" }
Thanks in advance!!!
Answer by Namey5 · Nov 04, 2016 at 10:59 PM
Shader "UnlitShadows/UnlitShadowReceive" {
Properties {
_MainTex("Base (RGB)", 2D) = "white" {}
}
SubShader {
Pass {
Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma multi_compile_fwdbase
#include "AutoLight.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
struct v2f {
float4 pos : SV_POSITION;
LIGHTING_COORDS(0,1)
float2 uv : TEXCOORD2;
};
v2f vert(appdata_base v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
fixed4 frag(v2f i) : COLOR
{
float attenuation = LIGHT_ATTENUATION(i);
return tex2D (_MainTex, i.uv) * attenuation;
}
ENDCG
}
Pass {
Blend One One
Tags { "LightMode" = "ForwardAdd" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma multi_compile_fwdadd_fullshadows
#include "AutoLight.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
struct v2f {
float4 pos : SV_POSITION;
LIGHTING_COORDS(0,1)
float2 uv : TEXCOORD2;
};
v2f vert(appdata_base v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
fixed4 frag(v2f i) : COLOR
{
float attenuation = LIGHT_ATTENUATION(i);
return tex2D (_MainTex, i.uv) * attenuation;
}
ENDCG
}
}
Fallback "VertexLit"
}
Hi @Namey5 !, Thank you for your answer, as you can see in the image, it still doesn't work 100% but it's so close. I beg you to help me XD... The soccer ball is casting shadows from the point light, and into the plane (which has the shader attached) but its shadow is only visible over the directional light shadow. I would like the shadow from the pont light to be rendered as in any surface. I think you get the Idea.
It's so close! XD, thank you very much.
The image you provided doesn't publicly exist according to my system, however it would be because you aren't rendering the shadows on top of the object, you are blending them with the texture. I've updated my answer to do it how you would generally do shadows.
Hi!, Thanks a lot, It was my bad, I didn't have a texture assigned to the quad so when you said that I'm blending de shadows I understood. You saved me.
I have one more request though, if it's possible. This works great and its enough for my client to be appy, but, I was wondering if it's possible to make it fully unlit but receive shadows. http://www.mediafire.com/view/2cb85c75qhhkdnr/unity%20answer.png Heres a link to another image. The view on the left shows you the scene whithout the point light and the other one shows it with the point light, and there's a little bit of light on the background quad. It so little that it is not a problem but if u cana dn have the time it would be great. Again Thank you so much for this!!
Heres a link
Wow! That's fantastic. Thanks so much for sharing this!
There is just one thing I noticed... When I apply the shader to my unlit texture the shadows on the object are completely black. While the shadows casted on a Standart $$anonymous$$aterial are slightly transparent. Is there any way to match the darkness of the shadows? So that the shadows on the unlit material are also slighly transparent? That would be awesome. Thanks in advance..
I just had to turn down the strength of my directional light :p Thanks again for sharing!!!
@Namey5 hello, i know this is really old thread!!
but ... is it possible to using that on HDRP??
i wants an HDRP Unlit shader with cast-receive shadow ... does exists an shader for that??
The new render pipelines (including HDRP) use their own shader language, so while it is possible, I haven't really looked into writing shaders for said pipelines yet. $$anonymous$$aybe in the future, but for now I can't help you.
Sorry to bump this again but the shadows that are being received are completely black and im not sure how to adjust them. Is there something I should add to the shader? Thank you!
This shader is purely unlit with shadows. If you want to add ambient lighting, then you can do so in the base pass (lines 19-37);
struct v2f
{
float4 pos : SV_POSITION;
LIGHTING_COORDS(0,1)
float2 uv : TEXCOORD2;
float3 worldNorm : TEXCOORD3;
};
v2f vert (appdata_full v)
{
v2f o;
o.pos = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, v.vertex);
o.uv = TRANSFOR$$anonymous$$_TEX (v.texcoord, _$$anonymous$$ainTex);
o.worldNorm = UnityObjectToWorldNormal (v.normal);
TRANSFER_VERTEX_TO_FRAG$$anonymous$$ENT(o);
return o;
}
fixed4 frag (v2f i) : COLOR
{
float attenuation = LIGHT_ATTENUATION(i);
//You can replace 'ambient' with a solid colour, or whatever you want.
//In this case, I'll use the environment SH to fit with normal ambient lighting
i.worldNorm = normalize (i.worldNorm);
half3 ambient = ShadeSH9 (float4 (i.worldNorm, 1));
half4 c = tex2D (_$$anonymous$$ainTex, i.uv);
c.rgb = c.rgb * (attenuation + ambient);
return c;
}