|
|
@@ -470,30 +470,253 @@ function normalize( value, array ) {
|
|
|
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * @class
|
|
|
+ * @classdesc A collection of math utility functions.
|
|
|
+ * @hideconstructor
|
|
|
+ */
|
|
|
const MathUtils = {
|
|
|
DEG2RAD: DEG2RAD,
|
|
|
RAD2DEG: RAD2DEG,
|
|
|
+ /**
|
|
|
+ * Generate a [UUID]{@link https://en.wikipedia.org/wiki/Universally_unique_identifier}
|
|
|
+ * (universally unique identifier).
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @return {string} The UUID.
|
|
|
+ */
|
|
|
generateUUID: generateUUID,
|
|
|
+ /**
|
|
|
+ * Clamps the given value between min and max.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} value - The value to clamp.
|
|
|
+ * @param {number} min - The min value.
|
|
|
+ * @param {number} max - The max value.
|
|
|
+ * @return {number} The clamped value.
|
|
|
+ */
|
|
|
clamp: clamp,
|
|
|
+ /**
|
|
|
+ * Computes the Euclidean modulo of the given parameters that
|
|
|
+ * is `( ( n % m ) + m ) % m`.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} n - The first parameter.
|
|
|
+ * @param {number} m - The second parameter.
|
|
|
+ * @return {number} The Euclidean modulo.
|
|
|
+ */
|
|
|
euclideanModulo: euclideanModulo,
|
|
|
+ /**
|
|
|
+ * Performs a linear mapping from range `<a1, a2>` to range `<b1, b2>`
|
|
|
+ * for the given value.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} x - The value to be mapped.
|
|
|
+ * @param {number} a1 - Minimum value for range A.
|
|
|
+ * @param {number} a2 - Maximum value for range A.
|
|
|
+ * @param {number} b1 - Minimum value for range B.
|
|
|
+ * @param {number} b2 - Maximum value for range B.
|
|
|
+ * @return {number} The mapped value.
|
|
|
+ */
|
|
|
mapLinear: mapLinear,
|
|
|
+ /**
|
|
|
+ * Returns the percentage in the closed interval `[0, 1]` of the given value
|
|
|
+ * between the start and end point.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} x - The start point
|
|
|
+ * @param {number} y - The end point.
|
|
|
+ * @param {number} value - A value between start and end.
|
|
|
+ * @return {number} The interpolation factor.
|
|
|
+ */
|
|
|
inverseLerp: inverseLerp,
|
|
|
+ /**
|
|
|
+ * Returns a value linearly interpolated from two known points based on the given interval -
|
|
|
+ * `t = 0` will return `x` and `t = 1` will return `y`.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} x - The start point
|
|
|
+ * @param {number} y - The end point.
|
|
|
+ * @param {number} t - The interpolation factor in the closed interval `[0, 1]`.
|
|
|
+ * @return {number} The interpolated value.
|
|
|
+ */
|
|
|
lerp: lerp,
|
|
|
+ /**
|
|
|
+ * Smoothly interpolate a number from `x` to `y` in a spring-like manner using a delta
|
|
|
+ * time to maintain frame rate independent movement. For details, see
|
|
|
+ * [Frame rate independent damping using lerp]{@link http://www.rorydriscoll.com/2016/03/07/frame-rate-independent-damping-using-lerp/}.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} x - The current point.
|
|
|
+ * @param {number} y - The target point.
|
|
|
+ * @param {number} lambda - A higher lambda value will make the movement more sudden,
|
|
|
+ * and a lower value will make the movement more gradual.
|
|
|
+ * @param {number} dt - Delta time in seconds.
|
|
|
+ * @return {number} The interpolated value.
|
|
|
+ */
|
|
|
damp: damp,
|
|
|
+ /**
|
|
|
+ * Returns a value that alternates between `0` and the given `length` parameter.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} x - The value to pingpong.
|
|
|
+ * @param {number} [length=1] - The positive value the function will pingpong to.
|
|
|
+ * @return {number} The alternated value.
|
|
|
+ */
|
|
|
pingpong: pingpong,
|
|
|
+ /**
|
|
|
+ * Returns a value in the range `[0,1]` that represents the percentage that `x` has
|
|
|
+ * moved between `min` and `max`, but smoothed or slowed down the closer `x` is to
|
|
|
+ * the `min` and `max`.
|
|
|
+ *
|
|
|
+ * See [Smoothstep]{@link http://en.wikipedia.org/wiki/Smoothstep} for more details.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} x - The value to evaluate based on its position between min and max.
|
|
|
+ * @param {number} min - The min value. Any x value below min will be `0`.
|
|
|
+ * @param {number} max - The max value. Any x value above max will be `1`.
|
|
|
+ * @return {number} The alternated value.
|
|
|
+ */
|
|
|
smoothstep: smoothstep,
|
|
|
+ /**
|
|
|
+ * A [variation on smoothstep]{@link https://en.wikipedia.org/wiki/Smoothstep#Variations}
|
|
|
+ * that has zero 1st and 2nd order derivatives at x=0 and x=1.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} x - The value to evaluate based on its position between min and max.
|
|
|
+ * @param {number} min - The min value. Any x value below min will be `0`.
|
|
|
+ * @param {number} max - The max value. Any x value above max will be `1`.
|
|
|
+ * @return {number} The alternated value.
|
|
|
+ */
|
|
|
smootherstep: smootherstep,
|
|
|
+ /**
|
|
|
+ * Returns a random integer from `<low, high>` interval.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} low - The lower value boundary.
|
|
|
+ * @param {number} high - The upper value boundary
|
|
|
+ * @return {number} A random integer.
|
|
|
+ */
|
|
|
randInt: randInt,
|
|
|
+ /**
|
|
|
+ * Returns a random float from `<low, high>` interval.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} low - The lower value boundary.
|
|
|
+ * @param {number} high - The upper value boundary
|
|
|
+ * @return {number} A random float.
|
|
|
+ */
|
|
|
randFloat: randFloat,
|
|
|
+ /**
|
|
|
+ * Returns a random integer from `<-range/2, range/2>` interval.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} range - Defines the value range.
|
|
|
+ * @return {number} A random float.
|
|
|
+ */
|
|
|
randFloatSpread: randFloatSpread,
|
|
|
+ /**
|
|
|
+ * Returns a deterministic pseudo-random float in the interval `[0, 1]`.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} [s] - The integer seed.
|
|
|
+ * @return {number} A random float.
|
|
|
+ */
|
|
|
seededRandom: seededRandom,
|
|
|
+ /**
|
|
|
+ * Converts degrees to radians.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} degrees - A value in degrees.
|
|
|
+ * @return {number} The converted value in radians.
|
|
|
+ */
|
|
|
degToRad: degToRad,
|
|
|
+ /**
|
|
|
+ * Converts radians to degrees.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} radians - A value in radians.
|
|
|
+ * @return {number} The converted value in degrees.
|
|
|
+ */
|
|
|
radToDeg: radToDeg,
|
|
|
+ /**
|
|
|
+ * Returns `true` if the given number is a power of two.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} value - The value to check.
|
|
|
+ * @return {boolean} Whether the given number is a power of two or not.
|
|
|
+ */
|
|
|
isPowerOfTwo: isPowerOfTwo,
|
|
|
+ /**
|
|
|
+ * Returns the smallest power of two that is greater than or equal to the given number.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} value - The value to find a POT for.
|
|
|
+ * @return {number} The smallest power of two that is greater than or equal to the given number.
|
|
|
+ */
|
|
|
ceilPowerOfTwo: ceilPowerOfTwo,
|
|
|
+ /**
|
|
|
+ * Returns the largest power of two that is less than or equal to the given number.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} value - The value to find a POT for.
|
|
|
+ * @return {number} The largest power of two that is less than or equal to the given number.
|
|
|
+ */
|
|
|
floorPowerOfTwo: floorPowerOfTwo,
|
|
|
+ /**
|
|
|
+ * Sets the given quaternion from the [Intrinsic Proper Euler Angles]{@link https://en.wikipedia.org/wiki/Euler_angles}
|
|
|
+ * defined by the given angles and order.
|
|
|
+ *
|
|
|
+ * Rotations are applied to the axes in the order specified by order:
|
|
|
+ * rotation by angle `a` is applied first, then by angle `b`, then by angle `c`.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {Quaternion} q - The quaternion to set.
|
|
|
+ * @param {number} a - The rotation applied to the first axis, in radians.
|
|
|
+ * @param {number} b - The rotation applied to the second axis, in radians.
|
|
|
+ * @param {number} c - The rotation applied to the third axis, in radians.
|
|
|
+ * @param {('XYX'|'XZX'|'YXY'|'YZY'|'ZXZ'|'ZYZ')} order - A string specifying the axes order.
|
|
|
+ */
|
|
|
setQuaternionFromProperEuler: setQuaternionFromProperEuler,
|
|
|
+ /**
|
|
|
+ * Normalizes the given value according to the given typed array.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} value - The float value in the range `[0,1]` to normalize.
|
|
|
+ * @param {TypedArray} array - The typed array that defines the data type of the value.
|
|
|
+ * @return {number} The normalize value.
|
|
|
+ */
|
|
|
normalize: normalize,
|
|
|
+ /**
|
|
|
+ * Denormalizes the given value according to the given typed array.
|
|
|
+ *
|
|
|
+ * @static
|
|
|
+ * @method
|
|
|
+ * @param {number} value - The value to denormalize.
|
|
|
+ * @param {TypedArray} array - The typed array that defines the data type of the value.
|
|
|
+ * @return {number} The denormalize (float) value in the range `[0,1]`.
|
|
|
+ */
|
|
|
denormalize: denormalize
|
|
|
};
|
|
|
|