Lighting equation help

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"
 
}

Eh, I solved it. The specular term weirdness was my fault. In case anyone wants to write a vertex lit shader that passes the vertex color to the frag shader, here is a good base. You could do a shader where you modulate real time vertex lighting with baked vertex shadows or something.

Note that I had to force shader model 3 to be able to calculate the diffuse and spec terms for all 4 vertex lights. Without SM3, I could only calculate the diff and spec of 2 vertex lights before hitting some sort of instruction limit. You can however calculate just the diffuse of all 4 lights without SM3.

Shader "Base Vtx Lit Pass"  {

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
    
    _MainTex ("Main Tex (RGB) Spec (A)", 2D) = "white" {}
}
 
Category {
    Blend AppSrcAdd AppDstAdd
    Fog { Color [_AddFog] }
 
    // ------------------------------------------------------------------
    // ARB fragment program
   
    SubShader {
        
        Pass {
            Name "BASE"
            Tags { "LightMode" = "Always" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_builtin
/*
----------------
*** ACHTUNG! ***
----------------
Without making it Shader Model 3, I can only calculate diff+spec only up to
2 lights before I hit some sort of instruction limit. Without SM3 you can
calculate all 4 lights by only doing the diffuse calculations
*/
#pragma target 3.0
 
#include "UnityCG.cginc"
#include "AutoLight.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;

// Calculates vertex lighting
void vertexLight(int glLight, float3 viewVector,  float4 position, float3 normal, inout float4 diffuse, inout float4 specular) {
    float4 lightPos = glstate.light[glLight].position;
    float4 lightColor = glstate.light[glLight].diffuse;
    float4 lightAtten = glstate.light[glLight].attenuation;
    
    // Light dir is different for dir light and point light
    float3 lightDir = 0;
    if (lightPos.w == 0) {
        lightDir = mul( lightPos, glstate.matrix.invtrans.modelview[0] ).xyz;
    }
    else {
        lightDir = mul( lightPos, glstate.matrix.invtrans.modelview[0] ).xyz - position.xyz;
    }
    
    float dist = length(lightDir);
    lightDir = normalize(lightDir);
    // Note to self: normalize light dir BEFORE calculating halfvect
    float3 halfVect = normalize(lightDir + viewVector); 
    float attn = max(0, 1 / (lightAtten.x + lightAtten.y * dist + lightAtten.z * dist * dist));
    
    // I'm not quite sure why the shininess needs to be scaled to 200 but it works
    float4 litVal = lit(dot(normal, lightDir), dot(normal, halfVect), _Shininess * 200); 
    diffuse += lightColor * litVal.y * attn;
    specular += lightColor * litVal.z * attn;
}

// Vertex shader
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;
    vertexLight(0, viewVector, v.vertex, v.normal, diff, spec);
    vertexLight(1, viewVector, v.vertex, v.normal, diff, spec);
    // Without forcing SM3, I hit an instruction limit at 2 lights.
    // if vertexLight() only calculated diffuse, you can do this without SM3
    vertexLight(2, viewVector, v.vertex, v.normal, diff, spec);
    vertexLight(3, viewVector, v.vertex, v.normal, diff, spec);
    
    o.diff = saturate(diff) * 2;
    o.spec = saturate(spec);
    
    return o;
}
 
uniform sampler2D _MainTex;
uniform float4 _Color;
uniform float4 _SpecColor;

float4 frag (v2f_vtx_lit_blend i) : COLOR
{
    float4 texcol = tex2D(_MainTex, i.texcoord);
    return (texcol * i.diff * _Color) + i.spec * _SpecColor + (texcol * _PPLAmbient * 2  * _Color);
}
ENDCG
        }
    }
}

FallBack "VertexLit"

}