| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- import * as THREE from 'three';
- const mouse = new THREE.Vector2();
- const raycaster = new THREE.Raycaster();
- class Selector {
- constructor( editor ) {
- const signals = editor.signals;
- this.editor = editor;
- this.signals = signals;
- // signals
- signals.intersectionsDetected.add( ( intersects ) => {
- if ( intersects.length > 0 ) {
- // Resolve helpers to their actual objects
- const objects = [];
- for ( let i = 0; i < intersects.length; i ++ ) {
- let object = intersects[ i ].object;
- if ( object.userData.object !== undefined ) {
- object = object.userData.object;
- }
- if ( objects.indexOf( object ) === - 1 ) {
- objects.push( object );
- }
- }
- // Cycle through objects if the first one is already selected
- const index = objects.indexOf( editor.selected );
- if ( index !== - 1 && index < objects.length - 1 ) {
- this.select( objects[ index + 1 ] );
- } else {
- this.select( objects[ 0 ] );
- }
- } else {
- this.select( null );
- }
- } );
- }
- getIntersects( raycaster ) {
- const objects = [];
- this.editor.scene.traverseVisible( function ( child ) {
- objects.push( child );
- } );
- this.editor.sceneHelpers.traverseVisible( function ( child ) {
- if ( child.name === 'picker' ) objects.push( child );
- } );
- return raycaster.intersectObjects( objects, false );
- }
- getPointerIntersects( point, camera ) {
- mouse.set( ( point.x * 2 ) - 1, - ( point.y * 2 ) + 1 );
- raycaster.setFromCamera( mouse, camera );
- return this.getIntersects( raycaster );
- }
- select( object ) {
- if ( this.editor.selected === object ) return;
- let uuid = null;
- if ( object !== null ) {
- uuid = object.uuid;
- }
- this.editor.selected = object;
- this.editor.config.setKey( 'selected', uuid );
- this.signals.objectSelected.dispatch( object );
- }
- deselect() {
- this.select( null );
- }
- }
- export { Selector };
|