babylon.animation.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. private _easingFunction: IEasingFunction;
  9. public targetPropertyPath: string[];
  10. public currentFrame: number;
  11. public static CreateAndStartAnimation(name: string, mesh: AbstractMesh, tartgetProperty: string,
  12. framePerSecond: number, totalFrame: number,
  13. from: any, to: any, loopMode?: number) {
  14. var dataType = undefined;
  15. if (!isNaN(parseFloat(from)) && isFinite(from)) {
  16. dataType = Animation.ANIMATIONTYPE_FLOAT;
  17. } else if (from instanceof Quaternion) {
  18. dataType = Animation.ANIMATIONTYPE_QUATERNION;
  19. } else if (from instanceof Vector3) {
  20. dataType = Animation.ANIMATIONTYPE_VECTOR3;
  21. } else if (from instanceof Vector2) {
  22. dataType = Animation.ANIMATIONTYPE_VECTOR2;
  23. } else if (from instanceof Color3) {
  24. dataType = Animation.ANIMATIONTYPE_COLOR3;
  25. }
  26. if (dataType == undefined) {
  27. return null;
  28. }
  29. var animation = new Animation(name, tartgetProperty, framePerSecond, dataType, loopMode);
  30. var keys = [];
  31. keys.push({ frame: 0, value: from });
  32. keys.push({ frame: totalFrame, value: to });
  33. animation.setKeys(keys);
  34. mesh.animations.push(animation);
  35. return mesh.getScene().beginAnimation(mesh, 0, totalFrame,(animation.loopMode === 1));
  36. }
  37. constructor(public name: string, public targetProperty: string, public framePerSecond: number, public dataType: number, public loopMode?: number) {
  38. this.targetPropertyPath = targetProperty.split(".");
  39. this.dataType = dataType;
  40. this.loopMode = loopMode === undefined ? Animation.ANIMATIONLOOPMODE_CYCLE : loopMode;
  41. }
  42. // Methods
  43. public isStopped(): boolean {
  44. return this._stopped;
  45. }
  46. public getKeys(): any[] {
  47. return this._keys;
  48. }
  49. public getEasingFunction() {
  50. return this._easingFunction;
  51. }
  52. public setEasingFunction(easingFunction: EasingFunction) {
  53. this._easingFunction = easingFunction;
  54. }
  55. public floatInterpolateFunction(startValue: number, endValue: number, gradient: number): number {
  56. return startValue + (endValue - startValue) * gradient;
  57. }
  58. public quaternionInterpolateFunction(startValue: Quaternion, endValue: Quaternion, gradient: number): Quaternion {
  59. return Quaternion.Slerp(startValue, endValue, gradient);
  60. }
  61. public vector3InterpolateFunction(startValue: Vector3, endValue: Vector3, gradient: number): Vector3 {
  62. return Vector3.Lerp(startValue, endValue, gradient);
  63. }
  64. public vector2InterpolateFunction(startValue: Vector2, endValue: Vector2, gradient: number): Vector2 {
  65. return Vector2.Lerp(startValue, endValue, gradient);
  66. }
  67. public color3InterpolateFunction(startValue: Color3, endValue: Color3, gradient: number): Color3 {
  68. return Color3.Lerp(startValue, endValue, gradient);
  69. }
  70. public matrixInterpolateFunction(startValue: Matrix, endValue: Matrix, gradient: number): Matrix {
  71. var startScale = new Vector3(0, 0, 0);
  72. var startRotation = new Quaternion();
  73. var startTranslation = new Vector3(0, 0, 0);
  74. startValue.decompose(startScale, startRotation, startTranslation);
  75. var endScale = new Vector3(0, 0, 0);
  76. var endRotation = new Quaternion();
  77. var endTranslation = new Vector3(0, 0, 0);
  78. endValue.decompose(endScale, endRotation, endTranslation);
  79. var resultScale = this.vector3InterpolateFunction(startScale, endScale, gradient);
  80. var resultRotation = this.quaternionInterpolateFunction(startRotation, endRotation, gradient);
  81. var resultTranslation = this.vector3InterpolateFunction(startTranslation, endTranslation, gradient);
  82. var result = Matrix.Compose(resultScale, resultRotation, resultTranslation);
  83. return result;
  84. }
  85. public clone(): Animation {
  86. var clone = new Animation(this.name, this.targetPropertyPath.join("."), this.framePerSecond, this.dataType, this.loopMode);
  87. clone.setKeys(this._keys);
  88. return clone;
  89. }
  90. public setKeys(values: Array<any>): void {
  91. this._keys = values.slice(0);
  92. this._offsetsCache = {};
  93. this._highLimitsCache = {};
  94. }
  95. private _getKeyValue(value: any): any {
  96. if (typeof value === "function") {
  97. return value();
  98. }
  99. return value;
  100. }
  101. private _interpolate(currentFrame: number, repeatCount: number, loopMode: number, offsetValue?, highLimitValue?) {
  102. if (loopMode === Animation.ANIMATIONLOOPMODE_CONSTANT && repeatCount > 0) {
  103. return highLimitValue.clone ? highLimitValue.clone() : highLimitValue;
  104. }
  105. this.currentFrame = currentFrame;
  106. // Try to get a hash to find the right key
  107. var startKey = Math.max(0, Math.min(this._keys.length - 1, Math.floor(this._keys.length * (currentFrame - this._keys[0].frame) / (this._keys[this._keys.length - 1].frame - this._keys[0].frame)) - 1));
  108. if (this._keys[startKey].frame >= currentFrame) {
  109. while (startKey - 1 >= 0 && this._keys[startKey].frame >= currentFrame) {
  110. startKey--;
  111. }
  112. }
  113. for (var key = startKey; key < this._keys.length ; key++) {
  114. if (this._keys[key + 1].frame >= currentFrame) {
  115. var startValue = this._getKeyValue(this._keys[key].value);
  116. var endValue = this._getKeyValue(this._keys[key + 1].value);
  117. // gradient : percent of currentFrame between the frame inf and the frame sup
  118. var gradient = (currentFrame - this._keys[key].frame) / (this._keys[key + 1].frame - this._keys[key].frame);
  119. // check for easingFunction and correction of gradient
  120. if (this._easingFunction != null) {
  121. gradient = this._easingFunction.ease(gradient);
  122. }
  123. switch (this.dataType) {
  124. // Float
  125. case Animation.ANIMATIONTYPE_FLOAT:
  126. switch (loopMode) {
  127. case Animation.ANIMATIONLOOPMODE_CYCLE:
  128. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  129. return this.floatInterpolateFunction(startValue, endValue, gradient);
  130. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  131. return offsetValue * repeatCount + this.floatInterpolateFunction(startValue, endValue, gradient);
  132. }
  133. break;
  134. // Quaternion
  135. case Animation.ANIMATIONTYPE_QUATERNION:
  136. var quaternion = null;
  137. switch (loopMode) {
  138. case Animation.ANIMATIONLOOPMODE_CYCLE:
  139. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  140. quaternion = this.quaternionInterpolateFunction(startValue, endValue, gradient);
  141. break;
  142. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  143. quaternion = this.quaternionInterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  144. break;
  145. }
  146. return quaternion;
  147. // Vector3
  148. case Animation.ANIMATIONTYPE_VECTOR3:
  149. switch (loopMode) {
  150. case Animation.ANIMATIONLOOPMODE_CYCLE:
  151. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  152. return this.vector3InterpolateFunction(startValue, endValue, gradient);
  153. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  154. return this.vector3InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  155. }
  156. // Vector2
  157. case Animation.ANIMATIONTYPE_VECTOR2:
  158. switch (loopMode) {
  159. case Animation.ANIMATIONLOOPMODE_CYCLE:
  160. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  161. return this.vector2InterpolateFunction(startValue, endValue, gradient);
  162. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  163. return this.vector2InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  164. }
  165. // Color3
  166. case Animation.ANIMATIONTYPE_COLOR3:
  167. switch (loopMode) {
  168. case Animation.ANIMATIONLOOPMODE_CYCLE:
  169. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  170. return this.color3InterpolateFunction(startValue, endValue, gradient);
  171. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  172. return this.color3InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  173. }
  174. // Matrix
  175. case Animation.ANIMATIONTYPE_MATRIX:
  176. switch (loopMode) {
  177. case Animation.ANIMATIONLOOPMODE_CYCLE:
  178. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  179. // return this.matrixInterpolateFunction(startValue, endValue, gradient);
  180. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  181. return startValue;
  182. }
  183. default:
  184. break;
  185. }
  186. break;
  187. }
  188. }
  189. return this._getKeyValue(this._keys[this._keys.length - 1].value);
  190. }
  191. public animate(delay: number, from: number, to: number, loop: boolean, speedRatio: number): boolean {
  192. if (!this.targetPropertyPath || this.targetPropertyPath.length < 1) {
  193. this._stopped = true;
  194. return false;
  195. }
  196. var returnValue = true;
  197. // Adding a start key at frame 0 if missing
  198. if (this._keys[0].frame !== 0) {
  199. var newKey = { frame: 0, value: this._keys[0].value };
  200. this._keys.splice(0, 0, newKey);
  201. }
  202. // Check limits
  203. if (from < this._keys[0].frame || from > this._keys[this._keys.length - 1].frame) {
  204. from = this._keys[0].frame;
  205. }
  206. if (to < this._keys[0].frame || to > this._keys[this._keys.length - 1].frame) {
  207. to = this._keys[this._keys.length - 1].frame;
  208. }
  209. // Compute ratio
  210. var range = to - from;
  211. var offsetValue;
  212. // ratio represents the frame delta between from and to
  213. var ratio = delay * (this.framePerSecond * speedRatio) / 1000.0;
  214. var highLimitValue = 0;
  215. if (ratio > range && !loop) { // If we are out of range and not looping get back to caller
  216. returnValue = false;
  217. highLimitValue = this._getKeyValue(this._keys[this._keys.length - 1].value);
  218. } else {
  219. // Get max value if required
  220. if (this.loopMode !== Animation.ANIMATIONLOOPMODE_CYCLE) {
  221. var keyOffset = to.toString() + from.toString();
  222. if (!this._offsetsCache[keyOffset]) {
  223. var fromValue = this._interpolate(from, 0, Animation.ANIMATIONLOOPMODE_CYCLE);
  224. var toValue = this._interpolate(to, 0, Animation.ANIMATIONLOOPMODE_CYCLE);
  225. switch (this.dataType) {
  226. // Float
  227. case Animation.ANIMATIONTYPE_FLOAT:
  228. this._offsetsCache[keyOffset] = toValue - fromValue;
  229. break;
  230. // Quaternion
  231. case Animation.ANIMATIONTYPE_QUATERNION:
  232. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  233. break;
  234. // Vector3
  235. case Animation.ANIMATIONTYPE_VECTOR3:
  236. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  237. // Vector2
  238. case Animation.ANIMATIONTYPE_VECTOR2:
  239. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  240. // Color3
  241. case Animation.ANIMATIONTYPE_COLOR3:
  242. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  243. default:
  244. break;
  245. }
  246. this._highLimitsCache[keyOffset] = toValue;
  247. }
  248. highLimitValue = this._highLimitsCache[keyOffset];
  249. offsetValue = this._offsetsCache[keyOffset];
  250. }
  251. }
  252. if (offsetValue === undefined) {
  253. switch (this.dataType) {
  254. // Float
  255. case Animation.ANIMATIONTYPE_FLOAT:
  256. offsetValue = 0;
  257. break;
  258. // Quaternion
  259. case Animation.ANIMATIONTYPE_QUATERNION:
  260. offsetValue = new Quaternion(0, 0, 0, 0);
  261. break;
  262. // Vector3
  263. case Animation.ANIMATIONTYPE_VECTOR3:
  264. offsetValue = Vector3.Zero();
  265. break;
  266. // Vector2
  267. case Animation.ANIMATIONTYPE_VECTOR2:
  268. offsetValue = Vector2.Zero();
  269. break;
  270. // Color3
  271. case Animation.ANIMATIONTYPE_COLOR3:
  272. offsetValue = Color3.Black();
  273. }
  274. }
  275. // Compute value
  276. var repeatCount = (ratio / range) >> 0;
  277. var currentFrame = returnValue ? from + ratio % range : to;
  278. var currentValue = this._interpolate(currentFrame, repeatCount, this.loopMode, offsetValue, highLimitValue);
  279. // Set value
  280. if (this.targetPropertyPath.length > 1) {
  281. var property = this._target[this.targetPropertyPath[0]];
  282. for (var index = 1; index < this.targetPropertyPath.length - 1; index++) {
  283. property = property[this.targetPropertyPath[index]];
  284. }
  285. property[this.targetPropertyPath[this.targetPropertyPath.length - 1]] = currentValue;
  286. } else {
  287. this._target[this.targetPropertyPath[0]] = currentValue;
  288. }
  289. if (this._target.markAsDirty) {
  290. this._target.markAsDirty(this.targetProperty);
  291. }
  292. if (!returnValue) {
  293. this._stopped = true;
  294. }
  295. return returnValue;
  296. }
  297. // Statics
  298. private static _ANIMATIONTYPE_FLOAT = 0;
  299. private static _ANIMATIONTYPE_VECTOR3 = 1;
  300. private static _ANIMATIONTYPE_QUATERNION = 2;
  301. private static _ANIMATIONTYPE_MATRIX = 3;
  302. private static _ANIMATIONTYPE_COLOR3 = 4;
  303. private static _ANIMATIONTYPE_VECTOR2 = 5;
  304. private static _ANIMATIONLOOPMODE_RELATIVE = 0;
  305. private static _ANIMATIONLOOPMODE_CYCLE = 1;
  306. private static _ANIMATIONLOOPMODE_CONSTANT = 2;
  307. public static get ANIMATIONTYPE_FLOAT(): number {
  308. return Animation._ANIMATIONTYPE_FLOAT;
  309. }
  310. public static get ANIMATIONTYPE_VECTOR3(): number {
  311. return Animation._ANIMATIONTYPE_VECTOR3;
  312. }
  313. public static get ANIMATIONTYPE_VECTOR2(): number {
  314. return Animation._ANIMATIONTYPE_VECTOR2;
  315. }
  316. public static get ANIMATIONTYPE_QUATERNION(): number {
  317. return Animation._ANIMATIONTYPE_QUATERNION;
  318. }
  319. public static get ANIMATIONTYPE_MATRIX(): number {
  320. return Animation._ANIMATIONTYPE_MATRIX;
  321. }
  322. public static get ANIMATIONTYPE_COLOR3(): number {
  323. return Animation._ANIMATIONTYPE_COLOR3;
  324. }
  325. public static get ANIMATIONLOOPMODE_RELATIVE(): number {
  326. return Animation._ANIMATIONLOOPMODE_RELATIVE;
  327. }
  328. public static get ANIMATIONLOOPMODE_CYCLE(): number {
  329. return Animation._ANIMATIONLOOPMODE_CYCLE;
  330. }
  331. public static get ANIMATIONLOOPMODE_CONSTANT(): number {
  332. return Animation._ANIMATIONLOOPMODE_CONSTANT;
  333. }
  334. }
  335. }