- Home /
Lighting shader doesn't work outside spotlight
Hi everyone! I'm new to shaders and i'm trying to modify a lambert function to make a sprite completely black when its inside the light and make some elements appear withe outside the light. It works great but the problem is that it doesn't work if the sprite is too far away from the light point.
here is the shader.
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Blend One OneMinusSrcAlpha
CGPROGRAM
#pragma surface surf SimpleLambert vertex:vert nofog keepalpha
#pragma multi_compile _ PIXELSNAP_ON
#pragma multi_compile _ ETC1_EXTERNAL_ALPHA
sampler2D _MainTex;
fixed4 _Color;
sampler2D _AlphaTex;
half4 LightingSimpleLambert (SurfaceOutput s, half3 lightDir, half atten) {
half NdotL = dot (s.Normal, lightDir);
half4 c;
c.rgb = s.Albedo *(1.3f - (_LightColor0.rgb * (NdotL * atten+0.3f)));
c.a = s.Alpha;
return c;
}
struct Input
{
float2 uv_MainTex;
fixed4 color;
};
void vert (inout appdata_full v, out Input o)
{
#if defined(PIXELSNAP_ON)
v.vertex = UnityPixelSnap (v.vertex);
#endif
UNITY_INITIALIZE_OUTPUT(Input, o);
o.color = v.color * _Color;
}
fixed4 SampleSpriteTexture (float2 uv)
{
fixed4 color = tex2D (_MainTex, uv);
#if ETC1_EXTERNAL_ALPHA
color.a = tex2D (_AlphaTex, uv).r;
#endif //ETC1_EXTERNAL_ALPHA
return color;
}
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 c = SampleSpriteTexture (IN.uv_MainTex) * IN.color;
o.Albedo = c.rgb * c.a * _LightColor0;
o.Alpha = c.a;
}
ENDCG
}
Fallback "Transparent/VertexLit"
}
Some pics:
In that pic appears the problem, when its far away the cap turns black again, Note that in the sprite the cap is originally white.
Your answer
![](https://koobas.hobune.stream/wayback/20220612095705im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to fix this point light bug in surface shader? 1 Answer
Lighting and shadows for LineRenderer 3 Answers
How to write unlit surface shader? 6 Answers
[3D] How to make lighting doesn't affect on character ? 2 Answers
Is it possible to force a shader to use a specific light for calculations 0 Answers