defaultViewer.ts 27 KB

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