Then you’re probably right. I haven’t had to do a lot of matrix algebra lately, but I think either way you’re just multiplying each x,y,z coordinate times each individual element of the matching row to produce a new x,y,z value.
Perhaps the person who wrote it was not comfortable with vectors and went out of their way to try and not look at it as a vector times a matrix. Unity is great in that it allows beginners to get started. But that’s also one of its downfalls in that you have a lot of beginners who don’t know how to do things properly and don’t understand even the basics of matrix algebra and such.
But it’s all good. We all have to start somewhere. I certainly would have written it more like what you are saying.
For what it’s worth. Here is my Blinn-Phong smooth shader. It’s in HLSL and Unity does Cg instead, but the concept is basically the same.
float4 SmoothPixelShaderFunction(SmoothVertexShaderOutput Input) : SV_TARGET
{
float3 LightDirection;
float DiffuseLightPercentage;
float4 OutputColor;
float4 SpecularColor;
float3 CameraDirection; //Float3 because the w component really doesn't belong in a 3D vector normal.
float4 AmbientLight;
float4 DiffuseLight;
float4 Texel;
LightDirection = -DiffuseLightDirection; //Normal must face into the light, rather than WITH the light to be lit up.
DiffuseLightPercentage = saturate(dot(Input.Normal, LightDirection)); //Percentage is based on angle between the direction of light and the vertex's normal.
DiffuseLight = saturate((DiffuseLightColor * Input.Color) * DiffuseLightPercentage); //Apply only the percentage of the diffuse color. Saturate clamps output between 0.0 and 1.0.
CameraDirection = normalize(CameraPosition - Input.WorldSpacePosition); //Create a normal that points in the direction from the pixel to the camera.
if (DiffuseLightPercentage == 0.0f)
{
SpecularColor = float4(0.0f, 0.0f, 0.0f, 1.0f);
}
else
{
//SpecularColor = BlinnSpecular(LightDirection, DiffuseLightColor, Input.Normal, CameraDirection, 45.0f);
SpecularColor = PhongSpecular(LightDirection, DiffuseLightColor, Input.Normal, CameraDirection, 35.0f);
}
Texel = tex2D(TextureSampler, Input.UV);
Texel.a = 1;
//OutputColor = saturate((AmbientLightColor * Input.Color) + DiffuseLight * DiffuseLightPercentage + SpecularColor);
OutputColor = saturate(AmbientLightColor + Input.Color + Texel * DiffuseLightPercentage + SpecularColor);
//OutputColor = saturate(Texel);
return OutputColor;
}
float4 BlinnSpecular(float3 LightDirection, float4 LightColor, float3 PixelNormal, float3 CameraDirection, float SpecularPower)
{
float3 HalfwayNormal;
float4 SpecularLight;
float SpecularHighlightAmount;
HalfwayNormal = normalize(LightDirection + CameraDirection);
SpecularHighlightAmount = pow(saturate(dot(PixelNormal, HalfwayNormal)), SpecularPower);
SpecularLight = SpecularHighlightAmount * LightColor;
return SpecularLight;
}
float4 PhongSpecular(float3 LightDirection, float4 LightColor, float3 PixelNormal, float3 CameraDirection, float SpecularPower)
{
float3 ReflectedLightDirection;
float4 SpecularLight;
float SpecularHighlightAmount;
ReflectedLightDirection = 2.0f * PixelNormal * saturate(dot(PixelNormal, LightDirection)) - LightDirection;
SpecularHighlightAmount = pow(saturate(dot(ReflectedLightDirection, CameraDirection)), SpecularPower);
SpecularLight = SpecularHighlightAmount * LightColor;
return SpecularLight;
}