Can anyone help me figure out whats going wrong with my lighting calculation? My diffuse term seems right but my specular term just comes out really strange. I’m pretty much just following the lighting equations from the Cg Tutorial but my specular term is just coming out wrong. I suspect my space transforms are wrong but I can’t quite see it.
Shader "Vertex Blended/Vertex Lit" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
_Emission ("Emissive Color", Color) = (0, 0, 0, 1)
_Shininess ("Shininess", Range (0.03, 1)) = 0.078125
_Diffuse1 ("Diffuse 1 (RGB) Spec (A)", 2D) = "white" {}
_Diffuse2 ("Diffuse 2 (RGB) Spec (A)", 2D) = "white" {}
_UseMix ("Use Mix Control", Range(0,1)) = 1
_MixControl ("Mix Control (A)", 2D) = "gray" {}
}
Category {
Blend AppSrcAdd AppDstAdd
Fog { Color [_AddFog] }
// ------------------------------------------------------------------
// ARB fragment program
SubShader {
Pass {
Name "TEXBLEND"
Blend AppSrcAdd AppDstAdd
Tags { "LightMode" = "Vertex" }
SeparateSpecular on
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_builtin
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#include "VertexBlend.cginc"
struct v2f_vtx_lit_blend {
float4 position : POSITION;
float fog : FOGC;
float4 color : COLOR ;
float2 texcoord : TEXCOORD0 ;
float4 diff : TEXCOORD1;
float4 spec : TEXCOORD2;
};
uniform float _Shininess;
v2f_vtx_lit_blend vert (appdata_vtx_blend v)
{
v2f_vtx_lit_blend o;
PositionFog(v.vertex, o.position, o.fog);
o.color = v.color; // Transfer the vertex color to frag shader
o.texcoord = v.texcoord;
float3 viewVector = normalize(ObjSpaceViewDir(v.vertex));
float4 diff = 0;
float4 spec = 0;
for (int i=0; i<2; i++) {
float3 lightDir = mul( glstate.light[i].position, glstate.matrix.invtrans.modelview[0]).xyz - v.vertex.xyz;
float3 halfVect = normalize(lightDir + viewVector);
float dist = length(lightDir);
lightDir = normalize(lightDir);
float4 attnVals = glstate.light[i].attenuation;
float attn = max(0, 1 / (attnVals.x + attnVals.y * dist + attnVals.z * dist * dist));
float diffVal = saturate(dot(v.normal, lightDir));
float specVal = pow(saturate(dot(v.normal, halfVect)), _Shininess * 100);
if (diffVal <= 0) { specVal = 0.0; }
diff += glstate.light[i].diffuse * diffVal * attn;
spec += glstate.light[i].diffuse * specVal;
}
o.diff = diff;
o.spec = spec;
return o;
}
uniform sampler2D _Diffuse1;
uniform sampler2D _Diffuse2;
uniform sampler2D _MixControl;
uniform float4 _Color;
uniform float4 _SpecColor;
uniform float4 _Emission;
uniform float _UseMix;
float4 frag (v2f_vtx_lit_blend i) : COLOR
{
return i.spec * _Color;
return (_Color * _PPLAmbient) + i.diff + _Emission;
float4 col1 = tex2D(_Diffuse2, i.texcoord);
return col1 * i.diff;//+ (col1 * _PPLAmbient * 2 * _Color);
}
ENDCG
}
}
}
FallBack "VertexLit"
}