viewerModel.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. import { ISceneLoaderPlugin, ISceneLoaderPluginAsync, AnimationGroup, Animatable, AbstractMesh, Tools, Scene, SceneLoader, Observable, SceneLoaderProgressEvent, Tags, ParticleSystem, Skeleton, IDisposable, Nullable, Animation, Quaternion, Material, Vector3, AnimationPropertiesOverride, QuinticEase, SineEase, CircleEase, BackEase, BounceEase, CubicEase, ElasticEase, ExponentialEase, PowerEase, QuadraticEase, QuarticEase, PBRMaterial, MultiMaterial } from "babylonjs";
  2. import { GLTFFileLoader, GLTF2 } from "babylonjs-loaders";
  3. import { IModelConfiguration, IModelAnimationConfiguration } from "../configuration/configuration";
  4. import { IModelAnimation, GroupModelAnimation, AnimationPlayMode, ModelAnimationConfiguration, EasingFunction, AnimationState } from "./modelAnimation";
  5. import * as deepmerge from '../../assets/deepmerge.min.js';
  6. import { AbstractViewer } from "..";
  7. import { extendClassWithConfig } from "../helper";
  8. export enum ModelState {
  9. INIT,
  10. LOADING,
  11. LOADED,
  12. ENTRY,
  13. ENTRYDONE,
  14. COMPLETE,
  15. CANCELED,
  16. ERROR
  17. }
  18. /**
  19. * The viewer model is a container for all assets representing a sngle loaded model.
  20. */
  21. export class ViewerModel implements IDisposable {
  22. /**
  23. * The loader used to load this model.
  24. */
  25. public loader: ISceneLoaderPlugin | ISceneLoaderPluginAsync;
  26. private _animations: Array<IModelAnimation>;
  27. /**
  28. * the list of meshes that are a part of this model
  29. */
  30. private _meshes: Array<AbstractMesh> = [];
  31. /**
  32. * This model's root mesh (the parent of all other meshes).
  33. * This mesh does not(!) exist in the meshes array.
  34. */
  35. public rootMesh: AbstractMesh;
  36. private _pivotMesh: AbstractMesh;
  37. /**
  38. * ParticleSystems connected to this model
  39. */
  40. public particleSystems: Array<ParticleSystem> = [];
  41. /**
  42. * Skeletons defined in this model
  43. */
  44. public skeletons: Array<Skeleton> = [];
  45. /**
  46. * The current model animation.
  47. * On init, this will be undefined.
  48. */
  49. public currentAnimation: IModelAnimation;
  50. /**
  51. * Observers registered here will be executed when the model is done loading
  52. */
  53. public onLoadedObservable: Observable<ViewerModel>;
  54. /**
  55. * Observers registered here will be executed when the loader notified of a progress event
  56. */
  57. public onLoadProgressObservable: Observable<SceneLoaderProgressEvent>;
  58. /**
  59. * Observers registered here will be executed when the loader notified of an error.
  60. */
  61. public onLoadErrorObservable: Observable<{ message: string; exception: any }>;
  62. /**
  63. * Will be executed after the model finished loading and complete, including entry animation and lod
  64. */
  65. public onCompleteObservable: Observable<ViewerModel>;
  66. /**
  67. * Observers registered here will be executed every time the model is being configured.
  68. * This can be used to extend the model's configuration without extending the class itself
  69. */
  70. public onAfterConfigure: Observable<ViewerModel>;
  71. /**
  72. * The current model state (loaded, error, etc)
  73. */
  74. public state: ModelState;
  75. /**
  76. * A loadID provided by the modelLoader, unique to ths (Abstract)Viewer instance.
  77. */
  78. public loadId: number;
  79. public loadInfo: GLTF2.IAsset;
  80. private _loadedUrl: string;
  81. private _modelConfiguration: IModelConfiguration;
  82. private _loaderDone: boolean = false;
  83. private _entryAnimation: ModelAnimationConfiguration;
  84. private _exitAnimation: ModelAnimationConfiguration;
  85. private _scaleTransition: Animation;
  86. private _animatables: Array<Animatable> = [];
  87. private _frameRate: number = 60;
  88. constructor(protected _viewer: AbstractViewer, modelConfiguration: IModelConfiguration) {
  89. this.onLoadedObservable = new Observable();
  90. this.onLoadErrorObservable = new Observable();
  91. this.onLoadProgressObservable = new Observable();
  92. this.onCompleteObservable = new Observable();
  93. this.onAfterConfigure = new Observable();
  94. this.state = ModelState.INIT;
  95. this.rootMesh = new AbstractMesh("modelRootMesh", this._viewer.sceneManager.scene);
  96. this._pivotMesh = new AbstractMesh("pivotMesh", this._viewer.sceneManager.scene);
  97. this._pivotMesh.parent = this.rootMesh;
  98. // rotate 180, gltf fun
  99. this._pivotMesh.rotation.y += Math.PI;
  100. this._scaleTransition = new Animation("scaleAnimation", "scaling", this._frameRate, Animation.ANIMATIONTYPE_VECTOR3, Animation.ANIMATIONLOOPMODE_CONSTANT);
  101. this._animations = [];
  102. //create a copy of the configuration to make sure it doesn't change even after it is changed in the viewer
  103. this._modelConfiguration = deepmerge(this._viewer.configuration.model || {}, modelConfiguration);
  104. this._viewer.sceneManager.models.push(this);
  105. this._viewer.onModelAddedObservable.notifyObservers(this);
  106. this.onLoadedObservable.add(() => {
  107. this.updateConfiguration(this._modelConfiguration);
  108. this._viewer.onModelLoadedObservable.notifyObservers(this);
  109. this._initAnimations();
  110. });
  111. this.onCompleteObservable.add(() => {
  112. this.state = ModelState.COMPLETE;
  113. });
  114. }
  115. /**
  116. * Is this model enabled?
  117. */
  118. public get enabled() {
  119. return this.rootMesh.isEnabled();
  120. }
  121. /**
  122. * Set whether this model is enabled or not.
  123. */
  124. public set enabled(enable: boolean) {
  125. this.rootMesh.setEnabled(enable);
  126. }
  127. public set loaderDone(done: boolean) {
  128. this._loaderDone = done;
  129. this._checkCompleteState();
  130. }
  131. private _checkCompleteState() {
  132. if (this._loaderDone && (this.state === ModelState.ENTRYDONE)) {
  133. this._modelComplete();
  134. }
  135. }
  136. /**
  137. * Get the viewer showing this model
  138. */
  139. public getViewer() {
  140. return this._viewer;
  141. }
  142. /**
  143. * Add a mesh to this model.
  144. * Any mesh that has no parent will be provided with the root mesh as its new parent.
  145. *
  146. * @param mesh the new mesh to add
  147. * @param triggerLoaded should this mesh trigger the onLoaded observable. Used when adding meshes manually.
  148. */
  149. public addMesh(mesh: AbstractMesh, triggerLoaded?: boolean) {
  150. if (!mesh.parent) {
  151. mesh.parent = this._pivotMesh;
  152. }
  153. mesh.receiveShadows = !!this.configuration.receiveShadows;
  154. this._meshes.push(mesh);
  155. if (triggerLoaded) {
  156. return this.onLoadedObservable.notifyObserversWithPromise(this);
  157. }
  158. }
  159. /**
  160. * get the list of meshes (excluding the root mesh)
  161. */
  162. public get meshes() {
  163. return this._meshes;
  164. }
  165. /**
  166. * Get the model's configuration
  167. */
  168. public get configuration(): IModelConfiguration {
  169. return this._modelConfiguration;
  170. }
  171. /**
  172. * (Re-)set the model's entire configuration
  173. * @param newConfiguration the new configuration to replace the new one
  174. */
  175. public set configuration(newConfiguration: IModelConfiguration) {
  176. this._modelConfiguration = newConfiguration;
  177. this._configureModel();
  178. }
  179. /**
  180. * Update the current configuration with new values.
  181. * Configuration will not be overwritten, but merged with the new configuration.
  182. * Priority is to the new configuration
  183. * @param newConfiguration the configuration to be merged into the current configuration;
  184. */
  185. public updateConfiguration(newConfiguration: Partial<IModelConfiguration>) {
  186. this._modelConfiguration = deepmerge(this._modelConfiguration, newConfiguration);
  187. this._configureModel();
  188. }
  189. private _initAnimations() {
  190. // check if this is not a gltf loader and init the animations
  191. if (this.skeletons.length) {
  192. this.skeletons.forEach((skeleton, idx) => {
  193. let ag = new AnimationGroup("animation-" + idx, this._viewer.sceneManager.scene);
  194. skeleton.getAnimatables().forEach(a => {
  195. if (a.animations[0]) {
  196. ag.addTargetedAnimation(a.animations[0], a);
  197. }
  198. });
  199. this.addAnimationGroup(ag);
  200. });
  201. }
  202. let completeCallback = () => {
  203. }
  204. if (this._modelConfiguration.animation) {
  205. if (this._modelConfiguration.animation.playOnce) {
  206. this._animations.forEach(a => {
  207. a.playMode = AnimationPlayMode.ONCE;
  208. });
  209. }
  210. if (this._modelConfiguration.animation.autoStart && this._animations.length) {
  211. let animationName = this._modelConfiguration.animation.autoStart === true ?
  212. this._animations[0].name : this._modelConfiguration.animation.autoStart;
  213. completeCallback = () => {
  214. this.playAnimation(animationName);
  215. }
  216. }
  217. }
  218. this._enterScene(completeCallback);
  219. }
  220. /**
  221. * Animates the model from the current position to the default position
  222. * @param completeCallback A function to call when the animation has completed
  223. */
  224. private _enterScene(completeCallback?: () => void): void {
  225. let callback = () => {
  226. this.state = ModelState.ENTRYDONE;
  227. this._viewer.sceneManager.animationBlendingEnabled = true;
  228. this._checkCompleteState();
  229. if (completeCallback) completeCallback();
  230. }
  231. if (!this._entryAnimation) {
  232. callback();
  233. return;
  234. }
  235. // disable blending for the sake of the entry animation;
  236. this._viewer.sceneManager.animationBlendingEnabled = false;
  237. this._applyAnimation(this._entryAnimation, true, callback);
  238. }
  239. /**
  240. * Animates the model from the current position to the exit-screen position
  241. * @param completeCallback A function to call when the animation has completed
  242. */
  243. private _exitScene(completeCallback: () => void): void {
  244. if (!this._exitAnimation) {
  245. completeCallback();
  246. return;
  247. }
  248. this._applyAnimation(this._exitAnimation, false, completeCallback);
  249. }
  250. private _modelComplete() {
  251. //reapply material defines to be sure:
  252. let meshes = this._pivotMesh.getChildMeshes(false);
  253. meshes.filter(m => m.material).forEach((mesh) => {
  254. this._applyModelMaterialConfiguration(mesh.material!);
  255. });
  256. this.state = ModelState.COMPLETE;
  257. this.onCompleteObservable.notifyObservers(this);
  258. }
  259. /**
  260. * Add a new animation group to this model.
  261. * @param animationGroup the new animation group to be added
  262. */
  263. public addAnimationGroup(animationGroup: AnimationGroup) {
  264. this._animations.push(new GroupModelAnimation(animationGroup));
  265. }
  266. /**
  267. * Get the ModelAnimation array
  268. */
  269. public getAnimations(): Array<IModelAnimation> {
  270. return this._animations;
  271. }
  272. /**
  273. * Get the animations' names. Using the names you can play a specific animation.
  274. */
  275. public getAnimationNames(): Array<string> {
  276. return this._animations.map(a => a.name);
  277. }
  278. /**
  279. * Get an animation by the provided name. Used mainly when playing n animation.
  280. * @param name the name of the animation to find
  281. */
  282. protected _getAnimationByName(name: string): Nullable<IModelAnimation> {
  283. // can't use .find, noe available on IE
  284. let filtered = this._animations.filter(a => a.name === name);
  285. // what the next line means - if two animations have the same name, they will not be returned!
  286. if (filtered.length === 1) {
  287. return filtered[0];
  288. } else {
  289. return null;
  290. }
  291. }
  292. /**
  293. * Choose an initialized animation using its name and start playing it
  294. * @param name the name of the animation to play
  295. * @returns The model aniamtion to be played.
  296. */
  297. public playAnimation(name: string): IModelAnimation {
  298. let animation = this.setCurrentAnimationByName(name);
  299. if (animation) {
  300. animation.start();
  301. }
  302. return animation;
  303. }
  304. public setCurrentAnimationByName(name: string) {
  305. let animation = this._getAnimationByName(name);
  306. if (animation) {
  307. if (this.currentAnimation && this.currentAnimation.state !== AnimationState.STOPPED) {
  308. this.currentAnimation.stop();
  309. }
  310. this.currentAnimation = animation;
  311. return animation;
  312. } else {
  313. throw new Error("animation not found - " + name);
  314. }
  315. }
  316. private _configureModel() {
  317. // this can be changed to the meshes that have rootMesh a parent without breaking anything.
  318. let meshesWithNoParent: Array<AbstractMesh> = [this.rootMesh] //this._meshes.filter(m => m.parent === this.rootMesh);
  319. let updateMeshesWithNoParent = (variable: string, value: any, param?: string) => {
  320. meshesWithNoParent.forEach(mesh => {
  321. if (param) {
  322. mesh[variable][param] = value;
  323. } else {
  324. mesh[variable] = value;
  325. }
  326. });
  327. }
  328. let updateXYZ = (variable: string, configValues: { x: number, y: number, z: number, w?: number }) => {
  329. if (configValues.x !== undefined) {
  330. updateMeshesWithNoParent(variable, configValues.x, 'x');
  331. }
  332. if (configValues.y !== undefined) {
  333. updateMeshesWithNoParent(variable, configValues.y, 'y');
  334. }
  335. if (configValues.z !== undefined) {
  336. updateMeshesWithNoParent(variable, configValues.z, 'z');
  337. }
  338. if (configValues.w !== undefined) {
  339. updateMeshesWithNoParent(variable, configValues.w, 'w');
  340. }
  341. }
  342. if (this._modelConfiguration.normalize) {
  343. let center = false;
  344. let unitSize = false;
  345. let parentIndex;
  346. if (this._modelConfiguration.normalize === true) {
  347. center = true;
  348. unitSize = true;
  349. } else {
  350. center = !!this._modelConfiguration.normalize.center;
  351. unitSize = !!this._modelConfiguration.normalize.unitSize;
  352. parentIndex = this._modelConfiguration.normalize.parentIndex;
  353. }
  354. let meshesToNormalize: Array<AbstractMesh> = [];
  355. if (parentIndex !== undefined) {
  356. meshesToNormalize.push(this._meshes[parentIndex]);
  357. } else {
  358. meshesToNormalize = this._pivotMesh.getChildMeshes(true).length === 1 ? [this._pivotMesh] : meshesWithNoParent;
  359. }
  360. if (unitSize) {
  361. meshesToNormalize.forEach(mesh => {
  362. mesh.normalizeToUnitCube(true);
  363. mesh.computeWorldMatrix(true);
  364. });
  365. }
  366. if (center) {
  367. meshesToNormalize.forEach(mesh => {
  368. const boundingInfo = mesh.getHierarchyBoundingVectors(true);
  369. const sizeVec = boundingInfo.max.subtract(boundingInfo.min);
  370. const halfSizeVec = sizeVec.scale(0.5);
  371. const center = boundingInfo.min.add(halfSizeVec);
  372. mesh.position = center.scale(-1);
  373. mesh.position.y += halfSizeVec.y;
  374. // Recompute Info.
  375. mesh.computeWorldMatrix(true);
  376. });
  377. }
  378. } else {
  379. // if centered, should be done here
  380. }
  381. // position?
  382. if (this._modelConfiguration.position) {
  383. updateXYZ('position', this._modelConfiguration.position);
  384. }
  385. if (this._modelConfiguration.rotation) {
  386. //quaternion?
  387. if (this._modelConfiguration.rotation.w) {
  388. meshesWithNoParent.forEach(mesh => {
  389. if (!mesh.rotationQuaternion) {
  390. mesh.rotationQuaternion = new Quaternion();
  391. }
  392. })
  393. updateXYZ('rotationQuaternion', this._modelConfiguration.rotation);
  394. } else {
  395. updateXYZ('rotation', this._modelConfiguration.rotation);
  396. }
  397. }
  398. if (this._modelConfiguration.rotationOffsetAxis) {
  399. let rotationAxis = new Vector3(0, 0, 0).copyFrom(this._modelConfiguration.rotationOffsetAxis as Vector3);
  400. meshesWithNoParent.forEach(m => {
  401. if (this._modelConfiguration.rotationOffsetAngle) {
  402. m.rotate(rotationAxis, this._modelConfiguration.rotationOffsetAngle);
  403. }
  404. });
  405. }
  406. if (this._modelConfiguration.scaling) {
  407. updateXYZ('scaling', this._modelConfiguration.scaling);
  408. }
  409. if (this._modelConfiguration.castShadow) {
  410. this._meshes.forEach(mesh => {
  411. Tags.AddTagsTo(mesh, 'castShadow');
  412. });
  413. }
  414. let meshes = this._pivotMesh.getChildMeshes(false);
  415. meshes.filter(m => m.material).forEach((mesh) => {
  416. this._applyModelMaterialConfiguration(mesh.material!);
  417. });
  418. if (this._modelConfiguration.entryAnimation) {
  419. this._entryAnimation = this._modelAnimationConfigurationToObject(this._modelConfiguration.entryAnimation);
  420. }
  421. if (this._modelConfiguration.exitAnimation) {
  422. this._exitAnimation = this._modelAnimationConfigurationToObject(this._modelConfiguration.exitAnimation);
  423. }
  424. this.onAfterConfigure.notifyObservers(this);
  425. }
  426. private _modelAnimationConfigurationToObject(animConfig: IModelAnimationConfiguration): ModelAnimationConfiguration {
  427. let anim: ModelAnimationConfiguration = {
  428. time: 0.5
  429. };
  430. if (animConfig.scaling) {
  431. anim.scaling = Vector3.Zero();
  432. }
  433. if (animConfig.easingFunction !== undefined) {
  434. anim.easingFunction = animConfig.easingFunction;
  435. }
  436. if (animConfig.easingMode !== undefined) {
  437. anim.easingMode = animConfig.easingMode;
  438. }
  439. extendClassWithConfig(anim, animConfig);
  440. return anim;
  441. }
  442. /**
  443. * Apply a material configuration to a material
  444. * @param material Material to apply configuration to
  445. */
  446. public _applyModelMaterialConfiguration(material: Material) {
  447. if (!this._modelConfiguration.material) return;
  448. extendClassWithConfig(material, this._modelConfiguration.material);
  449. if (material instanceof PBRMaterial) {
  450. if (this._modelConfiguration.material.directIntensity !== undefined) {
  451. material.directIntensity = this._modelConfiguration.material.directIntensity;
  452. }
  453. if (this._modelConfiguration.material.emissiveIntensity !== undefined) {
  454. material.emissiveIntensity = this._modelConfiguration.material.emissiveIntensity;
  455. }
  456. if (this._modelConfiguration.material.environmentIntensity !== undefined) {
  457. material.environmentIntensity = this._modelConfiguration.material.environmentIntensity;
  458. }
  459. if (this._modelConfiguration.material.directEnabled !== undefined) {
  460. material.disableLighting = !this._modelConfiguration.material.directEnabled;
  461. }
  462. if (this._viewer.sceneManager.reflectionColor) {
  463. material.reflectionColor = this._viewer.sceneManager.reflectionColor;
  464. }
  465. }
  466. else if (material instanceof MultiMaterial) {
  467. for (let i = 0; i < material.subMaterials.length; i++) {
  468. const subMaterial = material.subMaterials[i];
  469. if (subMaterial) {
  470. this._applyModelMaterialConfiguration(subMaterial);
  471. }
  472. }
  473. }
  474. }
  475. /**
  476. * Start entry/exit animation given an animation configuration
  477. * @param animationConfiguration Entry/Exit animation configuration
  478. * @param isEntry Pass true if the animation is an entry animation
  479. * @param completeCallback Callback to execute when the animation completes
  480. */
  481. private _applyAnimation(animationConfiguration: ModelAnimationConfiguration, isEntry: boolean, completeCallback?: () => void) {
  482. let animations: Animation[] = [];
  483. //scale
  484. if (animationConfiguration.scaling) {
  485. let scaleStart: Vector3 = isEntry ? animationConfiguration.scaling : new Vector3(1, 1, 1);
  486. let scaleEnd: Vector3 = isEntry ? new Vector3(1, 1, 1) : animationConfiguration.scaling;
  487. if (!scaleStart.equals(scaleEnd)) {
  488. this.rootMesh.scaling = scaleStart;
  489. this._setLinearKeys(
  490. this._scaleTransition,
  491. this.rootMesh.scaling,
  492. scaleEnd,
  493. animationConfiguration.time
  494. );
  495. animations.push(this._scaleTransition);
  496. }
  497. }
  498. //Start the animation(s)
  499. this.transitionTo(
  500. animations,
  501. animationConfiguration.time,
  502. this._createEasingFunction(animationConfiguration.easingFunction),
  503. animationConfiguration.easingMode,
  504. () => { if (completeCallback) completeCallback(); }
  505. );
  506. }
  507. /**
  508. * Begin @animations with the specified @easingFunction
  509. * @param animations The BABYLON Animations to begin
  510. * @param duration of transition, in seconds
  511. * @param easingFunction An easing function to apply
  512. * @param easingMode A easing mode to apply to the easingFunction
  513. * @param onAnimationEnd Call back trigger at the end of the animation.
  514. */
  515. public transitionTo(
  516. animations: Animation[],
  517. duration: number,
  518. easingFunction: any,
  519. easingMode: number = BABYLON.EasingFunction.EASINGMODE_EASEINOUT,
  520. onAnimationEnd: () => void): void {
  521. if (easingFunction) {
  522. for (let animation of animations) {
  523. easingFunction.setEasingMode(easingMode);
  524. animation.setEasingFunction(easingFunction);
  525. }
  526. }
  527. //Stop any current animations before starting the new one - merging not yet supported.
  528. this.stopAllAnimations();
  529. this.rootMesh.animations = animations;
  530. if (this._viewer.sceneManager.scene.beginAnimation) {
  531. let animatable: Animatable = this._viewer.sceneManager.scene.beginAnimation(this.rootMesh, 0, this._frameRate * duration, false, 1, () => {
  532. if (onAnimationEnd) {
  533. onAnimationEnd();
  534. }
  535. });
  536. this._animatables.push(animatable);
  537. }
  538. }
  539. /**
  540. * Sets key values on an Animation from first to last frame.
  541. * @param animation The Babylon animation object to set keys on
  542. * @param startValue The value of the first key
  543. * @param endValue The value of the last key
  544. * @param duration The duration of the animation, used to determine the end frame
  545. */
  546. private _setLinearKeys(animation: Animation, startValue: any, endValue: any, duration: number) {
  547. animation.setKeys([
  548. {
  549. frame: 0,
  550. value: startValue
  551. },
  552. {
  553. frame: this._frameRate * duration,
  554. value: endValue
  555. }
  556. ]);
  557. }
  558. /**
  559. * Creates and returns a Babylon easing funtion object based on a string representing the Easing function
  560. * @param easingFunctionID The enum of the easing funtion to create
  561. * @return The newly created Babylon easing function object
  562. */
  563. private _createEasingFunction(easingFunctionID?: number): any {
  564. let easingFunction;
  565. switch (easingFunctionID) {
  566. case EasingFunction.CircleEase:
  567. easingFunction = new CircleEase();
  568. break;
  569. case EasingFunction.BackEase:
  570. easingFunction = new BackEase(0.3);
  571. break;
  572. case EasingFunction.BounceEase:
  573. easingFunction = new BounceEase();
  574. break;
  575. case EasingFunction.CubicEase:
  576. easingFunction = new CubicEase();
  577. break;
  578. case EasingFunction.ElasticEase:
  579. easingFunction = new ElasticEase();
  580. break;
  581. case EasingFunction.ExponentialEase:
  582. easingFunction = new ExponentialEase();
  583. break;
  584. case EasingFunction.PowerEase:
  585. easingFunction = new PowerEase();
  586. break;
  587. case EasingFunction.QuadraticEase:
  588. easingFunction = new QuadraticEase();
  589. break;
  590. case EasingFunction.QuarticEase:
  591. easingFunction = new QuarticEase();
  592. break;
  593. case EasingFunction.QuinticEase:
  594. easingFunction = new QuinticEase();
  595. break;
  596. case EasingFunction.SineEase:
  597. easingFunction = new SineEase();
  598. break;
  599. default:
  600. Tools.Log("No ease function found");
  601. break;
  602. }
  603. return easingFunction;
  604. }
  605. /**
  606. * Stops and removes all animations that have been applied to the model
  607. */
  608. public stopAllAnimations(): void {
  609. if (this.rootMesh) {
  610. this.rootMesh.animations = [];
  611. }
  612. if (this.currentAnimation) {
  613. this.currentAnimation.stop();
  614. }
  615. while (this._animatables.length) {
  616. this._animatables[0].onAnimationEnd = null;
  617. this._animatables[0].stop();
  618. this._animatables.shift();
  619. }
  620. }
  621. /**
  622. * Will remove this model from the viewer (but NOT dispose it).
  623. */
  624. public remove() {
  625. this.stopAllAnimations();
  626. this._viewer.sceneManager.models.splice(this._viewer.sceneManager.models.indexOf(this), 1);
  627. // hide it
  628. this.rootMesh.isVisible = false;
  629. this._viewer.onModelRemovedObservable.notifyObservers(this);
  630. }
  631. /**
  632. * Dispose this model, including all of its associated assets.
  633. */
  634. public dispose() {
  635. this.remove();
  636. this.onAfterConfigure.clear();
  637. this.onLoadedObservable.clear();
  638. this.onLoadErrorObservable.clear();
  639. this.onLoadProgressObservable.clear();
  640. if (this.loader && this.loader.name === "gltf") {
  641. (<GLTFFileLoader>this.loader).dispose();
  642. }
  643. this.particleSystems.forEach(ps => ps.dispose());
  644. this.particleSystems.length = 0;
  645. this.skeletons.forEach(s => s.dispose());
  646. this.skeletons.length = 0;
  647. this._animations.forEach(ag => ag.dispose());
  648. this._animations.length = 0;
  649. this.rootMesh.dispose(false, true);
  650. }
  651. }