defaultViewer.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. import { ViewerConfiguration, IModelConfiguration, ILightConfiguration, ISceneConfiguration } from './../configuration';
  2. import { Template, EventCallback } from '../templating/templateManager';
  3. import { FilesInput } from 'babylonjs/Misc/filesInput';
  4. import { SpotLight } from 'babylonjs/Lights/spotLight';
  5. import { Vector3 } from 'babylonjs/Maths/math';
  6. import { TemplateManager } from '../templating/templateManager';
  7. import { AbstractViewerWithTemplate } from './viewerWithTemplate';
  8. import { StandardMaterial } from 'babylonjs/Materials/standardMaterial';
  9. import { PBRMaterial } from 'babylonjs/Materials/PBR/pbrMaterial';
  10. import { extendClassWithConfig } from '../helper';
  11. import { ViewerModel } from '../model/viewerModel';
  12. import { IModelAnimation, AnimationState } from '../model/modelAnimation';
  13. import { IViewerTemplatePlugin } from '../templating/viewerTemplatePlugin';
  14. import { HDButtonPlugin } from '../templating/plugins/hdButtonPlugin';
  15. import { PrintButtonPlugin } from '../templating/plugins/printButton';
  16. /**
  17. * The Default viewer is the default implementation of the AbstractViewer.
  18. * It uses the templating system to render a new canvas and controls.
  19. */
  20. export class DefaultViewer extends AbstractViewerWithTemplate {
  21. /**
  22. * The corresponsing template manager of this viewer.
  23. */
  24. public templateManager: TemplateManager;
  25. public fullscreenElement?: Element;
  26. /**
  27. * Create a new default viewer
  28. * @param containerElement the element in which the templates will be rendered
  29. * @param initialConfiguration the initial configuration. Defaults to extending the default configuration
  30. */
  31. constructor(public containerElement: Element, initialConfiguration: ViewerConfiguration = { extends: 'default' }) {
  32. super(containerElement, initialConfiguration);
  33. this.onModelLoadedObservable.add(this._onModelLoaded);
  34. this.onModelRemovedObservable.add(() => {
  35. this._configureTemplate();
  36. });
  37. this.onEngineInitObservable.add(() => {
  38. this.sceneManager.onLightsConfiguredObservable.add((data) => {
  39. this._configureLights();
  40. });
  41. });
  42. this.onInitDoneObservable.add(() => {
  43. this.sceneManager.setDefaultMaterial = function(sceneConfig: ISceneConfiguration){
  44. let conf = sceneConfig.defaultMaterial;
  45. if(!conf){
  46. return;
  47. }
  48. if ((conf.materialType === 'standard' && this.scene.defaultMaterial.getClassName() !== 'StandardMaterial') ||
  49. (conf.materialType === 'pbr' && this.scene.defaultMaterial.getClassName() !== 'PBRMaterial')) {
  50. this.scene.defaultMaterial.dispose();
  51. if (conf.materialType === 'standard') {
  52. this.scene.defaultMaterial = new StandardMaterial("defaultMaterial", this.scene);
  53. } else {
  54. this.scene.defaultMaterial = new PBRMaterial("defaultMaterial", this.scene);
  55. }
  56. }
  57. extendClassWithConfig(this.scene.defaultMaterial, conf);
  58. }
  59. if (!this.sceneManager.models.length) {
  60. this.hideLoadingScreen();
  61. }
  62. });
  63. }
  64. private _registeredPlugins: Array<IViewerTemplatePlugin> = [];
  65. public registerTemplatePlugin(plugin: IViewerTemplatePlugin) {
  66. //validate
  67. if (!plugin.templateName) {
  68. throw new Error("No template name provided");
  69. }
  70. this._registeredPlugins.push(plugin);
  71. let template = this.templateManager.getTemplate(plugin.templateName);
  72. if (!template) {
  73. throw new Error(`Template ${plugin.templateName} not found`);
  74. }
  75. if (plugin.addHTMLTemplate) {
  76. template.onHTMLRendered.add((tmpl) => {
  77. plugin.addHTMLTemplate!(tmpl);
  78. });
  79. template.redraw();
  80. }
  81. if (plugin.eventsToAttach) {
  82. plugin.eventsToAttach.forEach((eventName) => {
  83. plugin.onEvent && this.templateManager.eventManager.registerCallback(plugin.templateName, (event) => {
  84. if (plugin.onEvent && plugin.interactionPredicate(event)) {
  85. plugin.onEvent(event);
  86. }
  87. }, eventName);
  88. });
  89. }
  90. }
  91. /**
  92. * This will be executed when the templates initialize.
  93. */
  94. protected _onTemplatesLoaded() {
  95. this.showLoadingScreen();
  96. // navbar
  97. this._initNavbar();
  98. // close overlay button
  99. let template = this.templateManager.getTemplate('overlay');
  100. if (template) {
  101. let closeButton = template.parent.querySelector('.close-button');
  102. if (closeButton) {
  103. closeButton.addEventListener('pointerdown', () => {
  104. this.hideOverlayScreen();
  105. });
  106. }
  107. }
  108. if (this.configuration.templates && this.configuration.templates.viewer) {
  109. if (this.configuration.templates.viewer.params && this.configuration.templates.viewer.params.enableDragAndDrop) {
  110. this.onSceneInitObservable.addOnce(() => {
  111. let filesInput = new FilesInput(this.engine, this.sceneManager.scene, () => {
  112. }, () => {
  113. }, () => {
  114. }, () => {
  115. }, function() {
  116. }, (file: File) => {
  117. this.loadModel(file);
  118. }, () => {
  119. });
  120. filesInput.monitorElementForDragNDrop(this.templateManager.getCanvas()!);
  121. });
  122. }
  123. }
  124. return super._onTemplatesLoaded();
  125. }
  126. private _initNavbar() {
  127. let navbar = this.templateManager.getTemplate('navBar');
  128. if (navbar) {
  129. this.onFrameRenderedObservable.add(this._updateProgressBar);
  130. this.templateManager.eventManager.registerCallback('navBar', this._handlePointerClick, 'click');
  131. // an example how to trigger the help button. publiclly available
  132. this.templateManager.eventManager.registerCallback("navBar", () => {
  133. // do your thing
  134. }, "pointerdown", ".help-button");
  135. this.templateManager.eventManager.registerCallback("navBar", (event: EventCallback) => {
  136. const evt = event.event;
  137. const element = <HTMLInputElement>(evt.target);
  138. if (!this._currentAnimation) { return; }
  139. const gotoFrame = +element.value / 100 * this._currentAnimation.frames;
  140. if (isNaN(gotoFrame)) { return; }
  141. this._currentAnimation.goToFrame(gotoFrame);
  142. }, "input");
  143. this.templateManager.eventManager.registerCallback("navBar", () => {
  144. if (this._resumePlay) {
  145. this._togglePlayPause(true);
  146. }
  147. this._resumePlay = false;
  148. }, "pointerup", ".progress-wrapper");
  149. if (window.devicePixelRatio === 1 && navbar.configuration.params && !navbar.configuration.params.hideHdButton) {
  150. navbar.updateParams({
  151. hideHdButton: true
  152. });
  153. }
  154. this.registerTemplatePlugin(new HDButtonPlugin(this));
  155. this.registerTemplatePlugin(new PrintButtonPlugin(this));
  156. }
  157. }
  158. private _animationList: string[];
  159. private _currentAnimation: IModelAnimation;
  160. private _isAnimationPaused: boolean;
  161. private _resumePlay: boolean;
  162. private _handlePointerClick = (event: EventCallback) => {
  163. let pointerDown = <PointerEvent>event.event;
  164. if (pointerDown.button !== 0) { return; }
  165. var element = (<HTMLElement>event.event.target);
  166. if (!element) {
  167. return;
  168. }
  169. let parentClasses = element.parentElement!.classList;
  170. let elementClasses = element.classList;
  171. let elementName = "";
  172. for (let i = 0; i < elementClasses.length; ++i) {
  173. let className = elementClasses[i];
  174. if (className.indexOf("-button") !== -1 || className.indexOf("-wrapper") !== -1) {
  175. elementName = className;
  176. break;
  177. }
  178. }
  179. switch (elementName) {
  180. case "speed-button":
  181. case "types-button":
  182. if (parentClasses.contains("open")) {
  183. parentClasses.remove("open");
  184. } else {
  185. parentClasses.add("open");
  186. }
  187. break;
  188. case "play-pause-button":
  189. this._togglePlayPause();
  190. break;
  191. case "label-option-button":
  192. var value = element.dataset["value"];
  193. var label = element.querySelector("span.animation-label");
  194. if (label && value) {
  195. this._updateAnimationType({ value: value.trim(), label: label.innerHTML });
  196. }
  197. break;
  198. case "speed-option-button":
  199. if (!this._currentAnimation) {
  200. return;
  201. }
  202. var speed = element.dataset["value"];
  203. if (speed) {
  204. this._updateAnimationSpeed(speed);
  205. }
  206. break;
  207. case "progress-wrapper":
  208. this._resumePlay = !this._isAnimationPaused;
  209. if (this._resumePlay) {
  210. this._togglePlayPause(true);
  211. }
  212. break;
  213. case "fullscreen-button":
  214. this.toggleFullscreen();
  215. break;
  216. case "vr-button":
  217. this.toggleVR();
  218. break;
  219. default:
  220. return;
  221. }
  222. }
  223. /**
  224. * Plays or Pauses animation
  225. */
  226. private _togglePlayPause = (noUiUpdate?: boolean) => {
  227. if (!this._currentAnimation) {
  228. return;
  229. }
  230. if (this._isAnimationPaused) {
  231. this._currentAnimation.restart();
  232. } else {
  233. this._currentAnimation.pause();
  234. }
  235. this._isAnimationPaused = !this._isAnimationPaused;
  236. if (noUiUpdate) { return; }
  237. let navbar = this.templateManager.getTemplate('navBar');
  238. if (!navbar) { return; }
  239. navbar.updateParams({
  240. paused: this._isAnimationPaused,
  241. });
  242. }
  243. private _oldIdleRotationValue: number;
  244. /**
  245. * Control progress bar position based on animation current frame
  246. */
  247. private _updateProgressBar = () => {
  248. let navbar = this.templateManager.getTemplate('navBar');
  249. if (!navbar) { return; }
  250. var progressSlider = <HTMLInputElement>navbar.parent.querySelector("input.progress-wrapper");
  251. if (progressSlider && this._currentAnimation) {
  252. const progress = this._currentAnimation.currentFrame / this._currentAnimation.frames * 100;
  253. var currentValue = progressSlider.valueAsNumber;
  254. if (Math.abs(currentValue - progress) > 0.5) { // Only move if greater than a 1% change
  255. progressSlider.value = '' + progress;
  256. }
  257. if (this._currentAnimation.state === AnimationState.PLAYING) {
  258. if (this.sceneManager.camera.autoRotationBehavior && !this._oldIdleRotationValue) {
  259. this._oldIdleRotationValue = this.sceneManager.camera.autoRotationBehavior.idleRotationSpeed;
  260. this.sceneManager.camera.autoRotationBehavior.idleRotationSpeed = 0;
  261. }
  262. } else {
  263. if (this.sceneManager.camera.autoRotationBehavior && this._oldIdleRotationValue) {
  264. this.sceneManager.camera.autoRotationBehavior.idleRotationSpeed = this._oldIdleRotationValue;
  265. this._oldIdleRotationValue = 0;
  266. }
  267. }
  268. }
  269. }
  270. /**
  271. * Update Current Animation Speed
  272. */
  273. private _updateAnimationSpeed = (speed: string, paramsObject?: any) => {
  274. let navbar = this.templateManager.getTemplate('navBar');
  275. if (!navbar) { return; }
  276. if (speed && this._currentAnimation) {
  277. this._currentAnimation.speedRatio = parseFloat(speed);
  278. if (!this._isAnimationPaused) {
  279. this._currentAnimation.restart();
  280. }
  281. if (paramsObject) {
  282. paramsObject.selectedSpeed = speed + "x";
  283. } else {
  284. navbar.updateParams({
  285. selectedSpeed: speed + "x",
  286. });
  287. }
  288. }
  289. }
  290. /**
  291. * Update Current Animation Type
  292. */
  293. private _updateAnimationType = (data: { label: string, value: string }, paramsObject?: any) => {
  294. let navbar = this.templateManager.getTemplate('navBar');
  295. if (!navbar) { return; }
  296. if (data) {
  297. this._currentAnimation = this.sceneManager.models[0].setCurrentAnimationByName(data.value);
  298. }
  299. if (paramsObject) {
  300. paramsObject.selectedAnimation = (this._animationList.indexOf(data.value) + 1);
  301. paramsObject.selectedAnimationName = data.label;
  302. } else {
  303. navbar.updateParams({
  304. selectedAnimation: (this._animationList.indexOf(data.value) + 1),
  305. selectedAnimationName: data.label
  306. });
  307. }
  308. this._updateAnimationSpeed("1.0", paramsObject);
  309. }
  310. protected _initVR() {
  311. this.engine.onVRDisplayChangedObservable.add(() => {
  312. let viewerTemplate = this.templateManager.getTemplate('viewer');
  313. let viewerElement = viewerTemplate && viewerTemplate.parent;
  314. if (viewerElement) {
  315. if (this.sceneManager.vrHelper!.isInVRMode) {
  316. viewerElement.classList.add("in-vr");
  317. } else {
  318. viewerElement.classList.remove("in-vr");
  319. }
  320. }
  321. });
  322. if (this.sceneManager.vrHelper) {
  323. // due to the way the experience helper is exisintg VR, this must be added.
  324. this.sceneManager.vrHelper.onExitingVR.add(() => {
  325. let viewerTemplate = this.templateManager.getTemplate('viewer');
  326. let viewerElement = viewerTemplate && viewerTemplate.parent;
  327. if (viewerElement) {
  328. viewerElement.classList.remove("in-vr");
  329. }
  330. });
  331. }
  332. super._initVR();
  333. }
  334. /**
  335. * Toggle fullscreen of the entire viewer
  336. */
  337. public toggleFullscreen = () => {
  338. let viewerTemplate = this.templateManager.getTemplate('viewer');
  339. let viewerElement = viewerTemplate && viewerTemplate.parent;
  340. let fullscreenElement = this.fullscreenElement || viewerElement;
  341. if (fullscreenElement) {
  342. let currentElement = (<any>document).fullscreenElement || (<any>document).webkitFullscreenElement || (<any>document).mozFullScreenElement || (<any>document).msFullscreenElement;
  343. if (!currentElement) {
  344. let requestFullScreen = fullscreenElement.requestFullscreen || (<any>fullscreenElement).webkitRequestFullscreen || (<any>fullscreenElement).msRequestFullscreen || (<any>fullscreenElement).mozRequestFullScreen;
  345. requestFullScreen.call(fullscreenElement);
  346. if (viewerElement) {
  347. viewerElement.classList.add("in-fullscreen");
  348. }
  349. } else {
  350. let exitFullscreen = document.exitFullscreen || (<any>document).webkitExitFullscreen || (<any>document).msExitFullscreen || (<any>document).mozCancelFullScreen;
  351. exitFullscreen.call(document);
  352. if (viewerElement) {
  353. viewerElement.classList.remove("in-fullscreen");
  354. }
  355. }
  356. }
  357. }
  358. /**
  359. * Preparing the container element to present the viewer
  360. */
  361. protected _prepareContainerElement() {
  362. const htmlElement = this.containerElement as HTMLElement;
  363. if (htmlElement.style) {
  364. htmlElement.style.position = 'relative';
  365. htmlElement.style.height = '100%';
  366. htmlElement.style.display = 'flex';
  367. }
  368. }
  369. /**
  370. * This function will configure the templates and update them after a model was loaded
  371. * It is mainly responsible to changing the title and subtitle etc'.
  372. * @param model the model to be used to configure the templates by
  373. */
  374. protected _configureTemplate(model?: ViewerModel) {
  375. let navbar = this.templateManager.getTemplate('navBar');
  376. if (!navbar) { return; }
  377. let newParams: any = navbar.configuration.params || {};
  378. if (!model) {
  379. newParams.animations = null;
  380. } else {
  381. let animationNames = model.getAnimationNames();
  382. newParams.animations = animationNames.map((a) => { return { label: a, value: a }; });
  383. if (animationNames.length) {
  384. this._isAnimationPaused = (model.configuration.animation && !model.configuration.animation.autoStart) || !model.configuration.animation;
  385. this._animationList = animationNames;
  386. newParams.paused = this._isAnimationPaused;
  387. let animationIndex = 0;
  388. if (model.configuration.animation && typeof model.configuration.animation.autoStart === 'string') {
  389. animationIndex = animationNames.indexOf(model.configuration.animation.autoStart);
  390. if (animationIndex === -1) {
  391. animationIndex = 0;
  392. }
  393. }
  394. this._updateAnimationType(newParams.animations[animationIndex], newParams);
  395. } else {
  396. newParams.animations = null;
  397. }
  398. if (model.configuration.thumbnail) {
  399. newParams.logoImage = model.configuration.thumbnail;
  400. }
  401. }
  402. navbar.updateParams(newParams, false);
  403. }
  404. /**
  405. * This will load a new model to the default viewer
  406. * overriding the AbstractViewer's loadModel.
  407. * The scene will automatically be cleared of the old models, if exist.
  408. * @param model the configuration object (or URL) to load.
  409. */
  410. public loadModel(model?: string | File | IModelConfiguration): Promise<ViewerModel> {
  411. if (!model) {
  412. model = this.configuration.model;
  413. }
  414. this.showLoadingScreen();
  415. return super.loadModel(model!, true).catch((error) => {
  416. console.log(error);
  417. this.hideLoadingScreen();
  418. this.showOverlayScreen('error');
  419. return Promise.reject(error);
  420. });
  421. }
  422. private _onModelLoaded = (model: ViewerModel) => {
  423. this._configureTemplate(model);
  424. // with a short timeout, making sure everything is there already.
  425. let hideLoadingDelay = 20;
  426. if (this.configuration.lab && this.configuration.lab.hideLoadingDelay !== undefined) {
  427. hideLoadingDelay = this.configuration.lab.hideLoadingDelay;
  428. }
  429. setTimeout(() => {
  430. this.sceneManager.scene.executeWhenReady(() => {
  431. this.hideLoadingScreen();
  432. });
  433. }, hideLoadingDelay);
  434. return;
  435. }
  436. /**
  437. * Show the overlay and the defined sub-screen.
  438. * Mainly used for help and errors
  439. * @param subScreen the name of the subScreen. Those can be defined in the configuration object
  440. */
  441. public showOverlayScreen(subScreen: string) {
  442. let template = this.templateManager.getTemplate('overlay');
  443. if (!template) { return Promise.resolve('Overlay template not found'); }
  444. return template.show(((template) => {
  445. var canvasRect = this.containerElement.getBoundingClientRect();
  446. template.parent.style.display = 'flex';
  447. template.parent.style.width = canvasRect.width + "px";
  448. template.parent.style.height = canvasRect.height + "px";
  449. template.parent.style.opacity = "1";
  450. let subTemplate = this.templateManager.getTemplate(subScreen);
  451. if (!subTemplate) {
  452. return Promise.reject(subScreen + ' template not found');
  453. }
  454. return subTemplate.show(((template) => {
  455. template.parent.style.display = 'flex';
  456. return Promise.resolve(template);
  457. }));
  458. }));
  459. }
  460. /**
  461. * Hide the overlay screen.
  462. */
  463. public hideOverlayScreen() {
  464. let template = this.templateManager.getTemplate('overlay');
  465. if (!template) { return Promise.resolve('Overlay template not found'); }
  466. return template.hide(((template) => {
  467. template.parent.style.opacity = "0";
  468. let onTransitionEnd = () => {
  469. template.parent.removeEventListener("transitionend", onTransitionEnd);
  470. template.parent.style.display = 'none';
  471. };
  472. template.parent.addEventListener("transitionend", onTransitionEnd);
  473. let overlays = template.parent.querySelectorAll('.overlay');
  474. if (overlays) {
  475. for (let i = 0; i < overlays.length; ++i) {
  476. let htmlElement = <HTMLElement>overlays.item(i);
  477. htmlElement.style.display = 'none';
  478. }
  479. }
  480. return Promise.resolve(template);
  481. }));
  482. }
  483. /**
  484. * show the viewer (in case it was hidden)
  485. *
  486. * @param visibilityFunction an optional function to execute in order to show the container
  487. */
  488. public show(visibilityFunction?: ((template: Template) => Promise<Template>)): Promise<Template> {
  489. let template = this.templateManager.getTemplate('main');
  490. //not possible, but yet:
  491. if (!template) { return Promise.reject('Main template not found'); }
  492. return template.show(visibilityFunction);
  493. }
  494. /**
  495. * hide the viewer (in case it is visible)
  496. *
  497. * @param visibilityFunction an optional function to execute in order to hide the container
  498. */
  499. public hide(visibilityFunction?: ((template: Template) => Promise<Template>)) {
  500. let template = this.templateManager.getTemplate('main');
  501. //not possible, but yet:
  502. if (!template) { return Promise.reject('Main template not found'); }
  503. return template.hide(visibilityFunction);
  504. }
  505. /**
  506. * Show the loading screen.
  507. * The loading screen can be configured using the configuration object
  508. */
  509. public showLoadingScreen() {
  510. let template = this.templateManager.getTemplate('loadingScreen');
  511. if (!template) { return Promise.resolve('Loading Screen template not found'); }
  512. return template.show(((template) => {
  513. var canvasRect = this.containerElement.getBoundingClientRect();
  514. // var canvasPositioning = window.getComputedStyle(this.containerElement).position;
  515. template.parent.style.display = 'flex';
  516. template.parent.style.width = canvasRect.width + "px";
  517. template.parent.style.height = canvasRect.height + "px";
  518. template.parent.style.opacity = "1";
  519. // from the configuration!!!
  520. let color = "black";
  521. if (this.configuration.templates && this.configuration.templates.loadingScreen) {
  522. color = (this.configuration.templates.loadingScreen.params &&
  523. <string>this.configuration.templates.loadingScreen.params.backgroundColor) || color;
  524. }
  525. template.parent.style.backgroundColor = color;
  526. return Promise.resolve(template);
  527. }));
  528. }
  529. /**
  530. * Hide the loading screen
  531. */
  532. public hideLoadingScreen() {
  533. let template = this.templateManager.getTemplate('loadingScreen');
  534. if (!template) { return Promise.resolve('Loading Screen template not found'); }
  535. return template.hide(((template) => {
  536. template.parent.style.opacity = "0";
  537. let onTransitionEnd = () => {
  538. template.parent.removeEventListener("transitionend", onTransitionEnd);
  539. template.parent.style.display = 'none';
  540. };
  541. template.parent.addEventListener("transitionend", onTransitionEnd);
  542. return Promise.resolve(template);
  543. }));
  544. }
  545. public dispose() {
  546. this.templateManager.dispose();
  547. super.dispose();
  548. }
  549. protected _onConfigurationLoaded(configuration: ViewerConfiguration) {
  550. super._onConfigurationLoaded(configuration);
  551. this.templateManager = new TemplateManager(this.containerElement);
  552. // initialize the templates
  553. let templateConfiguration = this.configuration.templates || {};
  554. this.templateManager.initTemplate(templateConfiguration);
  555. // when done, execute onTemplatesLoaded()
  556. this.templateManager.onAllLoaded.add(() => {
  557. let canvas = this.templateManager.getCanvas();
  558. if (canvas) {
  559. this._canvas = canvas;
  560. }
  561. this._onTemplateLoaded();
  562. });
  563. }
  564. /**
  565. * An extension of the light configuration of the abstract viewer.
  566. * @param lightsConfiguration the light configuration to use
  567. * @param model the model that will be used to configure the lights (if the lights are model-dependant)
  568. */
  569. private _configureLights() {
  570. // labs feature - flashlight
  571. if (this.configuration.lab && this.configuration.lab.flashlight) {
  572. let lightTarget;
  573. let angle = 0.5;
  574. let exponent = Math.PI / 2;
  575. if (typeof this.configuration.lab.flashlight === "object") {
  576. exponent = this.configuration.lab.flashlight.exponent || exponent;
  577. angle = this.configuration.lab.flashlight.angle || angle;
  578. }
  579. var flashlight = new SpotLight("flashlight", Vector3.Zero(),
  580. Vector3.Zero(), exponent, angle, this.sceneManager.scene);
  581. if (typeof this.configuration.lab.flashlight === "object") {
  582. flashlight.intensity = this.configuration.lab.flashlight.intensity || flashlight.intensity;
  583. if (this.configuration.lab.flashlight.diffuse) {
  584. flashlight.diffuse.r = this.configuration.lab.flashlight.diffuse.r;
  585. flashlight.diffuse.g = this.configuration.lab.flashlight.diffuse.g;
  586. flashlight.diffuse.b = this.configuration.lab.flashlight.diffuse.b;
  587. }
  588. if (this.configuration.lab.flashlight.specular) {
  589. flashlight.specular.r = this.configuration.lab.flashlight.specular.r;
  590. flashlight.specular.g = this.configuration.lab.flashlight.specular.g;
  591. flashlight.specular.b = this.configuration.lab.flashlight.specular.b;
  592. }
  593. }
  594. this.sceneManager.scene.constantlyUpdateMeshUnderPointer = true;
  595. this.sceneManager.scene.onPointerObservable.add((eventData) => {
  596. if (eventData.type === 4 && eventData.pickInfo) {
  597. lightTarget = (eventData.pickInfo.pickedPoint);
  598. } else {
  599. lightTarget = undefined;
  600. }
  601. });
  602. let updateFlashlightFunction = () => {
  603. if (this.sceneManager.camera && flashlight) {
  604. flashlight.position.copyFrom(this.sceneManager.camera.position);
  605. if (lightTarget) {
  606. lightTarget.subtractToRef(flashlight.position, flashlight.direction);
  607. }
  608. }
  609. };
  610. this.sceneManager.scene.registerBeforeRender(updateFlashlightFunction);
  611. this._registeredOnBeforeRenderFunctions.push(updateFlashlightFunction);
  612. }
  613. }
  614. }