yskang

Untitled

Apr 10th, 2022 (edited)
1,080
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 修改 Viewer.js
  2.  
  3. class Viewer {
  4.   constructor(container) {
  5.     // ... other code snippet
  6.     this.selection = []; //!<<< add this line
  7.   }
  8.  
  9.   init() {
  10.     // ... other code snippet
  11.  
  12.     this.buildContextMenu(); //!<<< add this line
  13.   }
  14.  
  15.   // 取代整個
  16.   onMouseClicked( event ) {
  17.     this.clearSelection();
  18.  
  19.     const result = this.hitTest(event.clientX, event.clientY);
  20.     if (!result) return;
  21.  
  22.     console.log(result);
  23.     const mesh = result.object;
  24.  
  25.     this.selection.push(mesh.id);
  26.  
  27.     const geometry = mesh.geometry.clone();
  28.     const selectionOverly = new THREE.Mesh(geometry, this.selectionMaterial);
  29.     selectionOverly.matrix.copy(mesh.matrixWorld);
  30.     selectionOverly.matrixAutoUpdate = false;
  31.     selectionOverly.matrixWorldNeedsUpdate = true;
  32.     selectionOverly.frustumCulled = false;
  33.     selectionOverly.dbId = mesh.id;
  34.     this.selectionScene.add(selectionOverly);
  35.   }
  36.  
  37.   clearSelection() {
  38.     while (this.selectionScene.children.length > 0) {
  39.       let mesh = this.selectionScene.children.pop();
  40.       mesh.material.dispose();
  41.       mesh.geometry.dispose();
  42.       mesh.material = null;
  43.       mesh.geometry = null;
  44.       mesh = null;
  45.     }
  46.  
  47.     while (this.selection.length) {
  48.       this.selection.pop();
  49.     }
  50.   }
  51.  
  52.   clientToViewport(clientX, clientY) {
  53.     const mouse = new THREE.Vector2();
  54.     mouse.x = (clientX / window.innerWidth) * 2 - 1;
  55.     mouse.y = -(clientY / window.innerHeight) * 2 + 1;
  56.     return mouse;
  57.   }
  58.  
  59.   hitTestViewport(vpVec) {
  60.     const raycaster = new THREE.Raycaster();
  61.     raycaster.setFromCamera(vpVec, this.camera);
  62.     const intersects = raycaster.intersectObjects(this.models, true);
  63.     console.log(intersects);
  64.  
  65.     return intersects[0];
  66.   }
  67.  
  68.   hitTest(clientX, clientY) {
  69.     return this.hitTestViewport(this.clientToViewport(clientX, clientY));
  70.   }
  71.  
  72.   buildContextMenu() {
  73.     const dataSource = [
  74.       {
  75.         title: 'Hide',
  76.         key: 'hide-selected',
  77.       },
  78.       {
  79.         title: 'Isolate',
  80.         key: 'isolate-selected'
  81.       },
  82.       {
  83.         title: 'Show All',
  84.         key: 'show-all'
  85.       }
  86.     ];
  87.  
  88.     const contextMenu = new SimpleContextMenu();
  89.     const callBack = (key) => {
  90.       alert(key);
  91.     };
  92.  
  93.     const options = {
  94.       delay: 500//delay submenu  
  95.     };
  96.  
  97.     contextMenu.register(`#${this.container.id}`, callBack, dataSource, options);
  98.     this.contextMenu = contextMenu;
  99.   }
  100.  
  101. // 修改 js/index.js
  102. (async function () {
  103.   const SimpleContextMenu = await require('simple-context-menu');
  104.   window.SimpleContextMenu = SimpleContextMenu.default;
  105.  
  106.   // ... other code snippet
  107. })();
  108.  
  109.  
  110. // 修改 index.html
  111. <script src="https://unpkg.com/require-unpkg"></script>
Advertisement
Add Comment
Please, Sign In to add comment