babylon.animation.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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;
  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. 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 m = Matrix.FromValues(resultScale.x, 0, 0, 0,
  83. 0, resultScale.y, 0, 0,
  84. 0, 0, resultScale.z, 0,
  85. 0, 0, 0, 1);
  86. var rotationMatrix = Matrix.Identity();
  87. resultRotation.toRotationMatrix(rotationMatrix);
  88. m = m.multiply(rotationMatrix);
  89. m.setTranslation(resultTranslation);
  90. return m;
  91. }
  92. public clone(): Animation {
  93. var clone = new Animation(this.name, this.targetPropertyPath.join("."), this.framePerSecond, this.dataType, this.loopMode);
  94. clone.setKeys(this._keys);
  95. return clone;
  96. }
  97. public setKeys(values: Array<any>): void {
  98. this._keys = values.slice(0);
  99. this._offsetsCache = {};
  100. this._highLimitsCache = {};
  101. }
  102. private _getKeyValue(value: any): any {
  103. if (typeof value === "function") {
  104. return value();
  105. }
  106. return value;
  107. }
  108. private _interpolate(currentFrame: number, repeatCount: number, loopMode: number, offsetValue?, highLimitValue?) {
  109. if (loopMode === Animation.ANIMATIONLOOPMODE_CONSTANT && repeatCount > 0) {
  110. return highLimitValue.clone ? highLimitValue.clone() : highLimitValue;
  111. }
  112. this.currentFrame = currentFrame;
  113. for (var key = 0; key < this._keys.length; key++) {
  114. // for each frame, we need the key just before the frame superior
  115. if (this._keys[key + 1].frame >= currentFrame) {
  116. var startValue = this._getKeyValue(this._keys[key].value);
  117. var endValue = this._getKeyValue(this._keys[key + 1].value);
  118. // gradient : percent of currentFrame between the frame inf and the frame sup
  119. var gradient = (currentFrame - this._keys[key].frame) / (this._keys[key + 1].frame - this._keys[key].frame);
  120. // check for easingFunction and correction of gradient
  121. if (this._easingFunction != null) {
  122. gradient = this._easingFunction.ease(gradient);
  123. }
  124. switch (this.dataType) {
  125. // Float
  126. case Animation.ANIMATIONTYPE_FLOAT:
  127. switch (loopMode) {
  128. case Animation.ANIMATIONLOOPMODE_CYCLE:
  129. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  130. return this.floatInterpolateFunction(startValue, endValue, gradient);
  131. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  132. return offsetValue * repeatCount + this.floatInterpolateFunction(startValue, endValue, gradient);
  133. }
  134. break;
  135. // Quaternion
  136. case Animation.ANIMATIONTYPE_QUATERNION:
  137. var quaternion = null;
  138. switch (loopMode) {
  139. case Animation.ANIMATIONLOOPMODE_CYCLE:
  140. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  141. quaternion = this.quaternionInterpolateFunction(startValue, endValue, gradient);
  142. break;
  143. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  144. quaternion = this.quaternionInterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  145. break;
  146. }
  147. return quaternion;
  148. // Vector3
  149. case Animation.ANIMATIONTYPE_VECTOR3:
  150. switch (loopMode) {
  151. case Animation.ANIMATIONLOOPMODE_CYCLE:
  152. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  153. return this.vector3InterpolateFunction(startValue, endValue, gradient);
  154. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  155. return this.vector3InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  156. }
  157. // Vector2
  158. case Animation.ANIMATIONTYPE_VECTOR2:
  159. switch (loopMode) {
  160. case Animation.ANIMATIONLOOPMODE_CYCLE:
  161. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  162. return this.vector2InterpolateFunction(startValue, endValue, gradient);
  163. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  164. return this.vector2InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  165. }
  166. // Color3
  167. case Animation.ANIMATIONTYPE_COLOR3:
  168. switch (loopMode) {
  169. case Animation.ANIMATIONLOOPMODE_CYCLE:
  170. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  171. return this.color3InterpolateFunction(startValue, endValue, gradient);
  172. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  173. return this.color3InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  174. }
  175. // Matrix
  176. case Animation.ANIMATIONTYPE_MATRIX:
  177. switch (loopMode) {
  178. case Animation.ANIMATIONLOOPMODE_CYCLE:
  179. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  180. return this.matrixInterpolateFunction(startValue, endValue, gradient);
  181. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  182. return startValue;
  183. }
  184. default:
  185. break;
  186. }
  187. break;
  188. }
  189. }
  190. return this._getKeyValue(this._keys[this._keys.length - 1].value);
  191. }
  192. public animate(delay: number, from: number, to: number, loop: boolean, speedRatio: number): boolean {
  193. if (!this.targetPropertyPath || this.targetPropertyPath.length < 1) {
  194. this._stopped = true;
  195. return false;
  196. }
  197. var returnValue = true;
  198. // Adding a start key at frame 0 if missing
  199. if (this._keys[0].frame !== 0) {
  200. var newKey = { frame: 0, value: this._keys[0].value };
  201. this._keys.splice(0, 0, newKey);
  202. }
  203. // Check limits
  204. if (from < this._keys[0].frame || from > this._keys[this._keys.length - 1].frame) {
  205. from = this._keys[0].frame;
  206. }
  207. if (to < this._keys[0].frame || to > this._keys[this._keys.length - 1].frame) {
  208. to = this._keys[this._keys.length - 1].frame;
  209. }
  210. // Compute ratio
  211. var range = to - from;
  212. var offsetValue;
  213. // ratio represents the frame delta between from and to
  214. var ratio = delay * (this.framePerSecond * speedRatio) / 1000.0;
  215. var highLimitValue = 0;
  216. if (ratio > range && !loop) { // If we are out of range and not looping get back to caller
  217. returnValue = false;
  218. highLimitValue = this._getKeyValue(this._keys[this._keys.length - 1].value);
  219. } else {
  220. // Get max value if required
  221. if (this.loopMode !== Animation.ANIMATIONLOOPMODE_CYCLE) {
  222. var keyOffset = to.toString() + from.toString();
  223. if (!this._offsetsCache[keyOffset]) {
  224. var fromValue = this._interpolate(from, 0, Animation.ANIMATIONLOOPMODE_CYCLE);
  225. var toValue = this._interpolate(to, 0, Animation.ANIMATIONLOOPMODE_CYCLE);
  226. switch (this.dataType) {
  227. // Float
  228. case Animation.ANIMATIONTYPE_FLOAT:
  229. this._offsetsCache[keyOffset] = toValue - fromValue;
  230. break;
  231. // Quaternion
  232. case Animation.ANIMATIONTYPE_QUATERNION:
  233. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  234. break;
  235. // Vector3
  236. case Animation.ANIMATIONTYPE_VECTOR3:
  237. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  238. // Vector2
  239. case Animation.ANIMATIONTYPE_VECTOR2:
  240. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  241. // Color3
  242. case Animation.ANIMATIONTYPE_COLOR3:
  243. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  244. default:
  245. break;
  246. }
  247. this._highLimitsCache[keyOffset] = toValue;
  248. }
  249. highLimitValue = this._highLimitsCache[keyOffset];
  250. offsetValue = this._offsetsCache[keyOffset];
  251. }
  252. }
  253. if (offsetValue === undefined) {
  254. switch (this.dataType) {
  255. // Float
  256. case Animation.ANIMATIONTYPE_FLOAT:
  257. offsetValue = 0;
  258. break;
  259. // Quaternion
  260. case Animation.ANIMATIONTYPE_QUATERNION:
  261. offsetValue = new Quaternion(0, 0, 0, 0);
  262. break;
  263. // Vector3
  264. case Animation.ANIMATIONTYPE_VECTOR3:
  265. offsetValue = Vector3.Zero();
  266. break;
  267. // Vector2
  268. case Animation.ANIMATIONTYPE_VECTOR2:
  269. offsetValue = Vector2.Zero();
  270. break;
  271. // Color3
  272. case Animation.ANIMATIONTYPE_COLOR3:
  273. offsetValue = Color3.Black();
  274. }
  275. }
  276. // Compute value
  277. var repeatCount = (ratio / range) >> 0;
  278. var currentFrame = returnValue ? from + ratio % range : to;
  279. var currentValue = this._interpolate(currentFrame, repeatCount, this.loopMode, offsetValue, highLimitValue);
  280. // Set value
  281. if (this.targetPropertyPath.length > 1) {
  282. var property = this._target[this.targetPropertyPath[0]];
  283. for (var index = 1; index < this.targetPropertyPath.length - 1; index++) {
  284. property = property[this.targetPropertyPath[index]];
  285. }
  286. property[this.targetPropertyPath[this.targetPropertyPath.length - 1]] = currentValue;
  287. } else {
  288. this._target[this.targetPropertyPath[0]] = currentValue;
  289. }
  290. if (this._target.markAsDirty) {
  291. this._target.markAsDirty(this.targetProperty);
  292. }
  293. if (!returnValue) {
  294. this._stopped = true;
  295. }
  296. return returnValue;
  297. }
  298. // Statics
  299. private static _ANIMATIONTYPE_FLOAT = 0;
  300. private static _ANIMATIONTYPE_VECTOR3 = 1;
  301. private static _ANIMATIONTYPE_QUATERNION = 2;
  302. private static _ANIMATIONTYPE_MATRIX = 3;
  303. private static _ANIMATIONTYPE_COLOR3 = 4;
  304. private static _ANIMATIONTYPE_VECTOR2 = 5;
  305. private static _ANIMATIONLOOPMODE_RELATIVE = 0;
  306. private static _ANIMATIONLOOPMODE_CYCLE = 1;
  307. private static _ANIMATIONLOOPMODE_CONSTANT = 2;
  308. public static get ANIMATIONTYPE_FLOAT(): number {
  309. return Animation._ANIMATIONTYPE_FLOAT;
  310. }
  311. public static get ANIMATIONTYPE_VECTOR3(): number {
  312. return Animation._ANIMATIONTYPE_VECTOR3;
  313. }
  314. public static get ANIMATIONTYPE_VECTOR2(): number {
  315. return Animation._ANIMATIONTYPE_VECTOR2;
  316. }
  317. public static get ANIMATIONTYPE_QUATERNION(): number {
  318. return Animation._ANIMATIONTYPE_QUATERNION;
  319. }
  320. public static get ANIMATIONTYPE_MATRIX(): number {
  321. return Animation._ANIMATIONTYPE_MATRIX;
  322. }
  323. public static get ANIMATIONTYPE_COLOR3(): number {
  324. return Animation._ANIMATIONTYPE_COLOR3;
  325. }
  326. public static get ANIMATIONLOOPMODE_RELATIVE(): number {
  327. return Animation._ANIMATIONLOOPMODE_RELATIVE;
  328. }
  329. public static get ANIMATIONLOOPMODE_CYCLE(): number {
  330. return Animation._ANIMATIONLOOPMODE_CYCLE;
  331. }
  332. public static get ANIMATIONLOOPMODE_CONSTANT(): number {
  333. return Animation._ANIMATIONLOOPMODE_CONSTANT;
  334. }
  335. }
  336. }