Skip to main content

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

function Ray( origin: Vector3, direction: Vector3 ): void;  

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

origin: Vector3;  

The origin of the Ray. Default 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 is a Vector3 at (0, 0, -1).

Methods

applyMatrix4

function applyMatrix4( matrix4: Matrix4 ): this;  

matrix4 - the Matrix4 to apply to this Ray.

Transform this Ray by the Matrix4.

at

function 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

function clone( ): Ray;  

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

closestPointToPoint

function 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

function copy( ray: Ray ): this;  

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

distanceSqToPoint

function 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

function 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

function 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

function 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

function equals( ray: Ray ): Boolean;  

ray - the Ray to compare to.

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

intersectBox

function 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

function 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

function 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

function 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

function intersectsBox( box: Box3 ): Boolean;  

box - the Box3 to intersect with.

Return true if this Ray intersects with the Box3.

intersectsPlane

function intersectsPlane( plane: Plane ): Boolean;  

plane - the Plane to intersect with.

Return true if this Ray intersects with the Plane.

intersectsSphere

function intersectsSphere( sphere: Sphere ): Boolean;  

sphere - the Sphere to intersect with.

Return true if this Ray intersects with the Sphere.

lookAt

function 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

function 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

function set( origin: Vector3, direction: Vector3 ): this;  

.ector3 - the .origin of the Ray.
.ector3 - 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

src/math/Ray.js