Hello I'm writing my own diffuse specular shader.
the diffuse and ambient part is working correctly but the specular highlight does not. it moves when i turn the camera in a way it is not supposed to. for example it can move behind the object even if there is no light.
it seems it is directly connected to the view direction.
therefore i ask myself if i calculated the lightdirection vector correctly because it seems to be (0,0,0), do i have to initialice it???
this is the shader code:
struct v2f
{ float4 position : POSITION; float4 color : COLOR; float2 uv : TEXCOORD0; };
v2f vert (appdata_base i) { v2f o;
o.position = mul(UNITY_MATRIX_MVP, i.vertex);
//Ambient Light
float3 ambient = _Color * UNITY_LIGHTMODEL_AMBIENT;
//Diffuse Light
float3 n = mul(UNITY_MATRIX_IT_MV, float4(i.normal,1)); //Transform normal to WorldSpace
float LdotN = saturate(dot(glstate.light[0].position, n)); //Lambertian Term
float3 diffuse = glstate.light[0].diffuse * LdotN * _Color;
//Specular Light
float3 L = normalize(ObjSpaceLightDir(i.vertex));
float3 V = normalize(ObjSpaceViewDir(i.vertex));
float3 H = normalize(L + V);
float NdotH = saturate(dot(i.normal, H));
float specular = _SpecColor * pow(NdotH, _Shininess*128);
o.color.xyz = diffuse + ambient + specular * glstate.light[0].diffuse;
o.uv = TRANSFORM_TEX(i.texcoord, _MainTex);
return o; }
thx for helping!