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

# Ray

A ray that emits from an origin in a certain direction. This is used by the Raycaster 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

### Ray(origin : Vector3, direction : Vector3)

- origin — (optional) the origin of the Ray. Default is a Vector3 at `(0, 0, 0)`.
- direction — Vector3 the direction of the Ray. This must be normalized (with Vector3.normalize) for the methods to operate properly. Default is a Vector3 at `(0, 0, -1)`.

Creates a new Ray.

## Properties

### # .origin : Vector3

The origin of the Ray. Default is a Vector3 at `(0, 0, 0)`.

### # .direction : Vector3

The direction of the Ray. This must be normalized (with Vector3.normalize) for the methods to operate properly. Default is a Vector3 at `(0, 0, -1)`.

## Methods

### # .applyMatrix4(matrix4 : Matrix4) → this

matrix4 — the Matrix4 to apply to this Ray.

Transform this Ray by the Matrix4.

### # .at(t : Float, target : Vector3) → Vector3

- t — the distance along the Ray to retrieve a position for.
- target — the result will be copied into this Vector3.

Get a Vector3 that is a given distance along this Ray.

### # .clone() → Ray

Creates a new Ray with identical origin and direction to this one.

### # .closestPointToPoint(point : Vector3, target : Vector3) → Vector3

- point — the point to get the closest approach to.
- target — the result will be copied into this Vector3.

Get the point along this Ray that is closest to the Vector3 provided.

### # .copy(ray : Ray) → this

Copies the origin and direction properties of ray into this ray.

### # .distanceSqToPoint(point : Vector3) → Float

point — the Vector3 to compute a distance to.

Get the squared distance of the closest approach between the Ray and the Vector3.

### # .distanceSqToSegment(v0 : Vector3, v1 : Vector3, optionalPointOnRay : Vector3, optionalPointOnSegment : Vector3) → Float

- v0 — the start of the line segment.
- v1 — the end of the line segment.
- optionalPointOnRay — (optional) if this is provided, it receives the point on this Ray that is closest to the segment.
- optionalPointOnSegment — (optional) if this is provided, it receives the point on the line segment that is closest to this Ray.

Get the squared distance between this Ray and a line segment.

### # .distanceToPlane(plane : Plane) → Float

plane — the plane to get the distance to.

Get the distance from origin to the Plane, or `null` if the Ray doesn't intersect the Plane.

### # .distanceToPoint(point : Vector3) → Float

point — Vector3 The Vector3 to compute a distance to.

Get the distance of the closest approach between the Ray and the point.

### # .equals(ray : Ray) → Boolean

ray — the Ray to compare to.

Returns `true` if this and the other ray have equal origin and direction.

### # .intersectBox(box : Box3, target : Vector3) → Vector3

- box — the Box3 to intersect with.
- target — the result will be copied into this Vector3.

Intersect this Ray with a Box3, returning the intersection point or `null` if there is no intersection.

### # .intersectPlane(plane : Plane, target : Vector3) → Vector3

- plane — the Plane to intersect with.
- target — the result will be copied into this Vector3.

Intersect this Ray with a Plane, returning the intersection point or `null` if there is no intersection.

### # .intersectSphere(sphere : Sphere, target : Vector3) → Vector3

- sphere — the Sphere to intersect with.
- target — the result will be copied into this Vector3.

Intersect this Ray with a Sphere, returning the intersection point or `null` if there is no intersection.

### # .intersectTriangle(a : Vector3, b : Vector3, c : Vector3, backfaceCulling : Boolean, target : Vector3) → Vector3

- a, b, c — the Vector3 points making up the triangle.
- backfaceCulling — whether to use backface culling.
- target — the result will be copied into this Vector3.

Intersect this Ray with a triangle, returning the intersection point or `null` if there is no intersection.

### # .intersectsBox(box : Box3) → Boolean

box — the Box3 to intersect with.

Return `true` if this Ray intersects with the Box3.

### # .intersectsPlane(plane : Plane) → Boolean

plane — the Plane to intersect with.

Return `true` if this Ray intersects with the Plane.

### # .intersectsSphere(sphere : Sphere) → Boolean

sphere — the Sphere to intersect with.

Return `true` if this Ray intersects with the Sphere.

### # .lookAt(v : Vector3) → this

v — The Vector3 to look at.

Adjusts the direction of the ray to point at the vector in world coordinates.

### # .recast(t : Float) → this

t — The distance along the Ray to interpolate.

Shift the origin of this Ray along its direction by the distance given.

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

- origin — the origin of the Ray.
- origin — the direction of the Ray. This must be normalized (with Vector3.normalize) for the methods to operate properly.

Sets this ray's origin and direction properties by copying the values from the given objects.

## Source

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