Selector.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import * as THREE from 'three';
  2. const mouse = new THREE.Vector2();
  3. const raycaster = new THREE.Raycaster();
  4. class Selector {
  5. constructor( editor ) {
  6. const signals = editor.signals;
  7. this.editor = editor;
  8. this.signals = signals;
  9. // signals
  10. signals.intersectionsDetected.add( ( intersects ) => {
  11. if ( intersects.length > 0 ) {
  12. // Resolve helpers to their actual objects
  13. const objects = [];
  14. for ( let i = 0; i < intersects.length; i ++ ) {
  15. let object = intersects[ i ].object;
  16. if ( object.userData.object !== undefined ) {
  17. object = object.userData.object;
  18. }
  19. if ( objects.indexOf( object ) === - 1 ) {
  20. objects.push( object );
  21. }
  22. }
  23. // Cycle through objects if the first one is already selected
  24. const index = objects.indexOf( editor.selected );
  25. if ( index !== - 1 && index < objects.length - 1 ) {
  26. this.select( objects[ index + 1 ] );
  27. } else {
  28. this.select( objects[ 0 ] );
  29. }
  30. } else {
  31. this.select( null );
  32. }
  33. } );
  34. }
  35. getIntersects( raycaster ) {
  36. const objects = [];
  37. this.editor.scene.traverseVisible( function ( child ) {
  38. objects.push( child );
  39. } );
  40. this.editor.sceneHelpers.traverseVisible( function ( child ) {
  41. if ( child.name === 'picker' ) objects.push( child );
  42. } );
  43. return raycaster.intersectObjects( objects, false );
  44. }
  45. getPointerIntersects( point, camera ) {
  46. mouse.set( ( point.x * 2 ) - 1, - ( point.y * 2 ) + 1 );
  47. raycaster.setFromCamera( mouse, camera );
  48. return this.getIntersects( raycaster );
  49. }
  50. select( object ) {
  51. if ( this.editor.selected === object ) return;
  52. let uuid = null;
  53. if ( object !== null ) {
  54. uuid = object.uuid;
  55. }
  56. this.editor.selected = object;
  57. this.editor.config.setKey( 'selected', uuid );
  58. this.signals.objectSelected.dispatch( object );
  59. }
  60. deselect() {
  61. this.select( null );
  62. }
  63. }
  64. export { Selector };
粤ICP备19079148号