babylon.animation.js 13 KB

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