Ray

A ray that emits from an origin in a certain direction. This is used by the Raycaster| Raycaster to assist with ray casting. Raycaster is used for mouse picking (working out what objects in the 3D space the mouse is over) amongst other things.

Math Class

A ray that emits from an origin in a certain direction. This is used by the Raycaster| Raycaster to assist with ray casting. Raycaster is used for mouse picking (working out what objects in the 3D space the mouse is over) amongst other things.

Constructors

Constructor

new Ray(origin?, direction?): Ray

Parameters

origin?

Vector3

direction?

Vector3

Returns

Ray

Properties

origin

origin: Vector3

The origin of the Ray.

Default Value

is a Vector3 at (0, 0, 0).


direction

direction: Vector3

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

Default Value

is a Vector3 at (0, 0, -1).

Methods

set()

set(origin, direction): Ray

This must be normalized (with Vector3.normalize) for the methods to operate properly. Sets this ray’s .origin| origin and .direction| direction properties by copying the values from the given objects.

Parameters

origin

Vector3

the origin of the Ray.

direction

Vector3

Returns

Ray


clone()

clone(): Ray

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

Returns

Ray


copy()

copy(ray): Ray

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

Parameters

ray

Ray

Returns

Ray


at()

at(t, target): Vector3

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

Parameters

t

number

the distance along the Ray to retrieve a position for.

target

Vector3

the result will be copied into this Vector3.

Returns

Vector3


lookAt()

lookAt(v): Ray

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

Parameters

v

Vector3

The Vector3 to look at.

Returns

Ray


recast()

recast(t): Ray

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

Parameters

t

number

The distance along the Ray to interpolate.

Returns

Ray


closestPointToPoint()

closestPointToPoint(point, target): Vector3

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

Parameters

point

Vector3

the point to get the closest approach to.

target

Vector3

the result will be copied into this Vector3.

Returns

Vector3


distanceToPoint()

distanceToPoint(point): number

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

Parameters

point

Vector3

Vector3 The Vector3 to compute a distance to.

Returns

number


distanceSqToPoint()

distanceSqToPoint(point): number

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

Parameters

point

Vector3

the Vector3 to compute a distance to.

Returns

number


distanceSqToSegment()

distanceSqToSegment(v0, v1, optionalPointOnRay?, optionalPointOnSegment?): number

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

Parameters

v0

Vector3

the start of the line segment.

v1

Vector3

the end of the line segment.

optionalPointOnRay?

Vector3

(optional) if this is provided, it receives the point on this Ray that is closest to the segment.

optionalPointOnSegment?

Vector3

(optional) if this is provided, it receives the point on the line segment that is closest to this Ray.

Returns

number


intersectSphere()

intersectSphere(sphere, target): Nullable<Vector3>

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

Parameters

sphere

Sphere

the Sphere to intersect with.

target

Vector3

the result will be copied into this Vector3.

Returns

Nullable<Vector3>


intersectSphereMaxDistance()

intersectSphereMaxDistance(sphere): number

Calculate the maximum distance between origin and one or two intersections if this ray through the sphere.

Parameters

sphere

Sphere

Returns

number


directionDistance()

directionDistance(point): number

Calculate the length, which is the distance projected on the ray from given point to origin.

Parameters

point

Vector3

Returns

number


intersectsSphere()

intersectsSphere(sphere): boolean

Return true if this Ray intersects with the Sphere.

Parameters

sphere

Sphere

the Sphere to intersect with

Returns

boolean


distanceToPlane()

distanceToPlane(plane): Nullable<number>

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

Parameters

plane

Plane

the Plane to get the distance to.

Returns

Nullable<number>


intersectPlane()

intersectPlane(plane, target): Nullable<Vector3>

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

Parameters

plane

Plane

the Plane to intersect with.

target

Vector3

the result will be copied into this Vector3.

Returns

Nullable<Vector3>


intersectsPlane()

intersectsPlane(plane): boolean

Return true if this Ray intersects with the Plane.

Parameters

plane

Plane

the Plane to intersect with.

Returns

boolean


intersectBox()

intersectBox(box, target?): Nullable<Vector3>

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

Parameters

box

Box3

the Box3 to intersect with.

target?

Vector3

the result will be copied into this Vector3.

Returns

Nullable<Vector3>


intersectsBox()

intersectsBox(box): boolean

Return true if this Ray intersects with the Box3.

Parameters

box

Box3

the Box3 to intersect with.

Returns

boolean


applyMatrix4()

applyMatrix4(matrix4): Ray

Transform this Ray by the Matrix4.

Parameters

matrix4

Matrix4

the Matrix4 to apply to this Ray.

Returns

Ray


equals()

equals(ray): boolean

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

Parameters

ray

Ray

the Ray to compare to.

Returns

boolean


intersectTriangle()

intersectTriangle(a, b, c, backfaceCulling, target): Nullable<Vector3>

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

Parameters

a

Vector3

b

Vector3

c

Vector3

backfaceCulling

boolean

whether to use backface culling

target

Vector3

the result will be copied into this Vector3.

Returns

Nullable<Vector3>