Game Development Reference
In-Depth Information
stage.stageObjects[rigidBody._id].isPicked=true;
rayCaster.pickedObject=stage.stageObjects[rigidBody._id];
};.............
}
We attach an
onmousedown
event to our canvas object. When the mouse is clicked,
we calculate the mouse click's
x
and
y
coordinates using the sum of
offsetLeft
/
offsetTop
of the canvas's parent objects. Then, we invoke the
pickObject
function
of our
rayCaster
object and the
pickObject
function returns the object of the
selected
rigidBody
object. The
_id
parameter of the
rigidBody
object is retrieved.
This
_id
parameter is the index of the
stageObject
in the
stageObjects
array.
We retrieve the selected
stageObject
and set its
isPicked
property to
true
(
stage.stageObjects[rigidBody._id].isPicked=true;
) and store the selected
stageObject
in the
pickedObject
attribute of the
RayCaster
class (
rayCaster.
pickedObject=stage.stageObjects[rigidBody._id];
). The
pickedObject
attribute of the
RayCaster
class is used to unset the
isPicked
property of the object
when a new object is selected.
Remember all stage objects have an associated
rigidBody
object, which is initialized
when the
addModel
function of the
Stage
class is invoked with the
stageObject
as
the parameter.
Our
drawScene
function is as follows:
function drawScene() {
...
for(var i=0;i<stage.stageObjects.length;++i){
...
if(stage.stageObjects[i].isPicked)
{
gl.uniform3f(shaderProgram.materialDiffuseColor,0,255,0);
}
else
{
gl.uniform3f(shaderProgram.materialDiffuseColor,
stage.stageObjects[i].diffuseColor[0],
stage.stageObjects[i].diffuseColor[1],
stage.stageObjects[i].diffuseColor[2]);
}
if(stage.stageObjects[i].materialFile!=
null&&!stage.stageObjects[i].isPicked){
gl.uniform1i(shaderProgram.hasTexture,1);
....
}