defaultViewer.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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?: HTMLElement;
  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: HTMLElement, 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._handlePointerDown, 'pointerdown');
  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 _handlePointerDown = (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, 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. break;
  180. case "progress-wrapper":
  181. this._resumePlay = !this._isAnimationPaused;
  182. if (this._resumePlay) {
  183. this._togglePlayPause(true);
  184. }
  185. break;
  186. case "fullscreen-button":
  187. this.toggleFullscreen();
  188. break;
  189. case "vr-button":
  190. this.toggleVR();
  191. break;
  192. default:
  193. return;
  194. }
  195. }
  196. /**
  197. * Plays or Pauses animation
  198. */
  199. private _togglePlayPause = (noUiUpdate?: boolean) => {
  200. if (!this._currentAnimation) {
  201. return;
  202. }
  203. if (this._isAnimationPaused) {
  204. this._currentAnimation.restart();
  205. } else {
  206. this._currentAnimation.pause();
  207. }
  208. this._isAnimationPaused = !this._isAnimationPaused;
  209. if (noUiUpdate) return;
  210. let navbar = this.templateManager.getTemplate('navBar');
  211. if (!navbar) return;
  212. navbar.updateParams({
  213. paused: this._isAnimationPaused,
  214. });
  215. }
  216. private _oldIdleRotationValue: number;
  217. /**
  218. * Control progress bar position based on animation current frame
  219. */
  220. private _updateProgressBar = () => {
  221. let navbar = this.templateManager.getTemplate('navBar');
  222. if (!navbar) return;
  223. var progressSlider = <HTMLInputElement>navbar.parent.querySelector("input.progress-wrapper");
  224. if (progressSlider && this._currentAnimation) {
  225. const progress = this._currentAnimation.currentFrame / this._currentAnimation.frames * 100;
  226. var currentValue = progressSlider.valueAsNumber;
  227. if (Math.abs(currentValue - progress) > 0.5) { // Only move if greater than a 1% change
  228. progressSlider.value = '' + progress;
  229. }
  230. if (this._currentAnimation.state === AnimationState.PLAYING) {
  231. if (this.sceneManager.camera.autoRotationBehavior && !this._oldIdleRotationValue) {
  232. this._oldIdleRotationValue = this.sceneManager.camera.autoRotationBehavior.idleRotationSpeed;
  233. this.sceneManager.camera.autoRotationBehavior.idleRotationSpeed = 0;
  234. }
  235. } else {
  236. if (this.sceneManager.camera.autoRotationBehavior && this._oldIdleRotationValue) {
  237. this.sceneManager.camera.autoRotationBehavior.idleRotationSpeed = this._oldIdleRotationValue;
  238. this._oldIdleRotationValue = 0;
  239. }
  240. }
  241. }
  242. }
  243. /**
  244. * Update Current Animation Speed
  245. */
  246. private _updateAnimationSpeed = (speed: string, paramsObject?: any) => {
  247. let navbar = this.templateManager.getTemplate('navBar');
  248. if (!navbar) return;
  249. if (speed && this._currentAnimation) {
  250. this._currentAnimation.speedRatio = parseFloat(speed);
  251. if (!this._isAnimationPaused) {
  252. this._currentAnimation.restart();
  253. }
  254. if (paramsObject) {
  255. paramsObject.selectedSpeed = speed + "x"
  256. } else {
  257. navbar.updateParams({
  258. selectedSpeed: speed + "x",
  259. });
  260. }
  261. }
  262. }
  263. /**
  264. * Update Current Animation Type
  265. */
  266. private _updateAnimationType = (data: { label: string, value: string }, paramsObject?: any) => {
  267. let navbar = this.templateManager.getTemplate('navBar');
  268. if (!navbar) return;
  269. if (data) {
  270. this._currentAnimation = this.sceneManager.models[0].setCurrentAnimationByName(data.value);
  271. }
  272. if (paramsObject) {
  273. paramsObject.selectedAnimation = (this._animationList.indexOf(data.value) + 1);
  274. paramsObject.selectedAnimationName = data.label;
  275. } else {
  276. navbar.updateParams({
  277. selectedAnimation: (this._animationList.indexOf(data.value) + 1),
  278. selectedAnimationName: data.label
  279. });
  280. }
  281. this._updateAnimationSpeed("1.0", paramsObject);
  282. }
  283. protected _initVR() {
  284. this.engine.onVRDisplayChangedObservable.add(() => {
  285. let viewerTemplate = this.templateManager.getTemplate('viewer');
  286. let viewerElement = viewerTemplate && viewerTemplate.parent;
  287. if (viewerElement) {
  288. if (this.sceneManager.vrHelper!.isInVRMode) {
  289. viewerElement.classList.add("in-vr");
  290. } else {
  291. viewerElement.classList.remove("in-vr");
  292. }
  293. }
  294. });
  295. if (this.sceneManager.vrHelper) {
  296. // due to the way the experience helper is exisintg VR, this must be added.
  297. this.sceneManager.vrHelper.onExitingVR.add(() => {
  298. let viewerTemplate = this.templateManager.getTemplate('viewer');
  299. let viewerElement = viewerTemplate && viewerTemplate.parent;
  300. if (viewerElement) {
  301. viewerElement.classList.remove("in-vr");
  302. }
  303. });
  304. }
  305. super._initVR();
  306. }
  307. /**
  308. * Toggle fullscreen of the entire viewer
  309. */
  310. public toggleFullscreen = () => {
  311. let viewerTemplate = this.templateManager.getTemplate('viewer');
  312. let viewerElement = viewerTemplate && viewerTemplate.parent;
  313. let fullscreenElement = this.fullscreenElement || viewerElement;
  314. if (fullscreenElement) {
  315. let currentElement = document.fullscreenElement || document.webkitFullscreenElement || (<any>document).mozFullScreenElement || (<any>document).msFullscreenElement;
  316. if (!currentElement) {
  317. let requestFullScreen = fullscreenElement.requestFullscreen || fullscreenElement.webkitRequestFullscreen || (<any>fullscreenElement).msRequestFullscreen || (<any>fullscreenElement).mozRequestFullScreen;
  318. requestFullScreen.call(fullscreenElement);
  319. if (viewerElement) {
  320. viewerElement.classList.add("in-fullscreen");
  321. }
  322. } else {
  323. let exitFullscreen = document.exitFullscreen || document.webkitExitFullscreen || (<any>document).msExitFullscreen || (<any>document).mozCancelFullScreen
  324. exitFullscreen.call(document);
  325. if (viewerElement) {
  326. viewerElement.classList.remove("in-fullscreen");
  327. }
  328. }
  329. }
  330. }
  331. /**
  332. * Preparing the container element to present the viewer
  333. */
  334. protected _prepareContainerElement() {
  335. this.containerElement.style.position = 'relative';
  336. this.containerElement.style.height = '100%';
  337. this.containerElement.style.display = 'flex';
  338. }
  339. /**
  340. * This function will configure the templates and update them after a model was loaded
  341. * It is mainly responsible to changing the title and subtitle etc'.
  342. * @param model the model to be used to configure the templates by
  343. */
  344. protected _configureTemplate(model?: ViewerModel) {
  345. let navbar = this.templateManager.getTemplate('navBar');
  346. if (!navbar) return;
  347. let newParams: any = navbar.configuration.params || {};
  348. if (!model) {
  349. newParams.animations = null;
  350. } else {
  351. let animationNames = model.getAnimationNames();
  352. newParams.animations = animationNames.map(a => { return { label: a, value: a } });
  353. if (animationNames.length) {
  354. this._isAnimationPaused = (model.configuration.animation && !model.configuration.animation.autoStart) || !model.configuration.animation;
  355. this._animationList = animationNames;
  356. newParams.paused = this._isAnimationPaused;
  357. let animationIndex = 0;
  358. if (model.configuration.animation && typeof model.configuration.animation.autoStart === 'string') {
  359. animationIndex = animationNames.indexOf(model.configuration.animation.autoStart);
  360. if (animationIndex === -1) {
  361. animationIndex = 0;
  362. }
  363. }
  364. this._updateAnimationType(newParams.animations[animationIndex], newParams);
  365. } else {
  366. newParams.animations = null;
  367. }
  368. if (model.configuration.thumbnail) {
  369. newParams.logoImage = model.configuration.thumbnail
  370. }
  371. }
  372. navbar.updateParams(newParams, false);
  373. }
  374. /**
  375. * This will load a new model to the default viewer
  376. * overriding the AbstractViewer's loadModel.
  377. * The scene will automatically be cleared of the old models, if exist.
  378. * @param model the configuration object (or URL) to load.
  379. */
  380. public loadModel(model?: string | File | IModelConfiguration): Promise<ViewerModel> {
  381. if (!model) {
  382. model = this.configuration.model;
  383. }
  384. this.showLoadingScreen();
  385. return super.loadModel(model!, true).catch((error) => {
  386. console.log(error);
  387. this.hideLoadingScreen();
  388. this.showOverlayScreen('error');
  389. return Promise.reject(error);
  390. });
  391. }
  392. private _onModelLoaded = (model: ViewerModel) => {
  393. this._configureTemplate(model);
  394. // with a short timeout, making sure everything is there already.
  395. let hideLoadingDelay = 20;
  396. if (this.configuration.lab && this.configuration.lab.hideLoadingDelay !== undefined) {
  397. hideLoadingDelay = this.configuration.lab.hideLoadingDelay;
  398. }
  399. setTimeout(() => {
  400. this.sceneManager.scene.executeWhenReady(() => {
  401. this.hideLoadingScreen();
  402. });
  403. }, hideLoadingDelay);
  404. return;
  405. }
  406. /**
  407. * Show the overlay and the defined sub-screen.
  408. * Mainly used for help and errors
  409. * @param subScreen the name of the subScreen. Those can be defined in the configuration object
  410. */
  411. public showOverlayScreen(subScreen: string) {
  412. let template = this.templateManager.getTemplate('overlay');
  413. if (!template) return Promise.resolve('Overlay template not found');
  414. return template.show((template => {
  415. var canvasRect = this.containerElement.getBoundingClientRect();
  416. template.parent.style.display = 'flex';
  417. template.parent.style.width = canvasRect.width + "px";
  418. template.parent.style.height = canvasRect.height + "px";
  419. template.parent.style.opacity = "1";
  420. let subTemplate = this.templateManager.getTemplate(subScreen);
  421. if (!subTemplate) {
  422. return Promise.reject(subScreen + ' template not found');
  423. }
  424. return subTemplate.show((template => {
  425. template.parent.style.display = 'flex';
  426. return Promise.resolve(template);
  427. }));
  428. }));
  429. }
  430. /**
  431. * Hide the overlay screen.
  432. */
  433. public hideOverlayScreen() {
  434. let template = this.templateManager.getTemplate('overlay');
  435. if (!template) return Promise.resolve('Overlay template not found');
  436. return template.hide((template => {
  437. template.parent.style.opacity = "0";
  438. let onTransitionEnd = () => {
  439. template.parent.removeEventListener("transitionend", onTransitionEnd);
  440. template.parent.style.display = 'none';
  441. }
  442. template.parent.addEventListener("transitionend", onTransitionEnd);
  443. let overlays = template.parent.querySelectorAll('.overlay');
  444. if (overlays) {
  445. for (let i = 0; i < overlays.length; ++i) {
  446. let htmlElement = <HTMLElement>overlays.item(i);
  447. htmlElement.style.display = 'none';
  448. }
  449. }
  450. return Promise.resolve(template);
  451. }));
  452. }
  453. /**
  454. * show the viewer (in case it was hidden)
  455. *
  456. * @param visibilityFunction an optional function to execute in order to show the container
  457. */
  458. public show(visibilityFunction?: ((template: Template) => Promise<Template>)): Promise<Template> {
  459. let template = this.templateManager.getTemplate('main');
  460. //not possible, but yet:
  461. if (!template) return Promise.reject('Main template not found');
  462. return template.show(visibilityFunction);
  463. }
  464. /**
  465. * hide the viewer (in case it is visible)
  466. *
  467. * @param visibilityFunction an optional function to execute in order to hide the container
  468. */
  469. public hide(visibilityFunction?: ((template: Template) => Promise<Template>)) {
  470. let template = this.templateManager.getTemplate('main');
  471. //not possible, but yet:
  472. if (!template) return Promise.reject('Main template not found');
  473. return template.hide(visibilityFunction);
  474. }
  475. /**
  476. * Show the loading screen.
  477. * The loading screen can be configured using the configuration object
  478. */
  479. public showLoadingScreen() {
  480. let template = this.templateManager.getTemplate('loadingScreen');
  481. if (!template) return Promise.resolve('Loading Screen template not found');
  482. return template.show((template => {
  483. var canvasRect = this.containerElement.getBoundingClientRect();
  484. // var canvasPositioning = window.getComputedStyle(this.containerElement).position;
  485. template.parent.style.display = 'flex';
  486. template.parent.style.width = canvasRect.width + "px";
  487. template.parent.style.height = canvasRect.height + "px";
  488. template.parent.style.opacity = "1";
  489. // from the configuration!!!
  490. let color = "black";
  491. if (this.configuration.templates && this.configuration.templates.loadingScreen) {
  492. color = (this.configuration.templates.loadingScreen.params &&
  493. <string>this.configuration.templates.loadingScreen.params.backgroundColor) || color;
  494. }
  495. template.parent.style.backgroundColor = color;
  496. return Promise.resolve(template);
  497. }));
  498. }
  499. /**
  500. * Hide the loading screen
  501. */
  502. public hideLoadingScreen() {
  503. let template = this.templateManager.getTemplate('loadingScreen');
  504. if (!template) return Promise.resolve('Loading Screen template not found');
  505. return template.hide((template => {
  506. template.parent.style.opacity = "0";
  507. let onTransitionEnd = () => {
  508. template.parent.removeEventListener("transitionend", onTransitionEnd);
  509. template.parent.style.display = 'none';
  510. }
  511. template.parent.addEventListener("transitionend", onTransitionEnd);
  512. return Promise.resolve(template);
  513. }));
  514. }
  515. public dispose() {
  516. this.templateManager.dispose();
  517. super.dispose();
  518. }
  519. protected _onConfigurationLoaded(configuration: ViewerConfiguration) {
  520. super._onConfigurationLoaded(configuration);
  521. // initialize the templates
  522. let templateConfiguration = this.configuration.templates || {};
  523. this.templateManager.initTemplate(templateConfiguration);
  524. // when done, execute onTemplatesLoaded()
  525. this.templateManager.onAllLoaded.add(() => {
  526. let canvas = this.templateManager.getCanvas();
  527. if (canvas) {
  528. this._canvas = canvas;
  529. }
  530. this._onTemplateLoaded();
  531. });
  532. }
  533. /**
  534. * An extension of the light configuration of the abstract viewer.
  535. * @param lightsConfiguration the light configuration to use
  536. * @param model the model that will be used to configure the lights (if the lights are model-dependant)
  537. */
  538. private _configureLights() {
  539. // labs feature - flashlight
  540. if (this.configuration.lab && this.configuration.lab.flashlight) {
  541. let lightTarget;
  542. let angle = 0.5;
  543. let exponent = Math.PI / 2;
  544. if (typeof this.configuration.lab.flashlight === "object") {
  545. exponent = this.configuration.lab.flashlight.exponent || exponent;
  546. angle = this.configuration.lab.flashlight.angle || angle;
  547. }
  548. var flashlight = new SpotLight("flashlight", Vector3.Zero(),
  549. Vector3.Zero(), exponent, angle, this.sceneManager.scene);
  550. if (typeof this.configuration.lab.flashlight === "object") {
  551. flashlight.intensity = this.configuration.lab.flashlight.intensity || flashlight.intensity;
  552. if (this.configuration.lab.flashlight.diffuse) {
  553. flashlight.diffuse.r = this.configuration.lab.flashlight.diffuse.r;
  554. flashlight.diffuse.g = this.configuration.lab.flashlight.diffuse.g;
  555. flashlight.diffuse.b = this.configuration.lab.flashlight.diffuse.b;
  556. }
  557. if (this.configuration.lab.flashlight.specular) {
  558. flashlight.specular.r = this.configuration.lab.flashlight.specular.r;
  559. flashlight.specular.g = this.configuration.lab.flashlight.specular.g;
  560. flashlight.specular.b = this.configuration.lab.flashlight.specular.b;
  561. }
  562. }
  563. this.sceneManager.scene.constantlyUpdateMeshUnderPointer = true;
  564. this.sceneManager.scene.onPointerObservable.add((eventData) => {
  565. if (eventData.type === 4 && eventData.pickInfo) {
  566. lightTarget = (eventData.pickInfo.pickedPoint);
  567. } else {
  568. lightTarget = undefined;
  569. }
  570. });
  571. let updateFlashlightFunction = () => {
  572. if (this.sceneManager.camera && flashlight) {
  573. flashlight.position.copyFrom(this.sceneManager.camera.position);
  574. if (lightTarget) {
  575. lightTarget.subtractToRef(flashlight.position, flashlight.direction);
  576. }
  577. }
  578. }
  579. this.sceneManager.scene.registerBeforeRender(updateFlashlightFunction);
  580. this._registeredOnBeforeRenderFunctions.push(updateFlashlightFunction);
  581. }
  582. }
  583. }