Math Library
Since the whole project is entirely zero-dependency, it comes with its own math library.
API
Mat4
/math/Mat4.ts ↗
orthographic
Generates orthogonal projection matrix with near/far clip planes corresponding to a normalized NDC z-range of [0, 1].
Type declaration
(
left: number,
right: number,
bottom: number,
top: number,
near: number,
far: number,
) => Mat4;
perspective
fov
is in radians. Assumes WebGPU NDC z-range of [0, 1].
Type declaration
(fov: number, aspect: number, near: number, far: number) => Mat4;
packShelves
/math/packShelves.ts ↗
Takes sizes of rectangles and packs them into a single texture. Width and height will be the next power of two.
Type declaration
(sizes: Vec2[]) => Packing;
triangulateLine
/math/triangulateLine.ts ↗
Given a line defined by a list of points, returns a flat array of points which are vertices of triangles.
Type declaration
(points: Vec2[], thickness: number) => Vec2[]
triangulatePolygon
/math/triangulatePolygon.ts ↗
Triangulates a polygon. Assumes that polygon is clockwise.
Type declaration
(polygon: Vec2[]) => Vec2[]
clamp
/math/utils.ts ↗
Makes sure that the given value is within the given range using combination of min() and max().
Type declaration
(value: number, min: number, max: number) => number;
lerp
/math/utils.ts ↗
Performs a linear interpolation between two values.
Type declaration
(a: number, b: number, t: number) => number;
smoothstep
/math/utils.ts ↗
Performs a smooth interpolation between two values.
t
should be in the range [0, 1].
Type declaration
(a: number, b: number, t: number) => number;
toRadians
/math/utils.ts ↗
Converts degrees to radians.
Type declaration
(degrees: number) => number;
toDegrees
/math/utils.ts ↗
Converts radians to degrees.
Type declaration
(radians: number) => number;
nextPowerOfTwo
/math/utils.ts ↗
Returns the next power of two that is greater than or equal to the given value.
Type declaration
(value: number) => number;
intersection
/math/utils.ts ↗
Returns intersection of two rectangles. If there is no intersection, returns a Vec4(0, 0, 0, 0).
Type declaration
(a: Vec4, b: Vec4) => Vec4;
isInside
/math/utils.ts ↗
Checks if the given point is inside the given rectangle.
Type declaration
(point: Vec2, rectangle: Vec4) => boolean;