|
@@ -82,6 +82,88 @@
|
|
|
public static Denormalize(normalized:number, min:number, max:number): number {
|
|
|
return (normalized * (max - min) + min);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Clamps value between 0 and 1 and returns value.
|
|
|
+ */
|
|
|
+ public static ClampValue(value: number): number {
|
|
|
+ var result: number = 0;
|
|
|
+ if (value < 0.0) {
|
|
|
+ result = 0.0;
|
|
|
+ } else if (value > 1.0) {
|
|
|
+ result = 1.0;
|
|
|
+ } else {
|
|
|
+ result = value;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Calculates the shortest difference between two given angles given in degrees.
|
|
|
+ */
|
|
|
+ public static DeltaAngle(current: number, target: number): number {
|
|
|
+ var num: number = MathTools.Repeat(target - current, 360.0);
|
|
|
+ if (num > 180.0) {
|
|
|
+ num -= 360.0;
|
|
|
+ }
|
|
|
+ return num;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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
|
|
|
+ */
|
|
|
+ public static PingPong(tx: number, length: number): number {
|
|
|
+ var t: number = MathTools.Repeat(tx, length * 2.0);
|
|
|
+ return length - Math.abs(t - length);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Interpolates between min and max with smoothing at the limits.
|
|
|
+ *
|
|
|
+ * 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.
|
|
|
+ */
|
|
|
+ public static SmoothStep(from: number, to: number, tx: number): number {
|
|
|
+ var t: number = MathTools.ClampValue(tx);
|
|
|
+ t = -2.0 * t * t * t + 3.0 * t * t;
|
|
|
+ return to * t + from * (1.0 - t);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Moves a value current towards target.
|
|
|
+ *
|
|
|
+ * 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.
|
|
|
+ */
|
|
|
+ public static MoveTowards(current: number, target: number, maxDelta: number): number {
|
|
|
+ var result: number = 0;
|
|
|
+ if (Math.abs(target - current) <= maxDelta) {
|
|
|
+ result = target;
|
|
|
+ } else {
|
|
|
+ result = current + BABYLON.MathTools.Sign(target - current) * maxDelta;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Same as MoveTowards but makes sure the values interpolate correctly when they wrap around 360 degrees.
|
|
|
+ *
|
|
|
+ * 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.
|
|
|
+ */
|
|
|
+ public static MoveTowardsAngle(current: number, target: number, maxDelta: number): number {
|
|
|
+ var num: number = MathTools.DeltaAngle(current, target);
|
|
|
+ var result: number = 0;
|
|
|
+ if (-maxDelta < num && num < maxDelta) {
|
|
|
+ result = target;
|
|
|
+ } else {
|
|
|
+ target = current + num;
|
|
|
+ result = MathTools.MoveTowards(current, target, maxDelta);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
@@ -94,6 +176,31 @@
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 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.
|
|
|
+ */
|
|
|
+ public static LerpAngle(start: number, end: number, amount: number): number {
|
|
|
+ var num: number = MathTools.Repeat(end - start, 360.0);
|
|
|
+ if (num > 180.0) {
|
|
|
+ num -= 360.0;
|
|
|
+ }
|
|
|
+ return start + num * MathTools.ClampValue(amount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Calculates the linear parameter t that produces the interpolant value within the range [a, b].
|
|
|
+ */
|
|
|
+ public static InverseLerp(a: number, b: number, value: number): number {
|
|
|
+ var result: number = 0;
|
|
|
+ if (a != b) {
|
|
|
+ result = MathTools.ClampValue((value - a) / (b - a));
|
|
|
+ } else {
|
|
|
+ result = 0.0;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* Returns a new scalar located for "amount" (float) on the Hermite spline defined by the scalars "value1", "value3", "tangent1", "tangent2".
|
|
|
*/
|
|
|
public static Hermite(value1: number, tangent1: number, value2: number, tangent2: number, amount: number): number {
|