viewerModel.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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 } 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._getAnimationByName(name);
  299. if (animation) {
  300. if (this.currentAnimation) {
  301. this.currentAnimation.stop();
  302. }
  303. this.currentAnimation = animation;
  304. animation.start();
  305. return animation;
  306. } else {
  307. throw new Error("animation not found - " + name);
  308. }
  309. }
  310. private _configureModel() {
  311. // this can be changed to the meshes that have rootMesh a parent without breaking anything.
  312. let meshesWithNoParent: Array<AbstractMesh> = [this.rootMesh] //this._meshes.filter(m => m.parent === this.rootMesh);
  313. let updateMeshesWithNoParent = (variable: string, value: any, param?: string) => {
  314. meshesWithNoParent.forEach(mesh => {
  315. if (param) {
  316. mesh[variable][param] = value;
  317. } else {
  318. mesh[variable] = value;
  319. }
  320. });
  321. }
  322. let updateXYZ = (variable: string, configValues: { x: number, y: number, z: number, w?: number }) => {
  323. if (configValues.x !== undefined) {
  324. updateMeshesWithNoParent(variable, configValues.x, 'x');
  325. }
  326. if (configValues.y !== undefined) {
  327. updateMeshesWithNoParent(variable, configValues.y, 'y');
  328. }
  329. if (configValues.z !== undefined) {
  330. updateMeshesWithNoParent(variable, configValues.z, 'z');
  331. }
  332. if (configValues.w !== undefined) {
  333. updateMeshesWithNoParent(variable, configValues.w, 'w');
  334. }
  335. }
  336. if (this._modelConfiguration.normalize) {
  337. let center = false;
  338. let unitSize = false;
  339. let parentIndex;
  340. if (this._modelConfiguration.normalize === true) {
  341. center = true;
  342. unitSize = true;
  343. } else {
  344. center = !!this._modelConfiguration.normalize.center;
  345. unitSize = !!this._modelConfiguration.normalize.unitSize;
  346. parentIndex = this._modelConfiguration.normalize.parentIndex;
  347. }
  348. let meshesToNormalize: Array<AbstractMesh> = [];
  349. if (parentIndex !== undefined) {
  350. meshesToNormalize.push(this._meshes[parentIndex]);
  351. } else {
  352. meshesToNormalize = this._pivotMesh.getChildMeshes(true).length === 1 ? [this._pivotMesh] : meshesWithNoParent;
  353. }
  354. if (unitSize) {
  355. meshesToNormalize.forEach(mesh => {
  356. mesh.normalizeToUnitCube(true);
  357. mesh.computeWorldMatrix(true);
  358. });
  359. }
  360. if (center) {
  361. meshesToNormalize.forEach(mesh => {
  362. const boundingInfo = mesh.getHierarchyBoundingVectors(true);
  363. const sizeVec = boundingInfo.max.subtract(boundingInfo.min);
  364. const halfSizeVec = sizeVec.scale(0.5);
  365. const center = boundingInfo.min.add(halfSizeVec);
  366. mesh.position = center.scale(-1);
  367. mesh.position.y += halfSizeVec.y;
  368. // Recompute Info.
  369. mesh.computeWorldMatrix(true);
  370. });
  371. }
  372. } else {
  373. // if centered, should be done here
  374. }
  375. // position?
  376. if (this._modelConfiguration.position) {
  377. updateXYZ('position', this._modelConfiguration.position);
  378. }
  379. if (this._modelConfiguration.rotation) {
  380. //quaternion?
  381. if (this._modelConfiguration.rotation.w) {
  382. meshesWithNoParent.forEach(mesh => {
  383. if (!mesh.rotationQuaternion) {
  384. mesh.rotationQuaternion = new Quaternion();
  385. }
  386. })
  387. updateXYZ('rotationQuaternion', this._modelConfiguration.rotation);
  388. } else {
  389. updateXYZ('rotation', this._modelConfiguration.rotation);
  390. }
  391. }
  392. if (this._modelConfiguration.rotationOffsetAxis) {
  393. let rotationAxis = new Vector3(0, 0, 0).copyFrom(this._modelConfiguration.rotationOffsetAxis as Vector3);
  394. meshesWithNoParent.forEach(m => {
  395. if (this._modelConfiguration.rotationOffsetAngle) {
  396. m.rotate(rotationAxis, this._modelConfiguration.rotationOffsetAngle);
  397. }
  398. });
  399. }
  400. if (this._modelConfiguration.scaling) {
  401. updateXYZ('scaling', this._modelConfiguration.scaling);
  402. }
  403. if (this._modelConfiguration.castShadow) {
  404. this._meshes.forEach(mesh => {
  405. Tags.AddTagsTo(mesh, 'castShadow');
  406. });
  407. }
  408. let meshes = this._pivotMesh.getChildMeshes(false);
  409. meshes.filter(m => m.material).forEach((mesh) => {
  410. this._applyModelMaterialConfiguration(mesh.material!);
  411. });
  412. if (this._modelConfiguration.entryAnimation) {
  413. this._entryAnimation = this._modelAnimationConfigurationToObject(this._modelConfiguration.entryAnimation);
  414. }
  415. if (this._modelConfiguration.exitAnimation) {
  416. this._exitAnimation = this._modelAnimationConfigurationToObject(this._modelConfiguration.exitAnimation);
  417. }
  418. this.onAfterConfigure.notifyObservers(this);
  419. }
  420. private _modelAnimationConfigurationToObject(animConfig: IModelAnimationConfiguration): ModelAnimationConfiguration {
  421. let anim: ModelAnimationConfiguration = {
  422. time: 0.5
  423. };
  424. if (animConfig.scaling) {
  425. anim.scaling = Vector3.Zero();
  426. }
  427. if (animConfig.easingFunction !== undefined) {
  428. anim.easingFunction = animConfig.easingFunction;
  429. }
  430. if (animConfig.easingMode !== undefined) {
  431. anim.easingMode = animConfig.easingMode;
  432. }
  433. extendClassWithConfig(anim, animConfig);
  434. return anim;
  435. }
  436. /**
  437. * Apply a material configuration to a material
  438. * @param material Material to apply configuration to
  439. */
  440. private _applyModelMaterialConfiguration(material: Material) {
  441. if (!this._modelConfiguration.material) return;
  442. extendClassWithConfig(material, this._modelConfiguration.material);
  443. if (material instanceof PBRMaterial) {
  444. if (this._modelConfiguration.material.directIntensity !== undefined) {
  445. material.directIntensity = this._modelConfiguration.material.directIntensity;
  446. }
  447. if (this._modelConfiguration.material.emissiveIntensity !== undefined) {
  448. material.emissiveIntensity = this._modelConfiguration.material.emissiveIntensity;
  449. }
  450. if (this._modelConfiguration.material.environmentIntensity !== undefined) {
  451. material.environmentIntensity = this._modelConfiguration.material.environmentIntensity;
  452. }
  453. if (this._modelConfiguration.material.directEnabled !== undefined) {
  454. material.disableLighting = !this._modelConfiguration.material.directEnabled;
  455. }
  456. if (this._viewer.sceneManager.reflectionColor) {
  457. material.reflectionColor = this._viewer.sceneManager.reflectionColor;
  458. }
  459. }
  460. else if (material instanceof MultiMaterial) {
  461. for (let i = 0; i < material.subMaterials.length; i++) {
  462. const subMaterial = material.subMaterials[i];
  463. if (subMaterial) {
  464. this._applyModelMaterialConfiguration(subMaterial);
  465. }
  466. }
  467. }
  468. }
  469. /**
  470. * Start entry/exit animation given an animation configuration
  471. * @param animationConfiguration Entry/Exit animation configuration
  472. * @param isEntry Pass true if the animation is an entry animation
  473. * @param completeCallback Callback to execute when the animation completes
  474. */
  475. private _applyAnimation(animationConfiguration: ModelAnimationConfiguration, isEntry: boolean, completeCallback?: () => void) {
  476. let animations: Animation[] = [];
  477. //scale
  478. if (animationConfiguration.scaling) {
  479. let scaleStart: Vector3 = isEntry ? animationConfiguration.scaling : new Vector3(1, 1, 1);
  480. let scaleEnd: Vector3 = isEntry ? new Vector3(1, 1, 1) : animationConfiguration.scaling;
  481. if (!scaleStart.equals(scaleEnd)) {
  482. this.rootMesh.scaling = scaleStart;
  483. this._setLinearKeys(
  484. this._scaleTransition,
  485. this.rootMesh.scaling,
  486. scaleEnd,
  487. animationConfiguration.time
  488. );
  489. animations.push(this._scaleTransition);
  490. }
  491. }
  492. //Start the animation(s)
  493. this.transitionTo(
  494. animations,
  495. animationConfiguration.time,
  496. this._createEasingFunction(animationConfiguration.easingFunction),
  497. animationConfiguration.easingMode,
  498. () => { if (completeCallback) completeCallback(); }
  499. );
  500. }
  501. /**
  502. * Begin @animations with the specified @easingFunction
  503. * @param animations The BABYLON Animations to begin
  504. * @param duration of transition, in seconds
  505. * @param easingFunction An easing function to apply
  506. * @param easingMode A easing mode to apply to the easingFunction
  507. * @param onAnimationEnd Call back trigger at the end of the animation.
  508. */
  509. public transitionTo(
  510. animations: Animation[],
  511. duration: number,
  512. easingFunction: any,
  513. easingMode: number = BABYLON.EasingFunction.EASINGMODE_EASEINOUT,
  514. onAnimationEnd: () => void): void {
  515. if (easingFunction) {
  516. for (let animation of animations) {
  517. easingFunction.setEasingMode(easingMode);
  518. animation.setEasingFunction(easingFunction);
  519. }
  520. }
  521. //Stop any current animations before starting the new one - merging not yet supported.
  522. this.stopAllAnimations();
  523. this.rootMesh.animations = animations;
  524. if (this._viewer.sceneManager.scene.beginAnimation) {
  525. let animatable: Animatable = this._viewer.sceneManager.scene.beginAnimation(this.rootMesh, 0, this._frameRate * duration, false, 1, () => {
  526. if (onAnimationEnd) {
  527. onAnimationEnd();
  528. }
  529. });
  530. this._animatables.push(animatable);
  531. }
  532. }
  533. /**
  534. * Sets key values on an Animation from first to last frame.
  535. * @param animation The Babylon animation object to set keys on
  536. * @param startValue The value of the first key
  537. * @param endValue The value of the last key
  538. * @param duration The duration of the animation, used to determine the end frame
  539. */
  540. private _setLinearKeys(animation: Animation, startValue: any, endValue: any, duration: number) {
  541. animation.setKeys([
  542. {
  543. frame: 0,
  544. value: startValue
  545. },
  546. {
  547. frame: this._frameRate * duration,
  548. value: endValue
  549. }
  550. ]);
  551. }
  552. /**
  553. * Creates and returns a Babylon easing funtion object based on a string representing the Easing function
  554. * @param easingFunctionID The enum of the easing funtion to create
  555. * @return The newly created Babylon easing function object
  556. */
  557. private _createEasingFunction(easingFunctionID?: number): any {
  558. let easingFunction;
  559. switch (easingFunctionID) {
  560. case EasingFunction.CircleEase:
  561. easingFunction = new CircleEase();
  562. break;
  563. case EasingFunction.BackEase:
  564. easingFunction = new BackEase(0.3);
  565. break;
  566. case EasingFunction.BounceEase:
  567. easingFunction = new BounceEase();
  568. break;
  569. case EasingFunction.CubicEase:
  570. easingFunction = new CubicEase();
  571. break;
  572. case EasingFunction.ElasticEase:
  573. easingFunction = new ElasticEase();
  574. break;
  575. case EasingFunction.ExponentialEase:
  576. easingFunction = new ExponentialEase();
  577. break;
  578. case EasingFunction.PowerEase:
  579. easingFunction = new PowerEase();
  580. break;
  581. case EasingFunction.QuadraticEase:
  582. easingFunction = new QuadraticEase();
  583. break;
  584. case EasingFunction.QuarticEase:
  585. easingFunction = new QuarticEase();
  586. break;
  587. case EasingFunction.QuinticEase:
  588. easingFunction = new QuinticEase();
  589. break;
  590. case EasingFunction.SineEase:
  591. easingFunction = new SineEase();
  592. break;
  593. default:
  594. Tools.Log("No ease function found");
  595. break;
  596. }
  597. return easingFunction;
  598. }
  599. /**
  600. * Stops and removes all animations that have been applied to the model
  601. */
  602. public stopAllAnimations(): void {
  603. if (this.rootMesh) {
  604. this.rootMesh.animations = [];
  605. }
  606. if (this.currentAnimation) {
  607. this.currentAnimation.stop();
  608. }
  609. while (this._animatables.length) {
  610. this._animatables[0].onAnimationEnd = null;
  611. this._animatables[0].stop();
  612. this._animatables.shift();
  613. }
  614. }
  615. /**
  616. * Will remove this model from the viewer (but NOT dispose it).
  617. */
  618. public remove() {
  619. this.stopAllAnimations();
  620. this._viewer.sceneManager.models.splice(this._viewer.sceneManager.models.indexOf(this), 1);
  621. // hide it
  622. this.rootMesh.isVisible = false;
  623. this._viewer.onModelRemovedObservable.notifyObservers(this);
  624. }
  625. /**
  626. * Dispose this model, including all of its associated assets.
  627. */
  628. public dispose() {
  629. this.remove();
  630. this.onAfterConfigure.clear();
  631. this.onLoadedObservable.clear();
  632. this.onLoadErrorObservable.clear();
  633. this.onLoadProgressObservable.clear();
  634. if (this.loader && this.loader.name === "gltf") {
  635. (<GLTFFileLoader>this.loader).dispose();
  636. }
  637. this.particleSystems.forEach(ps => ps.dispose());
  638. this.particleSystems.length = 0;
  639. this.skeletons.forEach(s => s.dispose());
  640. this.skeletons.length = 0;
  641. this._animations.forEach(ag => ag.dispose());
  642. this._animations.length = 0;
  643. this._meshes.forEach(m => m.dispose());
  644. this._meshes.length = 0;
  645. this.rootMesh.dispose();
  646. }
  647. }