babylon.animation.ts 16 KB

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