defaultViewer.ts 19 KB

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