babylon.animation.ts 12 KB

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