API Reference

Class List

math

Math API.

Summary

Static Properties

DEG_TO_RAD

Conversion factor between degrees and radians.

RAD_TO_DEG

Conversion factor between degrees and radians.

Static Methods

bytesToInt24

Convert 3 8 bit Numbers into a single unsigned 24 bit Number.

bytesToInt32

Convert 4 1-byte Numbers into a single unsigned 32bit Number.

clamp

Clamp a number between min and max inclusive.

intToBytes24

Convert an 24 bit integer into an array of 3 bytes.

intToBytes32

Convert an 32 bit integer into an array of 4 bytes.

lerp

Calculates the linear interpolation of two numbers.

lerpAngle

Calculates the linear interpolation of two angles ensuring that interpolation is correctly performed across the 360 to 0 degree boundary.

nearestPowerOfTwo

Returns the nearest (smaller or larger) power of 2 for the specified value.

nextPowerOfTwo

Returns the next power of 2 for the specified value.

powerOfTwo

Returns true if argument is a power-of-two and false otherwise.

random

Return a pseudo-random number between min and max.

roundUp

Rounds a number up to nearest multiple.

smootherstep

An improved version of the math.smoothstep function which has zero 1st and 2nd order derivatives at t=0 and t=1.

smoothstep

The function interpolates smoothly between two input values based on a third one that should be between the first two.

Details

Static Properties

DEG_TO_RAD

Conversion factor between degrees and radians.

RAD_TO_DEG

Conversion factor between degrees and radians.

Static Methods

bytesToInt24(r, g, b)

Convert 3 8 bit Numbers into a single unsigned 24 bit Number.

// Set result1 to 0x112233 from an array of 3 values
const result1 = pc.math.bytesToInt24([0x11, 0x22, 0x33]);

// Set result2 to 0x112233 from 3 discrete values
const result2 = pc.math.bytesToInt24(0x11, 0x22, 0x33);

Parameters

rnumber

A single byte (0-255).

gnumber

A single byte (0-255).

bnumber

A single byte (0-255).

Returns

number

A single unsigned 24 bit Number.

bytesToInt32(r, g, b, a)

Convert 4 1-byte Numbers into a single unsigned 32bit Number.

// Set result1 to 0x11223344 from an array of 4 values
const result1 = pc.math.bytesToInt32([0x11, 0x22, 0x33, 0x44]);

// Set result2 to 0x11223344 from 4 discrete values
const result2 = pc.math.bytesToInt32(0x11, 0x22, 0x33, 0x44);

Parameters

rnumber

A single byte (0-255).

gnumber

A single byte (0-255).

bnumber

A single byte (0-255).

anumber

A single byte (0-255).

Returns

number

A single unsigned 32bit Number.

clamp(value, min, max)

Clamp a number between min and max inclusive.

Parameters

valuenumber

Number to clamp.

minnumber

Min value.

maxnumber

Max value.

Returns

number

The clamped value.

intToBytes24(i)

Convert an 24 bit integer into an array of 3 bytes.

// Set bytes to [0x11, 0x22, 0x33]
const bytes = pc.math.intToBytes24(0x112233);

Parameters

inumber

Number holding an integer value.

Returns

number[]

An array of 3 bytes.

intToBytes32(i)

Convert an 32 bit integer into an array of 4 bytes.

// Set bytes to [0x11, 0x22, 0x33, 0x44]
const bytes = pc.math.intToBytes32(0x11223344);

Parameters

inumber

Number holding an integer value.

Returns

number[]

An array of 4 bytes.

lerp(a, b, alpha)

Calculates the linear interpolation of two numbers.

Parameters

anumber

Number to linearly interpolate from.

bnumber

Number to linearly interpolate to.

alphanumber

The value controlling the result of interpolation. When alpha is 0, a is returned. When alpha is 1, b is returned. Between 0 and 1, a linear interpolation between a and b is returned. alpha is clamped between 0 and 1.

Returns

number

The linear interpolation of two numbers.

lerpAngle(a, b, alpha)

Calculates the linear interpolation of two angles ensuring that interpolation is correctly performed across the 360 to 0 degree boundary. Angles are supplied in degrees.

Parameters

anumber

Angle (in degrees) to linearly interpolate from.

bnumber

Angle (in degrees) to linearly interpolate to.

alphanumber

The value controlling the result of interpolation. When alpha is 0, a is returned. When alpha is 1, b is returned. Between 0 and 1, a linear interpolation between a and b is returned. alpha is clamped between 0 and 1.

Returns

number

The linear interpolation of two angles.

nearestPowerOfTwo(val)

Returns the nearest (smaller or larger) power of 2 for the specified value.

Parameters

valnumber

The value for which to calculate the nearest power of 2.

Returns

number

The nearest power of 2.

nextPowerOfTwo(val)

Returns the next power of 2 for the specified value.

Parameters

valnumber

The value for which to calculate the next power of 2.

Returns

number

The next power of 2.

powerOfTwo(x)

Returns true if argument is a power-of-two and false otherwise.

Parameters

xnumber

Number to check for power-of-two property.

Returns

boolean

true if power-of-two and false otherwise.

random(min, max)

Return a pseudo-random number between min and max. The number generated is in the range [min, max), that is inclusive of the minimum but exclusive of the maximum.

Parameters

minnumber

Lower bound for range.

maxnumber

Upper bound for range.

Returns

number

Pseudo-random number between the supplied range.

roundUp(numToRound, multiple)

Rounds a number up to nearest multiple.

Parameters

numToRoundnumber

The number to round up.

multiplenumber

The multiple to round up to.

Returns

number

A number rounded up to nearest multiple.

smootherstep(min, max, x)

An improved version of the math.smoothstep function which has zero 1st and 2nd order derivatives at t=0 and t=1.

See http://en.wikipedia.org/wiki/Smoothstep for more details.

Parameters

minnumber

The lower bound of the interpolation range.

maxnumber

The upper bound of the interpolation range.

xnumber

The value to interpolate.

Returns

number

The smoothly interpolated value clamped between zero and one.

smoothstep(min, max, x)

The function interpolates smoothly between two input values based on a third one that should be between the first two. The returned value is clamped between 0 and 1.

The slope (i.e. derivative) of the smoothstep function starts at 0 and ends at 0. This makes it easy to create a sequence of transitions using smoothstep to interpolate each segment rather than using a more sophisticated or expensive interpolation technique.

See http://en.wikipedia.org/wiki/Smoothstep for more details.

Parameters

minnumber

The lower bound of the interpolation range.

maxnumber

The upper bound of the interpolation range.

xnumber

The value to interpolate.

Returns

number

The smoothly interpolated value clamped between zero and one.