|
@@ -67,16 +67,108 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
return Math.log(value) * Math.LOG2E;
|
|
|
};
|
|
|
/**
|
|
|
- * Loops the value, so that it is never larger than length and never smaller than 0.
|
|
|
- *
|
|
|
- * This is similar to the modulo operator but it works with floating point numbers.
|
|
|
- * 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.
|
|
|
- * Note, however, that the behaviour is not defined for negative numbers as it is for the modulo operator
|
|
|
- */
|
|
|
+ * Loops the value, so that it is never larger than length and never smaller than 0.
|
|
|
+ *
|
|
|
+ * This is similar to the modulo operator but it works with floating point numbers.
|
|
|
+ * 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.
|
|
|
+ * Note, however, that the behaviour is not defined for negative numbers as it is for the modulo operator
|
|
|
+ */
|
|
|
MathTools.Repeat = function (value, length) {
|
|
|
return value - Math.floor(value / length) * length;
|
|
|
};
|
|
|
+ /**
|
|
|
+ * Normalize the value between 0.0 and 1.0 using min and max values
|
|
|
+ */
|
|
|
+ MathTools.Normalize = function (value, min, max) {
|
|
|
+ return (value - min) / (max - min);
|
|
|
+ };
|
|
|
+ /**
|
|
|
+ * Denormalize the value from 0.0 and 1.0 using min and max values
|
|
|
+ */
|
|
|
+ MathTools.Denormalize = function (normalized, min, max) {
|
|
|
+ return (normalized * (max - min) + min);
|
|
|
+ };
|
|
|
+ /**
|
|
|
+ * Clamps value between 0 and 1 and returns value.
|
|
|
+ */
|
|
|
+ MathTools.ClampValue = function (value) {
|
|
|
+ var result = 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.
|
|
|
+ */
|
|
|
+ MathTools.DeltaAngle = function (current, target) {
|
|
|
+ var num = 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
|
|
|
+ */
|
|
|
+ MathTools.PingPong = function (tx, length) {
|
|
|
+ var t = 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.
|
|
|
+ */
|
|
|
+ MathTools.SmoothStep = function (from, to, tx) {
|
|
|
+ var t = 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.
|
|
|
+ */
|
|
|
+ MathTools.MoveTowards = function (current, target, maxDelta) {
|
|
|
+ var result = 0;
|
|
|
+ if (Math.abs(target - current) <= maxDelta) {
|
|
|
+ result = target;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ result = current + 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.
|
|
|
+ */
|
|
|
+ MathTools.MoveTowardsAngle = function (current, target, maxDelta) {
|
|
|
+ var num = MathTools.DeltaAngle(current, target);
|
|
|
+ var result = 0;
|
|
|
+ if (-maxDelta < num && num < maxDelta) {
|
|
|
+ result = target;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ target = current + num;
|
|
|
+ result = MathTools.MoveTowards(current, target, maxDelta);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ };
|
|
|
return MathTools;
|
|
|
}());
|
|
|
BABYLON.MathTools = MathTools;
|
|
@@ -90,6 +182,30 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
return start + ((end - start) * amount);
|
|
|
};
|
|
|
/**
|
|
|
+ * 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.
|
|
|
+ */
|
|
|
+ Scalar.LerpAngle = function (start, end, amount) {
|
|
|
+ var num = 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].
|
|
|
+ */
|
|
|
+ Scalar.InverseLerp = function (a, b, value) {
|
|
|
+ var result = 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".
|
|
|
*/
|
|
|
Scalar.Hermite = function (value1, tangent1, value2, tangent2, amount) {
|