defaultViewer.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. import { ViewerConfiguration, IModelConfiguration, ILightConfiguration } from './../configuration/configuration';
  2. import { Template, EventCallback } from './../templateManager';
  3. import { AbstractViewer } from './viewer';
  4. import { SpotLight, MirrorTexture, Plane, ShadowGenerator, Texture, BackgroundMaterial, Observable, ShadowLight, CubeTexture, BouncingBehavior, FramingBehavior, Behavior, Light, Engine, Scene, AutoRotationBehavior, AbstractMesh, Quaternion, StandardMaterial, ArcRotateCamera, ImageProcessingConfiguration, Color3, Vector3, SceneLoader, Mesh, HemisphericLight } from 'babylonjs';
  5. import { CameraBehavior } from '../interfaces';
  6. import { ViewerModel } from '../model/viewerModel';
  7. import { extendClassWithConfig } from '../helper';
  8. import { IModelAnimation } from 'model/modelAnimation';
  9. /**
  10. * The Default viewer is the default implementation of the AbstractViewer.
  11. * It uses the templating system to render a new canvas and controls.
  12. */
  13. export class DefaultViewer extends AbstractViewer {
  14. /**
  15. * Create a new default viewer
  16. * @param containerElement the element in which the templates will be rendered
  17. * @param initialConfiguration the initial configuration. Defaults to extending the default configuration
  18. */
  19. constructor(public containerElement: HTMLElement, initialConfiguration: ViewerConfiguration = { extends: 'default' }) {
  20. super(containerElement, initialConfiguration);
  21. this.onModelLoadedObservable.add(this._onModelLoaded);
  22. this.sceneManager.onSceneInitObservable.add(() => {
  23. // extendClassWithConfig(this.sceneManager.scene, this._configuration.scene);
  24. return this.sceneManager.scene;
  25. });
  26. this.sceneManager.onLightsConfiguredObservable.add((data) => {
  27. this._configureLights(data.newConfiguration, data.model!);
  28. })
  29. }
  30. /**
  31. * This will be executed when the templates initialize.
  32. */
  33. protected _onTemplatesLoaded() {
  34. this.showLoadingScreen();
  35. // navbar
  36. this._initNavbar();
  37. // close overlay button
  38. let closeButton = document.getElementById('close-button');
  39. if (closeButton) {
  40. closeButton.addEventListener('pointerdown', () => {
  41. this.hideOverlayScreen();
  42. })
  43. }
  44. return super._onTemplatesLoaded();
  45. }
  46. private _initNavbar() {
  47. let navbar = this.templateManager.getTemplate('navBar');
  48. if (navbar) {
  49. this.onFrameRenderedObservable.add(this._updateProgressBar);
  50. this.templateManager.eventManager.registerCallback('navBar', this._handlePointerDown, 'pointerdown');
  51. // an example how to trigger the help button. publiclly available
  52. this.templateManager.eventManager.registerCallback("navBar", () => {
  53. // do your thing
  54. }, "pointerdown", "#help-button");
  55. }
  56. }
  57. private _animationList: string[];
  58. private _currentAnimation: IModelAnimation;
  59. private _isAnimationPaused: boolean;
  60. private _handlePointerDown = (event: EventCallback) => {
  61. let pointerDown = <PointerEvent>event.event;
  62. if (pointerDown.button !== 0) return;
  63. var element = (<HTMLElement>event.event.srcElement);
  64. if (!element) {
  65. return;
  66. }
  67. let parentClasses = element.parentElement!.classList;
  68. switch (element.id) {
  69. case "speed-button":
  70. case "types-button":
  71. if (parentClasses.contains("open")) {
  72. parentClasses.remove("open");
  73. } else {
  74. parentClasses.add("open");
  75. }
  76. break;
  77. case "play-pause-button":
  78. this._togglePlayPause();
  79. break;
  80. case "label-option-button":
  81. var label = element.dataset["value"];
  82. if (label) {
  83. this._updateAnimationType(label);
  84. }
  85. break;
  86. case "speed-option-button":
  87. if (!this._currentAnimation) {
  88. return;
  89. }
  90. var speed = element.dataset["value"];
  91. if (speed)
  92. this._updateAnimationSpeed(speed);
  93. break;
  94. case "progress-bar-container":
  95. if (!this._currentAnimation) return;
  96. const gotoFrame = (pointerDown.offsetX / element.clientWidth) * this._currentAnimation.frames;
  97. if (isNaN(gotoFrame)) return;
  98. this._currentAnimation.goToFrame(gotoFrame);
  99. break;
  100. case "fullscreen-button":
  101. this.toggleFullscreen();
  102. default:
  103. return;
  104. }
  105. }
  106. /**
  107. * Plays or Pauses animation
  108. */
  109. private _togglePlayPause = () => {
  110. if (!this._currentAnimation) {
  111. return;
  112. }
  113. if (this._isAnimationPaused) {
  114. this._currentAnimation.restart();
  115. } else {
  116. this._currentAnimation.pause();
  117. }
  118. this._isAnimationPaused = !this._isAnimationPaused;
  119. let navbar = this.templateManager.getTemplate('navBar');
  120. if (!navbar) return;
  121. navbar.updateParams({
  122. paused: this._isAnimationPaused,
  123. });
  124. }
  125. /**
  126. * Control progress bar position based on animation current frame
  127. */
  128. private _updateProgressBar = () => {
  129. var progressWrapper = document.getElementById("progress-wrapper");
  130. if (progressWrapper && this._currentAnimation) {
  131. const progress = this._currentAnimation.currentFrame / this._currentAnimation.frames * 100;
  132. if (isNaN(progress)) return;
  133. progressWrapper.style.transform = "translateX(" + progress + "%)";
  134. }
  135. }
  136. /**
  137. * Update Current Animation Speed
  138. */
  139. private _updateAnimationSpeed = (speed: string) => {
  140. let navbar = this.templateManager.getTemplate('navBar');
  141. if (!navbar) return;
  142. if (speed && this._currentAnimation) {
  143. this._currentAnimation.speedRatio = parseFloat(speed);
  144. if (!this._isAnimationPaused) {
  145. this._currentAnimation.restart();
  146. }
  147. navbar.updateParams({
  148. selectedSpeed: speed + "x",
  149. });
  150. }
  151. }
  152. /**
  153. * Update Current Animation Type
  154. */
  155. private _updateAnimationType = (label: string) => {
  156. let navbar = this.templateManager.getTemplate('navBar');
  157. if (!navbar) return;
  158. if (label) {
  159. this._currentAnimation = this.sceneManager.models[0].setCurrentAnimationByName(label);
  160. }
  161. navbar.updateParams({
  162. selectedAnimation: (this._animationList.indexOf(label) + 1),
  163. });
  164. // reset speed when a new animation is selected
  165. this._updateAnimationSpeed("1.0");
  166. }
  167. /**
  168. * Toggle fullscreen of the entire viewer
  169. */
  170. public toggleFullscreen = () => {
  171. let viewerTemplate = this.templateManager.getTemplate('viewer');
  172. let viewerElement = viewerTemplate && viewerTemplate.parent;
  173. if (viewerElement) {
  174. let fullscreenElement = document.fullscreenElement || document.webkitFullscreenElement || (<any>document).mozFullScreenElement || (<any>document).msFullscreenElement;
  175. if (!fullscreenElement) {
  176. let requestFullScreen = viewerElement.requestFullscreen || viewerElement.webkitRequestFullscreen || (<any>viewerElement).msRequestFullscreen || (<any>viewerElement).mozRequestFullScreen;
  177. requestFullScreen.call(viewerElement);
  178. } else {
  179. let exitFullscreen = document.exitFullscreen || document.webkitExitFullscreen || (<any>document).msExitFullscreen || (<any>document).mozCancelFullScreen
  180. exitFullscreen.call(document);
  181. }
  182. }
  183. }
  184. /**
  185. * Preparing the container element to present the viewer
  186. */
  187. protected _prepareContainerElement() {
  188. this.containerElement.style.position = 'relative';
  189. this.containerElement.style.display = 'flex';
  190. }
  191. /**
  192. * This function will configure the templates and update them after a model was loaded
  193. * It is mainly responsible to changing the title and subtitle etc'.
  194. * @param model the model to be used to configure the templates by
  195. */
  196. protected _configureTemplate(model: ViewerModel) {
  197. let navbar = this.templateManager.getTemplate('navBar');
  198. if (!navbar) return;
  199. if (model.getAnimationNames().length >= 1) {
  200. this._animationList = model.getAnimationNames(),
  201. navbar.updateParams({
  202. animations: this._animationList,
  203. });
  204. // default animation & speed
  205. this._updateAnimationSpeed("1.0");
  206. this._isAnimationPaused = !!(model.configuration.animation && model.configuration.animation.autoStart);
  207. this._updateAnimationType(this._animationList[0]);
  208. }
  209. let modelConfiguration = model.configuration;
  210. }
  211. /**
  212. * This will load a new model to the default viewer
  213. * overriding the AbstractViewer's loadModel.
  214. * The scene will automatically be cleared of the old models, if exist.
  215. * @param model the configuration object (or URL) to load.
  216. */
  217. public loadModel(model: any = this._configuration.model): Promise<ViewerModel> {
  218. this.showLoadingScreen();
  219. return super.loadModel(model, true).catch((error) => {
  220. console.log(error);
  221. this.hideLoadingScreen();
  222. this.showOverlayScreen('error');
  223. return Promise.reject(error);
  224. });
  225. }
  226. private _onModelLoaded = (model: ViewerModel) => {
  227. this._configureTemplate(model);
  228. // with a short timeout, making sure everything is there already.
  229. let hideLoadingDelay = 500;
  230. if (this._configuration.lab && this._configuration.lab.hideLoadingDelay !== undefined) {
  231. hideLoadingDelay = this._configuration.lab.hideLoadingDelay;
  232. }
  233. setTimeout(() => {
  234. this.hideLoadingScreen();
  235. }, hideLoadingDelay);
  236. return;
  237. }
  238. /**
  239. * Show the overlay and the defined sub-screen.
  240. * Mainly used for help and errors
  241. * @param subScreen the name of the subScreen. Those can be defined in the configuration object
  242. */
  243. public showOverlayScreen(subScreen: string) {
  244. let template = this.templateManager.getTemplate('overlay');
  245. if (!template) return Promise.resolve('Overlay template not found');
  246. return template.show((template => {
  247. var canvasRect = this.containerElement.getBoundingClientRect();
  248. var canvasPositioning = window.getComputedStyle(this.containerElement).position;
  249. template.parent.style.display = 'flex';
  250. template.parent.style.width = canvasRect.width + "px";
  251. template.parent.style.height = canvasRect.height + "px";
  252. template.parent.style.opacity = "1";
  253. let subTemplate = this.templateManager.getTemplate(subScreen);
  254. if (!subTemplate) {
  255. return Promise.reject(subScreen + ' template not found');
  256. }
  257. return subTemplate.show((template => {
  258. template.parent.style.display = 'flex';
  259. return Promise.resolve(template);
  260. }));
  261. }));
  262. }
  263. /**
  264. * Hide the overlay screen.
  265. */
  266. public hideOverlayScreen() {
  267. let template = this.templateManager.getTemplate('overlay');
  268. if (!template) return Promise.resolve('Overlay template not found');
  269. return template.hide((template => {
  270. template.parent.style.opacity = "0";
  271. let onTransitionEnd = () => {
  272. template.parent.removeEventListener("transitionend", onTransitionEnd);
  273. template.parent.style.display = 'none';
  274. }
  275. template.parent.addEventListener("transitionend", onTransitionEnd);
  276. let overlays = template.parent.querySelectorAll('.overlay');
  277. if (overlays) {
  278. for (let i = 0; i < overlays.length; ++i) {
  279. let htmlElement = <HTMLElement>overlays.item(i);
  280. htmlElement.style.display = 'none';
  281. }
  282. }
  283. return Promise.resolve(template);
  284. }));
  285. }
  286. /**
  287. * show the viewer (in case it was hidden)
  288. *
  289. * @param visibilityFunction an optional function to execute in order to show the container
  290. */
  291. public show(visibilityFunction?: ((template: Template) => Promise<Template>)): Promise<Template> {
  292. let template = this.templateManager.getTemplate('main');
  293. //not possible, but yet:
  294. if (!template) return Promise.reject('Main template not found');
  295. return template.show(visibilityFunction);
  296. }
  297. /**
  298. * hide the viewer (in case it is visible)
  299. *
  300. * @param visibilityFunction an optional function to execute in order to hide the container
  301. */
  302. public hide(visibilityFunction?: ((template: Template) => Promise<Template>)) {
  303. let template = this.templateManager.getTemplate('main');
  304. //not possible, but yet:
  305. if (!template) return Promise.reject('Main template not found');
  306. return template.hide(visibilityFunction);
  307. }
  308. /**
  309. * Show the loading screen.
  310. * The loading screen can be configured using the configuration object
  311. */
  312. public showLoadingScreen() {
  313. let template = this.templateManager.getTemplate('loadingScreen');
  314. if (!template) return Promise.resolve('Loading Screen template not found');
  315. return template.show((template => {
  316. var canvasRect = this.containerElement.getBoundingClientRect();
  317. var canvasPositioning = window.getComputedStyle(this.containerElement).position;
  318. template.parent.style.display = 'flex';
  319. template.parent.style.width = canvasRect.width + "px";
  320. template.parent.style.height = canvasRect.height + "px";
  321. template.parent.style.opacity = "1";
  322. // from the configuration!!!
  323. template.parent.style.backgroundColor = "black";
  324. return Promise.resolve(template);
  325. }));
  326. }
  327. /**
  328. * Hide the loading screen
  329. */
  330. public hideLoadingScreen() {
  331. let template = this.templateManager.getTemplate('loadingScreen');
  332. if (!template) return Promise.resolve('Loading Screen template not found');
  333. return template.hide((template => {
  334. template.parent.style.opacity = "0";
  335. let onTransitionEnd = () => {
  336. template.parent.removeEventListener("transitionend", onTransitionEnd);
  337. template.parent.style.display = 'none';
  338. }
  339. template.parent.addEventListener("transitionend", onTransitionEnd);
  340. return Promise.resolve(template);
  341. }));
  342. }
  343. /**
  344. * An extension of the light configuration of the abstract viewer.
  345. * @param lightsConfiguration the light configuration to use
  346. * @param model the model that will be used to configure the lights (if the lights are model-dependant)
  347. */
  348. private _configureLights(lightsConfiguration: { [name: string]: ILightConfiguration | boolean } = {}, model?: ViewerModel) {
  349. // labs feature - flashlight
  350. if (this._configuration.lab && this._configuration.lab.flashlight) {
  351. let pointerPosition = Vector3.Zero();
  352. let lightTarget;
  353. let angle = 0.5;
  354. let exponent = Math.PI / 2;
  355. if (typeof this._configuration.lab.flashlight === "object") {
  356. exponent = this._configuration.lab.flashlight.exponent || exponent;
  357. angle = this._configuration.lab.flashlight.angle || angle;
  358. }
  359. var flashlight = new SpotLight("flashlight", Vector3.Zero(),
  360. Vector3.Zero(), exponent, angle, this.sceneManager.scene);
  361. if (typeof this._configuration.lab.flashlight === "object") {
  362. flashlight.intensity = this._configuration.lab.flashlight.intensity || flashlight.intensity;
  363. if (this._configuration.lab.flashlight.diffuse) {
  364. flashlight.diffuse.r = this._configuration.lab.flashlight.diffuse.r;
  365. flashlight.diffuse.g = this._configuration.lab.flashlight.diffuse.g;
  366. flashlight.diffuse.b = this._configuration.lab.flashlight.diffuse.b;
  367. }
  368. if (this._configuration.lab.flashlight.specular) {
  369. flashlight.specular.r = this._configuration.lab.flashlight.specular.r;
  370. flashlight.specular.g = this._configuration.lab.flashlight.specular.g;
  371. flashlight.specular.b = this._configuration.lab.flashlight.specular.b;
  372. }
  373. }
  374. this.sceneManager.scene.constantlyUpdateMeshUnderPointer = true;
  375. this.sceneManager.scene.onPointerObservable.add((eventData, eventState) => {
  376. if (eventData.type === 4 && eventData.pickInfo) {
  377. lightTarget = (eventData.pickInfo.pickedPoint);
  378. } else {
  379. lightTarget = undefined;
  380. }
  381. });
  382. let updateFlashlightFunction = () => {
  383. if (this.sceneManager.camera && flashlight) {
  384. flashlight.position.copyFrom(this.sceneManager.camera.position);
  385. if (lightTarget) {
  386. lightTarget.subtractToRef(flashlight.position, flashlight.direction);
  387. }
  388. }
  389. }
  390. this.sceneManager.scene.registerBeforeRender(updateFlashlightFunction);
  391. this._registeredOnBeforeRenderFunctions.push(updateFlashlightFunction);
  392. }
  393. }
  394. }