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
r | number | A single byte (0-255). |
g | number | A single byte (0-255). |
b | number | A single byte (0-255). |
Returns
numberA 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
r | number | A single byte (0-255). |
g | number | A single byte (0-255). |
b | number | A single byte (0-255). |
a | number | A single byte (0-255). |
Returns
numberA single unsigned 32bit Number.
clamp(value, min, max)
Clamp a number between min and max inclusive.
Parameters
value | number | Number to clamp. |
min | number | Min value. |
max | number | Max value. |
Returns
numberThe 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
i | number | 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
i | number | Number holding an integer value. |
Returns
number[]An array of 4 bytes.
lerp(a, b, alpha)
Calculates the linear interpolation of two numbers.
Parameters
a | number | Number to linearly interpolate from. |
b | number | Number to linearly interpolate to. |
alpha | number | 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
numberThe 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
a | number | Angle (in degrees) to linearly interpolate from. |
b | number | Angle (in degrees) to linearly interpolate to. |
alpha | number | 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
numberThe linear interpolation of two angles.
nearestPowerOfTwo(val)
Returns the nearest (smaller or larger) power of 2 for the specified value.
Parameters
val | number | The value for which to calculate the nearest power of 2. |
Returns
numberThe nearest power of 2.
nextPowerOfTwo(val)
Returns the next power of 2 for the specified value.
Parameters
val | number | The value for which to calculate the next power of 2. |
Returns
numberThe next power of 2.
powerOfTwo(x)
Returns true if argument is a power-of-two and false otherwise.
Parameters
x | number | Number to check for power-of-two property. |
Returns
booleantrue 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
min | number | Lower bound for range. |
max | number | Upper bound for range. |
Returns
numberPseudo-random number between the supplied range.
roundUp(numToRound, multiple)
Rounds a number up to nearest multiple.
Parameters
numToRound | number | The number to round up. |
multiple | number | The multiple to round up to. |
Returns
numberA 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
min | number | The lower bound of the interpolation range. |
max | number | The upper bound of the interpolation range. |
x | number | The value to interpolate. |
Returns
numberThe 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
min | number | The lower bound of the interpolation range. |
max | number | The upper bound of the interpolation range. |
x | number | The value to interpolate. |
Returns
numberThe smoothly interpolated value clamped between zero and one.