Raycaster

This class is designed to assist with raycasting. Raycasting is used for mouse picking (working out what objects in the 3d space the mouse is over) amongst other things.

Constructor

new Raycaster(origin, direction)

Constructs a new raycaster.

Parameters:
NameTypeDescription
originVector3

— The origin vector where the ray casts from.

directionVector3

— The (normalized) direction vector that gives direction to the ray.

Members

ray :Ray

The ray used for raycasting.

Type:

Methods

intersectObject(object, recursiveopt, intersectsopt) → {Array.<object>}

Checks all intersection between the ray and the object with or without the descendants. Intersections are returned sorted by distance, closest first. An array of intersections is returned: [ { distance, point, face, faceIndex, object, uv }, ... ]

Parameters:
NameTypeAttributesDefaultDescription
objectObject3D

— The 3D object to check for intersection with the ray.

recursiveboolean<optional>
false

— If set to true, it also checks all descendants. Otherwise it only checks intersection with the object.

intersectsArray.<object><optional>
[]

The target array that holds the result of the method.

Returns:

An array holding the intersection points.

Type: 
Array.<object>

intersectObjects(objects, recursiveopt, intersectsopt) → {Array.<object>}

Checks all intersection between the ray and the objects with or without the descendants. Intersections are returned sorted by distance, closest first. An array of intersections is returned: [ { distance, point, face, faceIndex, object, uv }, ... ]

Parameters:
NameTypeAttributesDefaultDescription
objectsArray.<Object3D>

— The 3D objects to check for intersection with the ray.

recursiveboolean<optional>
false

— If set to true, it also checks all descendants. Otherwise it only checks intersection with the object.

intersectsArray.<object><optional>
[]

The target array that holds the result of the method.

Returns:

An array holding the intersection points.

Type: 
Array.<object>

set(origin, direction)

Updates the ray with a new origin and direction by copying the values from the arguments.

Parameters:
NameTypeDescription
originVector3

— The origin vector where the ray casts from.

directionVector3

— The (normalized) direction vector that gives direction to the ray.

setFromCamera(coords, camera)

Uses the given coordinates and camera to compute a new origin and direction for the internal ray.

Parameters:
NameTypeDescription
coordsVector2

— 2D coordinates of the mouse, in normalized device coordinates (NDC). X and Y components should be between -1 and 1.

cameraCamera

— The camera from which the ray should originate.