Graphics Reference
In-Depth Information
var intersects =
raycaster.intersectObjects([sphere,
cylinder, cube]);
Here, we first create
THREE.Raycaster
and use the
intersectObjects
function to determine whether
sphere
,
cylinder
, or
cube
are selected. If
an object is selected, it will be stored in the
intersects
array.
5. Now we can process the
intersects
array. The first element will be the
element closest to the camera, and in this recipe, this is the one we're inter-
ested in:
if (intersects.length > 0) {
intersects[0].object.material.transparent
= true;
if
(intersects[0].object.material.opacity
=== 0.5) {
intersects[0].object.material.opacity = 1;
} else {
intersects[0].object.material.opacity =
0.5;
}
}
In this recipe, we just switch the opacity of an object whenever it is clicked
on.
That's it. With this setup, you can select objects using your mouse.
How it works...
This recipe works by using
THREE.RayCaster
. With
THREE.RayCaster
, as the
name implies, you shoot out a ray into the scene. The path of this ray is based on