- Home /
Objects Go Into Shadow as Soon as They are Outside of a Point Light's Range
I have recently been trying to program a simple diffuse shader that supports multiple point lights. It works for the most part, but I have encountered this weird effect where the objects lit by the point light won't dim and fade like they do when using the standard surface shader. Instead, as soon as they are outside of the radius of the point light, they go from being lit to being in shadow. There is nothing in between. Just light, or shadow.
I have programmed light attenuation in; however, I am still not sure why this is happening.
SHADER CODE:
SubShader
{
Tags { ... }
Pass
{
Tags {"LightMode" = "ForwardAdd"}
Blend One One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"
#define POINT
struct v2f
{
float4 pos : SV_POSITION;
float3 worldPos : TEXCOORD0;
float3 normal : TEXCOORD1;
};
v2f vert (appdata_full v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
o.normal = UnityObjectToWorldNormal(v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return float4(0,0,0,1);
}
ENDCG
}
Pass
{
Tags {"LightMode" = "ForwardAdd"}
Blend One One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"
#define POINT
struct v2f
{
float4 pos : SV_POSITION;
float3 worldPos : TEXCOORD0;
float3 normal : TEXCOORD1;
};
v2f vert (appdata_full v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
o.normal = UnityObjectToWorldNormal(v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float3 lightDir = _WorldSpaceLightPos0.w == 0 ? _WorldSpaceLightPos0.xyz : normalize(_WorldSpaceLightPos0 - i.worldPos);
float ndotl = saturate(dot(lightDir, i.normal));
UNITY_LIGHT_ATTENUATION(attenuation, 0, i.worldPos);
fixed4 color = ndotl * attenuation * _LightColor0;
return color;
}
ENDCG
}
}
} `
Answer by DaUr3 · Jul 25, 2020 at 10:29 AM
I looked at some of Unity's other built-in shaders and it seems like this effect was mainly caused by the fact that my shader is not vertex lit. This becomes apparent once you The Standard Shader seems to compute lighting per-vertex and apply a custom light falloff method in order to calculate the fadeaway.
Your answer
Follow this Question
Related Questions
why can a array that must be declared by 'static' be read correctly 0 Answers
Multiple Vectors in shader/CGINC 0 Answers
Dissolve Shader Problem 1 Answer