Advertisement
yskang

threejs-viewer-30

Apr 20th, 2022 (edited)
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. constructor(container) {
  2.   //... othe code snippet above
  3.   this.onMouseClicked = this.onMouseClicked.bind(this); //!<<< Add this line
  4. }
  5.  
  6. // Replace this one in buildEvents() this.renderer.domElement.addEventListener( 'click', this.onMouseClicked );
  7.  
  8. init() {
  9.     //... othe code snippet above
  10.     this.sensorManger = new SensorManager(this);  //!<<< Add this line
  11.     // Other function calls
  12.     this.clock = new THREE.Clock();
  13.     //…
  14. }
  15.  
  16. //>>>>>>>>>>>>>> Replace all Start
  17. clientToViewport(clientX, clientY) {
  18.     let rect = this.renderer.domElement.getBoundingClientRect();
  19.     return new THREE.Vector3( ((clientX + 0.5) / rect.width) * 2 - 1, -((clientY + 0.5) / rect.height) * 2 + 1, 1);
  20. }
  21.  
  22. viewportToClient(viewportX, viewportY) {
  23.     let rect = this.renderer.domElement.getBoundingClientRect();
  24.     return new THREE.Vector3( (viewportX + 1) * 0.5 * rect.width - 0.5, (viewportY - 1) * -0.5 * rect.height - 0.5, 0);
  25. }
  26.  
  27. worldToClient(point) {
  28.     let p = new THREE.Vector4(point.x, point.y, point.z, 1);
  29.     p.applyMatrix4(this.camera.matrixWorldInverse);    
  30.     p.applyMatrix4(this.camera.projectionMatrix);
  31.     // Don't want to mirror values with negative z (behind camera)
  32.     if (p.w > 0) {
  33.         p.x /= p.w;
  34.         p.y /= p.w;
  35.         p.z /= p.w;
  36.     }
  37.     return this.viewportToClient(p.x, p.y);
  38. }
  39. //<<<<<<<<<<<<< Replace all End
  40.  
  41. const dataSource = [
  42.     // ... Other code snippet above
  43.     {
  44.         title: 'Focus',
  45.         key: 'focus-selected'
  46.     },
  47.     {
  48.         title: 'Add Sensor',
  49.         key: 'add-sensor'
  50.     },
  51.     {
  52.         title: 'Remove Sensor',
  53.         key: 'remove-sensor'
  54.     },
  55.     // ... Other code snippet below
  56. ];
  57.  
  58. const callBack = async (key) => {      //!<<< Add async keyword
  59.      // ... Other code snippet above
  60.      case 'focus-selected':
  61.          if (this.selection.length <= 0) return;
  62.  
  63.          this.fitToView(this.selection);
  64.          break;
  65.       case 'add-sensor':
  66.          if (this.selection.length <= 0) return;
  67.  
  68.          dbId = this.selection[0];
  69.          this.clearSelection();
  70.  
  71.          await this.sensorManger.addSensorAsync(dbId);
  72.          break;
  73.       case 'remove-sensor':
  74.          if (this.selection.length <= 0) return;
  75.  
  76.          dbId = this.selection[0];
  77.          this.clearSelection();
  78.  
  79.          await this.sensorManger.removeSensorAsync(dbId);
  80.          break;
  81.        // ... Other code snippet below
  82. };
  83.  
  84. // Modify content of this.container.addEventListener('contextmenu', ...
  85. const hideSelectedMenuItem3 = document.querySelector('#contextMenu li[data-key="focus-selected"]');
  86. const hideSelectedMenuItem4 = document.querySelector('#contextMenu li[data-key="add-sensor"]');
  87. const hideSelectedMenuItem5 = document.querySelector('#contextMenu li[data-key="remove-sensor"]');
  88.  
  89. if (this.selection.length > 0) {
  90.    // ... Other code snippet above
  91.    hideSelectedMenuItem2.style.display = 'block';
  92.    hideSelectedMenuItem3.style.display = 'block';
  93.  
  94.    if (document.querySelector(`label.markup[data-objectId="${this.selection[0]}"]`)) {
  95.        hideSelectedMenuItem4.style.display = 'none';
  96.        hideSelectedMenuItem5.style.display = 'block';
  97.    } else {
  98.        hideSelectedMenuItem4.style.display = 'block';
  99.        hideSelectedMenuItem5.style.display = 'none';
  100.    }
  101. } else {
  102.    // ... Other code snippet above
  103.    hideSelectedMenuItem3.style.display = 'none';
  104.    hideSelectedMenuItem4.style.display = 'none';
  105.    hideSelectedMenuItem5.style.display = 'none';
  106. }
  107.  
  108. // Add this line to init()
  109. this.sensorManger = new SensorManger(this);
  110.  
  111.  
  112. // Add this line to render()
  113. this.sensorManger.calibrateSensorMakeups();
  114.  
  115.  
  116. objectLoader.load(url, (obj) => {
  117.   // ... Other code snippet above
  118.   this.sensorManger.showAllMarkups(); //!<<< Add this line
  119. });
  120.  
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement