- Home /
Do I have to initialize "ObjSpaceLightDir" in some way?
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!
Answer by giulio-pierucci · Jan 29, 2014 at 10:59 PM
Yes, you have to tag pass at least with "LightMode" = "ForwardBase", otherwise world light position is always vec4 zero.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133705im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Coloured specularity mapping? 1 Answer
Specular Shader On Lightmapped Objects [iOS] 1 Answer
Bump Specular with Hue Shift 0 Answers
Cartoon specular, regular diffuse, fresnel reflective? 1 Answer
Spec only Shader, Again. 1 Answer