yskang

threejs-viewer-10

May 6th, 2020
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. onMouseClicked( event ) {
  2.     while( this.selectionScene.children.length > 0 ) {
  3.         let mesh = this.selectionScene.children.pop();
  4.         mesh.material.dispose();
  5.         mesh.geometry.dispose();
  6.         mesh.material = null;
  7.         mesh.geometry = null;
  8.         mesh = null;
  9.     }
  10.  
  11.     const raycaster = new THREE.Raycaster();
  12.     const mouse = new THREE.Vector2();
  13.     mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  14.     mouse.y = -( event.clientY / window.innerHeight ) * 2 + 1;
  15.     raycaster.setFromCamera( mouse.clone(), this.camera );
  16.  
  17.     const intersects = raycaster.intersectObjects( this.models, true );
  18.     console.log( intersects );
  19.  
  20.     const result = intersects[0];
  21.     if( !result ) return;
  22.  
  23.     console.log( result );
  24.     const mesh = result.object;
  25.     const geometry = mesh.geometry.clone();
  26.     const selectionOverly = new THREE.Mesh( geometry, this.selectionMaterial );
  27.     selectionOverly.matrix.copy( mesh.matrixWorld );
  28.     selectionOverly.matrixAutoUpdate = false;
  29.     selectionOverly.matrixWorldNeedsUpdate = true;
  30.     selectionOverly.frustumCulled = false;
  31.     this.selectionScene.add( selectionOverly );
  32. }
Add Comment
Please, Sign In to add comment