babylon.animation.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var Animation = (function () {
  4. function Animation(name, targetProperty, framePerSecond, dataType, loopMode) {
  5. this.name = name;
  6. this.targetProperty = targetProperty;
  7. this.framePerSecond = framePerSecond;
  8. this.dataType = dataType;
  9. this.loopMode = loopMode;
  10. this._offsetsCache = {};
  11. this._highLimitsCache = {};
  12. this._stopped = false;
  13. this.targetPropertyPath = targetProperty.split(".");
  14. this.dataType = dataType;
  15. this.loopMode = loopMode === undefined ? Animation.ANIMATIONLOOPMODE_CYCLE : loopMode;
  16. }
  17. Animation.CreateAndStartAnimation = function (name, mesh, tartgetProperty, framePerSecond, totalFrame, from, to, loopMode) {
  18. var dataType = undefined;
  19. if (!isNaN(parseFloat(from)) && isFinite(from)) {
  20. dataType = Animation.ANIMATIONTYPE_FLOAT;
  21. } else if (from instanceof BABYLON.Quaternion) {
  22. dataType = Animation.ANIMATIONTYPE_QUATERNION;
  23. } else if (from instanceof BABYLON.Vector3) {
  24. dataType = Animation.ANIMATIONTYPE_VECTOR3;
  25. } else if (from instanceof BABYLON.Vector2) {
  26. dataType = Animation.ANIMATIONTYPE_VECTOR2;
  27. } else if (from instanceof BABYLON.Color3) {
  28. dataType = Animation.ANIMATIONTYPE_COLOR3;
  29. }
  30. if (dataType == undefined) {
  31. return;
  32. }
  33. var animation = new Animation(name, tartgetProperty, framePerSecond, dataType, loopMode);
  34. var keys = [];
  35. keys.push({ frame: 0, value: from });
  36. keys.push({ frame: totalFrame, value: to });
  37. animation.setKeys(keys);
  38. mesh.animations.push(animation);
  39. mesh.getScene().beginAnimation(mesh, 0, totalFrame, (animation.loopMode === 1));
  40. };
  41. // Methods
  42. Animation.prototype.isStopped = function () {
  43. return this._stopped;
  44. };
  45. Animation.prototype.getKeys = function () {
  46. return this._keys;
  47. };
  48. Animation.prototype.getEasingFunction = function () {
  49. return this._easingFunction;
  50. };
  51. Animation.prototype.setEasingFunction = function (easingFunction) {
  52. this._easingFunction = easingFunction;
  53. };
  54. Animation.prototype.floatInterpolateFunction = function (startValue, endValue, gradient) {
  55. return startValue + (endValue - startValue) * gradient;
  56. };
  57. Animation.prototype.quaternionInterpolateFunction = function (startValue, endValue, gradient) {
  58. return BABYLON.Quaternion.Slerp(startValue, endValue, gradient);
  59. };
  60. Animation.prototype.vector3InterpolateFunction = function (startValue, endValue, gradient) {
  61. return BABYLON.Vector3.Lerp(startValue, endValue, gradient);
  62. };
  63. Animation.prototype.vector2InterpolateFunction = function (startValue, endValue, gradient) {
  64. return BABYLON.Vector2.Lerp(startValue, endValue, gradient);
  65. };
  66. Animation.prototype.color3InterpolateFunction = function (startValue, endValue, gradient) {
  67. return BABYLON.Color3.Lerp(startValue, endValue, gradient);
  68. };
  69. Animation.prototype.matrixInterpolateFunction = function (startValue, endValue, gradient) {
  70. var startScale = new BABYLON.Vector3(0, 0, 0);
  71. var startRotation = new BABYLON.Quaternion();
  72. var startTranslation = new BABYLON.Vector3(0, 0, 0);
  73. startValue.decompose(startScale, startRotation, startTranslation);
  74. var endScale = new BABYLON.Vector3(0, 0, 0);
  75. var endRotation = new BABYLON.Quaternion();
  76. var endTranslation = new BABYLON.Vector3(0, 0, 0);
  77. endValue.decompose(endScale, endRotation, endTranslation);
  78. var resultScale = this.vector3InterpolateFunction(startScale, endScale, gradient);
  79. var resultRotation = this.quaternionInterpolateFunction(startRotation, endRotation, gradient);
  80. var resultTranslation = this.vector3InterpolateFunction(startTranslation, endTranslation, gradient);
  81. var result = BABYLON.Matrix.Compose(resultScale, resultRotation, resultTranslation);
  82. return result;
  83. };
  84. Animation.prototype.clone = function () {
  85. var clone = new Animation(this.name, this.targetPropertyPath.join("."), this.framePerSecond, this.dataType, this.loopMode);
  86. clone.setKeys(this._keys);
  87. return clone;
  88. };
  89. Animation.prototype.setKeys = function (values) {
  90. this._keys = values.slice(0);
  91. this._offsetsCache = {};
  92. this._highLimitsCache = {};
  93. };
  94. Animation.prototype._getKeyValue = function (value) {
  95. if (typeof value === "function") {
  96. return value();
  97. }
  98. return value;
  99. };
  100. Animation.prototype._interpolate = function (currentFrame, repeatCount, loopMode, offsetValue, highLimitValue) {
  101. if (loopMode === Animation.ANIMATIONLOOPMODE_CONSTANT && repeatCount > 0) {
  102. return highLimitValue.clone ? highLimitValue.clone() : highLimitValue;
  103. }
  104. this.currentFrame = currentFrame;
  105. for (var key = 0; key < this._keys.length; key++) {
  106. // for each frame, we need the key just before the frame superior
  107. if (this._keys[key + 1].frame >= currentFrame) {
  108. var startValue = this._getKeyValue(this._keys[key].value);
  109. var endValue = this._getKeyValue(this._keys[key + 1].value);
  110. // gradient : percent of currentFrame between the frame inf and the frame sup
  111. var gradient = (currentFrame - this._keys[key].frame) / (this._keys[key + 1].frame - this._keys[key].frame);
  112. // check for easingFunction and correction of gradient
  113. if (this._easingFunction != null) {
  114. gradient = this._easingFunction.ease(gradient);
  115. }
  116. switch (this.dataType) {
  117. case Animation.ANIMATIONTYPE_FLOAT:
  118. switch (loopMode) {
  119. case Animation.ANIMATIONLOOPMODE_CYCLE:
  120. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  121. return this.floatInterpolateFunction(startValue, endValue, gradient);
  122. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  123. return offsetValue * repeatCount + this.floatInterpolateFunction(startValue, endValue, gradient);
  124. }
  125. break;
  126. case Animation.ANIMATIONTYPE_QUATERNION:
  127. var quaternion = null;
  128. switch (loopMode) {
  129. case Animation.ANIMATIONLOOPMODE_CYCLE:
  130. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  131. quaternion = this.quaternionInterpolateFunction(startValue, endValue, gradient);
  132. break;
  133. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  134. quaternion = this.quaternionInterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  135. break;
  136. }
  137. return quaternion;
  138. case Animation.ANIMATIONTYPE_VECTOR3:
  139. switch (loopMode) {
  140. case Animation.ANIMATIONLOOPMODE_CYCLE:
  141. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  142. return this.vector3InterpolateFunction(startValue, endValue, gradient);
  143. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  144. return this.vector3InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  145. }
  146. case Animation.ANIMATIONTYPE_VECTOR2:
  147. switch (loopMode) {
  148. case Animation.ANIMATIONLOOPMODE_CYCLE:
  149. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  150. return this.vector2InterpolateFunction(startValue, endValue, gradient);
  151. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  152. return this.vector2InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  153. }
  154. case Animation.ANIMATIONTYPE_COLOR3:
  155. switch (loopMode) {
  156. case Animation.ANIMATIONLOOPMODE_CYCLE:
  157. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  158. return this.color3InterpolateFunction(startValue, endValue, gradient);
  159. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  160. return this.color3InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  161. }
  162. case Animation.ANIMATIONTYPE_MATRIX:
  163. switch (loopMode) {
  164. case Animation.ANIMATIONLOOPMODE_CYCLE:
  165. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  166. return this.matrixInterpolateFunction(startValue, endValue, gradient);
  167. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  168. return startValue;
  169. }
  170. default:
  171. break;
  172. }
  173. break;
  174. }
  175. }
  176. return this._getKeyValue(this._keys[this._keys.length - 1].value);
  177. };
  178. Animation.prototype.animate = function (delay, from, to, loop, speedRatio) {
  179. if (!this.targetPropertyPath || this.targetPropertyPath.length < 1) {
  180. this._stopped = true;
  181. return false;
  182. }
  183. var returnValue = true;
  184. // Adding a start key at frame 0 if missing
  185. if (this._keys[0].frame !== 0) {
  186. var newKey = { frame: 0, value: this._keys[0].value };
  187. this._keys.splice(0, 0, newKey);
  188. }
  189. // Check limits
  190. if (from < this._keys[0].frame || from > this._keys[this._keys.length - 1].frame) {
  191. from = this._keys[0].frame;
  192. }
  193. if (to < this._keys[0].frame || to > this._keys[this._keys.length - 1].frame) {
  194. to = this._keys[this._keys.length - 1].frame;
  195. }
  196. // Compute ratio
  197. var range = to - from;
  198. var offsetValue;
  199. // ratio represents the frame delta between from and to
  200. var ratio = delay * (this.framePerSecond * speedRatio) / 1000.0;
  201. var highLimitValue = 0;
  202. if (ratio > range && !loop) {
  203. returnValue = false;
  204. highLimitValue = this._getKeyValue(this._keys[this._keys.length - 1].value);
  205. } else {
  206. // Get max value if required
  207. if (this.loopMode !== Animation.ANIMATIONLOOPMODE_CYCLE) {
  208. var keyOffset = to.toString() + from.toString();
  209. if (!this._offsetsCache[keyOffset]) {
  210. var fromValue = this._interpolate(from, 0, Animation.ANIMATIONLOOPMODE_CYCLE);
  211. var toValue = this._interpolate(to, 0, Animation.ANIMATIONLOOPMODE_CYCLE);
  212. switch (this.dataType) {
  213. case Animation.ANIMATIONTYPE_FLOAT:
  214. this._offsetsCache[keyOffset] = toValue - fromValue;
  215. break;
  216. case Animation.ANIMATIONTYPE_QUATERNION:
  217. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  218. break;
  219. case Animation.ANIMATIONTYPE_VECTOR3:
  220. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  221. case Animation.ANIMATIONTYPE_VECTOR2:
  222. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  223. case Animation.ANIMATIONTYPE_COLOR3:
  224. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  225. default:
  226. break;
  227. }
  228. this._highLimitsCache[keyOffset] = toValue;
  229. }
  230. highLimitValue = this._highLimitsCache[keyOffset];
  231. offsetValue = this._offsetsCache[keyOffset];
  232. }
  233. }
  234. if (offsetValue === undefined) {
  235. switch (this.dataType) {
  236. case Animation.ANIMATIONTYPE_FLOAT:
  237. offsetValue = 0;
  238. break;
  239. case Animation.ANIMATIONTYPE_QUATERNION:
  240. offsetValue = new BABYLON.Quaternion(0, 0, 0, 0);
  241. break;
  242. case Animation.ANIMATIONTYPE_VECTOR3:
  243. offsetValue = BABYLON.Vector3.Zero();
  244. break;
  245. case Animation.ANIMATIONTYPE_VECTOR2:
  246. offsetValue = BABYLON.Vector2.Zero();
  247. break;
  248. case Animation.ANIMATIONTYPE_COLOR3:
  249. offsetValue = BABYLON.Color3.Black();
  250. }
  251. }
  252. // Compute value
  253. var repeatCount = (ratio / range) >> 0;
  254. var currentFrame = returnValue ? from + ratio % range : to;
  255. var currentValue = this._interpolate(currentFrame, repeatCount, this.loopMode, offsetValue, highLimitValue);
  256. // Set value
  257. if (this.targetPropertyPath.length > 1) {
  258. var property = this._target[this.targetPropertyPath[0]];
  259. for (var index = 1; index < this.targetPropertyPath.length - 1; index++) {
  260. property = property[this.targetPropertyPath[index]];
  261. }
  262. property[this.targetPropertyPath[this.targetPropertyPath.length - 1]] = currentValue;
  263. } else {
  264. this._target[this.targetPropertyPath[0]] = currentValue;
  265. }
  266. if (this._target.markAsDirty) {
  267. this._target.markAsDirty(this.targetProperty);
  268. }
  269. if (!returnValue) {
  270. this._stopped = true;
  271. }
  272. return returnValue;
  273. };
  274. Object.defineProperty(Animation, "ANIMATIONTYPE_FLOAT", {
  275. get: function () {
  276. return Animation._ANIMATIONTYPE_FLOAT;
  277. },
  278. enumerable: true,
  279. configurable: true
  280. });
  281. Object.defineProperty(Animation, "ANIMATIONTYPE_VECTOR3", {
  282. get: function () {
  283. return Animation._ANIMATIONTYPE_VECTOR3;
  284. },
  285. enumerable: true,
  286. configurable: true
  287. });
  288. Object.defineProperty(Animation, "ANIMATIONTYPE_VECTOR2", {
  289. get: function () {
  290. return Animation._ANIMATIONTYPE_VECTOR2;
  291. },
  292. enumerable: true,
  293. configurable: true
  294. });
  295. Object.defineProperty(Animation, "ANIMATIONTYPE_QUATERNION", {
  296. get: function () {
  297. return Animation._ANIMATIONTYPE_QUATERNION;
  298. },
  299. enumerable: true,
  300. configurable: true
  301. });
  302. Object.defineProperty(Animation, "ANIMATIONTYPE_MATRIX", {
  303. get: function () {
  304. return Animation._ANIMATIONTYPE_MATRIX;
  305. },
  306. enumerable: true,
  307. configurable: true
  308. });
  309. Object.defineProperty(Animation, "ANIMATIONTYPE_COLOR3", {
  310. get: function () {
  311. return Animation._ANIMATIONTYPE_COLOR3;
  312. },
  313. enumerable: true,
  314. configurable: true
  315. });
  316. Object.defineProperty(Animation, "ANIMATIONLOOPMODE_RELATIVE", {
  317. get: function () {
  318. return Animation._ANIMATIONLOOPMODE_RELATIVE;
  319. },
  320. enumerable: true,
  321. configurable: true
  322. });
  323. Object.defineProperty(Animation, "ANIMATIONLOOPMODE_CYCLE", {
  324. get: function () {
  325. return Animation._ANIMATIONLOOPMODE_CYCLE;
  326. },
  327. enumerable: true,
  328. configurable: true
  329. });
  330. Object.defineProperty(Animation, "ANIMATIONLOOPMODE_CONSTANT", {
  331. get: function () {
  332. return Animation._ANIMATIONLOOPMODE_CONSTANT;
  333. },
  334. enumerable: true,
  335. configurable: true
  336. });
  337. Animation._ANIMATIONTYPE_FLOAT = 0;
  338. Animation._ANIMATIONTYPE_VECTOR3 = 1;
  339. Animation._ANIMATIONTYPE_QUATERNION = 2;
  340. Animation._ANIMATIONTYPE_MATRIX = 3;
  341. Animation._ANIMATIONTYPE_COLOR3 = 4;
  342. Animation._ANIMATIONTYPE_VECTOR2 = 5;
  343. Animation._ANIMATIONLOOPMODE_RELATIVE = 0;
  344. Animation._ANIMATIONLOOPMODE_CYCLE = 1;
  345. Animation._ANIMATIONLOOPMODE_CONSTANT = 2;
  346. return Animation;
  347. })();
  348. BABYLON.Animation = Animation;
  349. })(BABYLON || (BABYLON = {}));
  350. //# sourceMappingURL=babylon.animation.js.map