viewer.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. import { viewerManager } from './viewerManager';
  2. import { SceneManager } from './sceneManager';
  3. import { TemplateManager } from './../templateManager';
  4. import { ConfigurationLoader } from './../configuration/loader';
  5. import { Skeleton, AnimationGroup, ParticleSystem, CubeTexture, Color3, IEnvironmentHelperOptions, EnvironmentHelper, Effect, SceneOptimizer, SceneOptimizerOptions, Observable, Engine, Scene, ArcRotateCamera, Vector3, SceneLoader, AbstractMesh, Mesh, HemisphericLight, Database, SceneLoaderProgressEvent, ISceneLoaderPlugin, ISceneLoaderPluginAsync, Quaternion, Light, ShadowLight, ShadowGenerator, Tags, AutoRotationBehavior, BouncingBehavior, FramingBehavior, Behavior, Tools, RenderingManager } from 'babylonjs';
  6. import { ViewerConfiguration, ISceneConfiguration, ISceneOptimizerConfiguration, IObserversConfiguration, IModelConfiguration, ISkyboxConfiguration, IGroundConfiguration, ILightConfiguration, ICameraConfiguration } from '../configuration/configuration';
  7. import * as deepmerge from '../../assets/deepmerge.min.js';
  8. import { ViewerModel } from '../model/viewerModel';
  9. import { GroupModelAnimation } from '../model/modelAnimation';
  10. import { ModelLoader } from '../loader/modelLoader';
  11. import { CameraBehavior } from '../interfaces';
  12. import { viewerGlobals } from '../configuration/globals';
  13. import { extendClassWithConfig } from '../helper';
  14. import { telemetryManager } from '../telemetryManager';
  15. import { Version } from '..';
  16. /**
  17. * The AbstractViewr is the center of Babylon's viewer.
  18. * It is the basic implementation of the default viewer and is responsible of loading and showing the model and the templates
  19. */
  20. export abstract class AbstractViewer {
  21. /**
  22. * The corresponsing template manager of this viewer.
  23. */
  24. public templateManager: TemplateManager;
  25. /**
  26. * Babylon Engine corresponding with this viewer
  27. */
  28. public engine: Engine;
  29. /**
  30. * The ID of this viewer. it will be generated randomly or use the HTML Element's ID.
  31. */
  32. public readonly baseId: string;
  33. /**
  34. * The last loader used to load a model.
  35. * @deprecated
  36. */
  37. public lastUsedLoader: ISceneLoaderPlugin | ISceneLoaderPluginAsync;
  38. /**
  39. * The ModelLoader instance connected with this viewer.
  40. */
  41. public modelLoader: ModelLoader;
  42. /**
  43. * A flag that controls whether or not the render loop should be executed
  44. */
  45. public runRenderLoop: boolean = true;
  46. /**
  47. * The scene manager connected with this viewer instance
  48. */
  49. public sceneManager: SceneManager;
  50. /**
  51. * the viewer configuration object
  52. */
  53. protected _configuration: ViewerConfiguration;
  54. // observables
  55. /**
  56. * Will notify when the scene was initialized
  57. */
  58. public onSceneInitObservable: Observable<Scene>;
  59. /**
  60. * will notify when the engine was initialized
  61. */
  62. public onEngineInitObservable: Observable<Engine>;
  63. /**
  64. * Will notify when a new model was added to the scene.
  65. * Note that added does not neccessarily mean loaded!
  66. */
  67. public onModelAddedObservable: Observable<ViewerModel>;
  68. /**
  69. * will notify after every model load
  70. */
  71. public onModelLoadedObservable: Observable<ViewerModel>;
  72. /**
  73. * will notify when any model notify of progress
  74. */
  75. public onModelLoadProgressObservable: Observable<SceneLoaderProgressEvent>;
  76. /**
  77. * will notify when any model load failed.
  78. */
  79. public onModelLoadErrorObservable: Observable<{ message: string; exception: any }>;
  80. /**
  81. * Will notify when a model was removed from the scene;
  82. */
  83. public onModelRemovedObservable: Observable<ViewerModel>;
  84. /**
  85. * will notify when a new loader was initialized.
  86. * Used mainly to know when a model starts loading.
  87. */
  88. public onLoaderInitObservable: Observable<ISceneLoaderPlugin | ISceneLoaderPluginAsync>;
  89. /**
  90. * Observers registered here will be executed when the entire load process has finished.
  91. */
  92. public onInitDoneObservable: Observable<AbstractViewer>;
  93. /**
  94. * Functions added to this observable will be executed on each frame rendered.
  95. */
  96. public onFrameRenderedObservable: Observable<AbstractViewer>;
  97. /**
  98. * The canvas associated with this viewer
  99. */
  100. protected _canvas: HTMLCanvasElement;
  101. /**
  102. * The (single) canvas of this viewer
  103. */
  104. public get canvas(): HTMLCanvasElement {
  105. return this._canvas;
  106. }
  107. /**
  108. * is this viewer disposed?
  109. */
  110. protected _isDisposed: boolean = false;
  111. /**
  112. * registered onBeforeRender functions.
  113. * This functions are also registered at the native scene. The reference can be used to unregister them.
  114. */
  115. protected _registeredOnBeforeRenderFunctions: Array<() => void>;
  116. /**
  117. * The configuration loader of this viewer
  118. */
  119. protected _configurationLoader: ConfigurationLoader;
  120. protected _isInit: boolean;
  121. constructor(public containerElement: HTMLElement, initialConfiguration: ViewerConfiguration = {}) {
  122. // if exists, use the container id. otherwise, generate a random string.
  123. if (containerElement.id) {
  124. this.baseId = containerElement.id;
  125. } else {
  126. this.baseId = containerElement.id = 'bjs' + Math.random().toString(32).substr(2, 8);
  127. }
  128. this.onSceneInitObservable = new Observable();
  129. this.onEngineInitObservable = new Observable();
  130. this.onModelLoadedObservable = new Observable();
  131. this.onModelLoadProgressObservable = new Observable();
  132. this.onModelLoadErrorObservable = new Observable();
  133. this.onModelAddedObservable = new Observable();
  134. this.onModelRemovedObservable = new Observable();
  135. this.onInitDoneObservable = new Observable();
  136. this.onLoaderInitObservable = new Observable();
  137. this.onFrameRenderedObservable = new Observable();
  138. this._registeredOnBeforeRenderFunctions = [];
  139. this.modelLoader = new ModelLoader(this);
  140. // add this viewer to the viewer manager
  141. viewerManager.addViewer(this);
  142. // create a new template manager. TODO - singleton?
  143. this.templateManager = new TemplateManager(containerElement);
  144. this.sceneManager = new SceneManager(this);
  145. this._prepareContainerElement();
  146. RenderingManager.AUTOCLEAR = false;
  147. // extend the configuration
  148. this._configurationLoader = new ConfigurationLoader();
  149. this._configurationLoader.loadConfiguration(initialConfiguration, (configuration) => {
  150. this._configuration = deepmerge(this._configuration || {}, configuration);
  151. if (this._configuration.observers) {
  152. this._configureObservers(this._configuration.observers);
  153. }
  154. if (this._configuration.loaderPlugins) {
  155. // TODO should plugins be removed?
  156. Object.keys(this._configuration.loaderPlugins).forEach((name => {
  157. if (this._configuration.loaderPlugins && this._configuration.loaderPlugins[name]) {
  158. this.modelLoader.addPlugin(name);
  159. }
  160. }))
  161. }
  162. // initialize the templates
  163. let templateConfiguration = this._configuration.templates || {};
  164. this.templateManager.initTemplate(templateConfiguration);
  165. // when done, execute onTemplatesLoaded()
  166. this.templateManager.onAllLoaded.add(() => {
  167. let canvas = this.templateManager.getCanvas();
  168. if (canvas) {
  169. this._canvas = canvas;
  170. }
  171. this._onTemplateLoaded();
  172. });
  173. });
  174. this.onModelLoadedObservable.add((model) => {
  175. //this.updateConfiguration(this._configuration, model);
  176. });
  177. this.onSceneInitObservable.add(() => {
  178. this.updateConfiguration();
  179. });
  180. this.onInitDoneObservable.add(() => {
  181. this._isInit = true;
  182. //this.sceneManager.scene.executeWhenReady(() => {
  183. this.engine.runRenderLoop(this._render);
  184. //});
  185. });
  186. }
  187. /**
  188. * get the baseId of this viewer
  189. */
  190. public getBaseId(): string {
  191. return this.baseId;
  192. }
  193. /**
  194. * Do we have a canvas to render on, and is it a part of the scene
  195. */
  196. public isCanvasInDOM(): boolean {
  197. return !!this._canvas && !!this._canvas.parentElement;
  198. }
  199. /**
  200. * Is the engine currently set to rende even when the page is in background
  201. */
  202. public get renderInBackground() {
  203. return this.engine.renderEvenInBackground;
  204. }
  205. /**
  206. * Set the viewer's background rendering flag.
  207. */
  208. public set renderInBackground(value: boolean) {
  209. this.engine.renderEvenInBackground = value;
  210. }
  211. /**
  212. * Get the configuration object. This is a reference only.
  213. * The configuration can ONLY be updated using the updateConfiguration function.
  214. * changing this object will have no direct effect on the scene.
  215. */
  216. public get configuration(): ViewerConfiguration {
  217. return this._configuration;
  218. }
  219. /**
  220. * force resizing the engine.
  221. */
  222. public forceResize() {
  223. this._resize();
  224. }
  225. /**
  226. * The resize function that will be registered with the window object
  227. */
  228. protected _resize = (): void => {
  229. // Only resize if Canvas is in the DOM
  230. if (!this.isCanvasInDOM()) {
  231. return;
  232. }
  233. if (this.canvas.clientWidth <= 0 || this.canvas.clientHeight <= 0) {
  234. return;
  235. }
  236. if (this._configuration.engine && this._configuration.engine.disableResize) {
  237. return;
  238. }
  239. this.engine.resize();
  240. }
  241. /**
  242. * Force a single render loop execution.
  243. */
  244. public forceRender() {
  245. this._render(true);
  246. }
  247. /**
  248. * render loop that will be executed by the engine
  249. */
  250. protected _render = (force: boolean = false): void => {
  251. if (force || (this.sceneManager.scene && this.sceneManager.scene.activeCamera)) {
  252. if (this.runRenderLoop || force) {
  253. this.engine.performanceMonitor.enable();
  254. this.sceneManager.scene.render();
  255. this.onFrameRenderedObservable.notifyObservers(this);
  256. } else {
  257. this.engine.performanceMonitor.disable();
  258. // TODO - is this needed?
  259. this.sceneManager.scene.activeCamera && this.sceneManager.scene.activeCamera.update();
  260. }
  261. }
  262. }
  263. /**
  264. * Takes a screenshot of the scene and returns it as a base64 encoded png.
  265. * @param callback optional callback that will be triggered when screenshot is done.
  266. * @param width Optional screenshot width (default to 512).
  267. * @param height Optional screenshot height (default to 512).
  268. * @returns a promise with the screenshot data
  269. */
  270. public takeScreenshot(callback?: (data: string) => void, width = 0, height = 0): Promise<string> {
  271. width = width || this.canvas.clientWidth;
  272. height = height || this.canvas.clientHeight;
  273. // Create the screenshot
  274. return new Promise<string>((resolve, reject) => {
  275. try {
  276. BABYLON.Tools.CreateScreenshot(this.engine, this.sceneManager.camera, { width, height }, (data) => {
  277. if (callback) {
  278. callback(data);
  279. }
  280. resolve(data);
  281. });
  282. } catch (e) {
  283. reject(e);
  284. }
  285. });
  286. }
  287. /**
  288. * Update the current viewer configuration with new values.
  289. * Only provided information will be updated, old configuration values will be kept.
  290. * If this.configuration was manually changed, you can trigger this function with no parameters,
  291. * and the entire configuration will be updated.
  292. * @param newConfiguration
  293. */
  294. public updateConfiguration(newConfiguration: Partial<ViewerConfiguration> = this._configuration, mode?: ViewerModel) {
  295. // update this.configuration with the new data
  296. this._configuration = deepmerge(this._configuration || {}, newConfiguration);
  297. this.sceneManager.updateConfiguration(newConfiguration, this._configuration, mode);
  298. // observers in configuration
  299. if (newConfiguration.observers) {
  300. this._configureObservers(newConfiguration.observers);
  301. }
  302. if (newConfiguration.loaderPlugins) {
  303. // TODO should plugins be removed?
  304. Object.keys(newConfiguration.loaderPlugins).forEach((name => {
  305. if (newConfiguration.loaderPlugins && newConfiguration.loaderPlugins[name]) {
  306. this.modelLoader.addPlugin(name);
  307. }
  308. }));
  309. }
  310. }
  311. /**
  312. * this is used to register native functions using the configuration object.
  313. * This will configure the observers.
  314. * @param observersConfiguration observers configuration
  315. */
  316. protected _configureObservers(observersConfiguration: IObserversConfiguration) {
  317. if (observersConfiguration.onEngineInit) {
  318. this.onEngineInitObservable.add(window[observersConfiguration.onEngineInit]);
  319. } else {
  320. if (observersConfiguration.onEngineInit === '' && this._configuration.observers && this._configuration.observers!.onEngineInit) {
  321. this.onEngineInitObservable.removeCallback(window[this._configuration.observers!.onEngineInit!]);
  322. }
  323. }
  324. if (observersConfiguration.onSceneInit) {
  325. this.onSceneInitObservable.add(window[observersConfiguration.onSceneInit]);
  326. } else {
  327. if (observersConfiguration.onSceneInit === '' && this._configuration.observers && this._configuration.observers!.onSceneInit) {
  328. this.onSceneInitObservable.removeCallback(window[this._configuration.observers!.onSceneInit!]);
  329. }
  330. }
  331. if (observersConfiguration.onModelLoaded) {
  332. this.onModelLoadedObservable.add(window[observersConfiguration.onModelLoaded]);
  333. } else {
  334. if (observersConfiguration.onModelLoaded === '' && this._configuration.observers && this._configuration.observers!.onModelLoaded) {
  335. this.onModelLoadedObservable.removeCallback(window[this._configuration.observers!.onModelLoaded!]);
  336. }
  337. }
  338. }
  339. /**
  340. * Dispoe the entire viewer including the scene and the engine
  341. */
  342. public dispose() {
  343. if (this._isDisposed) {
  344. return;
  345. }
  346. window.removeEventListener('resize', this._resize);
  347. //observers
  348. this.onEngineInitObservable.clear();
  349. delete this.onEngineInitObservable;
  350. this.onInitDoneObservable.clear();
  351. delete this.onInitDoneObservable;
  352. this.onLoaderInitObservable.clear();
  353. delete this.onLoaderInitObservable;
  354. this.onModelLoadedObservable.clear();
  355. delete this.onModelLoadedObservable;
  356. this.onModelLoadErrorObservable.clear();
  357. delete this.onModelLoadErrorObservable;
  358. this.onModelLoadProgressObservable.clear();
  359. delete this.onModelLoadProgressObservable;
  360. this.onSceneInitObservable.clear();
  361. delete this.onSceneInitObservable;
  362. this.onFrameRenderedObservable.clear();
  363. delete this.onFrameRenderedObservable;
  364. if (this.sceneManager.scene.activeCamera) {
  365. this.sceneManager.scene.activeCamera.detachControl(this.canvas);
  366. }
  367. this._fpsTimeout && clearTimeout(this._fpsTimeout);
  368. this.sceneManager.dispose();
  369. this.modelLoader.dispose();
  370. this.engine.dispose();
  371. this.templateManager.dispose();
  372. viewerManager.removeViewer(this);
  373. this._isDisposed = true;
  374. }
  375. /**
  376. * This will prepare the container element for the viewer
  377. */
  378. protected abstract _prepareContainerElement();
  379. /**
  380. * This function will execute when the HTML templates finished initializing.
  381. * It should initialize the engine and continue execution.
  382. *
  383. * @returns {Promise<AbstractViewer>} The viewer object will be returned after the object was loaded.
  384. */
  385. protected _onTemplatesLoaded(): Promise<AbstractViewer> {
  386. return Promise.resolve(this);
  387. }
  388. /**
  389. * This will force the creation of an engine and a scene.
  390. * It will also load a model if preconfigured.
  391. * But first - it will load the extendible onTemplateLoaded()!
  392. */
  393. private _onTemplateLoaded(): Promise<AbstractViewer> {
  394. return this._onTemplatesLoaded().then(() => {
  395. let autoLoad = typeof this._configuration.model === 'string' || (this._configuration.model && this._configuration.model.url);
  396. return this._initEngine().then((engine) => {
  397. return this.onEngineInitObservable.notifyObserversWithPromise(engine);
  398. }).then(() => {
  399. this._initTelemetryEvents();
  400. if (autoLoad) {
  401. return this.loadModel(this._configuration.model!).catch(e => { }).then(() => { return this.sceneManager.scene });
  402. } else {
  403. return this.sceneManager.scene || this.sceneManager.initScene(this._configuration.scene);
  404. }
  405. }).then(() => {
  406. return this.onInitDoneObservable.notifyObserversWithPromise(this);
  407. }).catch(e => {
  408. Tools.Warn(e.toString());
  409. return this;
  410. });
  411. })
  412. }
  413. /**
  414. * Initialize the engine. Retruns a promise in case async calls are needed.
  415. *
  416. * @protected
  417. * @returns {Promise<Engine>}
  418. * @memberof Viewer
  419. */
  420. protected _initEngine(): Promise<Engine> {
  421. // init custom shaders
  422. this._injectCustomShaders();
  423. let canvasElement = this.templateManager.getCanvas();
  424. if (!canvasElement) {
  425. return Promise.reject('Canvas element not found!');
  426. }
  427. let config = this._configuration.engine || {};
  428. // TDO enable further configuration
  429. // check for webgl2 support, force-disable if needed.
  430. if (viewerGlobals.disableWebGL2Support) {
  431. config.engineOptions = config.engineOptions || {};
  432. config.engineOptions.disableWebGL2Support = true;
  433. }
  434. this.engine = new Engine(canvasElement, !!config.antialiasing, config.engineOptions);
  435. // Disable manifest checking
  436. Database.IDBStorageEnabled = false;
  437. if (!config.disableResize) {
  438. window.addEventListener('resize', this._resize);
  439. }
  440. if (this._configuration.engine && this._configuration.engine.adaptiveQuality) {
  441. var scale = Math.max(0.5, 1 / (window.devicePixelRatio || 2));
  442. this.engine.setHardwareScalingLevel(scale);
  443. }
  444. return Promise.resolve(this.engine);
  445. }
  446. private _isLoading: boolean;
  447. private _nextLoading: Function;
  448. /**
  449. * Initialize a model loading. The returns object (a ViewerModel object) will be loaded in the background.
  450. * The difference between this and loadModel is that loadModel will fulfill the promise when the model finished loading.
  451. *
  452. * @param modelConfig model configuration to use when loading the model.
  453. * @param clearScene should the scene be cleared before loading this model
  454. * @returns a ViewerModel object that is not yet fully loaded.
  455. */
  456. public initModel(modelConfig: string | IModelConfiguration, clearScene: boolean = true): ViewerModel {
  457. let modelUrl = (typeof modelConfig === 'string') ? modelConfig : modelConfig.url;
  458. if (!modelUrl) {
  459. throw new Error("no model url provided");
  460. }
  461. if (clearScene) {
  462. }
  463. let configuration: IModelConfiguration;
  464. if (typeof modelConfig === 'string') {
  465. configuration = {
  466. url: modelConfig
  467. }
  468. } else {
  469. configuration = modelConfig
  470. }
  471. //merge the configuration for future models:
  472. if (this._configuration.model && typeof this._configuration.model === 'object') {
  473. deepmerge(this._configuration.model, configuration)
  474. } else {
  475. this._configuration.model = configuration;
  476. }
  477. this._isLoading = true;
  478. let model = this.modelLoader.load(configuration);
  479. this.lastUsedLoader = model.loader;
  480. model.onLoadErrorObservable.add((errorObject) => {
  481. this.onModelLoadErrorObservable.notifyObserversWithPromise(errorObject);
  482. });
  483. model.onLoadProgressObservable.add((progressEvent) => {
  484. this.onModelLoadProgressObservable.notifyObserversWithPromise(progressEvent);
  485. });
  486. this.onLoaderInitObservable.notifyObserversWithPromise(this.lastUsedLoader);
  487. model.onLoadedObservable.add(() => {
  488. this._isLoading = false;
  489. });
  490. return model;
  491. }
  492. /**
  493. * load a model using the provided configuration
  494. *
  495. * @param modelConfig the model configuration or URL to load.
  496. * @param clearScene Should the scene be cleared before loading the model
  497. * @returns a Promise the fulfills when the model finished loading successfully.
  498. */
  499. public loadModel(modelConfig: string | IModelConfiguration, clearScene: boolean = true): Promise<ViewerModel> {
  500. if (this._isLoading) {
  501. // We can decide here whether or not to cancel the lst load, but the developer can do that.
  502. return Promise.reject("another model is curently being loaded.");
  503. }
  504. return Promise.resolve(this.sceneManager.scene).then((scene) => {
  505. if (!scene) return this.sceneManager.initScene(this._configuration.scene, this._configuration.optimizer);
  506. return scene;
  507. }).then(() => {
  508. let model = this.initModel(modelConfig, clearScene);
  509. return new Promise<ViewerModel>((resolve, reject) => {
  510. // at this point, configuration.model is an object, not a string
  511. model.onLoadedObservable.add(() => {
  512. resolve(model);
  513. });
  514. model.onLoadErrorObservable.add((error) => {
  515. reject(error);
  516. });
  517. });
  518. })
  519. }
  520. private _fpsTimeout: number;
  521. protected _initTelemetryEvents() {
  522. telemetryManager.broadcast("Engine Capabilities", this, this.engine.getCaps());
  523. telemetryManager.broadcast("Platform Details", this, {
  524. userAgent: navigator.userAgent,
  525. platform: navigator.platform
  526. });
  527. telemetryManager.flushWebGLErrors(this);
  528. let trackFPS: Function = () => {
  529. telemetryManager.broadcast("Current FPS", this, { fps: this.engine.getFps() });
  530. };
  531. trackFPS();
  532. // Track the FPS again after 60 seconds
  533. this._fpsTimeout = window.setTimeout(trackFPS, 60 * 1000);
  534. }
  535. /**
  536. * Injects all the spectre shader in the babylon shader store
  537. */
  538. protected _injectCustomShaders(): void {
  539. let customShaders = this._configuration.customShaders;
  540. // Inject all the spectre shader in the babylon shader store.
  541. if (!customShaders) {
  542. return;
  543. }
  544. if (customShaders.shaders) {
  545. Object.keys(customShaders.shaders).forEach(key => {
  546. // typescript considers a callback "unsafe", so... '!'
  547. Effect.ShadersStore[key] = customShaders!.shaders![key];
  548. });
  549. }
  550. if (customShaders.includes) {
  551. Object.keys(customShaders.includes).forEach(key => {
  552. // typescript considers a callback "unsafe", so... '!'
  553. Effect.IncludesShadersStore[key] = customShaders!.includes![key];
  554. });
  555. }
  556. }
  557. }