babylon.animation.ts 11 KB

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