XR controllers on scene
This commit is contained in:
+16
-7
@@ -1,7 +1,7 @@
|
||||
import { Raycaster, Vector3 } from "three"
|
||||
|
||||
class Draggable{
|
||||
constructor(defaultDistance){
|
||||
constructor(engine, defaultDistance){
|
||||
const objects = [];
|
||||
const raycaster = new Raycaster();
|
||||
let v = new Vector3;
|
||||
@@ -16,13 +16,22 @@ class Draggable{
|
||||
objects.splice(objects.indexOf(object), 1);
|
||||
}
|
||||
|
||||
this.update = function(pointer, camera, action){
|
||||
raycaster.setFromCamera(pointer, camera);
|
||||
if (action == 'start'){
|
||||
this.handleController = function(controller, action){
|
||||
raycaster.setFromXRController(controller);
|
||||
this.handle(action);
|
||||
}
|
||||
|
||||
this.handleMouse = function (mouse, action) {
|
||||
raycaster.setFromCamera(mouse, engine.camera);
|
||||
this.handle(action);
|
||||
};
|
||||
|
||||
this.handle = function(action){
|
||||
if (['start', 'selectstart'].includes(action)){
|
||||
let forExecute = [];
|
||||
objects.forEach(o=>{
|
||||
o.getWorldPosition(v);
|
||||
if (camera.position.distanceTo(v) <= o._draggable.distance && o.visible){
|
||||
if (engine.cameraWorld.position.distanceTo(v) <= o._draggable.distance && o.visible){
|
||||
const intersects = raycaster.intersectObject(o);
|
||||
if (intersects[0]) forExecute.push({o, i:intersects[0]})
|
||||
}
|
||||
@@ -34,10 +43,10 @@ class Draggable{
|
||||
dragging = s;
|
||||
dragging.zone = raycaster.intersectObject(s.o._draggable.dragZone)[0];
|
||||
}
|
||||
}else if (action == 'end' && dragging){
|
||||
}else if (['end', 'selectend'].includes(action) && dragging){
|
||||
dragging.o._draggable.fn.end && dragging.o._draggable.fn.end(dragging);
|
||||
dragging = null;
|
||||
}else if(action == 'drag' && dragging){
|
||||
}else if(['drag', 'move'].includes(action) && dragging){
|
||||
const intersect = raycaster.intersectObject(dragging.o._draggable.dragZone)[0];
|
||||
if (intersect?.uv && dragging.zone?.uv){
|
||||
dragging.o.position.x += -4*(dragging.zone.uv.x - intersect.uv.x);
|
||||
|
||||
Reference in New Issue
Block a user