defaultViewer.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. import { ViewerConfiguration } from './../configuration/configuration';
  2. import { Template } from './../templateManager';
  3. import { AbstractViewer } from './viewer';
  4. import { 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. 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 || (<any>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(meshes);
  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(focusMeshes: Array<AbstractMesh> = []): 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. box && focusMeshes.push(box);
  173. }
  174. }
  175. if (this.configuration.ground) {
  176. let groundConfig = (typeof this.configuration.ground === 'boolean') ? {} : this.configuration.ground;
  177. let groundSize = groundConfig.size || (this.configuration.skybox && this.configuration.skybox.scale) || 3000;
  178. let ground = Mesh.CreatePlane("BackgroundPlane", groundSize, this.scene);
  179. let backgroundMaterial = new BackgroundMaterial('groundmat', this.scene);
  180. ground.rotation.x = Math.PI / 2; // Face up by default.
  181. ground.receiveShadows = groundConfig.receiveShadows || false;
  182. // default values
  183. backgroundMaterial.alpha = 0.9;
  184. backgroundMaterial.alphaMode = Engine.ALPHA_PREMULTIPLIED_PORTERDUFF;
  185. backgroundMaterial.shadowLevel = 0.5;
  186. backgroundMaterial.primaryLevel = 1;
  187. backgroundMaterial.primaryColor = new Color3(0.2, 0.2, 0.3).toLinearSpace().scale(3);
  188. backgroundMaterial.secondaryLevel = 0;
  189. backgroundMaterial.tertiaryLevel = 0;
  190. backgroundMaterial.useRGBColor = false;
  191. backgroundMaterial.enableNoise = true;
  192. // if config provided, extend the default values
  193. if (groundConfig.material) {
  194. this.extendClassWithConfig(ground, ground.material);
  195. }
  196. ground.material = backgroundMaterial;
  197. if (this.configuration.ground === true || groundConfig.shadowOnly) {
  198. // shadow only:
  199. ground.receiveShadows = true;
  200. const diffuseTexture = new Texture("https://assets.babylonjs.com/environments/backgroundGround.png", this.scene);
  201. diffuseTexture.gammaSpace = false;
  202. diffuseTexture.hasAlpha = true;
  203. backgroundMaterial.diffuseTexture = diffuseTexture;
  204. } else if (groundConfig.mirror) {
  205. var mirror = new MirrorTexture("mirror", 512, this.scene);
  206. mirror.mirrorPlane = new Plane(0, -1, 0, 0);
  207. mirror.renderList = mirror.renderList || [];
  208. focusMeshes.length && focusMeshes.forEach(m => {
  209. m && mirror.renderList && mirror.renderList.push(m);
  210. });
  211. backgroundMaterial.reflectionTexture = mirror;
  212. } else {
  213. if (groundConfig.material) {
  214. if (groundConfig.material.diffuseTexture) {
  215. const diffuseTexture = new Texture(groundConfig.material.diffuseTexture, this.scene);
  216. backgroundMaterial.diffuseTexture = diffuseTexture;
  217. }
  218. }
  219. // ground.material = new StandardMaterial('groundmat', this.scene);
  220. }
  221. //default configuration
  222. if (this.configuration.ground === true) {
  223. ground.receiveShadows = true;
  224. if (ground.material)
  225. ground.material.alpha = 0.4;
  226. }
  227. this.extendClassWithConfig(ground, groundConfig);
  228. }
  229. return Promise.resolve(this.scene);
  230. }
  231. public showOverlayScreen(subScreen: string) {
  232. let template = this.templateManager.getTemplate('overlay');
  233. if (!template) return Promise.reject('Overlay template not found');
  234. return template.show((template => {
  235. var canvasRect = this.containerElement.getBoundingClientRect();
  236. var canvasPositioning = window.getComputedStyle(this.containerElement).position;
  237. template.parent.style.display = 'flex';
  238. template.parent.style.width = canvasRect.width + "px";
  239. template.parent.style.height = canvasRect.height + "px";
  240. template.parent.style.opacity = "1";
  241. let subTemplate = this.templateManager.getTemplate(subScreen);
  242. if (!subTemplate) {
  243. return Promise.reject(subScreen + ' template not found');
  244. }
  245. return subTemplate.show((template => {
  246. template.parent.style.display = 'flex';
  247. return Promise.resolve(template);
  248. }));
  249. }));
  250. }
  251. public hideOverlayScreen() {
  252. let template = this.templateManager.getTemplate('overlay');
  253. if (!template) return Promise.reject('Overlay template not found');
  254. return template.hide((template => {
  255. template.parent.style.opacity = "0";
  256. let onTransitionEnd = () => {
  257. template.parent.removeEventListener("transitionend", onTransitionEnd);
  258. template.parent.style.display = 'none';
  259. }
  260. template.parent.addEventListener("transitionend", onTransitionEnd);
  261. let overlays = template.parent.querySelectorAll('.overlay');
  262. if (overlays) {
  263. for (let i = 0; i < overlays.length; ++i) {
  264. let htmlElement = <HTMLElement>overlays.item(i);
  265. htmlElement.style.display = 'none';
  266. }
  267. }
  268. /*return this.templateManager.getTemplate(subScreen).show((template => {
  269. template.parent.style.display = 'none';
  270. return Promise.resolve(template);
  271. }));*/
  272. return Promise.resolve(template);
  273. }));
  274. }
  275. public showLoadingScreen() {
  276. let template = this.templateManager.getTemplate('loadingScreen');
  277. if (!template) return Promise.reject('oading Screen template not found');
  278. return template.show((template => {
  279. var canvasRect = this.containerElement.getBoundingClientRect();
  280. var canvasPositioning = window.getComputedStyle(this.containerElement).position;
  281. template.parent.style.display = 'flex';
  282. template.parent.style.width = canvasRect.width + "px";
  283. template.parent.style.height = canvasRect.height + "px";
  284. template.parent.style.opacity = "1";
  285. // from the configuration!!!
  286. template.parent.style.backgroundColor = "black";
  287. return Promise.resolve(template);
  288. }));
  289. }
  290. public hideLoadingScreen() {
  291. let template = this.templateManager.getTemplate('loadingScreen');
  292. if (!template) return Promise.reject('oading Screen template not found');
  293. return template.hide((template => {
  294. template.parent.style.opacity = "0";
  295. let onTransitionEnd = () => {
  296. template.parent.removeEventListener("transitionend", onTransitionEnd);
  297. template.parent.style.display = 'none';
  298. }
  299. template.parent.addEventListener("transitionend", onTransitionEnd);
  300. return Promise.resolve(template);
  301. }));
  302. }
  303. private setupLights(focusMeshes: Array<AbstractMesh> = []) {
  304. let sceneConfig = this.configuration.scene || { defaultLight: true };
  305. if (!sceneConfig.defaultLight && (this.configuration.lights && Object.keys(this.configuration.lights).length)) {
  306. // remove old lights
  307. this.scene.lights.forEach(l => {
  308. l.dispose();
  309. });
  310. Object.keys(this.configuration.lights).forEach((name, idx) => {
  311. let lightConfig = this.configuration.lights && this.configuration.lights[name] || { name: name, type: 0 };
  312. lightConfig.name = name;
  313. let constructor = Light.GetConstructorFromName(lightConfig.type, lightConfig.name, this.scene);
  314. if (!constructor) return;
  315. let light = constructor();
  316. //enabled
  317. if (light.isEnabled() !== !lightConfig.disabled) {
  318. light.setEnabled(!lightConfig.disabled);
  319. }
  320. this.extendClassWithConfig(light, lightConfig);
  321. //position. Some lights don't support shadows
  322. if (light instanceof ShadowLight) {
  323. if (lightConfig.shadowEnabled) {
  324. var shadowGenerator = new ShadowGenerator(512, light);
  325. this.extendClassWithConfig(shadowGenerator, lightConfig.shadowConfig || {});
  326. // add the focues meshes to the shadow list
  327. let shadownMap = shadowGenerator.getShadowMap();
  328. if (!shadownMap) return;
  329. let renderList = shadownMap.renderList;
  330. for (var index = 0; index < focusMeshes.length; index++) {
  331. renderList && renderList.push(focusMeshes[index]);
  332. }
  333. }
  334. }
  335. });
  336. }
  337. }
  338. private setupCamera(focusMeshes: Array<AbstractMesh> = []) {
  339. let cameraConfig = this.configuration.camera || {};
  340. let sceneConfig = this.configuration.scene || { autoRotate: false, defaultCamera: true };
  341. if (!this.configuration.camera && sceneConfig.defaultCamera) {
  342. if (sceneConfig.autoRotate) {
  343. this.camera.useAutoRotationBehavior = true;
  344. }
  345. return;
  346. }
  347. if (cameraConfig.position) {
  348. this.camera.position.copyFromFloats(cameraConfig.position.x || 0, cameraConfig.position.y || 0, cameraConfig.position.z || 0);
  349. }
  350. if (cameraConfig.rotation) {
  351. this.camera.rotationQuaternion = new Quaternion(cameraConfig.rotation.x || 0, cameraConfig.rotation.y || 0, cameraConfig.rotation.z || 0, cameraConfig.rotation.w || 0)
  352. }
  353. this.camera.minZ = cameraConfig.minZ || this.camera.minZ;
  354. this.camera.maxZ = cameraConfig.maxZ || this.camera.maxZ;
  355. if (cameraConfig.behaviors) {
  356. for (let name in cameraConfig.behaviors) {
  357. this.setCameraBehavior(cameraConfig.behaviors[name], focusMeshes);
  358. }
  359. };
  360. if (sceneConfig.autoRotate) {
  361. this.camera.useAutoRotationBehavior = true;
  362. }
  363. }
  364. private setCameraBehavior(behaviorConfig: number | {
  365. type: number;
  366. [propName: string]: any;
  367. }, payload: any) {
  368. let behavior: Behavior<ArcRotateCamera> | null;
  369. let type = (typeof behaviorConfig !== "object") ? behaviorConfig : behaviorConfig.type;
  370. let config: { [propName: string]: any } = (typeof behaviorConfig === "object") ? behaviorConfig : {};
  371. // constructing behavior
  372. switch (type) {
  373. case CameraBehavior.AUTOROTATION:
  374. behavior = new AutoRotationBehavior();
  375. break;
  376. case CameraBehavior.BOUNCING:
  377. behavior = new BouncingBehavior();
  378. break;
  379. case CameraBehavior.FRAMING:
  380. behavior = new FramingBehavior();
  381. break;
  382. default:
  383. behavior = null;
  384. break;
  385. }
  386. if (behavior) {
  387. if (typeof behaviorConfig === "object") {
  388. this.extendClassWithConfig(behavior, behaviorConfig);
  389. }
  390. this.camera.addBehavior(behavior);
  391. }
  392. // post attach configuration. Some functionalities require the attached camera.
  393. switch (type) {
  394. case CameraBehavior.AUTOROTATION:
  395. break;
  396. case CameraBehavior.BOUNCING:
  397. break;
  398. case CameraBehavior.FRAMING:
  399. if (config.zoomOnBoundingInfo) {
  400. //payload is an array of meshes
  401. let meshes = <Array<AbstractMesh>>payload;
  402. let bounding = meshes[0].getHierarchyBoundingVectors();
  403. (<FramingBehavior>behavior).zoomOnBoundingInfo(bounding.min, bounding.max);
  404. }
  405. break;
  406. }
  407. }
  408. private extendClassWithConfig(object: any, config: any) {
  409. if (!config) return;
  410. Object.keys(config).forEach(key => {
  411. if (key in object && typeof object[key] !== 'function') {
  412. if (typeof object[key] === 'function') return;
  413. // if it is an object, iterate internally until reaching basic types
  414. if (typeof object[key] === 'object') {
  415. this.extendClassWithConfig(object[key], config[key]);
  416. } else {
  417. object[key] = config[key];
  418. }
  419. }
  420. });
  421. }
  422. }