Skip to content

UWAL / MathUtils

Description ​

A set of mathematical variables and functions for general-purpose usage.

Variables ​

HPI ​

ts
const HPI: number;

Half PI, defined as Math.PI / 2.


TAU ​

ts
const TAU: number;

Double PI, defined as Math.PI * 2.

Functions ​

Clamp() ​

ts
function Clamp(
   value, 
   min?, 
   max?
): number;

Constrain a number between min and max values (inclusive).

Parameters ​

ParameterTypeDefault valueDescription
valuenumberundefinedValue to constrain.
min?number0Lower limit.
max?number1Upper limit.

Returns ​

number


Random() ​

ts
function Random(min?, max?): number;

Get a pseudorandom float between min (inclusive) and max (exclusive). When max is omitted, it defaults to min while min = 0.

Parameters ​

ParameterTypeDefault valueDescription
min?number1Lower or upper limit.
max?numberundefinedUpper limit.

Returns ​

number


RandomInt() ​

ts
function RandomInt(min, max?): number;

Get a pseudorandom integer between min and max values (inclusive). When max is omitted, it defaults to min while min = 0.

Parameters ​

ParameterTypeDescription
minnumberLower or upper limit.
max?numberUpper limit.

Returns ​

number


SmoothStep() ​

ts
function SmoothStep(
   value, 
   min?, 
   max?
): number;

Perform Hermite interpolation between two values. Adapted from GLSL's smoothstep function.

Parameters ​

ParameterTypeDefault valueDescription
valuenumberundefinedValue to interpolate.
min?number0Lower limit.
max?number1Upper limit.

Returns ​

number


SmootherStep() ​

ts
function SmootherStep(
   value, 
   min?, 
   max?
): number;

A variation on the SmoothStep function that has zero 1st and 2nd order derivatives at x = 0 and x = 1. Adapted from three.js' smootherstep function.

Parameters ​

ParameterTypeDefault valueDescription
valuenumberundefinedValue to interpolate.
min?number0Lower limit.
max?number1Upper limit.

Returns ​

number


CopyMat4Rotation() ​

ts
function CopyMat4Rotation(src, dst?): Float32Array<ArrayBufferLike>;

Copy the matrix rotation component into the given 4x4 matrix. Adapted from three.js' extractRotation method.

Parameters ​

ParameterTypeDescription
srcFloat32Array<ArrayBufferLike>Matrix to copy the rotation from.
dst?Float32Array<ArrayBufferLike>Destination matrix.

Returns ​

Float32Array<ArrayBufferLike>


GetMat4Rotation() ​

ts
function GetMat4Rotation(
   src, 
   dst?, 
   order?
): Float32Array<ArrayBufferLike>;

Get the matrix rotation as Euler angles, assuming the upper 3x3 matrix is a pure rotation matrix. Adapted from OGL's fromRotationMatrix function.

Parameters ​

ParameterTypeDefault valueDescription
srcFloat32Array<ArrayBufferLike>undefinedMatrix to get the rotation from.
dst?Float32Array<ArrayBufferLike>...Destination vector.
order?string"XYZ"Rotation order.

Returns ​

Float32Array<ArrayBufferLike>

Throws ​

ERROR.INVALID_ROTATION_ORDER if the order argument is not valid.


GetMaxAxisScale() ​

ts
function GetMaxAxisScale(src): number;

Get the maximum scale on a matrix axis. Adapted from OGL's getMaxScaleOnAxis function.

Parameters ​

ParameterTypeDescription
srcFloat32Array<ArrayBufferLike>Matrix to get the scale from.

Returns ​

number

Released under the MIT License.