babylon.animation.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.Animation = function (name, targetProperty, framePerSecond, dataType, loopMode) {
  5. this.name = name;
  6. this.targetProperty = targetProperty;
  7. this.targetPropertyPath = targetProperty.split(".");
  8. this.framePerSecond = framePerSecond;
  9. this.dataType = dataType;
  10. this.loopMode = loopMode === undefined ? BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE : loopMode;
  11. this._keys = [];
  12. // Cache
  13. this._offsetsCache = {};
  14. this._highLimitsCache = {};
  15. };
  16. // Methods
  17. BABYLON.Animation.prototype.floatInterpolateFunction = function (startValue, endValue, gradient) {
  18. return startValue + (endValue - startValue) * gradient;
  19. };
  20. BABYLON.Animation.prototype.quaternionInterpolateFunction = function (startValue, endValue, gradient) {
  21. return BABYLON.Quaternion.Slerp(startValue, endValue, gradient);
  22. };
  23. BABYLON.Animation.prototype.vector3InterpolateFunction = function (startValue, endValue, gradient) {
  24. return BABYLON.Vector3.Lerp(startValue, endValue, gradient);
  25. };
  26. BABYLON.Animation.prototype.clone = function() {
  27. var clone = new BABYLON.Animation(this.name, this.targetPropertyPath.join("."), this.framePerSecond, this.dataType, this.loopMode);
  28. clone.setKeys(this._keys);
  29. return clone;
  30. };
  31. BABYLON.Animation.prototype.setKeys = function(values) {
  32. this._keys = values.slice(0);
  33. this._offsetsCache = {};
  34. this._highLimitsCache = {};
  35. };
  36. BABYLON.Animation.prototype._interpolate = function (currentFrame, repeatCount, loopMode, offsetValue, highLimitValue) {
  37. if (loopMode === BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT && repeatCount > 0) {
  38. return highLimitValue.clone ? highLimitValue.clone() : highLimitValue;
  39. }
  40. this.currentFrame = currentFrame;
  41. for (var key = 0; key < this._keys.length; key++) {
  42. if (this._keys[key + 1].frame >= currentFrame) {
  43. var startValue = this._keys[key].value;
  44. var endValue = this._keys[key + 1].value;
  45. var gradient = (currentFrame - this._keys[key].frame) / (this._keys[key + 1].frame - this._keys[key].frame);
  46. switch (this.dataType) {
  47. // Float
  48. case BABYLON.Animation.ANIMATIONTYPE_FLOAT:
  49. switch (loopMode) {
  50. case BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE:
  51. case BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT:
  52. return this.floatInterpolateFunction(startValue, endValue, gradient);
  53. case BABYLON.Animation.ANIMATIONLOOPMODE_RELATIVE:
  54. return offsetValue * repeatCount + this.floatInterpolateFunction(startValue, endValue, gradient);
  55. }
  56. break;
  57. // Quaternion
  58. case BABYLON.Animation.ANIMATIONTYPE_QUATERNION:
  59. var quaternion = null;
  60. switch (loopMode) {
  61. case BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE:
  62. case BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT:
  63. quaternion = this.quaternionInterpolateFunction(startValue, endValue, gradient);
  64. break;
  65. case BABYLON.Animation.ANIMATIONLOOPMODE_RELATIVE:
  66. quaternion = this.quaternionInterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  67. break;
  68. }
  69. return quaternion;
  70. // Vector3
  71. case BABYLON.Animation.ANIMATIONTYPE_VECTOR3:
  72. switch (loopMode) {
  73. case BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE:
  74. case BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT:
  75. return this.vector3InterpolateFunction(startValue, endValue, gradient);
  76. case BABYLON.Animation.ANIMATIONLOOPMODE_RELATIVE:
  77. return this.vector3InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  78. }
  79. // Matrix
  80. case BABYLON.Animation.ANIMATIONTYPE_MATRIX:
  81. switch (loopMode) {
  82. case BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE:
  83. case BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT:
  84. case BABYLON.Animation.ANIMATIONLOOPMODE_RELATIVE:
  85. return startValue;
  86. }
  87. default:
  88. break;
  89. }
  90. break;
  91. }
  92. }
  93. return this._keys[this._keys.length - 1].value;
  94. };
  95. BABYLON.Animation.prototype.animate = function (target, delay, from, to, loop, speedRatio) {
  96. if (!this.targetPropertyPath || this.targetPropertyPath.length < 1) {
  97. return false;
  98. }
  99. var returnValue = true;
  100. // Adding a start key at frame 0 if missing
  101. if (this._keys[0].frame != 0) {
  102. var newKey = {
  103. frame: 0,
  104. value: this._keys[0].value
  105. };
  106. this._keys.splice(0, 0, newKey);
  107. }
  108. // Check limits
  109. if (from < this._keys[0].frame || from > this._keys[this._keys.length - 1].frame) {
  110. from = this._keys[0].frame;
  111. }
  112. if (to < this._keys[0].frame || to > this._keys[this._keys.length - 1].frame) {
  113. to = this._keys[this._keys.length - 1].frame;
  114. }
  115. // Compute ratio
  116. var range = to - from;
  117. var ratio = delay * (this.framePerSecond * speedRatio) / 1000.0;
  118. if (ratio > range && !loop) { // If we are out of range and not looping get back to caller
  119. offsetValue = 0;
  120. returnValue = false;
  121. } else {
  122. // Get max value if required
  123. var offsetValue = 0;
  124. var highLimitValue = 0;
  125. if (this.loopMode != BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE) {
  126. var keyOffset = to.toString() + from.toString();
  127. if (!this._offsetsCache[keyOffset]) {
  128. var fromValue = this._interpolate(from, 0, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  129. var toValue = this._interpolate(to, 0, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  130. switch (this.dataType) {
  131. // Float
  132. case BABYLON.Animation.ANIMATIONTYPE_FLOAT:
  133. this._offsetsCache[keyOffset] = toValue - fromValue;
  134. break;
  135. // Quaternion
  136. case BABYLON.Animation.ANIMATIONTYPE_QUATERNION:
  137. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  138. break;
  139. // Vector3
  140. case BABYLON.Animation.ANIMATIONTYPE_VECTOR3:
  141. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  142. default:
  143. break;
  144. }
  145. this._highLimitsCache[keyOffset] = toValue;
  146. }
  147. highLimitValue = this._highLimitsCache[keyOffset];
  148. offsetValue = this._offsetsCache[keyOffset];
  149. }
  150. }
  151. // Compute value
  152. var repeatCount = (ratio / range) >> 0;
  153. var currentFrame = returnValue ? from + ratio % range : to;
  154. var currentValue = this._interpolate(currentFrame, repeatCount, this.loopMode, offsetValue, highLimitValue);
  155. // Set value
  156. if (this.targetPropertyPath.length > 1) {
  157. var property = target[this.targetPropertyPath[0]];
  158. for (var index = 1; index < this.targetPropertyPath.length - 1; index++) {
  159. property = property[this.targetPropertyPath[index]];
  160. }
  161. property[this.targetPropertyPath[this.targetPropertyPath.length - 1]] = currentValue;
  162. } else {
  163. target[this.targetPropertyPath[0]] = currentValue;
  164. }
  165. if (target.markAsDirty) {
  166. target.markAsDirty(this.targetProperty);
  167. }
  168. return returnValue;
  169. };
  170. // Statics
  171. BABYLON.Animation.ANIMATIONTYPE_FLOAT = 0;
  172. BABYLON.Animation.ANIMATIONTYPE_VECTOR3 = 1;
  173. BABYLON.Animation.ANIMATIONTYPE_QUATERNION = 2;
  174. BABYLON.Animation.ANIMATIONTYPE_MATRIX = 3;
  175. BABYLON.Animation.ANIMATIONLOOPMODE_RELATIVE = 0;
  176. BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE = 1;
  177. BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT = 2;
  178. })();