babylon.animation.ts 17 KB

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