I've implemented Three.DragControls to allow for dragging an object within a scene. However, as the object is dragged, it appears to move further away from the camera.
My query is reminiscent of this unanswered post on Stack Overflow: Drag object locked at certain distance/radius from camera view
Is there a clever workaround for ensuring that _intersection.sub( _offset ) remains at a fixed distance from the camera in the center of the scene?
To address this issue, I have introduced a sphere into the scene:
dragSphere = new THREE.Mesh(new THREE.SphereGeometry(200, 60, 40 ),new THREE.MeshBasicMaterial());
dragSphere.name = "dragSphere";
dragSphere.visible = false;
dragSphere.scale.x = -1;
scene.add(dragSphere);
The challenge lies somewhere within this code snippet:
function onDocumentMouseMove( event ) {
event.preventDefault();
var offset = $("#container").offset();
var rect = _domElement.getBoundingClientRect();
_mouse.x = ( ( event.clientX - rect.left - (offset.left/2) ) / ( rect.width - rect.left ) ) * 2 - 1;
_mouse.y = - ( ( event.clientY - rect.top ) / ( rect.bottom - rect.top) ) * 2 + 1;
_raycaster.setFromCamera( _mouse, _camera );
if ( _selected && scope.enabled ) {
if ( _raycaster.ray.intersectPlane( _plane, _intersection ) ) {
if(_right){
_deltaX = event.x - _startPoint.x;
_deltaY = event.y - _startPoint.y;
_selected.rotation.y += (_deltaX/50);
_selected.rotation.x += (_deltaY/50);
console.log(_selected.position);
_startPoint.x = event.x;
_startPoint.y = event.y;
_lastMoveTimestamp = new Date();
}
else{
_selected.position.copy( _intersection.sub( _offset ) );
_selected.lookAt(camera.position);
}
}
scope.dispatchEvent( { type: 'drag', object: _selected } );
return;
}
_raycaster.setFromCamera(_mouse,_camera);
var intersects = _raycaster.intersectObjects(_objects,true);
if(intersects.length >0){
var object =intersects[0].object;
if(object.type ==="Mesh")
if(object.parent)
if(object.parent.type=="Object3D")
object=object.parent;
_plane.setFromNormalAndCoplanarPoint( _camera.getWorldDirection( _plane.normal ), object.position );
if(_hovered !==object){
scope.dispatchEvent({type:'hoveron',object:object});
DomElement.style.cursor='pointer';
hovered=object;
}
}
else{}
if(_hovered!==null){
scope.dispatchCvent({Type:'hoveroff'})
DOM.element.Style.cursor ='auto';
}
}
function onDocumentMouseDown(event){
event.PreVENTDEFULT();
_right=event.which==3||event.button==2;
_rayCASTER.SEtfromCAMERA(_MOUSE,_caMera)
variables Intersection=_raycaster.INTERSECToBJECTS(_OBJECts,True);
If(intersection.LENGTH > o){
_selected=INTERSECTS[0].OBJECT;
if(_SELECTED.TYPE='mesh'&&_SeleCTEd.PAREnT.TYPE=='OBjeCtc'){
selected=_selected.paRingecj
if('R}=ACSTEk.RAYNTERsECTpLUsENp_
_intErrUtion.Sub_sELECTED.POSitio
_domElememStYLee.cuRSOR
scopeDISpatCHEvent({Type:dRAgJoeceF:{Object Selected}}})
}
}
elsEscopReDecimalew.dISSARYSHN.(TYPE);ACY.mob\FIrextcMLEci.Gursr..AUToHovEngBLengthw.
_pSing.something drastically! ahahaha!
In this part where we're handling the drag operation:
Whendfdessedd.th.selectsion(Editor.js
Wstoth.v57),weath.Nentsecidahavso18
Ne sitkeAriLOS(atJiuATURupragden
_lt raopr).eESeloudkeshare(jndlockAbincen atmosphericende(Mneat
tr.Byaketo.pKeasthoatfthet draiconlruesohomeehapethesoonselectin gintacåtie@
Yortsotdescape.timent(pargesmeglo).
In conclusion, my objective is to ensure that when an object is dragged, its position remains aligned along the surface of the sphere, utilizing the intersection with the sphere itself for positioning.