I am very new to shader programming and I’m trying to write my first diffuse lighting model, but I have encountered a strange problem.
At a certain camera distance, the shading inverses. Attached is a screenshot of the scene view, where the shading is correct and the sphere is lit up on the left, and the game view, where the camera is closer and the sphere is lit (wrongly) from the right. This error also appears when moving the camera or object in the scene view.
Here is the fragment function of my shader:
fixed4 frag(v2f IN) : COLOR
{
fixed4 col = tex2D(_MainTex, IN.texcoord);
float3 normalDirection = normalize(IN.normal);
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 diffuse = _LightColor0.rgb * max(0.0, dot(lightDirection, normalDirection));
return fixed4(col * float4(diffuse, 0));
}
