defaultViewer.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. import { Template } from './../templateManager';
  2. import { AbstractViewer } from './viewer';
  3. import { Observable, ShadowLight, CubeTexture, BouncingBehavior, FramingBehavior, Behavior, Light, Engine, Scene, AutoRotationBehavior, AbstractMesh, Quaternion, StandardMaterial, ShadowOnlyMaterial, ArcRotateCamera, ImageProcessingConfiguration, Color3, Vector3, SceneLoader, Mesh, HemisphericLight } from 'babylonjs';
  4. import { CameraBehavior } from '../interfaces';
  5. // A small hack for the inspector. to be removed!
  6. import * as BABYLON from 'babylonjs';
  7. window['BABYLON'] = BABYLON;
  8. export class DefaultViewer extends AbstractViewer {
  9. private camera: ArcRotateCamera;
  10. public initScene(): Promise<Scene> {
  11. return super.initScene().then(() => {
  12. this.extendClassWithConfig(this.scene, this.configuration.scene);
  13. return this.scene;
  14. })
  15. }
  16. protected onTemplatesLoaded() {
  17. this.showLoadingScreen();
  18. // navbar
  19. let viewerElement = this.templateManager.getTemplate('viewer');
  20. let navbar = this.templateManager.getTemplate('navBar');
  21. if (viewerElement && navbar) {
  22. let navbarHeight = navbar.parent.clientHeight + 'px';
  23. let navbarShown: boolean = true;
  24. let timeoutCancel /*: number*/;
  25. let triggerNavbar = function (show: boolean = false, evt: PointerEvent) {
  26. // only left-click on no-button.
  27. if (!navbar || evt.button > 0) return;
  28. // clear timeout
  29. timeoutCancel && clearTimeout(timeoutCancel);
  30. // if state is the same, do nothing
  31. if (show === navbarShown) return;
  32. //showing? simply show it!
  33. if (show) {
  34. navbar.parent.style.bottom = show ? '0px' : '-' + navbarHeight;
  35. navbarShown = show;
  36. } else {
  37. let visibilityTimeout = 2000;
  38. if (navbar.configuration.params && navbar.configuration.params.visibilityTimeout !== undefined) {
  39. visibilityTimeout = <number>navbar.configuration.params.visibilityTimeout;
  40. }
  41. // not showing? set timeout until it is removed.
  42. timeoutCancel = setTimeout(function () {
  43. if (navbar) {
  44. navbar.parent.style.bottom = '-' + navbarHeight;
  45. }
  46. navbarShown = show;
  47. }, visibilityTimeout);
  48. }
  49. }
  50. viewerElement.parent.addEventListener('pointerout', triggerNavbar.bind(this, false));
  51. viewerElement.parent.addEventListener('pointerdown', triggerNavbar.bind(this, true));
  52. viewerElement.parent.addEventListener('pointerup', triggerNavbar.bind(this, false));
  53. navbar.parent.addEventListener('pointerover', triggerNavbar.bind(this, true))
  54. // triggerNavbar(false);
  55. // events registration
  56. this.registerNavbarButtons();
  57. }
  58. // close overlay button
  59. let closeButton = document.getElementById('close-button');
  60. if (closeButton) {
  61. closeButton.addEventListener('pointerdown', () => {
  62. this.hideOverlayScreen();
  63. })
  64. }
  65. return super.onTemplatesLoaded();
  66. }
  67. private registerNavbarButtons() {
  68. let isFullscreen = false;
  69. let navbar = this.templateManager.getTemplate('navBar');
  70. let viewerTemplate = this.templateManager.getTemplate('viewer');
  71. if (!navbar || !viewerTemplate) return;
  72. let viewerElement = viewerTemplate.parent;
  73. navbar.onEventTriggered.add((data) => {
  74. switch (data.event.type) {
  75. case 'pointerdown':
  76. let event: PointerEvent = <PointerEvent>data.event;
  77. if (event.button === 0) {
  78. switch (data.selector) {
  79. case '#fullscreen-button':
  80. if (!isFullscreen) {
  81. let requestFullScreen = viewerElement.requestFullscreen || viewerElement.webkitRequestFullscreen || (<any>viewerElement).msRequestFullscreen || (<any>viewerElement).mozRequestFullScreen;
  82. requestFullScreen.call(viewerElement);
  83. } else {
  84. let exitFullscreen = document.exitFullscreen || document.webkitExitFullscreen
  85. exitFullscreen.call(document);
  86. }
  87. isFullscreen = !isFullscreen;
  88. break;
  89. case '#help-button':
  90. this.showOverlayScreen('help');
  91. break;
  92. }
  93. }
  94. break;
  95. }
  96. });
  97. }
  98. protected prepareContainerElement() {
  99. this.containerElement.style.position = 'relative';
  100. this.containerElement.style.display = 'flex';
  101. }
  102. public loadModel(model: any = this.configuration.model): Promise<Scene> {
  103. this.showLoadingScreen();
  104. return super.loadModel(model, true).catch((error) => {
  105. console.log(error);
  106. this.hideLoadingScreen();
  107. this.showOverlayScreen('error');
  108. return this.scene;
  109. });
  110. }
  111. public onModelLoaded(meshes: Array<AbstractMesh>) {
  112. // here we could set the navbar's model information:
  113. this.setModelMetaData();
  114. // with a short timeout, making sure everything is there already.
  115. setTimeout(() => {
  116. this.hideLoadingScreen();
  117. }, 500);
  118. // recreate the camera
  119. this.scene.createDefaultCameraOrLight(true, true, true);
  120. this.camera = <ArcRotateCamera>this.scene.activeCamera;
  121. meshes[0].rotation.y += Math.PI;
  122. this.setupCamera(meshes);
  123. this.setupLights(meshes);
  124. return this.initEnvironment();
  125. }
  126. private setModelMetaData() {
  127. let navbar = this.templateManager.getTemplate('navBar');
  128. if (!navbar) return;
  129. let metadataContainer = navbar.parent.querySelector('#model-metadata');
  130. //title
  131. if (metadataContainer && typeof this.configuration.model === 'object') {
  132. if (this.configuration.model.title) {
  133. let element = metadataContainer.querySelector('span.model-title');
  134. if (element) {
  135. element.innerHTML = this.configuration.model.title;
  136. }
  137. }
  138. if (this.configuration.model.subtitle) {
  139. let element = metadataContainer.querySelector('span.model-subtitle');
  140. if (element) {
  141. element.innerHTML = this.configuration.model.subtitle;
  142. }
  143. }
  144. if (this.configuration.model.thumbnail) {
  145. (<HTMLDivElement>metadataContainer.querySelector('.thumbnail')).style.backgroundImage = `url('${this.configuration.model.thumbnail}')`;
  146. }
  147. }
  148. }
  149. public initEnvironment(): Promise<Scene> {
  150. if (this.configuration.skybox) {
  151. // Define a general environment textue
  152. let texture;
  153. // this is obligatory, but still - making sure it is there.
  154. if (this.configuration.skybox.cubeTexture) {
  155. if (typeof this.configuration.skybox.cubeTexture.url === 'string') {
  156. texture = CubeTexture.CreateFromPrefilteredData(this.configuration.skybox.cubeTexture.url, this.scene);
  157. } else {
  158. texture = CubeTexture.CreateFromImages(this.configuration.skybox.cubeTexture.url, this.scene, this.configuration.skybox.cubeTexture.noMipMap);
  159. }
  160. }
  161. if (texture) {
  162. this.extendClassWithConfig(texture, this.configuration.skybox.cubeTexture);
  163. let scale = this.configuration.skybox.scale || (this.scene.activeCamera.maxZ - this.scene.activeCamera.minZ) / 2;
  164. let box = this.scene.createDefaultSkybox(texture, this.configuration.skybox.pbr, scale, this.configuration.skybox.blur);
  165. // before extending, set the material's imageprocessing configuration object, if needed:
  166. if (this.configuration.skybox.material && this.configuration.skybox.material.imageProcessingConfiguration) {
  167. (<StandardMaterial>box.material).imageProcessingConfiguration = new ImageProcessingConfiguration();
  168. }
  169. this.extendClassWithConfig(box, this.configuration.skybox);
  170. }
  171. }
  172. if (this.configuration.ground) {
  173. let groundConfig = (typeof this.configuration.ground === 'boolean') ? {} : this.configuration.ground;
  174. var ground = Mesh.CreateGround('ground', groundConfig.size || 1000, groundConfig.size || 1000, 8, this.scene);
  175. if (this.configuration.ground === true || groundConfig.shadowOnly) {
  176. ground.material = new ShadowOnlyMaterial('groundmat', this.scene);
  177. } else {
  178. ground.material = new StandardMaterial('groundmat', this.scene);
  179. }
  180. //default configuration
  181. if (this.configuration.ground === true) {
  182. ground.receiveShadows = true;
  183. ground.material.alpha = 0.4;
  184. }
  185. this.extendClassWithConfig(ground, groundConfig);
  186. }
  187. return Promise.resolve(this.scene);
  188. }
  189. public showOverlayScreen(subScreen: string) {
  190. let template = this.templateManager.getTemplate('overlay');
  191. if (!template) return Promise.reject('Overlay template not found');
  192. return template.show((template => {
  193. var canvasRect = this.containerElement.getBoundingClientRect();
  194. var canvasPositioning = window.getComputedStyle(this.containerElement).position;
  195. template.parent.style.display = 'flex';
  196. template.parent.style.width = canvasRect.width + "px";
  197. template.parent.style.height = canvasRect.height + "px";
  198. template.parent.style.opacity = "1";
  199. let subTemplate = this.templateManager.getTemplate(subScreen);
  200. if (!subTemplate) {
  201. return Promise.reject(subScreen + ' template not found');
  202. }
  203. return subTemplate.show((template => {
  204. template.parent.style.display = 'flex';
  205. return Promise.resolve(template);
  206. }));
  207. }));
  208. }
  209. public hideOverlayScreen() {
  210. let template = this.templateManager.getTemplate('overlay');
  211. if (!template) return Promise.reject('Overlay template not found');
  212. return template.hide((template => {
  213. template.parent.style.opacity = "0";
  214. let onTransitionEnd = () => {
  215. template.parent.removeEventListener("transitionend", onTransitionEnd);
  216. template.parent.style.display = 'none';
  217. }
  218. template.parent.addEventListener("transitionend", onTransitionEnd);
  219. let overlays = template.parent.querySelectorAll('.overlay');
  220. if (overlays) {
  221. for (let i = 0; i < overlays.length; ++i) {
  222. let htmlElement = <HTMLElement>overlays.item(i);
  223. htmlElement.style.display = 'none';
  224. }
  225. }
  226. /*return this.templateManager.getTemplate(subScreen).show((template => {
  227. template.parent.style.display = 'none';
  228. return Promise.resolve(template);
  229. }));*/
  230. return Promise.resolve(template);
  231. }));
  232. }
  233. public showLoadingScreen() {
  234. let template = this.templateManager.getTemplate('loadingScreen');
  235. if (!template) return Promise.reject('oading Screen template not found');
  236. return template.show((template => {
  237. var canvasRect = this.containerElement.getBoundingClientRect();
  238. var canvasPositioning = window.getComputedStyle(this.containerElement).position;
  239. template.parent.style.display = 'flex';
  240. template.parent.style.width = canvasRect.width + "px";
  241. template.parent.style.height = canvasRect.height + "px";
  242. template.parent.style.opacity = "1";
  243. // from the configuration!!!
  244. template.parent.style.backgroundColor = "black";
  245. return Promise.resolve(template);
  246. }));
  247. }
  248. public hideLoadingScreen() {
  249. let template = this.templateManager.getTemplate('loadingScreen');
  250. if (!template) return Promise.reject('oading Screen template not found');
  251. return template.hide((template => {
  252. template.parent.style.opacity = "0";
  253. let onTransitionEnd = () => {
  254. template.parent.removeEventListener("transitionend", onTransitionEnd);
  255. template.parent.style.display = 'none';
  256. }
  257. template.parent.addEventListener("transitionend", onTransitionEnd);
  258. return Promise.resolve(template);
  259. }));
  260. }
  261. private setupLights(focusMeshes: Array<AbstractMesh> = []) {
  262. let sceneConfig = this.configuration.scene || { defaultLight: true };
  263. if (!sceneConfig.defaultLight && (this.configuration.lights && Object.keys(this.configuration.lights).length)) {
  264. // remove old lights
  265. this.scene.lights.forEach(l => {
  266. l.dispose();
  267. });
  268. Object.keys(this.configuration.lights).forEach((name, idx) => {
  269. let lightConfig = this.configuration.lights && this.configuration.lights[name] || { name: name, type: 0 };
  270. lightConfig.name = name;
  271. let constructor = Light.GetConstructorFromName(lightConfig.type, lightConfig.name, this.scene);
  272. let light = constructor();
  273. //enabled
  274. if (light.isEnabled() !== !lightConfig.disabled) {
  275. light.setEnabled(!lightConfig.disabled);
  276. }
  277. this.extendClassWithConfig(light, lightConfig);
  278. //position. Some lights don't support shadows
  279. if (light instanceof ShadowLight) {
  280. if (lightConfig.shadowEnabled) {
  281. var shadowGenerator = new BABYLON.ShadowGenerator(512, light)
  282. this.extendClassWithConfig(shadowGenerator, lightConfig.shadowConfig || {});
  283. // add the focues meshes to the shadow list
  284. for (var index = 0; index < focusMeshes.length; index++) {
  285. shadowGenerator.getShadowMap().renderList.push(focusMeshes[index]);
  286. }
  287. }
  288. }
  289. });
  290. }
  291. }
  292. private setupCamera(focusMeshes: Array<AbstractMesh> = []) {
  293. let sceneConfig = this.configuration.scene || { autoRotate: false, defaultCamera: true };
  294. if (sceneConfig.defaultCamera) {
  295. return;
  296. }
  297. let cameraConfig = this.configuration.camera || {};
  298. if (cameraConfig.position) {
  299. this.camera.position.copyFromFloats(cameraConfig.position.x || 0, cameraConfig.position.y || 0, cameraConfig.position.z || 0);
  300. }
  301. if (cameraConfig.rotation) {
  302. this.camera.rotationQuaternion = new Quaternion(cameraConfig.rotation.x || 0, cameraConfig.rotation.y || 0, cameraConfig.rotation.z || 0, cameraConfig.rotation.w || 0)
  303. }
  304. this.camera.minZ = cameraConfig.minZ || this.camera.minZ;
  305. this.camera.maxZ = cameraConfig.maxZ || this.camera.maxZ;
  306. if (cameraConfig.behaviors) {
  307. for (let name in cameraConfig.behaviors) {
  308. this.setCameraBehavior(cameraConfig.behaviors[name], focusMeshes);
  309. }
  310. };
  311. if (sceneConfig.autoRotate) {
  312. this.camera.useAutoRotationBehavior = true;
  313. }
  314. }
  315. private setCameraBehavior(behaviorConfig: number | {
  316. type: number;
  317. [propName: string]: any;
  318. }, payload: any) {
  319. let behavior: Behavior<ArcRotateCamera> | null;
  320. let type = (typeof behaviorConfig !== "object") ? behaviorConfig : behaviorConfig.type;
  321. let config: { [propName: string]: any } = (typeof behaviorConfig === "object") ? behaviorConfig : {};
  322. // constructing behavior
  323. switch (type) {
  324. case CameraBehavior.AUTOROTATION:
  325. behavior = new AutoRotationBehavior();
  326. break;
  327. case CameraBehavior.BOUNCING:
  328. behavior = new BouncingBehavior();
  329. break;
  330. case CameraBehavior.FRAMING:
  331. behavior = new FramingBehavior();
  332. break;
  333. default:
  334. behavior = null;
  335. break;
  336. }
  337. if (behavior) {
  338. if (typeof behaviorConfig === "object") {
  339. this.extendClassWithConfig(behavior, behaviorConfig);
  340. }
  341. this.camera.addBehavior(behavior);
  342. }
  343. // post attach configuration. Some functionalities require the attached camera.
  344. switch (type) {
  345. case CameraBehavior.AUTOROTATION:
  346. break;
  347. case CameraBehavior.BOUNCING:
  348. break;
  349. case CameraBehavior.FRAMING:
  350. if (config.zoomOnBoundingInfo) {
  351. //payload is an array of meshes
  352. let meshes = <Array<AbstractMesh>>payload;
  353. let bounding = meshes[0].getHierarchyBoundingVectors();
  354. (<FramingBehavior>behavior).zoomOnBoundingInfo(bounding.min, bounding.max);
  355. }
  356. break;
  357. }
  358. }
  359. private extendClassWithConfig(object: any, config: any) {
  360. if (!config) return;
  361. Object.keys(config).forEach(key => {
  362. if (key in object && typeof object[key] !== 'function') {
  363. if (typeof object[key] === 'function') return;
  364. // if it is an object, iterate internally until reaching basic types
  365. if (typeof object[key] === 'object') {
  366. this.extendClassWithConfig(object[key], config[key]);
  367. } else {
  368. object[key] = config[key];
  369. }
  370. }
  371. });
  372. }
  373. }