Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DragControls: Add support for multiple groups. #27791

Merged
merged 4 commits into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions examples/jsm/controls/DragControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class DragControls extends EventDispatcher {
_domElement.style.touchAction = 'none'; // disable touch scroll

let _selected = null, _hovered = null;

const _intersections = [];

this.mode = 'translate';
Expand Down Expand Up @@ -73,6 +73,10 @@ class DragControls extends EventDispatcher {

}

function setObjects(object){ //array of object
Fixed Show fixed Hide fixed
_objects = object
}

function getRaycaster() {

return _raycaster;
Expand Down Expand Up @@ -178,7 +182,17 @@ class DragControls extends EventDispatcher {

if ( _intersections.length > 0 ) {

_selected = ( scope.transformGroup === true ) ? _objects[ 0 ] : _intersections[ 0 ].object;
if ( scope.transformGroup === true ) {

// look for the outermost group in the object's upper hierarchy

_selected = findGroup( _intersections[ 0 ].object );

} else {

_selected = _intersections[ 0 ].object;

}

_plane.setFromNormalAndCoplanarPoint( _camera.getWorldDirection( _plane.normal ), _worldPosition.setFromMatrixPosition( _selected.matrixWorld ) );

Expand Down Expand Up @@ -234,6 +248,16 @@ class DragControls extends EventDispatcher {

}

function findGroup( obj, group = null ) {

if ( obj.isGroup ) group = obj;

if ( obj.parent === null ) return group;

return findGroup( obj.parent, group );

}

activate();

// API
Expand All @@ -247,9 +271,11 @@ class DragControls extends EventDispatcher {
this.dispose = dispose;
this.getObjects = getObjects;
this.getRaycaster = getRaycaster;
this.setObjects = setObjects;

}

}


export { DragControls };