Game Development Reference
In-Depth Information
Applying Lights.js
This class holds the objects of all the lights added to our game scene. We have
defined a function
getDataByType()
to return the array of the component of the
light. For example, if we invoke
lights.getDataByType("ambient")
, it will sum
all the components (R, G, B) of the light ambient colors to return an array of three
values (r, g, b). If we invoke
lights.getDataByType("position")
and if we have
defined three positional lights, it will return an array with nine elements ([x0, y0, z0,
x1, y1, z1, x2, y2, z2], the positions of the three lights in a single array). Similarly for
the directional light, it will return a single array. If we invoke
lights.getDataByT
ype("diffuse","position")
, it will return a diffuse component (R, G, B) of each
positional light concatenated in a single array. The
Light
class is as follows:
Lights = function ( ) {
this.lights=[];
};
Lights.prototype = {
constructor: Lights,
addLight:function(light){
this.lights.push(light);
},
getDataByType:function(type,lightType){
var sum=[];
if(type=="position"){
for(var i=0;i<this.lights.length;++i){
if(this.lights[i].position.length>0){
sum=sum.concat(this.lights[i][type])
}
}
} else if(type=="direction"){
for(var i=0;i<this.lights.length;++i){
if(this.lights[i].direction.length>0){
sum= sum.concat(this.lights[i][type])
}
}
}
else if(type=="ambientColor"){
sum=[0,0,0];
for(var i=0;i<this.lights.length;++i){
sum[0]=sum[0]+this.lights[i].ambientColor[0];
sum[1]=sum[1]+this.lights[i].ambientColor[1];
sum[2]=sum[2]+this.lights[i].ambientColor[2];
}
}