Skip to content

Commit 0433407

Browse files
XEOK-125 Change the way axis interaction is interpreted - looking for a point on the axis closest to the pointer ray (also #1717)
1 parent cbbe01a commit 0433407

File tree

1 file changed

+43
-29
lines changed

1 file changed

+43
-29
lines changed

src/plugins/SectionPlanesPlugin/Control.js

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -259,17 +259,51 @@ class Control {
259259
isObject: false
260260
}), NO_STATE_INHERIT);
261261

262-
const lastCanvasPos = math.vec2();
262+
const closestPointOnAxis = (function() {
263+
const worldAxis = math.vec4();
264+
const org = math.vec3();
265+
const dir = math.vec3();
266+
return (canvasPos, dst) => {
267+
localToWorldVec(rgb, worldAxis);
268+
269+
const P = rootNode.position;
270+
const D = worldAxis;
271+
272+
math.canvasPosToWorldRay(canvas, scene.camera.viewMatrix, scene.camera.projMatrix, scene.camera.projection, canvasPos, org, dir);
273+
274+
const d01 = math.dotVec3(D, dir);
275+
const v = math.subVec3(org, P, dst);
276+
const v0 = math.dotVec3(v, D);
277+
const v1 = math.dotVec3(v, dir);
278+
const det = 1 - d01 * d01;
279+
280+
if (Math.abs(det) > 1e-10) { // if lines are not parallel
281+
const s = (v0 - d01 * v1) / det;
282+
math.addVec3(P, math.mulVec3Scalar(D, s, dst), dst);
283+
return true;
284+
} else {
285+
return false;
286+
}
287+
};
288+
})();
289+
const initOffset = math.vec3();
290+
const tempVec3 = math.vec3();
263291
handlers[arrowHandle.id] = handlers[shaftHandle.id] = {
264292
affordanceMesh: bigArrowHead,
265293
initDragAction: (initCanvasPos) => {
266-
lastCanvasPos.set(initCanvasPos);
267-
return canvasPos => {
268-
dragTranslateSectionPlane(rgb, lastCanvasPos, canvasPos);
269-
lastCanvasPos.set(canvasPos);
270-
};
294+
return closestPointOnAxis(initCanvasPos, initOffset) && math.subVec3(initOffset, rootNode.position, initOffset) && ((canvasPos) => {
295+
if (closestPointOnAxis(canvasPos, tempVec3)) {
296+
math.subVec3(tempVec3, initOffset, tempVec3);
297+
rootNode.position = tempVec3;
298+
if (self._sectionPlane) {
299+
self._sectionPlane.pos = tempVec3;
300+
}
301+
}
302+
});
271303
}
272304
};
305+
306+
const lastCanvasPos = math.vec2();
273307
handlers[rotateHandle.id] = {
274308
affordanceMesh: hoop,
275309
initDragAction: (initCanvasPos) => {
@@ -447,28 +481,6 @@ class Control {
447481
};
448482

449483
const self = this;
450-
451-
const dragTranslateSectionPlane = (function () {
452-
const p1 = math.vec3();
453-
const p2 = math.vec3();
454-
const worldAxis = math.vec4();
455-
const planeNormal = math.vec3();
456-
return function (baseAxis, fromMouse, toMouse) {
457-
localToWorldVec(baseAxis, worldAxis);
458-
getTranslationPlane(worldAxis, planeNormal);
459-
getPointerPlaneIntersect(fromMouse, planeNormal, p1);
460-
getPointerPlaneIntersect(toMouse, planeNormal, p2);
461-
math.subVec3(p2, p1, p2);
462-
const dot = math.dotVec3(p2, worldAxis);
463-
math.mulVec3Scalar(worldAxis, dot, p1);
464-
math.addVec3(rootNode.position, p1, p1);
465-
rootNode.position = p1;
466-
if (self._sectionPlane) {
467-
self._sectionPlane.pos = rootNode.position;
468-
}
469-
};
470-
})();
471-
472484
var dragRotateSectionPlane = (function () {
473485
const p1 = math.vec4();
474486
const p2 = math.vec4();
@@ -587,9 +599,11 @@ class Control {
587599
addCanvasEventListener("mousedown", (e) => {
588600
e.preventDefault();
589601
if (this._visible && (e.which === 1) && nextDragAction) { // Left button
590-
cameraControl.pointerEnabled = false;
591602
getClickCoordsWithinElement(e, canvasPos);
592603
dragAction = nextDragAction(canvasPos);
604+
if (dragAction) {
605+
cameraControl.pointerEnabled = false;
606+
}
593607
}
594608
});
595609

0 commit comments

Comments
 (0)