@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
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
readonlydirection:Vec3
invDirection
readonlyinvDirection:Vec3
Precomputed 1/direction for fast slab-method AABB tests.
origin
readonlyorigin:Vec3
sign
readonlysign: 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
Returns
distanceSqToPoint()
distanceSqToPoint(
point):number
Returns the squared distance from the ray to a point.
Parameters
point
Returns
number
distanceToPoint()
distanceToPoint(
point):number
Returns the distance from the ray to a point.
Parameters
point
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
Returns
Vec3 | null
intersectPlane()
intersectPlane(
plane):Vec3|null
Returns the intersection point with a plane, or null.
Parameters
plane
Returns
Vec3 | null
intersectsAABB()
intersectsAABB(
box): [number,number] |null
Ray-AABB intersection using the slab method with precomputed inverse direction.
Parameters
box
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
radius
number
Returns
Vec3 | null
intersectsPlane()
intersectsPlane(
plane):number|null
Ray-plane intersection.
Parameters
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
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
v1
v2
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
v1
v2
Returns
Vec3 | null
pointAt()
pointAt(
t):Vec3
Returns the point at parameter t along the ray: origin + t * direction.
Parameters
t
number
Returns
pointAtOut()
pointAtOut(
t,out):Vec3
Writes the point at parameter t into a preallocated Vec3. Zero-allocation.
Parameters
t
number
out
Returns
toString()
toString():
string
Returns
string
transform()
transform(
matrix):Ray
Transforms the ray by a matrix.
Parameters
matrix
Returns
Ray
withDirection()
withDirection(
direction):Ray
Returns a copy with a different direction.
Parameters
direction
Returns
Ray
withOrigin()
withOrigin(
origin):Ray
Returns a copy with a different origin.
Parameters
origin
Returns
Ray
fromDirection()
staticfromDirection(origin,direction):Ray
Creates a ray from origin and unnormalized direction. Normalizes the direction automatically.
Parameters
origin
direction
Returns
Ray
fromPoints()
staticfromPoints(origin,target):Ray
Creates a ray from two points (origin and target). Direction is normalized.
Parameters
origin
target
Returns
Ray