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

XCD-231 Change rotation gizmo to rely on 2D canvas pos->point angle #1780

Merged
merged 1 commit into from
Jan 20, 2025
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
141 changes: 26 additions & 115 deletions src/plugins/SectionPlanesPlugin/Control.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,10 @@ class Control {
isObject: false
}), NO_STATE_INHERIT);

const localToWorldVec = (localVec, worldVec) => math.vec3ApplyQuaternion(rootNode.quaternion, localVec, worldVec);

const closestPointOnAxis = (function() {
const worldAxis = math.vec4();
const worldAxis = math.vec3();
const org = math.vec3();
const dir = math.vec3();
return (canvasPos, dst) => {
Expand Down Expand Up @@ -306,22 +308,39 @@ class Control {
if (closestPointOnAxis(canvasPos, tempVec3)) {
math.subVec3(tempVec3, initOffset, tempVec3);
setPos(tempVec3);
if (self._sectionPlane) {
self._sectionPlane.pos = tempVec3;
if (this._sectionPlane) {
this._sectionPlane.pos = tempVec3;
}
}
});
}
};

const lastCanvasPos = math.vec2();
handlers[rotateHandle.id] = {
setActivated: a => hoop.visible = a,
initDragAction: (initCanvasPos) => {
lastCanvasPos.set(initCanvasPos);
const rotationFromCanvasPos = (function() {
const planeCanvasPos = camera.projectWorldPos(pos);
localToWorldVec(rgb, tempVec3);
math.transformVec3(camera.normalMatrix, tempVec3, tempVec3);
const axisCoeff = Math.sign(tempVec3[2]);
return (canvasPos) => {
const dx = canvasPos[0] - planeCanvasPos[0];
const dy = canvasPos[1] - planeCanvasPos[1];
return axisCoeff * Math.atan2(-dy, dx);
};
})();

let lastRotation = rotationFromCanvasPos(initCanvasPos);

return canvasPos => {
dragRotateSectionPlane(rgb, lastCanvasPos, canvasPos);
lastCanvasPos.set(canvasPos);
const rotation = rotationFromCanvasPos(canvasPos);
rootNode.rotate(rgb, (rotation - lastRotation) * 180 / Math.PI);
if (this._sectionPlane) {
ignoreNextSectionPlaneDirUpdate = true;
this._sectionPlane.dir = math.vec3ApplyQuaternion(rootNode.quaternion, [0, 0, 1], tempVec3);
}
lastRotation = rotation;
};
}
};
Expand Down Expand Up @@ -457,114 +476,6 @@ class Control {
cleanups.push(() => scene.off(onSceneTick));
}

const getClickCoordsWithinElement = (event, canvasPos) => {
if (!event) {
event = window.event;
canvasPos[0] = event.x;
canvasPos[1] = event.y;
} else {
const element = event.target;
const rect = element.getBoundingClientRect();
canvasPos[0] = event.clientX - rect.left;
canvasPos[1] = event.clientY - rect.top;
}
};

const localToWorldVec = (function () {
const mat = math.mat4();
return function (localVec, worldVec) {
math.quaternionToMat4(rootNode.quaternion, mat);
math.transformVec3(mat, localVec, worldVec);
math.normalizeVec3(worldVec);
return worldVec;
};
})();

const getTranslationPlane = (worldAxis, planeNormal) => {
const absX = Math.abs(worldAxis[0]);
if (absX > Math.abs(worldAxis[1]) && absX > Math.abs(worldAxis[2])) {
math.cross3Vec3(worldAxis, [0, 1, 0], planeNormal);
} else {
math.cross3Vec3(worldAxis, [1, 0, 0], planeNormal);
}
math.cross3Vec3(planeNormal, worldAxis, planeNormal);
math.normalizeVec3(planeNormal);
};

const self = this;
var dragRotateSectionPlane = (function () {
const p1 = math.vec4();
const p2 = math.vec4();
const c = math.vec4();
const worldAxis = math.vec4();
const dir = math.vec3();
const mat = math.mat4();
const planeNormal = math.vec3();
return function (baseAxis, fromMouse, toMouse) {
localToWorldVec(baseAxis, worldAxis);
const hasData = getPointerPlaneIntersect(fromMouse, worldAxis, p1) && getPointerPlaneIntersect(toMouse, worldAxis, p2);
if (!hasData) { // Find intersections with view plane and project down to origin
getTranslationPlane(worldAxis, planeNormal);
getPointerPlaneIntersect(fromMouse, planeNormal, p1, 1); // Ensure plane moves closer to camera so angles become workable
getPointerPlaneIntersect(toMouse, planeNormal, p2, 1);
var dot = math.dotVec3(p1, worldAxis);
p1[0] -= dot * worldAxis[0];
p1[1] -= dot * worldAxis[1];
p1[2] -= dot * worldAxis[2];
dot = math.dotVec3(p2, worldAxis);
p2[0] -= dot * worldAxis[0];
p2[1] -= dot * worldAxis[1];
p2[2] -= dot * worldAxis[2];
}
math.normalizeVec3(p1);
math.normalizeVec3(p2);
dot = math.dotVec3(p1, p2);
dot = math.clamp(dot, -1.0, 1.0); // Rounding errors cause dot to exceed allowed range
var incDegrees = Math.acos(dot) * math.RADTODEG;
math.cross3Vec3(p1, p2, c);
if (math.dotVec3(c, worldAxis) < 0.0) {
incDegrees = -incDegrees;
}
rootNode.rotate(baseAxis, incDegrees);

if (self._sectionPlane) {
math.quaternionToMat4(rootNode.quaternion, mat); // << ---
math.transformVec3(mat, [0, 0, 1], dir);
ignoreNextSectionPlaneDirUpdate = true;
self._sectionPlane.dir = dir;
}
};
})();

var getPointerPlaneIntersect = (function () {
const dir = math.vec4([0, 0, 0, 1]);
const matrix = math.mat4();
return function (mouse, axis, dest, offset) {
offset = offset || 0;
dir[0] = mouse[0] / canvas.width * 2.0 - 1.0;
dir[1] = -(mouse[1] / canvas.height * 2.0 - 1.0);
dir[2] = 0.0;
dir[3] = 1.0;
math.mulMat4(camera.projMatrix, camera.viewMatrix, matrix); // Unproject norm device coords to view coords
math.inverseMat4(matrix);
math.transformVec4(matrix, dir, dir);
math.mulVec4Scalar(dir, 1.0 / dir[3]); // This is now point A on the ray in world space
var rayO = camera.eye; // The direction
math.subVec4(dir, rayO, dir);
const origin = self._sectionPlane.pos; // Plane origin:
var d = -math.dotVec3(origin, axis) - offset;
var dot = math.dotVec3(axis, dir);
if (Math.abs(dot) > 0.005) {
var t = -(math.dotVec3(axis, rayO) + d) / dot;
math.mulVec3Scalar(dir, t, dest);
math.addVec3(dest, rayO);
math.subVec3(dest, origin, dest);
return true;
}
return false;
};
})();

{
let deactivateActive = null;
let currentDrag = null;
Expand Down
Loading