babylon.animation.ts 16 KB

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