I was following the tutorial on CGCookie on writing shaders in Unity. And when i wrote the code to add ambient light to the shader, it just did not work. I tried the same code in Unity 4.6 and it works. Am i doing anything wrong??
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
uniform float4 _Color;
//unity defined
uniform float4 _LightColor0;
///base structs
struct vertexInput
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct vertexOutput
{
float4 pos : SV_POSITION;
float4 col : COLOR;
};
//vertex function
vertexOutput vert(vertexInput v)
{
vertexOutput o;
float3 normalDirection = normalize(mul(float4(v.normal, 0.0), _World2Object).xyz);
float3 lightDirection;
float atten = 1.0;
lightDirection = normalize( _WorldSpaceLightPos0.xyz);
float3 diffuseReflection = max(0.0, (atten * _LightColor0.xyz * dot(normalDirection , lightDirection)));
float3 finalLight = 2 *(diffuseReflection + UNITY_LIGHTMODEL_AMBIENT.xyz);
o.col = float4(finalLight * _Color.rgb , 1.0);
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
}
//fragment shader
float4 frag(vertexOutput i) : COLOR
{
return i.col;
}
ENDCG
}
}
}
i tried to change this line
float3 finalLight = 2 *(diffuseReflection + UNITY_LIGHTMODEL_AMBIENT.xyz);
to
float3 finalLight = 2 *( UNITY_LIGHTMODEL_AMBIENT.xyz);
just to check if it is working and the mesh is completely black after this change.
Is this a bug or has anything been changed?