I`ve been following this tutorial , but the result turned out not as It had been excepted: the lighting on geometry with the shader applied looked inverted. http://snag.gy/PIqia.jpg I found the cause pretty quickly though I cannot figure out why it behaves that way, here take a look(pay attention to lines 37-40): Code (CSharp): Shader "1_Lambdert" { Properties { _Color ("Color", Color) = (1.0,1.0,1.0,1.0) } SubShader { Pass { Tags { "LightMode" = "ForwardBase"} CGPROGRAM #pragma vertex vert #pragma fragment frag //user defined variables uniform float4 _Color; //unity defined variables uniform float4 _LightColor0; //base input structs struct vertexInput{ float4 vertex : POSITION; float3 normal : NORMAL; }; struct vertexOutput{ float4 pos : SV_POSITION; float4 col : COLOR; }; //vertex functions vertexOutput vert(vertexInput v){ vertexOutput o; float3 normalDirection = normalize( mul( float4(v.normal,0.0), _World2Object ).xyz ); float lightDirection; float atten = 1.0; lightDirection = normalize( _WorldSpaceLightPos0.xyz ); float3 diffuseReflection = max( 0.0, dot(normalDirection, lightDirection)); //replacing to upper lines with these fixes the error //float3 diffuseReflection = max( 0.0, dot(normalDirection, normalize( _WorldSpaceLightPos0.xyz ))); o.col = float4(diffuseReflection, 1.0); o.pos = mul(UNITY_MATRIX_MVP, v.vertex); return o; } //fragment function float4 frag(vertexOutput i) : COLOR { return i.col; } ENDCG } } } Does anyone has any ideas what`s wrong with that assignment?
You're assigning a vector of 3 floats to just a single float, but you then use it as a vector again. So instead of the .xyz components being used, the components .xxx form the vector, because a float can only store one number. Unity unfortunately does not throw a warning when this happens, so you'll have to watch out for this yourself. So, "float lightDirection" should be "float3 lightDirection" instead.