Game Development Reference
In-Depth Information
The returned out parameter holds information such as out.position , out.
rigidbody , and out.frac :
out.position : This variable holds the position of the collider at the time of
intersection with the ray, in case the collider is moving.
out.rigidBody : This variable holds the collider the ray intersected with.
out.frac : This variable holds the probability of intersection of the object
with the ray. This is used internally by the function to check the most
probable object intersecting with the ray.
Learning the basics of picking
All 3D platforms have a very basic problem to solve: what is under the mouse?
This problem in the 3D world is referred to as picking. It is not simple and
straightforward and involves nontrivial mathematics and some complex algorithms,
but it is solvable once you understand the core concept. To solve the problem, there
are two approaches in picking, based on an object color and ray casting.
Picking based on an object's color
Each object in the scene is assigned a unique diffuse color, and then the complete
scene is rendered in an offscreen framebuffer. When the user clicks on the scene, we
get the color from the texture in a framebuffer of the corresponding click coordinate.
Then, we iterate through the list of objects and match the retrieved color with the
object diffuse color. Let's look at the pseudo code:
createFrameBuffer(framebuffer);
//Function invoked at each tick;
function render(){
assignDiffuseColorToEachObject();
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
drawScene();
assignActualTextureOrColorToEachObect();
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
drawScene();
}
If you observe the preceding pseudo code, you will realize that we render the scene
twice; once in the offscreen framebuffer with the assigned color code for each object
and then on the onscreen buffer with the actual texture and the color data. The
following two screenshots explain the concept; the first screenshot shows how the
scene is rendered in the framebuffer and the second screenshot shows how the scene
appears on the actual screen. When the user clicks on the scene, click coordinates are
mapped to the offscreen buffer texture.
 
Search WWH ::




Custom Search