Ray

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

new Ray(originopt, directionopt)

Constructs a new ray.

Parameters:
NameTypeAttributesDefaultDescription
originVector3<optional>
(0,0,0)

The origin of the ray.

directionVector3<optional>
(0,0,-1)

The (normalized) direction of the ray.

Members

direction :Vector3

The (normalized) direction of the ray.

Type:

(readonly) isRay :boolean

This flag can be used for type testing.

Type:
  • boolean
Default Value
  • true

origin :Vector3

The origin of the ray.

Type:

Methods

applyMatrix4(matrix4) → {Ray}

Transforms this ray with the given 4x4 transformation matrix.

Parameters:
NameTypeDescription
matrix4Matrix4

The transformation matrix.

Returns:

A reference to this ray.

Type: 
Ray

at(t, target) → {Vector3}

Returns a vector that is located at a given distance along this ray.

Parameters:
NameTypeDescription
tnumber

The distance along the ray to retrieve a position for.

targetVector3

The target vector that is used to store the method's result.

Returns:

A position on the ray.

Type: 
Vector3

clone() → {Ray}

Returns a new ray with copied values from this instance.

Returns:

A clone of this instance.

Type: 
Ray

closestPointToPoint(point, target) → {Vector3}

Returns the point along this ray that is closest to the given point.

Parameters:
NameTypeDescription
pointVector3

A point in 3D space to get the closet location on the ray for.

targetVector3

The target vector that is used to store the method's result.

Returns:

The closest point on this ray.

Type: 
Vector3

copy(ray) → {Ray}

Copies the values of the given ray to this instance.

Parameters:
NameTypeDescription
rayRay

The ray to copy.

Returns:

A reference to this ray.

Type: 
Ray

distanceSqToPoint(point) → {number}

Returns the squared distance of the closest approach between this ray and the given point.

Parameters:
NameTypeDescription
pointVector3

A point in 3D space to compute the distance to.

Returns:

The squared distance.

Type: 
number

distanceToPlane(plane) → (nullable) {number}

Computes the distance from the ray's origin to the given plane. Returns null if the ray does not intersect with the plane.

Parameters:
NameTypeDescription
planePlane

The plane to compute the distance to.

Returns:

Whether this ray intersects with the given sphere or not.

Type: 
number

distanceToPoint(point) → {number}

Returns the distance of the closest approach between this ray and the given point.

Parameters:
NameTypeDescription
pointVector3

A point in 3D space to compute the distance to.

Returns:

The distance.

Type: 
number

equals(ray) → {boolean}

Returns true if this ray is equal with the given one.

Parameters:
NameTypeDescription
rayRay

The ray to test for equality.

Returns:

Whether this ray is equal with the given one.

Type: 
boolean

intersectBox(box, target) → (nullable) {Vector3}

Intersects this ray with the given bounding box, returning the intersection point or null if there is no intersection.

Parameters:
NameTypeDescription
boxBox3

The box to intersect.

targetVector3

The target vector that is used to store the method's result.

Returns:

The intersection point.

Type: 
Vector3

intersectPlane(plane, target) → (nullable) {Vector3}

Intersects this ray with the given plane, returning the intersection point or null if there is no intersection.

Parameters:
NameTypeDescription
planePlane

The plane to intersect.

targetVector3

The target vector that is used to store the method's result.

Returns:

The intersection point.

Type: 
Vector3

intersectSphere(sphere, target) → (nullable) {Vector3}

Intersects this ray with the given sphere, returning the intersection point or null if there is no intersection.

Parameters:
NameTypeDescription
sphereSphere

The sphere to intersect.

targetVector3

The target vector that is used to store the method's result.

Returns:

The intersection point.

Type: 
Vector3

intersectTriangle(a, b, c, backfaceCulling, target) → (nullable) {Vector3}

Intersects this ray with the given triangle, returning the intersection point or null if there is no intersection.

Parameters:
NameTypeDescription
aVector3

The first vertex of the triangle.

bVector3

The second vertex of the triangle.

cVector3

The third vertex of the triangle.

backfaceCullingboolean

Whether to use backface culling or not.

targetVector3

The target vector that is used to store the method's result.

Returns:

The intersection point.

Type: 
Vector3

intersectsBox(box) → {boolean}

Returns true if this ray intersects with the given box.

Parameters:
NameTypeDescription
boxBox3

The box to intersect.

Returns:

Whether this ray intersects with the given box or not.

Type: 
boolean

intersectsPlane(plane) → {boolean}

Returns true if this ray intersects with the given plane.

Parameters:
NameTypeDescription
planePlane

The plane to intersect.

Returns:

Whether this ray intersects with the given plane or not.

Type: 
boolean

intersectsSphere(sphere) → {boolean}

Returns true if this ray intersects with the given sphere.

Parameters:
NameTypeDescription
sphereSphere

The sphere to intersect.

Returns:

Whether this ray intersects with the given sphere or not.

Type: 
boolean

recast(t) → {Ray}

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

Parameters:
NameTypeDescription
tnumber

The distance along the ray to interpolate.

Returns:

A reference to this ray.

Type: 
Ray

set(origin, direction) → {Ray}

Sets the ray's components by copying the given values.

Parameters:
NameTypeDescription
originVector3

The origin.

directionVector3

The direction.

Returns:

A reference to this ray.

Type: 
Ray