babylon.animation.js 8.4 KB

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