babylon.easing.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var BABYLON;
  7. (function (BABYLON) {
  8. var EasingFunction = (function () {
  9. function EasingFunction() {
  10. // Properties
  11. this._easingMode = EasingFunction.EASINGMODE_EASEIN;
  12. }
  13. Object.defineProperty(EasingFunction, "EASINGMODE_EASEIN", {
  14. get: function () {
  15. return EasingFunction._EASINGMODE_EASEIN;
  16. },
  17. enumerable: true,
  18. configurable: true
  19. });
  20. Object.defineProperty(EasingFunction, "EASINGMODE_EASEOUT", {
  21. get: function () {
  22. return EasingFunction._EASINGMODE_EASEOUT;
  23. },
  24. enumerable: true,
  25. configurable: true
  26. });
  27. Object.defineProperty(EasingFunction, "EASINGMODE_EASEINOUT", {
  28. get: function () {
  29. return EasingFunction._EASINGMODE_EASEINOUT;
  30. },
  31. enumerable: true,
  32. configurable: true
  33. });
  34. EasingFunction.prototype.setEasingMode = function (easingMode) {
  35. var n = Math.min(Math.max(easingMode, 0), 2);
  36. this._easingMode = n;
  37. };
  38. EasingFunction.prototype.getEasingMode = function () {
  39. return this._easingMode;
  40. };
  41. EasingFunction.prototype.easeInCore = function (gradient) {
  42. throw new Error('You must implement this method');
  43. };
  44. EasingFunction.prototype.ease = function (gradient) {
  45. switch (this._easingMode) {
  46. case EasingFunction.EASINGMODE_EASEIN:
  47. return this.easeInCore(gradient);
  48. case EasingFunction.EASINGMODE_EASEOUT:
  49. return (1 - this.easeInCore(1 - gradient));
  50. }
  51. if (gradient >= 0.5) {
  52. return (((1 - this.easeInCore((1 - gradient) * 2)) * 0.5) + 0.5);
  53. }
  54. return (this.easeInCore(gradient * 2) * 0.5);
  55. };
  56. //Statics
  57. EasingFunction._EASINGMODE_EASEIN = 0;
  58. EasingFunction._EASINGMODE_EASEOUT = 1;
  59. EasingFunction._EASINGMODE_EASEINOUT = 2;
  60. return EasingFunction;
  61. })();
  62. BABYLON.EasingFunction = EasingFunction;
  63. var CircleEase = (function (_super) {
  64. __extends(CircleEase, _super);
  65. function CircleEase() {
  66. _super.apply(this, arguments);
  67. }
  68. CircleEase.prototype.easeInCore = function (gradient) {
  69. gradient = Math.max(0, Math.min(1, gradient));
  70. return (1.0 - Math.sqrt(1.0 - (gradient * gradient)));
  71. };
  72. return CircleEase;
  73. })(EasingFunction);
  74. BABYLON.CircleEase = CircleEase;
  75. var BackEase = (function (_super) {
  76. __extends(BackEase, _super);
  77. function BackEase(amplitude) {
  78. if (amplitude === void 0) { amplitude = 1; }
  79. _super.call(this);
  80. this.amplitude = amplitude;
  81. }
  82. BackEase.prototype.easeInCore = function (gradient) {
  83. var num = Math.max(0, this.amplitude);
  84. return (Math.pow(gradient, 3.0) - ((gradient * num) * Math.sin(3.1415926535897931 * gradient)));
  85. };
  86. return BackEase;
  87. })(EasingFunction);
  88. BABYLON.BackEase = BackEase;
  89. var BounceEase = (function (_super) {
  90. __extends(BounceEase, _super);
  91. function BounceEase(bounces, bounciness) {
  92. if (bounces === void 0) { bounces = 3; }
  93. if (bounciness === void 0) { bounciness = 2; }
  94. _super.call(this);
  95. this.bounces = bounces;
  96. this.bounciness = bounciness;
  97. }
  98. BounceEase.prototype.easeInCore = function (gradient) {
  99. var y = Math.max(0.0, this.bounces);
  100. var bounciness = this.bounciness;
  101. if (bounciness <= 1.0) {
  102. bounciness = 1.001;
  103. }
  104. var num9 = Math.pow(bounciness, y);
  105. var num5 = 1.0 - bounciness;
  106. var num4 = ((1.0 - num9) / num5) + (num9 * 0.5);
  107. var num15 = gradient * num4;
  108. var num65 = Math.log((-num15 * (1.0 - bounciness)) + 1.0) / Math.log(bounciness);
  109. var num3 = Math.floor(num65);
  110. var num13 = num3 + 1.0;
  111. var num8 = (1.0 - Math.pow(bounciness, num3)) / (num5 * num4);
  112. var num12 = (1.0 - Math.pow(bounciness, num13)) / (num5 * num4);
  113. var num7 = (num8 + num12) * 0.5;
  114. var num6 = gradient - num7;
  115. var num2 = num7 - num8;
  116. return (((-Math.pow(1.0 / bounciness, y - num3) / (num2 * num2)) * (num6 - num2)) * (num6 + num2));
  117. };
  118. return BounceEase;
  119. })(EasingFunction);
  120. BABYLON.BounceEase = BounceEase;
  121. var CubicEase = (function (_super) {
  122. __extends(CubicEase, _super);
  123. function CubicEase() {
  124. _super.apply(this, arguments);
  125. }
  126. CubicEase.prototype.easeInCore = function (gradient) {
  127. return (gradient * gradient * gradient);
  128. };
  129. return CubicEase;
  130. })(EasingFunction);
  131. BABYLON.CubicEase = CubicEase;
  132. var ElasticEase = (function (_super) {
  133. __extends(ElasticEase, _super);
  134. function ElasticEase(oscillations, springiness) {
  135. if (oscillations === void 0) { oscillations = 3; }
  136. if (springiness === void 0) { springiness = 3; }
  137. _super.call(this);
  138. this.oscillations = oscillations;
  139. this.springiness = springiness;
  140. }
  141. ElasticEase.prototype.easeInCore = function (gradient) {
  142. var num2;
  143. var num3 = Math.max(0.0, this.oscillations);
  144. var num = Math.max(0.0, this.springiness);
  145. if (num == 0) {
  146. num2 = gradient;
  147. }
  148. else {
  149. num2 = (Math.exp(num * gradient) - 1.0) / (Math.exp(num) - 1.0);
  150. }
  151. return (num2 * Math.sin(((6.2831853071795862 * num3) + 1.5707963267948966) * gradient));
  152. };
  153. return ElasticEase;
  154. })(EasingFunction);
  155. BABYLON.ElasticEase = ElasticEase;
  156. var ExponentialEase = (function (_super) {
  157. __extends(ExponentialEase, _super);
  158. function ExponentialEase(exponent) {
  159. if (exponent === void 0) { exponent = 2; }
  160. _super.call(this);
  161. this.exponent = exponent;
  162. }
  163. ExponentialEase.prototype.easeInCore = function (gradient) {
  164. if (this.exponent <= 0) {
  165. return gradient;
  166. }
  167. return ((Math.exp(this.exponent * gradient) - 1.0) / (Math.exp(this.exponent) - 1.0));
  168. };
  169. return ExponentialEase;
  170. })(EasingFunction);
  171. BABYLON.ExponentialEase = ExponentialEase;
  172. var PowerEase = (function (_super) {
  173. __extends(PowerEase, _super);
  174. function PowerEase(power) {
  175. if (power === void 0) { power = 2; }
  176. _super.call(this);
  177. this.power = power;
  178. }
  179. PowerEase.prototype.easeInCore = function (gradient) {
  180. var y = Math.max(0.0, this.power);
  181. return Math.pow(gradient, y);
  182. };
  183. return PowerEase;
  184. })(EasingFunction);
  185. BABYLON.PowerEase = PowerEase;
  186. var QuadraticEase = (function (_super) {
  187. __extends(QuadraticEase, _super);
  188. function QuadraticEase() {
  189. _super.apply(this, arguments);
  190. }
  191. QuadraticEase.prototype.easeInCore = function (gradient) {
  192. return (gradient * gradient);
  193. };
  194. return QuadraticEase;
  195. })(EasingFunction);
  196. BABYLON.QuadraticEase = QuadraticEase;
  197. var QuarticEase = (function (_super) {
  198. __extends(QuarticEase, _super);
  199. function QuarticEase() {
  200. _super.apply(this, arguments);
  201. }
  202. QuarticEase.prototype.easeInCore = function (gradient) {
  203. return (gradient * gradient * gradient * gradient);
  204. };
  205. return QuarticEase;
  206. })(EasingFunction);
  207. BABYLON.QuarticEase = QuarticEase;
  208. var QuinticEase = (function (_super) {
  209. __extends(QuinticEase, _super);
  210. function QuinticEase() {
  211. _super.apply(this, arguments);
  212. }
  213. QuinticEase.prototype.easeInCore = function (gradient) {
  214. return (gradient * gradient * gradient * gradient * gradient);
  215. };
  216. return QuinticEase;
  217. })(EasingFunction);
  218. BABYLON.QuinticEase = QuinticEase;
  219. var SineEase = (function (_super) {
  220. __extends(SineEase, _super);
  221. function SineEase() {
  222. _super.apply(this, arguments);
  223. }
  224. SineEase.prototype.easeInCore = function (gradient) {
  225. return (1.0 - Math.sin(1.5707963267948966 * (1.0 - gradient)));
  226. };
  227. return SineEase;
  228. })(EasingFunction);
  229. BABYLON.SineEase = SineEase;
  230. var BezierCurveEase = (function (_super) {
  231. __extends(BezierCurveEase, _super);
  232. function BezierCurveEase(x1, y1, x2, y2) {
  233. if (x1 === void 0) { x1 = 0; }
  234. if (y1 === void 0) { y1 = 0; }
  235. if (x2 === void 0) { x2 = 1; }
  236. if (y2 === void 0) { y2 = 1; }
  237. _super.call(this);
  238. this.x1 = x1;
  239. this.y1 = y1;
  240. this.x2 = x2;
  241. this.y2 = y2;
  242. }
  243. BezierCurveEase.prototype.easeInCore = function (gradient) {
  244. return BABYLON.BezierCurve.interpolate(gradient, this.x1, this.y1, this.x2, this.y2);
  245. };
  246. return BezierCurveEase;
  247. })(EasingFunction);
  248. BABYLON.BezierCurveEase = BezierCurveEase;
  249. })(BABYLON || (BABYLON = {}));