[name]
This class makes raycasting easier. Raycasting is used for picking and more.
Example
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
function onMouseMove(event) {
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1
}
function pickingRay( raycaster, camera, mouse ) {
var vector = new THREE.Vector3( mouse.x, mouse.y, -1 ); // z = - 1 important!
var end = new THREE.Vector3( vector.x, vector.y, 1.0 );
vector.unproject(camera);
end.unproject(camera);
end.sub( vector ).normalize();
raycaster.ray.set( vector, end );
}
window.addEventListener( 'mousemove', onMouseMove, false );
function render() {
pickingRay( raycaster, camera, mouse );
var intersects = raycaster.intersectObjects( scene.children );
for ( var intersect in intersects ) {
intersect.object.material.color = new THREE.Color( 0xff0000 );
}
renderer.render( scene, camera );
}
window.requestAnimationFrame(render);
Constructor
[name]( [page:Vector3 origin], [page:Vector3 direction], [page:Float near], [page:Float far] ) {
[page:Vector3 origin] — The origin vector where the ray casts from.
[page:Vector3 direction] — The direction vector that gives direction to the ray.
[page:Float near] — All results returned are further away than near. Near can't be negative. Default value is 0.
[page:Float far] — All results returned are closer then far. Far can't be lower then near . Default value is Infinity.
This creates a new raycaster object.
Properties
[property:Ray ray]
The Ray used for the raycasting.
[property:float near]
The near factor of the raycaster. This value indicates which objects can be discarded based on the distance.
This value shouldn't be negative and should be smaller than the far property.
[property:float far]
The far factor of the raycaster. This value indicates which objects can be discarded based on the distance.
This value shouldn't be negative and should be larger than the near property.
[property:float precision]
The precision factor of the raycaster when intersecting [page:Mesh] objects.
.[page:float linePrecision]
The precision factor of the raycaster when intersecting [page:Line] objects.
Methods
[method:null set]( [page:Vector3 origin], [page:Vector3 direction] )
[page:Vector3 origin] — The origin vector where the ray casts from.
[page:Vector3 direction] — The direction vector that gives direction to the ray.
Updates the ray with a new origin and direction.
[method:Array intersectObject]( [page:Object3D object], [page:Boolean recursive] )
[page:Object3D object] — The object to check for intersection with the ray.
[page:Boolean recursive] — If set, it also checks all descendants. Otherwise it only checks intersecton with the object.
Checks all intersection between the ray and the object with or without the descendants. Intersections are returned sorted by distance, closest first.
[ { distance, point, face, faceIndex, indices, object }, ... ]
[page:Float distance] – distance between the origin of the ray and the intersection
[page:Vector3 point] – point of intersection, in world coordinates
[page:Face3 face] – intersected face
[page:Integer faceIndex] – index of the intersected face
[page:Array indices] – indices of vertices comprising the intersected face
[page:Object3D object] – the intersected object
When intersecting a [page:Mesh] with a [page:BufferGeometry], the *faceIndex* will be *undefined*, and *indices* will be set; when intersecting a [page:Mesh] with a [page:Geometry], *indices* will be *undefined*.
*Raycaster* delegates to the [page:Object3D.raycast raycast] method of the passed object, when evaluating whether the ray intersects the object or not. This allows [page:Mesh meshes] to respond differently than [page:Line lines] to ray casting.
[method:Array intersectObjects]( [page:Array objects], [page:Boolean recursive] )
[page:Array objects] — The objects to check for intersection with the ray.
[page:Boolean recursive] — If set, it also checks all descendants of the objects. Otherwise it only checks intersecton with the objects.
Checks all intersection between the ray and the objects with or without the descendants. Intersections are returned sorted by distance, closest first. Intersections are of the same form as those returned by [page:.intersectObject].
Source
[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]