babylon.animation.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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, targetProperty, 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, targetProperty, 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. // Float
  128. case Animation.ANIMATIONTYPE_FLOAT:
  129. switch (loopMode) {
  130. case Animation.ANIMATIONLOOPMODE_CYCLE:
  131. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  132. return this.floatInterpolateFunction(startValue, endValue, gradient);
  133. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  134. return offsetValue * repeatCount + this.floatInterpolateFunction(startValue, endValue, gradient);
  135. }
  136. break;
  137. // Quaternion
  138. case Animation.ANIMATIONTYPE_QUATERNION:
  139. var quaternion = null;
  140. switch (loopMode) {
  141. case Animation.ANIMATIONLOOPMODE_CYCLE:
  142. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  143. quaternion = this.quaternionInterpolateFunction(startValue, endValue, gradient);
  144. break;
  145. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  146. quaternion = this.quaternionInterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  147. break;
  148. }
  149. return quaternion;
  150. // Vector3
  151. case Animation.ANIMATIONTYPE_VECTOR3:
  152. switch (loopMode) {
  153. case Animation.ANIMATIONLOOPMODE_CYCLE:
  154. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  155. return this.vector3InterpolateFunction(startValue, endValue, gradient);
  156. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  157. return this.vector3InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  158. }
  159. // Vector2
  160. case Animation.ANIMATIONTYPE_VECTOR2:
  161. switch (loopMode) {
  162. case Animation.ANIMATIONLOOPMODE_CYCLE:
  163. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  164. return this.vector2InterpolateFunction(startValue, endValue, gradient);
  165. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  166. return this.vector2InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  167. }
  168. // Color3
  169. case Animation.ANIMATIONTYPE_COLOR3:
  170. switch (loopMode) {
  171. case Animation.ANIMATIONLOOPMODE_CYCLE:
  172. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  173. return this.color3InterpolateFunction(startValue, endValue, gradient);
  174. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  175. return this.color3InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  176. }
  177. // Matrix
  178. case Animation.ANIMATIONTYPE_MATRIX:
  179. switch (loopMode) {
  180. case Animation.ANIMATIONLOOPMODE_CYCLE:
  181. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  182. // return this.matrixInterpolateFunction(startValue, endValue, gradient);
  183. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  184. return startValue;
  185. }
  186. default:
  187. break;
  188. }
  189. break;
  190. }
  191. }
  192. return this._getKeyValue(this._keys[this._keys.length - 1].value);
  193. };
  194. Animation.prototype.animate = function (delay, from, to, loop, speedRatio) {
  195. if (!this.targetPropertyPath || this.targetPropertyPath.length < 1) {
  196. this._stopped = true;
  197. return false;
  198. }
  199. var returnValue = true;
  200. // Adding a start key at frame 0 if missing
  201. if (this._keys[0].frame !== 0) {
  202. var newKey = { frame: 0, value: this._keys[0].value };
  203. this._keys.splice(0, 0, newKey);
  204. }
  205. // Check limits
  206. if (from < this._keys[0].frame || from > this._keys[this._keys.length - 1].frame) {
  207. from = this._keys[0].frame;
  208. }
  209. if (to < this._keys[0].frame || to > this._keys[this._keys.length - 1].frame) {
  210. to = this._keys[this._keys.length - 1].frame;
  211. }
  212. // Compute ratio
  213. var range = to - from;
  214. var offsetValue;
  215. // ratio represents the frame delta between from and to
  216. var ratio = delay * (this.framePerSecond * speedRatio) / 1000.0;
  217. var highLimitValue = 0;
  218. if (ratio > range && !loop) {
  219. returnValue = false;
  220. highLimitValue = this._getKeyValue(this._keys[this._keys.length - 1].value);
  221. }
  222. else {
  223. // Get max value if required
  224. if (this.loopMode !== Animation.ANIMATIONLOOPMODE_CYCLE) {
  225. var keyOffset = to.toString() + from.toString();
  226. if (!this._offsetsCache[keyOffset]) {
  227. var fromValue = this._interpolate(from, 0, Animation.ANIMATIONLOOPMODE_CYCLE);
  228. var toValue = this._interpolate(to, 0, Animation.ANIMATIONLOOPMODE_CYCLE);
  229. switch (this.dataType) {
  230. // Float
  231. case Animation.ANIMATIONTYPE_FLOAT:
  232. this._offsetsCache[keyOffset] = toValue - fromValue;
  233. break;
  234. // Quaternion
  235. case Animation.ANIMATIONTYPE_QUATERNION:
  236. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  237. break;
  238. // Vector3
  239. case Animation.ANIMATIONTYPE_VECTOR3:
  240. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  241. // Vector2
  242. case Animation.ANIMATIONTYPE_VECTOR2:
  243. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  244. // Color3
  245. case Animation.ANIMATIONTYPE_COLOR3:
  246. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  247. default:
  248. break;
  249. }
  250. this._highLimitsCache[keyOffset] = toValue;
  251. }
  252. highLimitValue = this._highLimitsCache[keyOffset];
  253. offsetValue = this._offsetsCache[keyOffset];
  254. }
  255. }
  256. if (offsetValue === undefined) {
  257. switch (this.dataType) {
  258. // Float
  259. case Animation.ANIMATIONTYPE_FLOAT:
  260. offsetValue = 0;
  261. break;
  262. // Quaternion
  263. case Animation.ANIMATIONTYPE_QUATERNION:
  264. offsetValue = new BABYLON.Quaternion(0, 0, 0, 0);
  265. break;
  266. // Vector3
  267. case Animation.ANIMATIONTYPE_VECTOR3:
  268. offsetValue = BABYLON.Vector3.Zero();
  269. break;
  270. // Vector2
  271. case Animation.ANIMATIONTYPE_VECTOR2:
  272. offsetValue = BABYLON.Vector2.Zero();
  273. break;
  274. // Color3
  275. case Animation.ANIMATIONTYPE_COLOR3:
  276. offsetValue = BABYLON.Color3.Black();
  277. }
  278. }
  279. // Compute value
  280. var repeatCount = (ratio / range) >> 0;
  281. var currentFrame = returnValue ? from + ratio % range : to;
  282. var currentValue = this._interpolate(currentFrame, repeatCount, this.loopMode, offsetValue, highLimitValue);
  283. // Set value
  284. if (this.targetPropertyPath.length > 1) {
  285. var property = this._target[this.targetPropertyPath[0]];
  286. for (var index = 1; index < this.targetPropertyPath.length - 1; index++) {
  287. property = property[this.targetPropertyPath[index]];
  288. }
  289. property[this.targetPropertyPath[this.targetPropertyPath.length - 1]] = currentValue;
  290. }
  291. else {
  292. this._target[this.targetPropertyPath[0]] = currentValue;
  293. }
  294. if (this._target.markAsDirty) {
  295. this._target.markAsDirty(this.targetProperty);
  296. }
  297. if (!returnValue) {
  298. this._stopped = true;
  299. }
  300. return returnValue;
  301. };
  302. Object.defineProperty(Animation, "ANIMATIONTYPE_FLOAT", {
  303. get: function () {
  304. return Animation._ANIMATIONTYPE_FLOAT;
  305. },
  306. enumerable: true,
  307. configurable: true
  308. });
  309. Object.defineProperty(Animation, "ANIMATIONTYPE_VECTOR3", {
  310. get: function () {
  311. return Animation._ANIMATIONTYPE_VECTOR3;
  312. },
  313. enumerable: true,
  314. configurable: true
  315. });
  316. Object.defineProperty(Animation, "ANIMATIONTYPE_VECTOR2", {
  317. get: function () {
  318. return Animation._ANIMATIONTYPE_VECTOR2;
  319. },
  320. enumerable: true,
  321. configurable: true
  322. });
  323. Object.defineProperty(Animation, "ANIMATIONTYPE_QUATERNION", {
  324. get: function () {
  325. return Animation._ANIMATIONTYPE_QUATERNION;
  326. },
  327. enumerable: true,
  328. configurable: true
  329. });
  330. Object.defineProperty(Animation, "ANIMATIONTYPE_MATRIX", {
  331. get: function () {
  332. return Animation._ANIMATIONTYPE_MATRIX;
  333. },
  334. enumerable: true,
  335. configurable: true
  336. });
  337. Object.defineProperty(Animation, "ANIMATIONTYPE_COLOR3", {
  338. get: function () {
  339. return Animation._ANIMATIONTYPE_COLOR3;
  340. },
  341. enumerable: true,
  342. configurable: true
  343. });
  344. Object.defineProperty(Animation, "ANIMATIONLOOPMODE_RELATIVE", {
  345. get: function () {
  346. return Animation._ANIMATIONLOOPMODE_RELATIVE;
  347. },
  348. enumerable: true,
  349. configurable: true
  350. });
  351. Object.defineProperty(Animation, "ANIMATIONLOOPMODE_CYCLE", {
  352. get: function () {
  353. return Animation._ANIMATIONLOOPMODE_CYCLE;
  354. },
  355. enumerable: true,
  356. configurable: true
  357. });
  358. Object.defineProperty(Animation, "ANIMATIONLOOPMODE_CONSTANT", {
  359. get: function () {
  360. return Animation._ANIMATIONLOOPMODE_CONSTANT;
  361. },
  362. enumerable: true,
  363. configurable: true
  364. });
  365. // Statics
  366. Animation._ANIMATIONTYPE_FLOAT = 0;
  367. Animation._ANIMATIONTYPE_VECTOR3 = 1;
  368. Animation._ANIMATIONTYPE_QUATERNION = 2;
  369. Animation._ANIMATIONTYPE_MATRIX = 3;
  370. Animation._ANIMATIONTYPE_COLOR3 = 4;
  371. Animation._ANIMATIONTYPE_VECTOR2 = 5;
  372. Animation._ANIMATIONLOOPMODE_RELATIVE = 0;
  373. Animation._ANIMATIONLOOPMODE_CYCLE = 1;
  374. Animation._ANIMATIONLOOPMODE_CONSTANT = 2;
  375. return Animation;
  376. })();
  377. BABYLON.Animation = Animation;
  378. })(BABYLON || (BABYLON = {}));
  379. //# sourceMappingURL=babylon.animation.js.map