babylon.animation.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 raiseOnChange(): PathCursor {
  55. this._onchange.forEach(f => f(this));
  56. return this;
  57. }
  58. public onchange(f: (cursor: PathCursor) => void): PathCursor {
  59. this._onchange.push(f);
  60. return this;
  61. }
  62. }
  63. export interface IAnimationKey {
  64. frame: number;
  65. value: any;
  66. inTangent?: any;
  67. outTangent?: any;
  68. interpolation?: AnimationKeyInterpolation;
  69. }
  70. export enum AnimationKeyInterpolation {
  71. /**
  72. * Do not interpolate between keys and use the start key value only. Tangents are ignored.
  73. */
  74. STEP = 1
  75. }
  76. export class Animation {
  77. public static AllowMatricesInterpolation = false;
  78. private _keys: Array<IAnimationKey>;
  79. private _easingFunction: IEasingFunction;
  80. public _runtimeAnimations = new Array<RuntimeAnimation>();
  81. // The set of event that will be linked to this animation
  82. private _events = new Array<AnimationEvent>();
  83. public targetPropertyPath: string[];
  84. public blendingSpeed = 0.01;
  85. private _ranges: { [name: string]: Nullable<AnimationRange> } = {};
  86. static _PrepareAnimation(name: string, targetProperty: string, framePerSecond: number, totalFrame: number,
  87. from: any, to: any, loopMode?: number, easingFunction?: EasingFunction): Nullable<Animation> {
  88. var dataType = undefined;
  89. if (!isNaN(parseFloat(from)) && isFinite(from)) {
  90. dataType = Animation.ANIMATIONTYPE_FLOAT;
  91. } else if (from instanceof Quaternion) {
  92. dataType = Animation.ANIMATIONTYPE_QUATERNION;
  93. } else if (from instanceof Vector3) {
  94. dataType = Animation.ANIMATIONTYPE_VECTOR3;
  95. } else if (from instanceof Vector2) {
  96. dataType = Animation.ANIMATIONTYPE_VECTOR2;
  97. } else if (from instanceof Color3) {
  98. dataType = Animation.ANIMATIONTYPE_COLOR3;
  99. } else if (from instanceof Size) {
  100. dataType = Animation.ANIMATIONTYPE_SIZE;
  101. }
  102. if (dataType == undefined) {
  103. return null;
  104. }
  105. var animation = new Animation(name, targetProperty, framePerSecond, dataType, loopMode);
  106. var keys: Array<IAnimationKey> = [{ frame: 0, value: from }, { frame: totalFrame, value: to }];
  107. animation.setKeys(keys);
  108. if (easingFunction !== undefined) {
  109. animation.setEasingFunction(easingFunction);
  110. }
  111. return animation;
  112. }
  113. /**
  114. * Sets up an animation.
  115. * @param property the property to animate
  116. * @param animationType the animation type to apply
  117. * @param easingFunction the easing function used in the animation
  118. * @returns The created animation
  119. */
  120. public static CreateAnimation(property: string, animationType: number, framePerSecond: number, easingFunction: EasingFunction): Animation {
  121. var animation: Animation = new Animation(property + "Animation",
  122. property,
  123. framePerSecond,
  124. animationType,
  125. Animation.ANIMATIONLOOPMODE_CONSTANT);
  126. animation.setEasingFunction(easingFunction);
  127. return animation;
  128. }
  129. /**
  130. * Create and start an animation on a node
  131. * @param {string} name defines the name of the global animation that will be run on all nodes
  132. * @param {BABYLON.Node} node defines the root node where the animation will take place
  133. * @param {string} targetProperty defines property to animate
  134. * @param {number} framePerSecond defines the number of frame per second yo use
  135. * @param {number} totalFrame defines the number of frames in total
  136. * @param {any} from defines the initial value
  137. * @param {any} to defines the final value
  138. * @param {number} loopMode defines which loop mode you want to use (off by default)
  139. * @param {BABYLON.EasingFunction} easingFunction defines the easing function to use (linear by default)
  140. * @param onAnimationEnd defines the callback to call when animation end
  141. * @returns the animatable created for this animation
  142. */
  143. public static CreateAndStartAnimation(name: string, node: Node, targetProperty: string,
  144. framePerSecond: number, totalFrame: number,
  145. from: any, to: any, loopMode?: number, easingFunction?: EasingFunction, onAnimationEnd?: () => void): Nullable<Animatable> {
  146. var animation = Animation._PrepareAnimation(name, targetProperty, framePerSecond, totalFrame, from, to, loopMode, easingFunction);
  147. if (!animation) {
  148. return null;
  149. }
  150. return node.getScene().beginDirectAnimation(node, [animation], 0, totalFrame, (animation.loopMode === 1), 1.0, onAnimationEnd);
  151. }
  152. /**
  153. * Create and start an animation on a node and its descendants
  154. * @param {string} name defines the name of the global animation that will be run on all nodes
  155. * @param {BABYLON.Node} node defines the root node where the animation will take place
  156. * @param {boolean} directDescendantsOnly if true only direct descendants will be used, if false direct and also indirect (children of children, an so on in a recursive manner) descendants will be used.
  157. * @param {string} targetProperty defines property to animate
  158. * @param {number} framePerSecond defines the number of frame per second yo use
  159. * @param {number} totalFrame defines the number of frames in total
  160. * @param {any} from defines the initial value
  161. * @param {any} to defines the final value
  162. * @param {number} loopMode defines which loop mode you want to use (off by default)
  163. * @param {BABYLON.EasingFunction} easingFunction defines the easing function to use (linear by default)
  164. * @param onAnimationEnd defines the callback to call when an animation ends (will be called once per node)
  165. * @returns the list of animatables created for all nodes
  166. * @example https://www.babylonjs-playground.com/#MH0VLI
  167. */
  168. public static CreateAndStartHierarchyAnimation(name: string, node: Node, directDescendantsOnly: boolean, targetProperty: string,
  169. framePerSecond: number, totalFrame: number,
  170. from: any, to: any, loopMode?: number, easingFunction?: EasingFunction, onAnimationEnd?: () => void): Nullable<Animatable[]> {
  171. var animation = Animation._PrepareAnimation(name, targetProperty, framePerSecond, totalFrame, from, to, loopMode, easingFunction);
  172. if (!animation) {
  173. return null;
  174. }
  175. let scene = node.getScene();
  176. return scene.beginDirectHierarchyAnimation(node, directDescendantsOnly, [animation], 0, totalFrame, (animation.loopMode === 1), 1.0, onAnimationEnd);
  177. }
  178. public static CreateMergeAndStartAnimation(name: string, node: Node, targetProperty: string,
  179. framePerSecond: number, totalFrame: number,
  180. from: any, to: any, loopMode?: number, easingFunction?: EasingFunction, onAnimationEnd?: () => void): Nullable<Animatable> {
  181. var animation = Animation._PrepareAnimation(name, targetProperty, framePerSecond, totalFrame, from, to, loopMode, easingFunction);
  182. if (!animation) {
  183. return null;
  184. }
  185. node.animations.push(animation);
  186. return node.getScene().beginAnimation(node, 0, totalFrame, (animation.loopMode === 1), 1.0, onAnimationEnd);
  187. }
  188. /**
  189. * Transition property of the Camera to the target Value.
  190. * @param property The property to transition
  191. * @param targetValue The target Value of the property
  192. * @param host The object where the property to animate belongs
  193. * @param scene Scene used to run the animation
  194. * @param frameRate Framerate (in frame/s) to use
  195. * @param transition The transition type we want to use
  196. * @param duration The duration of the animation, in milliseconds
  197. * @param onAnimationEnd Call back trigger at the end of the animation.
  198. */
  199. public static TransitionTo(property: string, targetValue: any, host: any, scene: Scene, frameRate: number, transition: Animation, duration: number, onAnimationEnd: Nullable<() => void> = null): Nullable<Animatable> {
  200. if (duration <= 0) {
  201. host[property] = targetValue;
  202. if (onAnimationEnd) {
  203. onAnimationEnd();
  204. }
  205. return null;
  206. }
  207. var endFrame: number = frameRate * (duration / 1000);
  208. transition.setKeys([{
  209. frame: 0,
  210. value: host[property].clone ? host[property].clone() : host[property]
  211. },
  212. {
  213. frame: endFrame,
  214. value: targetValue
  215. }]);
  216. if (!host.animations) {
  217. host.animations = [];
  218. }
  219. host.animations.push(transition);
  220. var animation: Animatable = scene.beginAnimation(host, 0, endFrame, false);
  221. animation.onAnimationEnd = onAnimationEnd;
  222. return animation;
  223. }
  224. /**
  225. * Return the array of runtime animations currently using this animation
  226. */
  227. public get runtimeAnimations(): RuntimeAnimation[] {
  228. return this._runtimeAnimations;
  229. }
  230. public get hasRunningRuntimeAnimations(): boolean {
  231. for (var runtimeAnimation of this._runtimeAnimations) {
  232. if (!runtimeAnimation.isStopped) {
  233. return true;
  234. }
  235. }
  236. return false;
  237. }
  238. constructor(public name: string, public targetProperty: string, public framePerSecond: number, public dataType: number, public loopMode?: number, public enableBlending?: boolean) {
  239. this.targetPropertyPath = targetProperty.split(".");
  240. this.dataType = dataType;
  241. this.loopMode = loopMode === undefined ? Animation.ANIMATIONLOOPMODE_CYCLE : loopMode;
  242. }
  243. // Methods
  244. /**
  245. * @param {boolean} fullDetails - support for multiple levels of logging within scene loading
  246. */
  247. public toString(fullDetails?: boolean): string {
  248. var ret = "Name: " + this.name + ", property: " + this.targetProperty;
  249. ret += ", datatype: " + (["Float", "Vector3", "Quaternion", "Matrix", "Color3", "Vector2"])[this.dataType];
  250. ret += ", nKeys: " + (this._keys ? this._keys.length : "none");
  251. ret += ", nRanges: " + (this._ranges ? Object.keys(this._ranges).length : "none");
  252. if (fullDetails) {
  253. ret += ", Ranges: {";
  254. var first = true;
  255. for (var name in this._ranges) {
  256. if (first) {
  257. ret += ", ";
  258. first = false;
  259. }
  260. ret += name;
  261. }
  262. ret += "}";
  263. }
  264. return ret;
  265. }
  266. /**
  267. * Add an event to this animation.
  268. */
  269. public addEvent(event: AnimationEvent): void {
  270. this._events.push(event);
  271. }
  272. /**
  273. * Remove all events found at the given frame
  274. * @param frame
  275. */
  276. public removeEvents(frame: number): void {
  277. for (var index = 0; index < this._events.length; index++) {
  278. if (this._events[index].frame === frame) {
  279. this._events.splice(index, 1);
  280. index--;
  281. }
  282. }
  283. }
  284. public getEvents(): AnimationEvent[] {
  285. return this._events;
  286. }
  287. public createRange(name: string, from: number, to: number): void {
  288. // check name not already in use; could happen for bones after serialized
  289. if (!this._ranges[name]) {
  290. this._ranges[name] = new AnimationRange(name, from, to);
  291. }
  292. }
  293. public deleteRange(name: string, deleteFrames = true): void {
  294. let range = this._ranges[name];
  295. if (!range) {
  296. return;
  297. }
  298. if (deleteFrames) {
  299. var from = range.from;
  300. var to = range.to;
  301. // this loop MUST go high to low for multiple splices to work
  302. for (var key = this._keys.length - 1; key >= 0; key--) {
  303. if (this._keys[key].frame >= from && this._keys[key].frame <= to) {
  304. this._keys.splice(key, 1);
  305. }
  306. }
  307. }
  308. this._ranges[name] = null; // said much faster than 'delete this._range[name]'
  309. }
  310. public getRange(name: string): Nullable<AnimationRange> {
  311. return this._ranges[name];
  312. }
  313. public getKeys(): Array<IAnimationKey> {
  314. return this._keys;
  315. }
  316. public getHighestFrame(): number {
  317. var ret = 0;
  318. for (var key = 0, nKeys = this._keys.length; key < nKeys; key++) {
  319. if (ret < this._keys[key].frame) {
  320. ret = this._keys[key].frame;
  321. }
  322. }
  323. return ret;
  324. }
  325. public getEasingFunction() {
  326. return this._easingFunction;
  327. }
  328. public setEasingFunction(easingFunction: EasingFunction) {
  329. this._easingFunction = easingFunction;
  330. }
  331. public floatInterpolateFunction(startValue: number, endValue: number, gradient: number): number {
  332. return Scalar.Lerp(startValue, endValue, gradient);
  333. }
  334. public floatInterpolateFunctionWithTangents(startValue: number, outTangent: number, endValue: number, inTangent: number, gradient: number): number {
  335. return Scalar.Hermite(startValue, outTangent, endValue, inTangent, gradient);
  336. }
  337. public quaternionInterpolateFunction(startValue: Quaternion, endValue: Quaternion, gradient: number): Quaternion {
  338. return Quaternion.Slerp(startValue, endValue, gradient);
  339. }
  340. public quaternionInterpolateFunctionWithTangents(startValue: Quaternion, outTangent: Quaternion, endValue: Quaternion, inTangent: Quaternion, gradient: number): Quaternion {
  341. return Quaternion.Hermite(startValue, outTangent, endValue, inTangent, gradient).normalize();
  342. }
  343. public vector3InterpolateFunction(startValue: Vector3, endValue: Vector3, gradient: number): Vector3 {
  344. return Vector3.Lerp(startValue, endValue, gradient);
  345. }
  346. public vector3InterpolateFunctionWithTangents(startValue: Vector3, outTangent: Vector3, endValue: Vector3, inTangent: Vector3, gradient: number): Vector3 {
  347. return Vector3.Hermite(startValue, outTangent, endValue, inTangent, gradient);
  348. }
  349. public vector2InterpolateFunction(startValue: Vector2, endValue: Vector2, gradient: number): Vector2 {
  350. return Vector2.Lerp(startValue, endValue, gradient);
  351. }
  352. public vector2InterpolateFunctionWithTangents(startValue: Vector2, outTangent: Vector2, endValue: Vector2, inTangent: Vector2, gradient: number): Vector2 {
  353. return Vector2.Hermite(startValue, outTangent, endValue, inTangent, gradient);
  354. }
  355. public sizeInterpolateFunction(startValue: Size, endValue: Size, gradient: number): Size {
  356. return Size.Lerp(startValue, endValue, gradient);
  357. }
  358. public color3InterpolateFunction(startValue: Color3, endValue: Color3, gradient: number): Color3 {
  359. return Color3.Lerp(startValue, endValue, gradient);
  360. }
  361. public matrixInterpolateFunction(startValue: Matrix, endValue: Matrix, gradient: number): Matrix {
  362. return Matrix.Lerp(startValue, endValue, gradient);
  363. }
  364. public clone(): Animation {
  365. var clone = new Animation(this.name, this.targetPropertyPath.join("."), this.framePerSecond, this.dataType, this.loopMode);
  366. clone.enableBlending = this.enableBlending;
  367. clone.blendingSpeed = this.blendingSpeed;
  368. if (this._keys) {
  369. clone.setKeys(this._keys);
  370. }
  371. if (this._ranges) {
  372. clone._ranges = {};
  373. for (var name in this._ranges) {
  374. let range = this._ranges[name];
  375. if (!range) {
  376. continue;
  377. }
  378. clone._ranges[name] = range.clone();
  379. }
  380. }
  381. return clone;
  382. }
  383. public setKeys(values: Array<IAnimationKey>): void {
  384. this._keys = values.slice(0);
  385. }
  386. public serialize(): any {
  387. var serializationObject: any = {};
  388. serializationObject.name = this.name;
  389. serializationObject.property = this.targetProperty;
  390. serializationObject.framePerSecond = this.framePerSecond;
  391. serializationObject.dataType = this.dataType;
  392. serializationObject.loopBehavior = this.loopMode;
  393. serializationObject.enableBlending = this.enableBlending;
  394. serializationObject.blendingSpeed = this.blendingSpeed;
  395. var dataType = this.dataType;
  396. serializationObject.keys = [];
  397. var keys = this.getKeys();
  398. for (var index = 0; index < keys.length; index++) {
  399. var animationKey = keys[index];
  400. var key: any = {};
  401. key.frame = animationKey.frame;
  402. switch (dataType) {
  403. case Animation.ANIMATIONTYPE_FLOAT:
  404. key.values = [animationKey.value];
  405. break;
  406. case Animation.ANIMATIONTYPE_QUATERNION:
  407. case Animation.ANIMATIONTYPE_MATRIX:
  408. case Animation.ANIMATIONTYPE_VECTOR3:
  409. case Animation.ANIMATIONTYPE_COLOR3:
  410. key.values = animationKey.value.asArray();
  411. break;
  412. }
  413. serializationObject.keys.push(key);
  414. }
  415. serializationObject.ranges = [];
  416. for (var name in this._ranges) {
  417. let source = this._ranges[name];
  418. if (!source) {
  419. continue;
  420. }
  421. var range: any = {};
  422. range.name = name;
  423. range.from = source.from;
  424. range.to = source.to;
  425. serializationObject.ranges.push(range);
  426. }
  427. return serializationObject;
  428. }
  429. // Statics
  430. private static _ANIMATIONTYPE_FLOAT = 0;
  431. private static _ANIMATIONTYPE_VECTOR3 = 1;
  432. private static _ANIMATIONTYPE_QUATERNION = 2;
  433. private static _ANIMATIONTYPE_MATRIX = 3;
  434. private static _ANIMATIONTYPE_COLOR3 = 4;
  435. private static _ANIMATIONTYPE_VECTOR2 = 5;
  436. private static _ANIMATIONTYPE_SIZE = 6;
  437. private static _ANIMATIONLOOPMODE_RELATIVE = 0;
  438. private static _ANIMATIONLOOPMODE_CYCLE = 1;
  439. private static _ANIMATIONLOOPMODE_CONSTANT = 2;
  440. public static get ANIMATIONTYPE_FLOAT(): number {
  441. return Animation._ANIMATIONTYPE_FLOAT;
  442. }
  443. public static get ANIMATIONTYPE_VECTOR3(): number {
  444. return Animation._ANIMATIONTYPE_VECTOR3;
  445. }
  446. public static get ANIMATIONTYPE_VECTOR2(): number {
  447. return Animation._ANIMATIONTYPE_VECTOR2;
  448. }
  449. public static get ANIMATIONTYPE_SIZE(): number {
  450. return Animation._ANIMATIONTYPE_SIZE;
  451. }
  452. public static get ANIMATIONTYPE_QUATERNION(): number {
  453. return Animation._ANIMATIONTYPE_QUATERNION;
  454. }
  455. public static get ANIMATIONTYPE_MATRIX(): number {
  456. return Animation._ANIMATIONTYPE_MATRIX;
  457. }
  458. public static get ANIMATIONTYPE_COLOR3(): number {
  459. return Animation._ANIMATIONTYPE_COLOR3;
  460. }
  461. public static get ANIMATIONLOOPMODE_RELATIVE(): number {
  462. return Animation._ANIMATIONLOOPMODE_RELATIVE;
  463. }
  464. public static get ANIMATIONLOOPMODE_CYCLE(): number {
  465. return Animation._ANIMATIONLOOPMODE_CYCLE;
  466. }
  467. public static get ANIMATIONLOOPMODE_CONSTANT(): number {
  468. return Animation._ANIMATIONLOOPMODE_CONSTANT;
  469. }
  470. public static Parse(parsedAnimation: any): Animation {
  471. var animation = new Animation(parsedAnimation.name, parsedAnimation.property, parsedAnimation.framePerSecond, parsedAnimation.dataType, parsedAnimation.loopBehavior);
  472. var dataType = parsedAnimation.dataType;
  473. var keys: Array<IAnimationKey> = [];
  474. var data;
  475. var index: number;
  476. if (parsedAnimation.enableBlending) {
  477. animation.enableBlending = parsedAnimation.enableBlending;
  478. }
  479. if (parsedAnimation.blendingSpeed) {
  480. animation.blendingSpeed = parsedAnimation.blendingSpeed;
  481. }
  482. for (index = 0; index < parsedAnimation.keys.length; index++) {
  483. var key = parsedAnimation.keys[index];
  484. var inTangent: any;
  485. var outTangent: any;
  486. switch (dataType) {
  487. case Animation.ANIMATIONTYPE_FLOAT:
  488. data = key.values[0];
  489. if (key.values.length >= 1) {
  490. inTangent = key.values[1];
  491. }
  492. if (key.values.length >= 2) {
  493. outTangent = key.values[2];
  494. }
  495. break;
  496. case Animation.ANIMATIONTYPE_QUATERNION:
  497. data = Quaternion.FromArray(key.values);
  498. if (key.values.length >= 8) {
  499. var _inTangent = Quaternion.FromArray(key.values.slice(4, 8));
  500. if (!_inTangent.equals(Quaternion.Zero())) {
  501. inTangent = _inTangent;
  502. }
  503. }
  504. if (key.values.length >= 12) {
  505. var _outTangent = Quaternion.FromArray(key.values.slice(8, 12));
  506. if (!_outTangent.equals(Quaternion.Zero())) {
  507. outTangent = _outTangent;
  508. }
  509. }
  510. break;
  511. case Animation.ANIMATIONTYPE_MATRIX:
  512. data = Matrix.FromArray(key.values);
  513. break;
  514. case Animation.ANIMATIONTYPE_COLOR3:
  515. data = Color3.FromArray(key.values);
  516. break;
  517. case Animation.ANIMATIONTYPE_VECTOR3:
  518. default:
  519. data = Vector3.FromArray(key.values);
  520. break;
  521. }
  522. var keyData: any = {};
  523. keyData.frame = key.frame;
  524. keyData.value = data;
  525. if (inTangent != undefined) {
  526. keyData.inTangent = inTangent;
  527. }
  528. if (outTangent != undefined) {
  529. keyData.outTangent = outTangent;
  530. }
  531. keys.push(keyData)
  532. }
  533. animation.setKeys(keys);
  534. if (parsedAnimation.ranges) {
  535. for (index = 0; index < parsedAnimation.ranges.length; index++) {
  536. data = parsedAnimation.ranges[index];
  537. animation.createRange(data.name, data.from, data.to);
  538. }
  539. }
  540. return animation;
  541. }
  542. public static AppendSerializedAnimations(source: IAnimatable, destination: any): any {
  543. if (source.animations) {
  544. destination.animations = [];
  545. for (var animationIndex = 0; animationIndex < source.animations.length; animationIndex++) {
  546. var animation = source.animations[animationIndex];
  547. destination.animations.push(animation.serialize());
  548. }
  549. }
  550. }
  551. }
  552. }