babylon.animation.ts 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. module BABYLON {
  2. export class AnimationRange {
  3. constructor(public name: string, public from: number, public to: number) {
  4. }
  5. public clone(): AnimationRange {
  6. return new AnimationRange(this.name, this.from, this.to);
  7. }
  8. }
  9. /**
  10. * Composed of a frame, and an action function
  11. */
  12. export class AnimationEvent {
  13. public isDone: boolean = false;
  14. constructor(public frame: number, public action: () => void, public onlyOnce?: boolean) {
  15. }
  16. }
  17. export class PathCursor {
  18. private _onchange = new Array<(cursor: PathCursor) => void>();
  19. value: number = 0;
  20. animations = new Array<Animation>();
  21. constructor(private path: Path2) {
  22. }
  23. public getPoint(): Vector3 {
  24. var point = this.path.getPointAtLengthPosition(this.value);
  25. return new Vector3(point.x, 0, point.y);
  26. }
  27. public moveAhead(step: number = 0.002): PathCursor {
  28. this.move(step);
  29. return this;
  30. }
  31. public moveBack(step: number = 0.002): PathCursor {
  32. this.move(-step);
  33. return this;
  34. }
  35. public move(step: number): PathCursor {
  36. if (Math.abs(step) > 1) {
  37. throw "step size should be less than 1.";
  38. }
  39. this.value += step;
  40. this.ensureLimits();
  41. this.raiseOnChange();
  42. return this;
  43. }
  44. private ensureLimits(): PathCursor {
  45. while (this.value > 1) {
  46. this.value -= 1;
  47. }
  48. while (this.value < 0) {
  49. this.value += 1;
  50. }
  51. return this;
  52. }
  53. // used by animation engine
  54. private markAsDirty(propertyName: string): PathCursor {
  55. this.ensureLimits();
  56. this.raiseOnChange();
  57. return this;
  58. }
  59. private raiseOnChange(): PathCursor {
  60. this._onchange.forEach(f => f(this));
  61. return this;
  62. }
  63. public onchange(f: (cursor: PathCursor) => void): PathCursor {
  64. this._onchange.push(f);
  65. return this;
  66. }
  67. }
  68. export class Animation {
  69. private _keys: Array<{frame:number, value: any}>;
  70. private _offsetsCache = {};
  71. private _highLimitsCache = {};
  72. private _stopped = false;
  73. public _target;
  74. private _blendingFactor = 0;
  75. private _easingFunction: IEasingFunction;
  76. // The set of event that will be linked to this animation
  77. private _events = new Array<AnimationEvent>();
  78. public targetPropertyPath: string[];
  79. public currentFrame: number;
  80. public allowMatricesInterpolation = false;
  81. public blendingSpeed = 0.01;
  82. private _originalBlendValue: any;
  83. private _ranges: { [name: string]: AnimationRange; } = {};
  84. static _PrepareAnimation(name: string, targetProperty: string, framePerSecond: number, totalFrame: number,
  85. from: any, to: any, loopMode?: number, easingFunction?: EasingFunction): Animation {
  86. var dataType = undefined;
  87. if (!isNaN(parseFloat(from)) && isFinite(from)) {
  88. dataType = Animation.ANIMATIONTYPE_FLOAT;
  89. } else if (from instanceof Quaternion) {
  90. dataType = Animation.ANIMATIONTYPE_QUATERNION;
  91. } else if (from instanceof Vector3) {
  92. dataType = Animation.ANIMATIONTYPE_VECTOR3;
  93. } else if (from instanceof Vector2) {
  94. dataType = Animation.ANIMATIONTYPE_VECTOR2;
  95. } else if (from instanceof Color3) {
  96. dataType = Animation.ANIMATIONTYPE_COLOR3;
  97. } else if (from instanceof Size) {
  98. dataType = Animation.ANIMATIONTYPE_SIZE;
  99. }
  100. if (dataType == undefined) {
  101. return null;
  102. }
  103. var animation = new Animation(name, targetProperty, framePerSecond, dataType, loopMode);
  104. var keys: Array<{frame: number, value:any}> = [{ frame: 0, value: from }, { frame: totalFrame, value: to }];
  105. animation.setKeys(keys);
  106. if (easingFunction !== undefined) {
  107. animation.setEasingFunction(easingFunction);
  108. }
  109. return animation;
  110. }
  111. public static CreateAndStartAnimation(name: string, node: Node, targetProperty: string,
  112. framePerSecond: number, totalFrame: number,
  113. from: any, to: any, loopMode?: number, easingFunction?: EasingFunction, onAnimationEnd?: () => void) {
  114. var animation = Animation._PrepareAnimation(name, targetProperty, framePerSecond, totalFrame, from, to, loopMode, easingFunction);
  115. return node.getScene().beginDirectAnimation(node, [animation], 0, totalFrame, (animation.loopMode === 1), 1.0, onAnimationEnd);
  116. }
  117. public static CreateMergeAndStartAnimation(name: string, node: Node, targetProperty: string,
  118. framePerSecond: number, totalFrame: number,
  119. from: any, to: any, loopMode?: number, easingFunction?: EasingFunction, onAnimationEnd?: () => void) {
  120. var animation = Animation._PrepareAnimation(name, targetProperty, framePerSecond, totalFrame, from, to, loopMode, easingFunction);
  121. node.animations.push(animation);
  122. return node.getScene().beginAnimation(node, 0, totalFrame, (animation.loopMode === 1), 1.0, onAnimationEnd);
  123. }
  124. constructor(public name: string, public targetProperty: string, public framePerSecond: number, public dataType: number, public loopMode?: number, public enableBlending?: boolean) {
  125. this.targetPropertyPath = targetProperty.split(".");
  126. this.dataType = dataType;
  127. this.loopMode = loopMode === undefined ? Animation.ANIMATIONLOOPMODE_CYCLE : loopMode;
  128. }
  129. // Methods
  130. /**
  131. * @param {boolean} fullDetails - support for multiple levels of logging within scene loading
  132. */
  133. public toString(fullDetails? : boolean) : string {
  134. var ret = "Name: " + this.name + ", property: " + this.targetProperty;
  135. ret += ", datatype: " + (["Float", "Vector3", "Quaternion", "Matrix", "Color3", "Vector2"])[this.dataType];
  136. ret += ", nKeys: " + (this._keys ? this._keys.length : "none");
  137. ret += ", nRanges: " + (this._ranges ? Object.keys(this._ranges).length : "none");
  138. if (fullDetails) {
  139. ret += ", Ranges: {";
  140. var first = true;
  141. for (var name in this._ranges) {
  142. if (first) {
  143. ret += ", ";
  144. first = false;
  145. }
  146. ret += name;
  147. }
  148. ret += "}";
  149. }
  150. return ret;
  151. }
  152. /**
  153. * Add an event to this animation.
  154. */
  155. public addEvent(event: AnimationEvent): void {
  156. this._events.push(event);
  157. }
  158. /**
  159. * Remove all events found at the given frame
  160. * @param frame
  161. */
  162. public removeEvents(frame: number): void {
  163. for (var index = 0; index < this._events.length; index++) {
  164. if (this._events[index].frame === frame) {
  165. this._events.splice(index, 1);
  166. index--;
  167. }
  168. }
  169. }
  170. public createRange(name: string, from: number, to: number): void {
  171. // check name not already in use; could happen for bones after serialized
  172. if (!this._ranges[name]) {
  173. this._ranges[name] = new AnimationRange(name, from, to);
  174. }
  175. }
  176. public deleteRange(name: string, deleteFrames = true): void {
  177. if (this._ranges[name]) {
  178. if (deleteFrames) {
  179. var from = this._ranges[name].from;
  180. var to = this._ranges[name].to;
  181. // this loop MUST go high to low for multiple splices to work
  182. for (var key = this._keys.length - 1; key >= 0; key--) {
  183. if (this._keys[key].frame >= from && this._keys[key].frame <= to) {
  184. this._keys.splice(key, 1);
  185. }
  186. }
  187. }
  188. this._ranges[name] = undefined; // said much faster than 'delete this._range[name]'
  189. }
  190. }
  191. public getRange(name: string): AnimationRange {
  192. return this._ranges[name];
  193. }
  194. public reset(): void {
  195. this._offsetsCache = {};
  196. this._highLimitsCache = {};
  197. this.currentFrame = 0;
  198. this._blendingFactor = 0;
  199. this._originalBlendValue = null;
  200. }
  201. public isStopped(): boolean {
  202. return this._stopped;
  203. }
  204. public getKeys(): Array<{ frame: number, value: any }> {
  205. return this._keys;
  206. }
  207. public getHighestFrame(): number {
  208. var ret = 0;
  209. for (var key = 0, nKeys = this._keys.length; key < nKeys; key++) {
  210. if (ret < this._keys[key].frame) {
  211. ret = this._keys[key].frame;
  212. }
  213. }
  214. return ret;
  215. }
  216. public getEasingFunction() {
  217. return this._easingFunction;
  218. }
  219. public setEasingFunction(easingFunction: EasingFunction) {
  220. this._easingFunction = easingFunction;
  221. }
  222. public floatInterpolateFunction(startValue: number, endValue: number, gradient: number): number {
  223. return startValue + (endValue - startValue) * gradient;
  224. }
  225. public quaternionInterpolateFunction(startValue: Quaternion, endValue: Quaternion, gradient: number): Quaternion {
  226. return Quaternion.Slerp(startValue, endValue, gradient);
  227. }
  228. public vector3InterpolateFunction(startValue: Vector3, endValue: Vector3, gradient: number): Vector3 {
  229. return Vector3.Lerp(startValue, endValue, gradient);
  230. }
  231. public vector2InterpolateFunction(startValue: Vector2, endValue: Vector2, gradient: number): Vector2 {
  232. return Vector2.Lerp(startValue, endValue, gradient);
  233. }
  234. public sizeInterpolateFunction(startValue: Size, endValue: Size, gradient: number): Size {
  235. return Size.Lerp(startValue, endValue, gradient);
  236. }
  237. public color3InterpolateFunction(startValue: Color3, endValue: Color3, gradient: number): Color3 {
  238. return Color3.Lerp(startValue, endValue, gradient);
  239. }
  240. public matrixInterpolateFunction(startValue: Matrix, endValue: Matrix, gradient: number): Matrix {
  241. return Matrix.Lerp(startValue, endValue, gradient);
  242. }
  243. public clone(): Animation {
  244. var clone = new Animation(this.name, this.targetPropertyPath.join("."), this.framePerSecond, this.dataType, this.loopMode);
  245. if (this._keys) {
  246. clone.setKeys(this._keys);
  247. }
  248. if (this._ranges) {
  249. clone._ranges = {};
  250. for (var name in this._ranges) {
  251. clone._ranges[name] = this._ranges[name].clone();
  252. }
  253. }
  254. return clone;
  255. }
  256. public setKeys(values: Array<{ frame: number, value: any }>): void {
  257. this._keys = values.slice(0);
  258. this._offsetsCache = {};
  259. this._highLimitsCache = {};
  260. }
  261. private _getKeyValue(value: any): any {
  262. if (typeof value === "function") {
  263. return value();
  264. }
  265. return value;
  266. }
  267. private _interpolate(currentFrame: number, repeatCount: number, loopMode: number, offsetValue?, highLimitValue?) {
  268. if (loopMode === Animation.ANIMATIONLOOPMODE_CONSTANT && repeatCount > 0) {
  269. return highLimitValue.clone ? highLimitValue.clone() : highLimitValue;
  270. }
  271. this.currentFrame = currentFrame;
  272. // Try to get a hash to find the right key
  273. 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));
  274. if (this._keys[startKey].frame >= currentFrame) {
  275. while (startKey - 1 >= 0 && this._keys[startKey].frame >= currentFrame) {
  276. startKey--;
  277. }
  278. }
  279. for (var key = startKey; key < this._keys.length; key++) {
  280. if (this._keys[key + 1].frame >= currentFrame) {
  281. var startValue = this._getKeyValue(this._keys[key].value);
  282. var endValue = this._getKeyValue(this._keys[key + 1].value);
  283. // gradient : percent of currentFrame between the frame inf and the frame sup
  284. var gradient = (currentFrame - this._keys[key].frame) / (this._keys[key + 1].frame - this._keys[key].frame);
  285. // check for easingFunction and correction of gradient
  286. if (this._easingFunction != null) {
  287. gradient = this._easingFunction.ease(gradient);
  288. }
  289. switch (this.dataType) {
  290. // Float
  291. case Animation.ANIMATIONTYPE_FLOAT:
  292. switch (loopMode) {
  293. case Animation.ANIMATIONLOOPMODE_CYCLE:
  294. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  295. return this.floatInterpolateFunction(startValue, endValue, gradient);
  296. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  297. return offsetValue * repeatCount + this.floatInterpolateFunction(startValue, endValue, gradient);
  298. }
  299. break;
  300. // Quaternion
  301. case Animation.ANIMATIONTYPE_QUATERNION:
  302. var quaternion = null;
  303. switch (loopMode) {
  304. case Animation.ANIMATIONLOOPMODE_CYCLE:
  305. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  306. quaternion = this.quaternionInterpolateFunction(startValue, endValue, gradient);
  307. break;
  308. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  309. quaternion = this.quaternionInterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  310. break;
  311. }
  312. return quaternion;
  313. // Vector3
  314. case Animation.ANIMATIONTYPE_VECTOR3:
  315. switch (loopMode) {
  316. case Animation.ANIMATIONLOOPMODE_CYCLE:
  317. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  318. return this.vector3InterpolateFunction(startValue, endValue, gradient);
  319. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  320. return this.vector3InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  321. }
  322. // Vector2
  323. case Animation.ANIMATIONTYPE_VECTOR2:
  324. switch (loopMode) {
  325. case Animation.ANIMATIONLOOPMODE_CYCLE:
  326. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  327. return this.vector2InterpolateFunction(startValue, endValue, gradient);
  328. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  329. return this.vector2InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  330. }
  331. // Size
  332. case Animation.ANIMATIONTYPE_SIZE:
  333. switch (loopMode) {
  334. case Animation.ANIMATIONLOOPMODE_CYCLE:
  335. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  336. return this.sizeInterpolateFunction(startValue, endValue, gradient);
  337. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  338. return this.sizeInterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  339. }
  340. // Color3
  341. case Animation.ANIMATIONTYPE_COLOR3:
  342. switch (loopMode) {
  343. case Animation.ANIMATIONLOOPMODE_CYCLE:
  344. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  345. return this.color3InterpolateFunction(startValue, endValue, gradient);
  346. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  347. return this.color3InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
  348. }
  349. // Matrix
  350. case Animation.ANIMATIONTYPE_MATRIX:
  351. switch (loopMode) {
  352. case Animation.ANIMATIONLOOPMODE_CYCLE:
  353. case Animation.ANIMATIONLOOPMODE_CONSTANT:
  354. if (this.allowMatricesInterpolation) {
  355. return this.matrixInterpolateFunction(startValue, endValue, gradient);
  356. }
  357. case Animation.ANIMATIONLOOPMODE_RELATIVE:
  358. return startValue;
  359. }
  360. default:
  361. break;
  362. }
  363. break;
  364. }
  365. }
  366. return this._getKeyValue(this._keys[this._keys.length - 1].value);
  367. }
  368. public setValue(currentValue: any, blend: boolean = false): void {
  369. // Set value
  370. var path: any;
  371. var destination: any;
  372. if (this.targetPropertyPath.length > 1) {
  373. var property = this._target[this.targetPropertyPath[0]];
  374. for (var index = 1; index < this.targetPropertyPath.length - 1; index++) {
  375. property = property[this.targetPropertyPath[index]];
  376. }
  377. path = this.targetPropertyPath[this.targetPropertyPath.length - 1];
  378. destination = property;
  379. } else {
  380. path = this.targetPropertyPath[0];
  381. destination = this._target;
  382. }
  383. // Blending
  384. if (this.enableBlending && this._blendingFactor <= 1.0) {
  385. if (!this._originalBlendValue) {
  386. if (destination[path].clone) {
  387. this._originalBlendValue = destination[path].clone();
  388. } else {
  389. this._originalBlendValue = destination[path];
  390. }
  391. }
  392. if (this._originalBlendValue.prototype) { // Complex value
  393. if (this._originalBlendValue.prototype.Lerp) { // Lerp supported
  394. destination[path] = this._originalBlendValue.construtor.prototype.Lerp(currentValue, this._originalBlendValue, this._blendingFactor);
  395. } else { // Blending not supported
  396. destination[path] = currentValue;
  397. }
  398. } else if (this._originalBlendValue.m) { // Matrix
  399. destination[path] = Matrix.Lerp(this._originalBlendValue, currentValue, this._blendingFactor);
  400. } else { // Direct value
  401. destination[path] = this._originalBlendValue * (1.0 - this._blendingFactor) + this._blendingFactor * currentValue;
  402. }
  403. this._blendingFactor += this.blendingSpeed;
  404. } else {
  405. destination[path] = currentValue;
  406. }
  407. if (this._target.markAsDirty) {
  408. this._target.markAsDirty(this.targetProperty);
  409. }
  410. }
  411. public goToFrame(frame: number): void {
  412. if (frame < this._keys[0].frame) {
  413. frame = this._keys[0].frame;
  414. } else if (frame > this._keys[this._keys.length - 1].frame) {
  415. frame = this._keys[this._keys.length - 1].frame;
  416. }
  417. var currentValue = this._interpolate(frame, 0, this.loopMode);
  418. this.setValue(currentValue);
  419. }
  420. public animate(delay: number, from: number, to: number, loop: boolean, speedRatio: number, blend: boolean = false): boolean {
  421. if (!this.targetPropertyPath || this.targetPropertyPath.length < 1) {
  422. this._stopped = true;
  423. return false;
  424. }
  425. var returnValue = true;
  426. // Adding a start key at frame 0 if missing
  427. if (this._keys[0].frame !== 0) {
  428. var newKey = { frame: 0, value: this._keys[0].value };
  429. this._keys.splice(0, 0, newKey);
  430. }
  431. // Check limits
  432. if (from < this._keys[0].frame || from > this._keys[this._keys.length - 1].frame) {
  433. from = this._keys[0].frame;
  434. }
  435. if (to < this._keys[0].frame || to > this._keys[this._keys.length - 1].frame) {
  436. to = this._keys[this._keys.length - 1].frame;
  437. }
  438. // Compute ratio
  439. var range = to - from;
  440. var offsetValue;
  441. // ratio represents the frame delta between from and to
  442. var ratio = delay * (this.framePerSecond * speedRatio) / 1000.0;
  443. var highLimitValue = 0;
  444. if (ratio > range && !loop) { // If we are out of range and not looping get back to caller
  445. returnValue = false;
  446. highLimitValue = this._getKeyValue(this._keys[this._keys.length - 1].value);
  447. } else {
  448. // Get max value if required
  449. if (this.loopMode !== Animation.ANIMATIONLOOPMODE_CYCLE) {
  450. var keyOffset = to.toString() + from.toString();
  451. if (!this._offsetsCache[keyOffset]) {
  452. var fromValue = this._interpolate(from, 0, Animation.ANIMATIONLOOPMODE_CYCLE);
  453. var toValue = this._interpolate(to, 0, Animation.ANIMATIONLOOPMODE_CYCLE);
  454. switch (this.dataType) {
  455. // Float
  456. case Animation.ANIMATIONTYPE_FLOAT:
  457. this._offsetsCache[keyOffset] = toValue - fromValue;
  458. break;
  459. // Quaternion
  460. case Animation.ANIMATIONTYPE_QUATERNION:
  461. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  462. break;
  463. // Vector3
  464. case Animation.ANIMATIONTYPE_VECTOR3:
  465. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  466. // Vector2
  467. case Animation.ANIMATIONTYPE_VECTOR2:
  468. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  469. // Size
  470. case Animation.ANIMATIONTYPE_SIZE:
  471. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  472. // Color3
  473. case Animation.ANIMATIONTYPE_COLOR3:
  474. this._offsetsCache[keyOffset] = toValue.subtract(fromValue);
  475. default:
  476. break;
  477. }
  478. this._highLimitsCache[keyOffset] = toValue;
  479. }
  480. highLimitValue = this._highLimitsCache[keyOffset];
  481. offsetValue = this._offsetsCache[keyOffset];
  482. }
  483. }
  484. if (offsetValue === undefined) {
  485. switch (this.dataType) {
  486. // Float
  487. case Animation.ANIMATIONTYPE_FLOAT:
  488. offsetValue = 0;
  489. break;
  490. // Quaternion
  491. case Animation.ANIMATIONTYPE_QUATERNION:
  492. offsetValue = new Quaternion(0, 0, 0, 0);
  493. break;
  494. // Vector3
  495. case Animation.ANIMATIONTYPE_VECTOR3:
  496. offsetValue = Vector3.Zero();
  497. break;
  498. // Vector2
  499. case Animation.ANIMATIONTYPE_VECTOR2:
  500. offsetValue = Vector2.Zero();
  501. break;
  502. // Size
  503. case Animation.ANIMATIONTYPE_SIZE:
  504. offsetValue = Size.Zero();
  505. break;
  506. // Color3
  507. case Animation.ANIMATIONTYPE_COLOR3:
  508. offsetValue = Color3.Black();
  509. }
  510. }
  511. // Compute value
  512. var repeatCount = (ratio / range) >> 0;
  513. var currentFrame = returnValue ? from + ratio % range : to;
  514. var currentValue = this._interpolate(currentFrame, repeatCount, this.loopMode, offsetValue, highLimitValue);
  515. // Set value
  516. this.setValue(currentValue);
  517. // Check events
  518. for (var index = 0; index < this._events.length; index++) {
  519. if (currentFrame >= this._events[index].frame) {
  520. var event = this._events[index];
  521. if (!event.isDone) {
  522. // If event should be done only once, remove it.
  523. if (event.onlyOnce) {
  524. this._events.splice(index, 1);
  525. index--;
  526. }
  527. event.isDone = true;
  528. event.action();
  529. } // Don't do anything if the event has already be done.
  530. } else if (this._events[index].isDone && !this._events[index].onlyOnce) {
  531. // reset event, the animation is looping
  532. this._events[index].isDone = false;
  533. }
  534. }
  535. if (!returnValue) {
  536. this._stopped = true;
  537. }
  538. return returnValue;
  539. }
  540. public serialize(): any {
  541. var serializationObject: any = {};
  542. serializationObject.name = this.name;
  543. serializationObject.property = this.targetProperty;
  544. serializationObject.framePerSecond = this.framePerSecond;
  545. serializationObject.dataType = this.dataType;
  546. serializationObject.loopBehavior = this.loopMode;
  547. var dataType = this.dataType;
  548. serializationObject.keys = [];
  549. var keys = this.getKeys();
  550. for (var index = 0; index < keys.length; index++) {
  551. var animationKey = keys[index];
  552. var key: any = {};
  553. key.frame = animationKey.frame;
  554. switch (dataType) {
  555. case Animation.ANIMATIONTYPE_FLOAT:
  556. key.values = [animationKey.value];
  557. break;
  558. case Animation.ANIMATIONTYPE_QUATERNION:
  559. case Animation.ANIMATIONTYPE_MATRIX:
  560. case Animation.ANIMATIONTYPE_VECTOR3:
  561. case Animation.ANIMATIONTYPE_COLOR3:
  562. key.values = animationKey.value.asArray();
  563. break;
  564. }
  565. serializationObject.keys.push(key);
  566. }
  567. serializationObject.ranges = [];
  568. for (var name in this._ranges) {
  569. var range: any = {};
  570. range.name = name;
  571. range.from = this._ranges[name].from;
  572. range.to = this._ranges[name].to;
  573. serializationObject.ranges.push(range);
  574. }
  575. return serializationObject;
  576. }
  577. // Statics
  578. private static _ANIMATIONTYPE_FLOAT = 0;
  579. private static _ANIMATIONTYPE_VECTOR3 = 1;
  580. private static _ANIMATIONTYPE_QUATERNION = 2;
  581. private static _ANIMATIONTYPE_MATRIX = 3;
  582. private static _ANIMATIONTYPE_COLOR3 = 4;
  583. private static _ANIMATIONTYPE_VECTOR2 = 5;
  584. private static _ANIMATIONTYPE_SIZE = 6;
  585. private static _ANIMATIONLOOPMODE_RELATIVE = 0;
  586. private static _ANIMATIONLOOPMODE_CYCLE = 1;
  587. private static _ANIMATIONLOOPMODE_CONSTANT = 2;
  588. public static get ANIMATIONTYPE_FLOAT(): number {
  589. return Animation._ANIMATIONTYPE_FLOAT;
  590. }
  591. public static get ANIMATIONTYPE_VECTOR3(): number {
  592. return Animation._ANIMATIONTYPE_VECTOR3;
  593. }
  594. public static get ANIMATIONTYPE_VECTOR2(): number {
  595. return Animation._ANIMATIONTYPE_VECTOR2;
  596. }
  597. public static get ANIMATIONTYPE_SIZE(): number {
  598. return Animation._ANIMATIONTYPE_SIZE;
  599. }
  600. public static get ANIMATIONTYPE_QUATERNION(): number {
  601. return Animation._ANIMATIONTYPE_QUATERNION;
  602. }
  603. public static get ANIMATIONTYPE_MATRIX(): number {
  604. return Animation._ANIMATIONTYPE_MATRIX;
  605. }
  606. public static get ANIMATIONTYPE_COLOR3(): number {
  607. return Animation._ANIMATIONTYPE_COLOR3;
  608. }
  609. public static get ANIMATIONLOOPMODE_RELATIVE(): number {
  610. return Animation._ANIMATIONLOOPMODE_RELATIVE;
  611. }
  612. public static get ANIMATIONLOOPMODE_CYCLE(): number {
  613. return Animation._ANIMATIONLOOPMODE_CYCLE;
  614. }
  615. public static get ANIMATIONLOOPMODE_CONSTANT(): number {
  616. return Animation._ANIMATIONLOOPMODE_CONSTANT;
  617. }
  618. public static Parse(parsedAnimation: any): Animation {
  619. var animation = new Animation(parsedAnimation.name, parsedAnimation.property, parsedAnimation.framePerSecond, parsedAnimation.dataType, parsedAnimation.loopBehavior);
  620. var dataType = parsedAnimation.dataType;
  621. var keys: Array<{ frame: number, value: any }> = [];
  622. var data;
  623. var index: number;
  624. for (index = 0; index < parsedAnimation.keys.length; index++) {
  625. var key = parsedAnimation.keys[index];
  626. switch (dataType) {
  627. case Animation.ANIMATIONTYPE_FLOAT:
  628. data = key.values[0];
  629. break;
  630. case Animation.ANIMATIONTYPE_QUATERNION:
  631. data = Quaternion.FromArray(key.values);
  632. break;
  633. case Animation.ANIMATIONTYPE_MATRIX:
  634. data = Matrix.FromArray(key.values);
  635. break;
  636. case Animation.ANIMATIONTYPE_COLOR3:
  637. data = Color3.FromArray(key.values);
  638. break;
  639. case Animation.ANIMATIONTYPE_VECTOR3:
  640. default:
  641. data = Vector3.FromArray(key.values);
  642. break;
  643. }
  644. keys.push({
  645. frame: key.frame,
  646. value: data
  647. });
  648. }
  649. animation.setKeys(keys);
  650. if (parsedAnimation.ranges) {
  651. for (index = 0; index < parsedAnimation.ranges.length; index++) {
  652. data = parsedAnimation.ranges[index];
  653. animation.createRange(data.name, data.from, data.to);
  654. }
  655. }
  656. return animation;
  657. }
  658. public static AppendSerializedAnimations(source: IAnimatable, destination: any): any {
  659. if (source.animations) {
  660. destination.animations = [];
  661. for (var animationIndex = 0; animationIndex < source.animations.length; animationIndex++) {
  662. var animation = source.animations[animationIndex];
  663. destination.animations.push(animation.serialize());
  664. }
  665. }
  666. }
  667. }
  668. }