|
@@ -1,5 +1,8 @@
|
|
module BABYLON {
|
|
module BABYLON {
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Scalar computation library
|
|
|
|
+ */
|
|
export class Scalar {
|
|
export class Scalar {
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -9,6 +12,10 @@
|
|
|
|
|
|
/**
|
|
/**
|
|
* Boolean : true if the absolute difference between a and b is lower than epsilon (default = 1.401298E-45)
|
|
* Boolean : true if the absolute difference between a and b is lower than epsilon (default = 1.401298E-45)
|
|
|
|
+ * @param a number
|
|
|
|
+ * @param b number
|
|
|
|
+ * @param epsilon (default = 1.401298E-45)
|
|
|
|
+ * @returns true if the absolute difference between a and b is lower than epsilon (default = 1.401298E-45)
|
|
*/
|
|
*/
|
|
public static WithinEpsilon(a: number, b: number, epsilon: number = 1.401298E-45): boolean {
|
|
public static WithinEpsilon(a: number, b: number, epsilon: number = 1.401298E-45): boolean {
|
|
var num = a - b;
|
|
var num = a - b;
|
|
@@ -17,6 +24,8 @@
|
|
|
|
|
|
/**
|
|
/**
|
|
* Returns a string : the upper case translation of the number i to hexadecimal.
|
|
* Returns a string : the upper case translation of the number i to hexadecimal.
|
|
|
|
+ * @param i number
|
|
|
|
+ * @returns the upper case translation of the number i to hexadecimal.
|
|
*/
|
|
*/
|
|
public static ToHex(i: number): string {
|
|
public static ToHex(i: number): string {
|
|
var str = i.toString(16);
|
|
var str = i.toString(16);
|
|
@@ -30,7 +39,8 @@
|
|
|
|
|
|
/**
|
|
/**
|
|
* Returns -1 if value is negative and +1 is value is positive.
|
|
* Returns -1 if value is negative and +1 is value is positive.
|
|
- * Returns the value itself if it's equal to zero.
|
|
|
|
|
|
+ * @param value the value
|
|
|
|
+ * @returns the value itself if it's equal to zero.
|
|
*/
|
|
*/
|
|
public static Sign(value: number): number {
|
|
public static Sign(value: number): number {
|
|
value = +value; // convert to a number
|
|
value = +value; // convert to a number
|
|
@@ -45,13 +55,19 @@
|
|
* Returns the value itself if it's between min and max.
|
|
* Returns the value itself if it's between min and max.
|
|
* Returns min if the value is lower than min.
|
|
* Returns min if the value is lower than min.
|
|
* Returns max if the value is greater than max.
|
|
* Returns max if the value is greater than max.
|
|
|
|
+ * @param value the value to clmap
|
|
|
|
+ * @param min the min value to clamp to (default: 0)
|
|
|
|
+ * @param max the max value to clamp to (default: 1)
|
|
|
|
+ * @returns the clamped value
|
|
*/
|
|
*/
|
|
public static Clamp(value: number, min = 0, max = 1): number {
|
|
public static Clamp(value: number, min = 0, max = 1): number {
|
|
return Math.min(max, Math.max(min, value));
|
|
return Math.min(max, Math.max(min, value));
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Returns the log2 of value.
|
|
|
|
|
|
+ * the log2 of value.
|
|
|
|
+ * @param value the value to compute log2 of
|
|
|
|
+ * @returns the log2 of value.
|
|
*/
|
|
*/
|
|
public static Log2(value: number): number {
|
|
public static Log2(value: number): number {
|
|
return Math.log(value) * Math.LOG2E;
|
|
return Math.log(value) * Math.LOG2E;
|
|
@@ -64,13 +80,20 @@
|
|
* For example, using 3.0 for t and 2.5 for length, the result would be 0.5.
|
|
* For example, using 3.0 for t and 2.5 for length, the result would be 0.5.
|
|
* With t = 5 and length = 2.5, the result would be 0.0.
|
|
* With t = 5 and length = 2.5, the result would be 0.0.
|
|
* Note, however, that the behaviour is not defined for negative numbers as it is for the modulo operator
|
|
* Note, however, that the behaviour is not defined for negative numbers as it is for the modulo operator
|
|
|
|
+ * @param value the value
|
|
|
|
+ * @param length the length
|
|
|
|
+ * @returns the looped value
|
|
*/
|
|
*/
|
|
public static Repeat(value: number, length: number): number {
|
|
public static Repeat(value: number, length: number): number {
|
|
return value - Math.floor(value / length) * length;
|
|
return value - Math.floor(value / length) * length;
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
|
|
+ /**
|
|
* Normalize the value between 0.0 and 1.0 using min and max values
|
|
* Normalize the value between 0.0 and 1.0 using min and max values
|
|
|
|
+ * @param value value to normalize
|
|
|
|
+ * @param min max to normalize between
|
|
|
|
+ * @param max min to normalize between
|
|
|
|
+ * @returns the normalized value
|
|
*/
|
|
*/
|
|
public static Normalize(value: number, min: number, max: number): number {
|
|
public static Normalize(value: number, min: number, max: number): number {
|
|
return (value - min) / (max - min);
|
|
return (value - min) / (max - min);
|
|
@@ -78,6 +101,10 @@
|
|
|
|
|
|
/**
|
|
/**
|
|
* Denormalize the value from 0.0 and 1.0 using min and max values
|
|
* Denormalize the value from 0.0 and 1.0 using min and max values
|
|
|
|
+ * @param normalized value to denormalize
|
|
|
|
+ * @param min max to denormalize between
|
|
|
|
+ * @param max min to denormalize between
|
|
|
|
+ * @returns the denormalized value
|
|
*/
|
|
*/
|
|
public static Denormalize(normalized: number, min: number, max: number): number {
|
|
public static Denormalize(normalized: number, min: number, max: number): number {
|
|
return (normalized * (max - min) + min);
|
|
return (normalized * (max - min) + min);
|
|
@@ -85,6 +112,9 @@
|
|
|
|
|
|
/**
|
|
/**
|
|
* Calculates the shortest difference between two given angles given in degrees.
|
|
* Calculates the shortest difference between two given angles given in degrees.
|
|
|
|
+ * @param current current angle in degrees
|
|
|
|
+ * @param target target angle in degrees
|
|
|
|
+ * @returns the delta
|
|
*/
|
|
*/
|
|
public static DeltaAngle(current: number, target: number): number {
|
|
public static DeltaAngle(current: number, target: number): number {
|
|
var num: number = Scalar.Repeat(target - current, 360.0);
|
|
var num: number = Scalar.Repeat(target - current, 360.0);
|
|
@@ -96,8 +126,9 @@
|
|
|
|
|
|
/**
|
|
/**
|
|
* PingPongs the value t, so that it is never larger than length and never smaller than 0.
|
|
* PingPongs the value t, so that it is never larger than length and never smaller than 0.
|
|
- *
|
|
|
|
- * The returned value will move back and forth between 0 and length
|
|
|
|
|
|
+ * @param tx value
|
|
|
|
+ * @param length length
|
|
|
|
+ * @returns The returned value will move back and forth between 0 and length
|
|
*/
|
|
*/
|
|
public static PingPong(tx: number, length: number): number {
|
|
public static PingPong(tx: number, length: number): number {
|
|
var t: number = Scalar.Repeat(tx, length * 2.0);
|
|
var t: number = Scalar.Repeat(tx, length * 2.0);
|
|
@@ -109,6 +140,10 @@
|
|
*
|
|
*
|
|
* This function interpolates between min and max in a similar way to Lerp. However, the interpolation will gradually speed up
|
|
* This function interpolates between min and max in a similar way to Lerp. However, the interpolation will gradually speed up
|
|
* from the start and slow down toward the end. This is useful for creating natural-looking animation, fading and other transitions.
|
|
* from the start and slow down toward the end. This is useful for creating natural-looking animation, fading and other transitions.
|
|
|
|
+ * @param from from
|
|
|
|
+ * @param to to
|
|
|
|
+ * @param tx value
|
|
|
|
+ * @returns the smooth stepped value
|
|
*/
|
|
*/
|
|
public static SmoothStep(from: number, to: number, tx: number): number {
|
|
public static SmoothStep(from: number, to: number, tx: number): number {
|
|
var t: number = Scalar.Clamp(tx);
|
|
var t: number = Scalar.Clamp(tx);
|
|
@@ -121,6 +156,10 @@
|
|
*
|
|
*
|
|
* This is essentially the same as Mathf.Lerp but instead the function will ensure that the speed never exceeds maxDelta.
|
|
* This is essentially the same as Mathf.Lerp but instead the function will ensure that the speed never exceeds maxDelta.
|
|
* Negative values of maxDelta pushes the value away from target.
|
|
* Negative values of maxDelta pushes the value away from target.
|
|
|
|
+ * @param current current value
|
|
|
|
+ * @param target target value
|
|
|
|
+ * @param maxDelta max distance to move
|
|
|
|
+ * @returns resulting value
|
|
*/
|
|
*/
|
|
public static MoveTowards(current: number, target: number, maxDelta: number): number {
|
|
public static MoveTowards(current: number, target: number, maxDelta: number): number {
|
|
var result: number = 0;
|
|
var result: number = 0;
|
|
@@ -137,6 +176,10 @@
|
|
*
|
|
*
|
|
* Variables current and target are assumed to be in degrees. For optimization reasons, negative values of maxDelta
|
|
* Variables current and target are assumed to be in degrees. For optimization reasons, negative values of maxDelta
|
|
* are not supported and may cause oscillation. To push current away from a target angle, add 180 to that angle instead.
|
|
* are not supported and may cause oscillation. To push current away from a target angle, add 180 to that angle instead.
|
|
|
|
+ * @param current current value
|
|
|
|
+ * @param target target value
|
|
|
|
+ * @param maxDelta max distance to move
|
|
|
|
+ * @returns resulting angle
|
|
*/
|
|
*/
|
|
public static MoveTowardsAngle(current: number, target: number, maxDelta: number): number {
|
|
public static MoveTowardsAngle(current: number, target: number, maxDelta: number): number {
|
|
var num: number = Scalar.DeltaAngle(current, target);
|
|
var num: number = Scalar.DeltaAngle(current, target);
|
|
@@ -150,8 +193,12 @@
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
|
|
+ /**
|
|
* Creates a new scalar with values linearly interpolated of "amount" between the start scalar and the end scalar.
|
|
* Creates a new scalar with values linearly interpolated of "amount" between the start scalar and the end scalar.
|
|
|
|
+ * @param start start value
|
|
|
|
+ * @param end target value
|
|
|
|
+ * @param amount amount to lerp between
|
|
|
|
+ * @returns the lerped value
|
|
*/
|
|
*/
|
|
public static Lerp(start: number, end: number, amount: number): number {
|
|
public static Lerp(start: number, end: number, amount: number): number {
|
|
return start + ((end - start) * amount);
|
|
return start + ((end - start) * amount);
|
|
@@ -160,6 +207,10 @@
|
|
/**
|
|
/**
|
|
* Same as Lerp but makes sure the values interpolate correctly when they wrap around 360 degrees.
|
|
* Same as Lerp but makes sure the values interpolate correctly when they wrap around 360 degrees.
|
|
* The parameter t is clamped to the range [0, 1]. Variables a and b are assumed to be in degrees.
|
|
* The parameter t is clamped to the range [0, 1]. Variables a and b are assumed to be in degrees.
|
|
|
|
+ * @param start start value
|
|
|
|
+ * @param end target value
|
|
|
|
+ * @param amount amount to lerp between
|
|
|
|
+ * @returns the lerped value
|
|
*/
|
|
*/
|
|
public static LerpAngle(start: number, end: number, amount: number): number {
|
|
public static LerpAngle(start: number, end: number, amount: number): number {
|
|
var num: number = Scalar.Repeat(end - start, 360.0);
|
|
var num: number = Scalar.Repeat(end - start, 360.0);
|
|
@@ -171,6 +222,10 @@
|
|
|
|
|
|
/**
|
|
/**
|
|
* Calculates the linear parameter t that produces the interpolant value within the range [a, b].
|
|
* Calculates the linear parameter t that produces the interpolant value within the range [a, b].
|
|
|
|
+ * @param a start value
|
|
|
|
+ * @param b target value
|
|
|
|
+ * @param value value between a and b
|
|
|
|
+ * @returns the inverseLerp value
|
|
*/
|
|
*/
|
|
public static InverseLerp(a: number, b: number, value: number): number {
|
|
public static InverseLerp(a: number, b: number, value: number): number {
|
|
var result: number = 0;
|
|
var result: number = 0;
|
|
@@ -184,6 +239,13 @@
|
|
|
|
|
|
/**
|
|
/**
|
|
* Returns a new scalar located for "amount" (float) on the Hermite spline defined by the scalars "value1", "value3", "tangent1", "tangent2".
|
|
* Returns a new scalar located for "amount" (float) on the Hermite spline defined by the scalars "value1", "value3", "tangent1", "tangent2".
|
|
|
|
+ * @see http://mathworld.wolfram.com/HermitePolynomial.html
|
|
|
|
+ * @param value1 spline value
|
|
|
|
+ * @param tangent1 spline value
|
|
|
|
+ * @param value2 spline value
|
|
|
|
+ * @param tangent2 spline value
|
|
|
|
+ * @param amount input value
|
|
|
|
+ * @returns hermite result
|
|
*/
|
|
*/
|
|
public static Hermite(value1: number, tangent1: number, value2: number, tangent2: number, amount: number): number {
|
|
public static Hermite(value1: number, tangent1: number, value2: number, tangent2: number, amount: number): number {
|
|
var squared = amount * amount;
|
|
var squared = amount * amount;
|
|
@@ -198,6 +260,9 @@
|
|
|
|
|
|
/**
|
|
/**
|
|
* Returns a random float number between and min and max values
|
|
* Returns a random float number between and min and max values
|
|
|
|
+ * @param min min value of random
|
|
|
|
+ * @param max max value of random
|
|
|
|
+ * @returns random value
|
|
*/
|
|
*/
|
|
public static RandomRange(min: number, max: number): number {
|
|
public static RandomRange(min: number, max: number): number {
|
|
if (min === max) return min;
|
|
if (min === max) return min;
|
|
@@ -209,6 +274,10 @@
|
|
*
|
|
*
|
|
* RangeToPercent(40,20,60) will return 0.5 (50%)
|
|
* RangeToPercent(40,20,60) will return 0.5 (50%)
|
|
* RangeToPercent(34,0,100) will return 0.34 (34%)
|
|
* RangeToPercent(34,0,100) will return 0.34 (34%)
|
|
|
|
+ * @param number to convert to percentage
|
|
|
|
+ * @param min min range
|
|
|
|
+ * @param max max range
|
|
|
|
+ * @returns the percentage
|
|
*/
|
|
*/
|
|
public static RangeToPercent(number: number, min: number, max: number): number {
|
|
public static RangeToPercent(number: number, min: number, max: number): number {
|
|
return ((number - min) / (max - min));
|
|
return ((number - min) / (max - min));
|
|
@@ -218,6 +287,10 @@
|
|
* This function returns number that corresponds to the percentage in a given range.
|
|
* This function returns number that corresponds to the percentage in a given range.
|
|
*
|
|
*
|
|
* PercentToRange(0.34,0,100) will return 34.
|
|
* PercentToRange(0.34,0,100) will return 34.
|
|
|
|
+ * @param percent to convert to number
|
|
|
|
+ * @param min min range
|
|
|
|
+ * @param max max range
|
|
|
|
+ * @returns the number
|
|
*/
|
|
*/
|
|
public static PercentToRange(percent: number, min: number, max: number): number {
|
|
public static PercentToRange(percent: number, min: number, max: number): number {
|
|
return ((max - min) * percent + min);
|
|
return ((max - min) * percent + min);
|