Skip to content

@web-engine-dev/math


@web-engine-dev/math / Ray

Class: Ray ​

A 3D ray defined by an origin point and a direction vector. Immutable by design - all operations return new instances.

The direction is expected to be normalized for correct distance calculations. Precomputes inverse direction for fast AABB intersection (slab method).

Example ​

ts
const ray = new Ray(cameraPosition, cameraDirection);
const hit = ray.intersectsAABB(box);
if (hit) {
  const hitPoint = ray.pointAt(hit.tMin);
}

Constructors ​

Constructor ​

new Ray(origin?, direction?): Ray

Parameters ​

origin? ​

Vec3 = ...

direction? ​

Vec3 = ...

Returns ​

Ray

Properties ​

direction ​

readonly direction: Vec3


invDirection ​

readonly invDirection: Vec3

Precomputed 1/direction for fast slab-method AABB tests.


origin ​

readonly origin: Vec3


sign ​

readonly sign: readonly [number, number, number]

Sign bits: 0 if direction component >= 0, 1 if < 0. Used for BVH traversal.

Methods ​

closestPointToPoint() ​

closestPointToPoint(point): Vec3

Returns the closest point on the ray to the given point.

Parameters ​

point ​

Vec3

Returns ​

Vec3


distanceSqToPoint() ​

distanceSqToPoint(point): number

Returns the squared distance from the ray to a point.

Parameters ​

point ​

Vec3

Returns ​

number


distanceToPoint() ​

distanceToPoint(point): number

Returns the distance from the ray to a point.

Parameters ​

point ​

Vec3

Returns ​

number


equals() ​

equals(ray, epsilon?): boolean

Parameters ​

ray ​

Ray

epsilon? ​

number = 0

Returns ​

boolean


intersectAABB() ​

intersectAABB(box): Vec3 | null

Returns the nearest intersection point with an AABB, or null.

Parameters ​

box ​

AABB

Returns ​

Vec3 | null


intersectPlane() ​

intersectPlane(plane): Vec3 | null

Returns the intersection point with a plane, or null.

Parameters ​

plane ​

Plane

Returns ​

Vec3 | null


intersectsAABB() ​

intersectsAABB(box): [number, number] | null

Ray-AABB intersection using the slab method with precomputed inverse direction.

Parameters ​

box ​

AABB

Returns ​

[number, number] | null

[tMin, tMax] if intersection, null otherwise.


intersectSphere() ​

intersectSphere(center, radius): Vec3 | null

Returns the nearest intersection point with a sphere, or null. Only returns hits in front of the ray origin (t >= 0).

Parameters ​

center ​

Vec3

radius ​

number

Returns ​

Vec3 | null


intersectsPlane() ​

intersectsPlane(plane): number | null

Ray-plane intersection.

Parameters ​

plane ​

Plane

Returns ​

number | null

t parameter where the ray hits the plane, or null if parallel or behind.


intersectsSphere() ​

intersectsSphere(center, radius): [number, number] | null

Ray-sphere intersection.

Parameters ​

center ​

Vec3

radius ​

number

Returns ​

[number, number] | null

[tNear, tFar] if the ray intersects the sphere, null otherwise. tNear may be negative if the origin is inside the sphere.


intersectsTriangle() ​

intersectsTriangle(v0, v1, v2): number | null

Ray-triangle intersection using Möller–Trumbore algorithm.

Parameters ​

v0 ​

Vec3

v1 ​

Vec3

v2 ​

Vec3

Returns ​

number | null

t parameter at intersection, or null if no hit.


intersectTriangle() ​

intersectTriangle(v0, v1, v2): Vec3 | null

Returns the intersection point with a triangle, or null.

Parameters ​

v0 ​

Vec3

v1 ​

Vec3

v2 ​

Vec3

Returns ​

Vec3 | null


pointAt() ​

pointAt(t): Vec3

Returns the point at parameter t along the ray: origin + t * direction.

Parameters ​

t ​

number

Returns ​

Vec3


pointAtOut() ​

pointAtOut(t, out): Vec3

Writes the point at parameter t into a preallocated Vec3. Zero-allocation.

Parameters ​

t ​

number

out ​

Vec3

Returns ​

Vec3


toString() ​

toString(): string

Returns ​

string


transform() ​

transform(matrix): Ray

Transforms the ray by a matrix.

Parameters ​

matrix ​

Mat4

Returns ​

Ray


withDirection() ​

withDirection(direction): Ray

Returns a copy with a different direction.

Parameters ​

direction ​

Vec3

Returns ​

Ray


withOrigin() ​

withOrigin(origin): Ray

Returns a copy with a different origin.

Parameters ​

origin ​

Vec3

Returns ​

Ray


fromDirection() ​

static fromDirection(origin, direction): Ray

Creates a ray from origin and unnormalized direction. Normalizes the direction automatically.

Parameters ​

origin ​

Vec3

direction ​

Vec3

Returns ​

Ray


fromPoints() ​

static fromPoints(origin, target): Ray

Creates a ray from two points (origin and target). Direction is normalized.

Parameters ​

origin ​

Vec3

target ​

Vec3

Returns ​

Ray

Proprietary software. All rights reserved.