babylon.animation.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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.clone = function () {
  70. var clone = new Animation(this.name, this.targetPropertyPath.join("."), this.framePerSecond, this.dataType, this.loopMode);
  71. clone.setKeys(this._keys);
  72. return clone;
  73. };
  74. Animation.prototype.setKeys = function (values) {
  75. this._keys = values.slice(0);
  76. this._offsetsCache = {};
  77. this._highLimitsCache = {};
  78. };
  79. Animation.prototype._getKeyValue = function (value) {
  80. if (typeof value === "function") {
  81. return value();
  82. }
  83. return value;
  84. };
  85. Animation.prototype._interpolate = function (currentFrame, repeatCount, loopMode, offsetValue, highLimitValue) {
  86. if (loopMode === Animation.ANIMATIONLOOPMODE_CONSTANT && repeatCount > 0) {
  87. return highLimitValue.clone ? highLimitValue.clone() : highLimitValue;
  88. }
  89. this.currentFrame = currentFrame;
  90. for (var key = 0; key < this._keys.length; key++) {
  91. // for each frame, we need the key just before the frame superior
  92. if (this._keys[key + 1].frame >= currentFrame) {
  93. var startValue = this._getKeyValue(this._keys[key].value);
  94. var endValue = this._getKeyValue(this._keys[key + 1].value);
  95. // gradient : percent of currentFrame between the frame inf and the frame sup
  96. var gradient = (currentFrame - this._keys[key].frame) / (this._keys[key + 1].frame - this._keys[key].frame);
  97. // check for easingFunction and correction of gradient
  98. if (this._easingFunction != null) {
  99. gradient = this._easingFunction.ease(gradient);
  100. }
  101. switch (this.dataType) {
  102. case Animation.ANIMATIONTYPE_FLOAT:
  103. switch (loopMode) {
  104. case Animation.ANIMATIONLOOPMODE_CYCLE:
  105. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  106. return this.floatInterpolateFunction(startValue, endValue, gradient);
  107. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  108. return offsetValue * repeatCount + this.floatInterpolateFunction(startValue, endValue, gradient);
  109. }
  110. break;
  111. case Animation.ANIMATIONTYPE_QUATERNION:
  112. var quaternion = null;
  113. switch (loopMode) {
  114. case Animation.ANIMATIONLOOPMODE_CYCLE:
  115. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  116. quaternion = this.quaternionInterpolateFunction(startValue, endValue, gradient);
  117. break;
  118. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  119. quaternion = this.quaternionInterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  120. break;
  121. }
  122. return quaternion;
  123. case Animation.ANIMATIONTYPE_VECTOR3:
  124. switch (loopMode) {
  125. case Animation.ANIMATIONLOOPMODE_CYCLE:
  126. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  127. return this.vector3InterpolateFunction(startValue, endValue, gradient);
  128. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  129. return this.vector3InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  130. }
  131. case Animation.ANIMATIONTYPE_VECTOR2:
  132. switch (loopMode) {
  133. case Animation.ANIMATIONLOOPMODE_CYCLE:
  134. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  135. return this.vector2InterpolateFunction(startValue, endValue, gradient);
  136. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  137. return this.vector2InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  138. }
  139. case Animation.ANIMATIONTYPE_COLOR3:
  140. switch (loopMode) {
  141. case Animation.ANIMATIONLOOPMODE_CYCLE:
  142. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  143. return this.color3InterpolateFunction(startValue, endValue, gradient);
  144. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  145. return this.color3InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  146. }
  147. case Animation.ANIMATIONTYPE_MATRIX:
  148. switch (loopMode) {
  149. case Animation.ANIMATIONLOOPMODE_CYCLE:
  150. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  151. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  152. return startValue;
  153. }
  154. default:
  155. break;
  156. }
  157. break;
  158. }
  159. }
  160. return this._getKeyValue(this._keys[this._keys.length - 1].value);
  161. };
  162. Animation.prototype.animate = function (delay, from, to, loop, speedRatio) {
  163. if (!this.targetPropertyPath || this.targetPropertyPath.length < 1) {
  164. this._stopped = true;
  165. return false;
  166. }
  167. var returnValue = true;
  168. // Adding a start key at frame 0 if missing
  169. if (this._keys[0].frame !== 0) {
  170. var newKey = { frame: 0, value: this._keys[0].value };
  171. this._keys.splice(0, 0, newKey);
  172. }
  173. // Check limits
  174. if (from < this._keys[0].frame || from > this._keys[this._keys.length - 1].frame) {
  175. from = this._keys[0].frame;
  176. }
  177. if (to < this._keys[0].frame || to > this._keys[this._keys.length - 1].frame) {
  178. to = this._keys[this._keys.length - 1].frame;
  179. }
  180. // Compute ratio
  181. var range = to - from;
  182. var offsetValue;
  183. // ratio represents the frame delta between from and to
  184. var ratio = delay * (this.framePerSecond * speedRatio) / 1000.0;
  185. var highLimitValue = 0;
  186. if (ratio > range && !loop) {
  187. returnValue = false;
  188. highLimitValue = this._getKeyValue(this._keys[this._keys.length - 1].value);
  189. } else {
  190. // Get max value if required
  191. if (this.loopMode !== Animation.ANIMATIONLOOPMODE_CYCLE) {
  192. var keyOffset = to.toString() + from.toString();
  193. if (!this._offsetsCache[keyOffset]) {
  194. var fromValue = this._interpolate(from, 0, Animation.ANIMATIONLOOPMODE_CYCLE);
  195. var toValue = this._interpolate(to, 0, Animation.ANIMATIONLOOPMODE_CYCLE);
  196. switch (this.dataType) {
  197. case Animation.ANIMATIONTYPE_FLOAT:
  198. this._offsetsCache[keyOffset] = toValue - fromValue;
  199. break;
  200. case Animation.ANIMATIONTYPE_QUATERNION:
  201. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  202. break;
  203. case Animation.ANIMATIONTYPE_VECTOR3:
  204. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  205. case Animation.ANIMATIONTYPE_VECTOR2:
  206. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  207. case Animation.ANIMATIONTYPE_COLOR3:
  208. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  209. default:
  210. break;
  211. }
  212. this._highLimitsCache[keyOffset] = toValue;
  213. }
  214. highLimitValue = this._highLimitsCache[keyOffset];
  215. offsetValue = this._offsetsCache[keyOffset];
  216. }
  217. }
  218. if (offsetValue === undefined) {
  219. switch (this.dataType) {
  220. case Animation.ANIMATIONTYPE_FLOAT:
  221. offsetValue = 0;
  222. break;
  223. case Animation.ANIMATIONTYPE_QUATERNION:
  224. offsetValue = new BABYLON.Quaternion(0, 0, 0, 0);
  225. break;
  226. case Animation.ANIMATIONTYPE_VECTOR3:
  227. offsetValue = BABYLON.Vector3.Zero();
  228. break;
  229. case Animation.ANIMATIONTYPE_VECTOR2:
  230. offsetValue = BABYLON.Vector2.Zero();
  231. break;
  232. case Animation.ANIMATIONTYPE_COLOR3:
  233. offsetValue = BABYLON.Color3.Black();
  234. }
  235. }
  236. // Compute value
  237. var repeatCount = (ratio / range) >> 0;
  238. var currentFrame = returnValue ? from + ratio % range : to;
  239. var currentValue = this._interpolate(currentFrame, repeatCount, this.loopMode, offsetValue, highLimitValue);
  240. // Set value
  241. if (this.targetPropertyPath.length > 1) {
  242. var property = this._target[this.targetPropertyPath[0]];
  243. for (var index = 1; index < this.targetPropertyPath.length - 1; index++) {
  244. property = property[this.targetPropertyPath[index]];
  245. }
  246. property[this.targetPropertyPath[this.targetPropertyPath.length - 1]] = currentValue;
  247. } else {
  248. this._target[this.targetPropertyPath[0]] = currentValue;
  249. }
  250. if (this._target.markAsDirty) {
  251. this._target.markAsDirty(this.targetProperty);
  252. }
  253. if (!returnValue) {
  254. this._stopped = true;
  255. }
  256. return returnValue;
  257. };
  258. Object.defineProperty(Animation, "ANIMATIONTYPE_FLOAT", {
  259. get: function () {
  260. return Animation._ANIMATIONTYPE_FLOAT;
  261. },
  262. enumerable: true,
  263. configurable: true
  264. });
  265. Object.defineProperty(Animation, "ANIMATIONTYPE_VECTOR3", {
  266. get: function () {
  267. return Animation._ANIMATIONTYPE_VECTOR3;
  268. },
  269. enumerable: true,
  270. configurable: true
  271. });
  272. Object.defineProperty(Animation, "ANIMATIONTYPE_VECTOR2", {
  273. get: function () {
  274. return Animation._ANIMATIONTYPE_VECTOR2;
  275. },
  276. enumerable: true,
  277. configurable: true
  278. });
  279. Object.defineProperty(Animation, "ANIMATIONTYPE_QUATERNION", {
  280. get: function () {
  281. return Animation._ANIMATIONTYPE_QUATERNION;
  282. },
  283. enumerable: true,
  284. configurable: true
  285. });
  286. Object.defineProperty(Animation, "ANIMATIONTYPE_MATRIX", {
  287. get: function () {
  288. return Animation._ANIMATIONTYPE_MATRIX;
  289. },
  290. enumerable: true,
  291. configurable: true
  292. });
  293. Object.defineProperty(Animation, "ANIMATIONTYPE_COLOR3", {
  294. get: function () {
  295. return Animation._ANIMATIONTYPE_COLOR3;
  296. },
  297. enumerable: true,
  298. configurable: true
  299. });
  300. Object.defineProperty(Animation, "ANIMATIONLOOPMODE_RELATIVE", {
  301. get: function () {
  302. return Animation._ANIMATIONLOOPMODE_RELATIVE;
  303. },
  304. enumerable: true,
  305. configurable: true
  306. });
  307. Object.defineProperty(Animation, "ANIMATIONLOOPMODE_CYCLE", {
  308. get: function () {
  309. return Animation._ANIMATIONLOOPMODE_CYCLE;
  310. },
  311. enumerable: true,
  312. configurable: true
  313. });
  314. Object.defineProperty(Animation, "ANIMATIONLOOPMODE_CONSTANT", {
  315. get: function () {
  316. return Animation._ANIMATIONLOOPMODE_CONSTANT;
  317. },
  318. enumerable: true,
  319. configurable: true
  320. });
  321. Animation._ANIMATIONTYPE_FLOAT = 0;
  322. Animation._ANIMATIONTYPE_VECTOR3 = 1;
  323. Animation._ANIMATIONTYPE_QUATERNION = 2;
  324. Animation._ANIMATIONTYPE_MATRIX = 3;
  325. Animation._ANIMATIONTYPE_COLOR3 = 4;
  326. Animation._ANIMATIONTYPE_VECTOR2 = 5;
  327. Animation._ANIMATIONLOOPMODE_RELATIVE = 0;
  328. Animation._ANIMATIONLOOPMODE_CYCLE = 1;
  329. Animation._ANIMATIONLOOPMODE_CONSTANT = 2;
  330. return Animation;
  331. })();
  332. BABYLON.Animation = Animation;
  333. })(BABYLON || (BABYLON = {}));
  334. //# sourceMappingURL=babylon.animation.js.map