2021-10-19 21:58:06 -04:00
|
|
|
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
|
|
|
|
|
|
2025-07-06 23:44:33 -04:00
|
|
|
import { api } from './api.js';
|
2025-07-09 13:00:52 -04:00
|
|
|
import { THREE } from '../../ext/three.js';
|
2025-09-02 08:48:50 -04:00
|
|
|
import { space } from '../../moto/space.js';
|
2021-10-10 00:16:22 -04:00
|
|
|
|
2022-02-24 19:53:22 -05:00
|
|
|
const { Vector3, Quaternion } = THREE;
|
2021-10-10 00:16:22 -04:00
|
|
|
|
2022-11-02 22:22:38 -04:00
|
|
|
const XAXIS = new THREE.Vector3(1,0,0);
|
|
|
|
|
const ZAXIS = new THREE.Vector3(0,0,1);
|
|
|
|
|
|
2022-02-20 22:47:04 -05:00
|
|
|
// circle
|
|
|
|
|
let pgeo = new THREE.CircleGeometry(8, 30);
|
2024-06-05 12:10:33 -07:00
|
|
|
let pmat = new THREE.MeshBasicMaterial({color: 0xddeeff, opacity: 0.6, transparent: true});
|
2022-02-20 22:47:04 -05:00
|
|
|
let pmesh = new THREE.Mesh(pgeo, pmat);
|
2022-02-24 19:53:22 -05:00
|
|
|
|
2022-02-20 22:47:04 -05:00
|
|
|
// circle outline
|
|
|
|
|
let pi = pgeo.index.array;
|
|
|
|
|
let pp = pgeo.attributes.position.array;
|
|
|
|
|
let np = [];
|
|
|
|
|
for (let i=0; i<pi.length; i++) {
|
|
|
|
|
if (pi[i] * pi[i+1]) {
|
|
|
|
|
np.push(pp[pi[i]*3+0]);
|
|
|
|
|
np.push(pp[pi[i]*3+1]);
|
|
|
|
|
np.push(pp[pi[i]*3+2]);
|
|
|
|
|
np.push(pp[pi[i+1]*3+0]);
|
|
|
|
|
np.push(pp[pi[i+1]*3+1]);
|
|
|
|
|
np.push(pp[pi[i+1]*3+2]);
|
2021-10-10 00:16:22 -04:00
|
|
|
}
|
2022-02-20 22:47:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let wgeo = new THREE.BufferGeometry();
|
|
|
|
|
wgeo.setAttribute('position', new THREE.BufferAttribute(np.toFloat32(), 3));
|
|
|
|
|
|
2024-06-05 12:10:33 -07:00
|
|
|
let wmat = new THREE.LineBasicMaterial({ color: 0x6699cc });
|
2022-02-20 22:47:04 -05:00
|
|
|
let wmesh = new THREE.LineSegments(wgeo, wmat);
|
|
|
|
|
pmesh.add(wmesh);
|
2021-10-10 00:16:22 -04:00
|
|
|
|
2022-02-20 22:47:04 -05:00
|
|
|
let alert;
|
|
|
|
|
let lastobj;
|
|
|
|
|
let lastface;
|
|
|
|
|
let enabled = false;
|
2022-10-29 17:06:45 -04:00
|
|
|
let opName = 'lay flat';
|
|
|
|
|
let onDone = onLayFlatSelect;
|
2022-02-20 22:47:04 -05:00
|
|
|
|
2022-10-29 17:06:45 -04:00
|
|
|
function onLayFlatSelect() {
|
|
|
|
|
let q = new Quaternion().setFromUnitVectors(lastface.normal, new Vector3(0,0,-1));
|
|
|
|
|
api.selection.rotate(q);
|
2024-06-05 12:10:33 -07:00
|
|
|
// endIt();
|
2022-10-29 17:06:45 -04:00
|
|
|
}
|
|
|
|
|
|
2025-10-26 19:17:03 -04:00
|
|
|
function onLeftySelect() {
|
|
|
|
|
// Project the face normal onto the XY plane (ignore Z component)
|
|
|
|
|
let projectedNormal = new Vector3(lastface.normal.x, lastface.normal.y, 0).normalize();
|
|
|
|
|
|
|
|
|
|
// Target direction on XY plane (negative Y axis)
|
|
|
|
|
let targetDir = new Vector3(0, -1, 0);
|
|
|
|
|
|
|
|
|
|
// Calculate angle between projected normal and target
|
|
|
|
|
let angle = Math.atan2(
|
|
|
|
|
projectedNormal.x * targetDir.y - projectedNormal.y * targetDir.x, // cross product Z component
|
|
|
|
|
projectedNormal.x * targetDir.x + projectedNormal.y * targetDir.y // dot product
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Create quaternion for Z-axis rotation only
|
|
|
|
|
let q = new Quaternion().setFromAxisAngle(ZAXIS, angle);
|
|
|
|
|
api.selection.rotate(q);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-29 17:06:45 -04:00
|
|
|
function onFaceUpSelect() {
|
2022-10-29 18:43:22 -04:00
|
|
|
api.event.emit('tool.mesh.face-normal', lastface.normal);
|
2024-06-05 12:10:33 -07:00
|
|
|
// endIt();
|
2022-10-29 17:06:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function startIt() {
|
|
|
|
|
if (enabled) {
|
|
|
|
|
endIt();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (api.feature.hover) {
|
|
|
|
|
console.log(`${opName} cannot pre-empt hover`);
|
|
|
|
|
return;
|
2021-10-10 00:16:22 -04:00
|
|
|
}
|
2022-10-29 17:06:45 -04:00
|
|
|
api.feature.hover = true;
|
|
|
|
|
enabled = true;
|
|
|
|
|
alert = api.show.alert(`[ESC] to end ${opName}`, 600000);
|
2022-02-20 22:47:04 -05:00
|
|
|
}
|
2021-10-10 00:16:22 -04:00
|
|
|
|
2022-10-29 17:06:45 -04:00
|
|
|
function endIt() {
|
2022-02-20 22:47:04 -05:00
|
|
|
if (enabled) {
|
|
|
|
|
api.hide.alert(alert);
|
|
|
|
|
api.feature.hover = false;
|
|
|
|
|
enabled = false;
|
|
|
|
|
alert = undefined;
|
2021-10-10 00:16:22 -04:00
|
|
|
cleanup();
|
2022-02-20 22:47:04 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-29 17:06:45 -04:00
|
|
|
function cleanup() {
|
|
|
|
|
if (lastobj) {
|
|
|
|
|
lastobj.remove(pmesh);
|
|
|
|
|
lastobj = undefined;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
api.event.on('key.esc', endIt);
|
|
|
|
|
|
2024-06-05 12:10:33 -07:00
|
|
|
api.event.on('tool.camera.focus', (fn) => {
|
|
|
|
|
opName = 'camera focus';
|
|
|
|
|
onDone = fn;
|
|
|
|
|
startIt();
|
|
|
|
|
});
|
|
|
|
|
|
2022-10-29 17:06:45 -04:00
|
|
|
api.event.on('tool.mesh.face-up', () => {
|
|
|
|
|
opName = 'face select';
|
2022-10-29 18:43:22 -04:00
|
|
|
onDone = onFaceUpSelect;
|
2022-10-29 17:06:45 -04:00
|
|
|
startIt();
|
|
|
|
|
});
|
2022-02-20 22:47:04 -05:00
|
|
|
|
|
|
|
|
api.event.on('tool.mesh.lay-flat', () => {
|
2022-10-29 17:06:45 -04:00
|
|
|
opName = 'lay flat';
|
2022-10-29 18:43:22 -04:00
|
|
|
onDone = onLayFlatSelect;
|
2022-10-29 17:06:45 -04:00
|
|
|
startIt();
|
2022-02-20 22:47:04 -05:00
|
|
|
});
|
|
|
|
|
|
2025-10-26 19:17:03 -04:00
|
|
|
api.event.on('tool.mesh.lefty', () => {
|
|
|
|
|
opName = 'align y axis';
|
|
|
|
|
onDone = onLeftySelect;
|
|
|
|
|
startIt();
|
|
|
|
|
});
|
|
|
|
|
|
2024-06-05 12:10:33 -07:00
|
|
|
function scale() {
|
2025-09-02 08:48:50 -04:00
|
|
|
let cam = space.internals().camera;
|
2024-06-05 12:10:33 -07:00
|
|
|
let dist = cam.position.distanceTo(pmesh.position);
|
|
|
|
|
let scale = opName === 'camera focus' ?
|
|
|
|
|
Math.min(0.25, 100 / dist) : dist / 100;
|
|
|
|
|
pmesh.scale.set(scale,scale,scale);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
api.event.on("space.view.zoom", scale);
|
|
|
|
|
|
2022-02-20 22:47:04 -05:00
|
|
|
api.event.on('mouse.hover', (ev) => {
|
|
|
|
|
if (!enabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
cleanup();
|
|
|
|
|
let { int, ints, event, point, type } = ev;
|
|
|
|
|
if (type === 'widget') {
|
|
|
|
|
lastface = int.face;
|
|
|
|
|
let obj = lastobj = int.object;
|
|
|
|
|
let norm = int.face.normal;
|
2022-10-29 17:06:45 -04:00
|
|
|
let widget = obj.widget;
|
2022-10-29 18:43:22 -04:00
|
|
|
let track = widget.track;
|
|
|
|
|
let opos = track.pos;
|
2022-02-20 22:47:04 -05:00
|
|
|
obj.add(pmesh);
|
|
|
|
|
pmesh.position.x = point.x - opos.x + norm.x * 0.1;
|
2022-10-29 18:43:22 -04:00
|
|
|
if (track.indexed) {
|
2022-11-01 13:30:22 -04:00
|
|
|
let rad = track.indexRad;
|
2022-11-02 22:22:38 -04:00
|
|
|
let py = point.y * 1.001;
|
|
|
|
|
let pz = point.z * 1.001;
|
|
|
|
|
let v = new THREE.Vector3(point.x, py, pz).applyAxisAngle(XAXIS, rad);
|
2022-11-01 13:30:22 -04:00
|
|
|
pmesh.position.y = -v.z;
|
|
|
|
|
pmesh.position.z = v.y;
|
|
|
|
|
} else {
|
2024-06-02 11:00:37 -04:00
|
|
|
const zcomp = api.mode.get() === 'CAM' ? track.tzoff : 0;
|
2022-11-01 13:30:22 -04:00
|
|
|
pmesh.position.y = -point.z - opos.y + norm.y * 0.1;
|
2024-06-02 11:00:37 -04:00
|
|
|
pmesh.position.z = point.y + zcomp + norm.z * 0.1;
|
2022-10-29 17:06:45 -04:00
|
|
|
}
|
2022-11-02 22:22:38 -04:00
|
|
|
let q = new Quaternion().setFromUnitVectors(ZAXIS,
|
2022-02-24 19:53:22 -05:00
|
|
|
new Vector3(norm.x,norm.y,norm.z)
|
2022-02-20 22:47:04 -05:00
|
|
|
);
|
|
|
|
|
pmesh.setRotationFromQuaternion(q);
|
2024-06-05 12:10:33 -07:00
|
|
|
scale();
|
2022-02-20 22:47:04 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
api.event.on('mouse.hover.up', (ev) => {
|
|
|
|
|
if (!enabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-29 17:06:45 -04:00
|
|
|
let { object } = ev;
|
2022-02-20 22:47:04 -05:00
|
|
|
if (!object) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-06-05 12:10:33 -07:00
|
|
|
onDone(ev);
|
|
|
|
|
endIt();
|
2022-02-20 22:47:04 -05:00
|
|
|
});
|
|
|
|
|
|
2025-07-06 23:44:33 -04:00
|
|
|
|
|
|
|
|
export { onLayFlatSelect, onFaceUpSelect, startIt, endIt, cleanup, scale, onDone };
|