defaultViewer.ts 19 KB

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