babylon.animation.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. // Methods
  18. Animation.prototype.isStopped = function () {
  19. return this._stopped;
  20. };
  21. Animation.prototype.getKeys = function () {
  22. return this._keys;
  23. };
  24. Animation.prototype.floatInterpolateFunction = function (startValue, endValue, gradient) {
  25. return startValue + (endValue - startValue) * gradient;
  26. };
  27. Animation.prototype.quaternionInterpolateFunction = function (startValue, endValue, gradient) {
  28. return BABYLON.Quaternion.Slerp(startValue, endValue, gradient);
  29. };
  30. Animation.prototype.vector3InterpolateFunction = function (startValue, endValue, gradient) {
  31. return BABYLON.Vector3.Lerp(startValue, endValue, gradient);
  32. };
  33. Animation.prototype.vector2InterpolateFunction = function (startValue, endValue, gradient) {
  34. return BABYLON.Vector2.Lerp(startValue, endValue, gradient);
  35. };
  36. Animation.prototype.color3InterpolateFunction = function (startValue, endValue, gradient) {
  37. return BABYLON.Color3.Lerp(startValue, endValue, gradient);
  38. };
  39. Animation.prototype.clone = function () {
  40. var clone = new Animation(this.name, this.targetPropertyPath.join("."), this.framePerSecond, this.dataType, this.loopMode);
  41. clone.setKeys(this._keys);
  42. return clone;
  43. };
  44. Animation.prototype.setKeys = function (values) {
  45. this._keys = values.slice(0);
  46. this._offsetsCache = {};
  47. this._highLimitsCache = {};
  48. };
  49. Animation.prototype._interpolate = function (currentFrame, repeatCount, loopMode, offsetValue, highLimitValue) {
  50. if (loopMode === Animation.ANIMATIONLOOPMODE_CONSTANT && repeatCount > 0) {
  51. return highLimitValue.clone ? highLimitValue.clone() : highLimitValue;
  52. }
  53. this.currentFrame = currentFrame;
  54. for (var key = 0; key < this._keys.length; key++) {
  55. if (this._keys[key + 1].frame >= currentFrame) {
  56. var startValue = this._keys[key].value;
  57. var endValue = this._keys[key + 1].value;
  58. var gradient = (currentFrame - this._keys[key].frame) / (this._keys[key + 1].frame - this._keys[key].frame);
  59. switch (this.dataType) {
  60. case Animation.ANIMATIONTYPE_FLOAT:
  61. switch (loopMode) {
  62. case Animation.ANIMATIONLOOPMODE_CYCLE:
  63. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  64. return this.floatInterpolateFunction(startValue, endValue, gradient);
  65. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  66. return offsetValue * repeatCount + this.floatInterpolateFunction(startValue, endValue, gradient);
  67. }
  68. break;
  69. case Animation.ANIMATIONTYPE_QUATERNION:
  70. var quaternion = null;
  71. switch (loopMode) {
  72. case Animation.ANIMATIONLOOPMODE_CYCLE:
  73. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  74. quaternion = this.quaternionInterpolateFunction(startValue, endValue, gradient);
  75. break;
  76. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  77. quaternion = this.quaternionInterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  78. break;
  79. }
  80. return quaternion;
  81. case Animation.ANIMATIONTYPE_VECTOR3:
  82. switch (loopMode) {
  83. case Animation.ANIMATIONLOOPMODE_CYCLE:
  84. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  85. return this.vector3InterpolateFunction(startValue, endValue, gradient);
  86. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  87. return this.vector3InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  88. }
  89. case Animation.ANIMATIONTYPE_VECTOR2:
  90. switch (loopMode) {
  91. case Animation.ANIMATIONLOOPMODE_CYCLE:
  92. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  93. return this.vector2InterpolateFunction(startValue, endValue, gradient);
  94. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  95. return this.vector2InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  96. }
  97. case Animation.ANIMATIONTYPE_COLOR3:
  98. switch (loopMode) {
  99. case Animation.ANIMATIONLOOPMODE_CYCLE:
  100. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  101. return this.color3InterpolateFunction(startValue, endValue, gradient);
  102. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  103. return this.color3InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  104. }
  105. case Animation.ANIMATIONTYPE_MATRIX:
  106. switch (loopMode) {
  107. case Animation.ANIMATIONLOOPMODE_CYCLE:
  108. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  109. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  110. return startValue;
  111. }
  112. default:
  113. break;
  114. }
  115. break;
  116. }
  117. }
  118. return this._keys[this._keys.length - 1].value;
  119. };
  120. Animation.prototype.animate = function (delay, from, to, loop, speedRatio) {
  121. if (!this.targetPropertyPath || this.targetPropertyPath.length < 1) {
  122. this._stopped = true;
  123. return false;
  124. }
  125. var returnValue = true;
  126. // Adding a start key at frame 0 if missing
  127. if (this._keys[0].frame != 0) {
  128. var newKey = {
  129. frame: 0,
  130. value: this._keys[0].value
  131. };
  132. this._keys.splice(0, 0, newKey);
  133. }
  134. // Check limits
  135. if (from < this._keys[0].frame || from > this._keys[this._keys.length - 1].frame) {
  136. from = this._keys[0].frame;
  137. }
  138. if (to < this._keys[0].frame || to > this._keys[this._keys.length - 1].frame) {
  139. to = this._keys[this._keys.length - 1].frame;
  140. }
  141. // Compute ratio
  142. var range = to - from;
  143. var offsetValue;
  144. var ratio = delay * (this.framePerSecond * speedRatio) / 1000.0;
  145. if (ratio > range && !loop) {
  146. returnValue = false;
  147. highLimitValue = this._keys[this._keys.length - 1].value;
  148. } else {
  149. // Get max value if required
  150. var highLimitValue = 0;
  151. if (this.loopMode != Animation.ANIMATIONLOOPMODE_CYCLE) {
  152. var keyOffset = to.toString() + from.toString();
  153. if (!this._offsetsCache[keyOffset]) {
  154. var fromValue = this._interpolate(from, 0, Animation.ANIMATIONLOOPMODE_CYCLE);
  155. var toValue = this._interpolate(to, 0, Animation.ANIMATIONLOOPMODE_CYCLE);
  156. switch (this.dataType) {
  157. case Animation.ANIMATIONTYPE_FLOAT:
  158. this._offsetsCache[keyOffset] = toValue - fromValue;
  159. break;
  160. case Animation.ANIMATIONTYPE_QUATERNION:
  161. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  162. break;
  163. case Animation.ANIMATIONTYPE_VECTOR3:
  164. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  165. case Animation.ANIMATIONTYPE_VECTOR2:
  166. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  167. case Animation.ANIMATIONTYPE_COLOR3:
  168. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  169. default:
  170. break;
  171. }
  172. this._highLimitsCache[keyOffset] = toValue;
  173. }
  174. highLimitValue = this._highLimitsCache[keyOffset];
  175. offsetValue = this._offsetsCache[keyOffset];
  176. }
  177. }
  178. if (offsetValue === undefined) {
  179. switch (this.dataType) {
  180. case Animation.ANIMATIONTYPE_FLOAT:
  181. offsetValue = 0;
  182. break;
  183. case Animation.ANIMATIONTYPE_QUATERNION:
  184. offsetValue = new BABYLON.Quaternion(0, 0, 0, 0);
  185. break;
  186. case Animation.ANIMATIONTYPE_VECTOR3:
  187. offsetValue = BABYLON.Vector3.Zero();
  188. break;
  189. case Animation.ANIMATIONTYPE_VECTOR2:
  190. offsetValue = BABYLON.Vector2.Zero();
  191. break;
  192. case Animation.ANIMATIONTYPE_COLOR3:
  193. offsetValue = BABYLON.Color3.Black();
  194. }
  195. }
  196. // Compute value
  197. var repeatCount = (ratio / range) >> 0;
  198. var currentFrame = returnValue ? from + ratio % range : to;
  199. var currentValue = this._interpolate(currentFrame, repeatCount, this.loopMode, offsetValue, highLimitValue);
  200. // Set value
  201. if (this.targetPropertyPath.length > 1) {
  202. var property = this._target[this.targetPropertyPath[0]];
  203. for (var index = 1; index < this.targetPropertyPath.length - 1; index++) {
  204. property = property[this.targetPropertyPath[index]];
  205. }
  206. property[this.targetPropertyPath[this.targetPropertyPath.length - 1]] = currentValue;
  207. } else {
  208. this._target[this.targetPropertyPath[0]] = currentValue;
  209. }
  210. if (this._target.markAsDirty) {
  211. this._target.markAsDirty(this.targetProperty);
  212. }
  213. if (!returnValue) {
  214. this._stopped = true;
  215. }
  216. return returnValue;
  217. };
  218. Object.defineProperty(Animation, "ANIMATIONTYPE_FLOAT", {
  219. get: function () {
  220. return Animation._ANIMATIONTYPE_FLOAT;
  221. },
  222. enumerable: true,
  223. configurable: true
  224. });
  225. Object.defineProperty(Animation, "ANIMATIONTYPE_VECTOR3", {
  226. get: function () {
  227. return Animation._ANIMATIONTYPE_VECTOR3;
  228. },
  229. enumerable: true,
  230. configurable: true
  231. });
  232. Object.defineProperty(Animation, "ANIMATIONTYPE_VECTOR2", {
  233. get: function () {
  234. return Animation._ANIMATIONTYPE_VECTOR2;
  235. },
  236. enumerable: true,
  237. configurable: true
  238. });
  239. Object.defineProperty(Animation, "ANIMATIONTYPE_QUATERNION", {
  240. get: function () {
  241. return Animation._ANIMATIONTYPE_QUATERNION;
  242. },
  243. enumerable: true,
  244. configurable: true
  245. });
  246. Object.defineProperty(Animation, "ANIMATIONTYPE_MATRIX", {
  247. get: function () {
  248. return Animation._ANIMATIONTYPE_MATRIX;
  249. },
  250. enumerable: true,
  251. configurable: true
  252. });
  253. Object.defineProperty(Animation, "ANIMATIONTYPE_COLOR3", {
  254. get: function () {
  255. return Animation._ANIMATIONTYPE_COLOR3;
  256. },
  257. enumerable: true,
  258. configurable: true
  259. });
  260. Object.defineProperty(Animation, "ANIMATIONLOOPMODE_RELATIVE", {
  261. get: function () {
  262. return Animation._ANIMATIONLOOPMODE_RELATIVE;
  263. },
  264. enumerable: true,
  265. configurable: true
  266. });
  267. Object.defineProperty(Animation, "ANIMATIONLOOPMODE_CYCLE", {
  268. get: function () {
  269. return Animation._ANIMATIONLOOPMODE_CYCLE;
  270. },
  271. enumerable: true,
  272. configurable: true
  273. });
  274. Object.defineProperty(Animation, "ANIMATIONLOOPMODE_CONSTANT", {
  275. get: function () {
  276. return Animation._ANIMATIONLOOPMODE_CONSTANT;
  277. },
  278. enumerable: true,
  279. configurable: true
  280. });
  281. Animation._ANIMATIONTYPE_FLOAT = 0;
  282. Animation._ANIMATIONTYPE_VECTOR3 = 1;
  283. Animation._ANIMATIONTYPE_QUATERNION = 2;
  284. Animation._ANIMATIONTYPE_MATRIX = 3;
  285. Animation._ANIMATIONTYPE_COLOR3 = 4;
  286. Animation._ANIMATIONTYPE_VECTOR2 = 5;
  287. Animation._ANIMATIONLOOPMODE_RELATIVE = 0;
  288. Animation._ANIMATIONLOOPMODE_CYCLE = 1;
  289. Animation._ANIMATIONLOOPMODE_CONSTANT = 2;
  290. return Animation;
  291. })();
  292. BABYLON.Animation = Animation;
  293. })(BABYLON || (BABYLON = {}));
  294. //# sourceMappingURL=babylon.animation.js.map