babylon.animation.js 12 KB

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