Game Development Reference
In-Depth Information
for(int i = 0;i < NUM_POSITIONAL_LIGHTS ; i++){
vec3 lightDirection = normalize(uLightRay[i]);
float directionalLightWeighting = max(dot(normal, -
lightDirection), 0.0);
iDiffuse+=uPositionalDiffuseColor[i] *materialDiffuseColor *
directionalLightWeighting;
if(directionalLightWeighting>0.0)
{
vec3 halfDir = normalize(-lightDirection + eyeVector);
float specAngle = max(dot(halfDir, normal), 0.0);
specular = pow(specAngle, 4.0);
iSpecular+=uPositionalSpecularColor[i]*
materialSpecularColor*specular;
}
}
vec3 iColor =iAmbient+ iDiffuse+iSpecular;
gl_FragColor = vec4(iColor, 1.0);
}
</script>
Implementing Light.js
We have added a new class to hold the components of light. Although the
Light
class has both
direction
and
position
variables defined, it is expected that we will
use either of them in an object, as shown in the following code:
Light = function ( ) {
this.specularColor=[0,0,0];
this.diffuseColor=[0,0,0];
this.ambientColor=[0,0,0];
this.direction=[];
this.position=[];
};
Light.prototype = {
constructor: Light
}