Shader Problem [Solved it]

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));
}

Normally I’d say your camera is in a different position to where you are viewing from in the editor but I’m assuming you checked that. What happens if you disable your shader script & just use normal directional lighting? If it still happens then the issue isn’t in your shader script.

It was in my shader, turns out I had forgotten to write
Tags { “LightMode” = “ForwardBase” }
in my pass function.

1 Like