babylon.animation.js 18 KB

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