Raycaster - Verge3D User Manual - Soft8Soft
Description: Learn how to use Raycaster in your interactive 3D apps made with Verge3D

# 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.

## Code Example

`const raycaster = new v3d.Raycaster(); let pointer = null; function onPointerMove(event) { pointer = pointer || new v3d.Vector2(-1, -1); // calculate pointer position in normalized device coordinates // (-1 to +1) for both components pointer.x = (event.offsetX / app.getWidth()) * 2 - 1; pointer.y = -(event.offsetY / app.getHeight()) * 2 + 1; } function pick() { // do not use uninitialized pointer if (!pointer) return; // update the picking ray with the camera and pointer position raycaster.setFromCamera(pointer, app.camera); // calculate objects intersecting the picking ray const intersects = raycaster.intersectObjects(app.scene.children); for (let i = 0; i < intersects.length; i++) { intersects[i].object.visible = false; } } app.container.addEventListener('pointermove', onPointerMove); app.renderCallbacks.push(pick);`

## Constructor

### Raycaster(origin : Vector3, direction : Vector3, near : Float, far : Float)

- origin — the origin vector where the ray casts from.
- direction — the direction vector that gives direction to the ray. Should be normalized.
- near — all results returned are further away than near. Near can't be negative. Default value is `0`.
- far — all results returned are closer than far. Far can't be lower than near. Default value is `Infinity`.

This creates a new Raycaster object.

## Properties

### # .far : Float

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.

### # .near : Float

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.

### # .camera : Camera

The camera to use when raycasting against view-dependent objects such as billboarded objects like Sprites. This field can be set manually or is set when calling .setFromCamera(). Defaults to `null`.

### # .layers : Layers

Used by Raycaster to selectively ignore 3D objects when performing intersection tests. The following code example ensures that only 3D objects on layer `1` will be honored by the instance of Raycaster.

`raycaster.layers.set(1); object.layers.enable(1);`

### # .params : Object

An object with the following properties:

`{ Mesh: { omitGeometry: false }, Line: { threshold: 1 }, LOD: {}, Points: { threshold: 1 }, Sprite: {}, checkVisibility: true }`

Where `threshold` is the precision of the raycaster when intersecting objects, in world units. Specifying `omitGeometry=true` allows you to cast the rays only for mesh boundings (not the geometry itself) which has superior performance. The `checkVisiblity` parameter allow you to enable/disable casting to invisible objects.

### # .ray : Ray

The Ray used for the raycasting.

## Methods

### # .set(origin : Vector3, direction : Vector3)

- origin — the origin vector where the ray casts from.
- direction — the normalized direction vector that gives direction to the ray.

Updates the ray with a new origin and direction. Please note that this method only copies the values from the arguments.

### # .setFromCamera(coords : Vector2, camera : Camera)

- coords — 2D coordinates of the mouse, in normalized device coordinates (NDC) — X and Y components should be between `-1` and `1`.
- camera — camera from which the ray should originate.

Updates the ray with a new origin and direction.

### # .intersectObject(object : Object3D, recursive : Boolean, optionalTarget : Array) → Array

- object — the object to check for intersection with the ray.
- recursive — if `true`, it also checks all descendants. Otherwise it only checks intersection with the object. Default is `true`.
- optionalTarget — (optional) target to set the result. Otherwise a new Array is instantiated. If set, you must clear this array prior to each call (i.e., `array.length = 0;`).

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 }, ...]`

- distance – distance between the origin of the ray and the intersection.
- point – point of intersection, in world coordinates.
- face – intersected face.
- faceIndex – index of the intersected face.
- object – the intersected object.
- uv — U,V coordinates at point of intersection.
- uv2 — Second set of U,V coordinates at point of intersection.

Raycaster delegates to the raycast method of the passed object, when evaluating whether the ray intersects the object or not. This allows meshes to respond differently to ray casting than lines and pointclouds.

For meshes faces must be pointed towards the origin of the ray in order to be detected; intersections of the ray passing through the back of a face will not be detected. To raycast against both faces of an object, you'll want to set the material's side property to `v3d.DoubleSide`.

### # .intersectObjects(objects : Array, recursive : Boolean, optionalTarget : Array) → Array

- objects — the objects to check for intersection with the ray.
- recursive — if `true`, it also checks all descendants of the objects. Otherwise it only checks intersection with the objects. Default is `true`.
- optionalTarget — (optional) target to set the result. Otherwise a new Array is instantiated. If set, you must clear this array prior to each call (i.e., `array.length = 0;`).

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 .intersectObject.

## Puzzles

The raycast puzzle allows to cast rays and collect the results with no coding.

## Source

For more info on how to obtain the source code of this module see this page.