float light = 0;
for(; light < iLights; light += 1.0) {
// light ray
float3 ray = vtx.wpos - vecLightPos[light].xyz;
// spotlight factor
float depth = saturate(dot(vecLightDir[light].xyz, ray) / vecLightPos[light].w);
float spot = 1.0 - saturate(length(cross(vecLightDir[light].xyz, ray)) / (vecLightPos[light].w * depth));
// normalize the light ray
float dist = length(ray);
ray /= dist;
// attenuation
float att = 1.0 - saturate(dist / vecLightPos[light].w);
// final light factor
att *= vecLightDir[light].w ? spot : 1.0;
// diffuse term
diffuse += vecLightColor[light].rgb * saturate(-dot(ray, vtx.normal.xyz)) * att;
// specular term
refl = reflect(ray, vtx.normal.xyz);
specular += vecLightColor[light].rgb * pow(saturate(-dot(refl, vecViewDir.xyz)), fPower) * att;
}