Advertisement
yskang

threejs-viewer-25

May 12th, 2021
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 修改 js/Viewer.js
  2. // … Other code snippets
  3.  
  4. getFitBounds(dbIds) {
  5.   let targets = [];
  6.  
  7.   if (Array.isArray(dbIds) && dbIds.length > 0) {
  8.     this.models.forEach(m => {
  9.       m.traverse(child => {
  10.         if (child.type !== 'Mesh' || !dbIds.includes(child.id)) return;
  11.  
  12.         targets.push(child);
  13.       });
  14.     });
  15.   } else {
  16.     targets = this.models.concat();
  17.   }
  18.  
  19.   // get bounding box of object - this will be used to setup navigation and camera
  20.   const bounds = new THREE.Box3();
  21.  
  22.   targets.forEach((object) => {
  23.     bounds.expandByObject(object);
  24.   });
  25.  
  26.   return bounds;
  27. }
  28.  
  29. fitToView(dbIds, fitRatio = 1.5) {
  30.   const camera = this.camera;
  31.   const nav = this.navigation;
  32.  
  33.   const bounds = this.getFitBounds(dbIds);
  34.   const currentDirection = nav.target.clone().sub(camera.position);
  35.   bounds.getCenter(nav.target);
  36.  
  37.   const size = bounds.getSize(new THREE.Vector3());
  38.   const maxSize = Math.max(size.x, size.y, size.z);
  39.   const fitHeightDistance = maxSize / (2 * Math.atan(Math.PI * camera.fov / 360));
  40.   const fitWidthDistance = fitHeightDistance / camera.aspect;
  41.  
  42.   const distance = fitRatio * Math.max(fitHeightDistance, fitWidthDistance);
  43.   const direction = currentDirection.normalize().multiplyScalar(distance);
  44.  
  45.   camera.position.copy(nav.target).sub(direction);
  46.   nav.update();
  47. }
  48.  
  49. buildEvents() {
  50.     // ... Other code snippets
  51.     document.addEventListener('keydown', (event) => {
  52.       if ((event.key == 'Escape' || event.key == 'Esc' || event.keyCode == 27)) {
  53.         event.preventDefault();
  54.  
  55.         this.clearSelection();
  56.  
  57.         return false;
  58.       }
  59.     });
  60.  
  61.     this.renderer.domElement.addEventListener(
  62.       'dblclick',
  63.       (event) => {
  64.         const result = this.hitTest(event.clientX, event.clientY);
  65.         if (!result) {
  66.           this.fitToView()
  67.           return;
  68.         } else {
  69.           const mesh = result.object;
  70.           this.fitToView([mesh.id]);
  71.         }
  72.       });
  73. }
  74.  
  75. // ... Other code snippets
  76. loadModel(url) {
  77.     if (typeof url != 'string')
  78.       throw new Error('Invalid model url');
  79.  
  80.     const objectLoader = new THREE.ObjectLoader();
  81.     objectLoader.load(url, (obj) => {
  82.       this.scene.add(obj);
  83.       this.models.push(obj);
  84.       this.render();
  85.  
  86.       this.fitToView(); //!<<< Add this line only
  87.     });
  88. }
  89.  
  90. buildContextMenu() {
  91.     const dataSource = [
  92.       // ... Other code snippets
  93.       {
  94.         title: 'Focus',
  95.         key: 'focus-selected'
  96.       },
  97.       // ... Other code snippets
  98.     ];
  99.  
  100.     // ... Other code snippets
  101.     const callBack = (key) => {
  102.       let objects, dbId;
  103.  
  104.       switch (key) {
  105.         // ... Other code snippets
  106.         case 'focus-selected':
  107.           if (this.selection.length <= 0) return;
  108.  
  109.           this.fitToView(this.selection);
  110.           break;
  111.       }
  112.     };
  113.  
  114.     // ... Other code snippets
  115.  
  116.     this.container.addEventListener('contextmenu', () => {
  117.       // ... Other code snippets
  118.       const hideSelectedMenuItem3 = document.querySelector('#contextMenu li[data-key="focus-selected"]');
  119.  
  120.       if (this.selection.length > 0) {
  121.         // ... Other code snippets
  122.         hideSelectedMenuItem3.style.display = 'block';
  123.       } else {
  124.         // ... Other code snippets
  125.         hideSelectedMenuItem3.style.display = 'none';
  126.       }
  127.     });
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement