- Offscreen worker backend publishes the camera probe, so the G/R/S modal's pointer basis, transform gizmo and wireframe work on both renderers. - Edit-mode INDIVIDUAL_ORIGINS pivots every connected island on its own centre, committed as one grouped transaction (T(P)·R·S·T(-P) in object space). - Gizmo hits carry the pivot index; orthographic views size the axes from the visible world height instead of the 45° fov fallback. - View-menu navigation no longer switches workspace when a viewport is already on screen (the engine workspace transaction dropped the editor out of Edit). - Modal commits queue behind the shared command tail, so a trailing editEditorContext cannot consume the user's first Ctrl+Z. - Floating sidebar rotation field confirmed in degrees; headed cases for the offscreen path, per-island pivots and the sidebar unit, plus receipts/docs. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
163 lines
7.5 KiB
TypeScript
163 lines
7.5 KiB
TypeScript
/* Transform guide for the three.js viewport: the active Transform Orientation's
|
|
* axes at the pivot, the pivot marker, and the constraint line.
|
|
*
|
|
* Desktop draws these in the overlay engine every modal transform
|
|
* (`transform.cc` → `drawTransform_*`): a fixed-size triad with X red, Y green,
|
|
* Z blue, plus a pivot circle, so the user always sees which space the gesture
|
|
* is working in. The geometry is built here as plain arrays so it can be unit
|
|
* tested without a renderer.
|
|
*/
|
|
import { LineSegments, LineBasicMaterial, BufferGeometry, Float32BufferAttribute, Scene } from "../vendor/three/three.module.js";
|
|
|
|
export type Vec3 = [number, number, number];
|
|
|
|
export interface GuideState {
|
|
/** Shared pivot; null means per-object pivots (INDIVIDUAL_ORIGINS). */
|
|
pivot: Vec3 | null;
|
|
/** Object origins, used when `pivot` is null. */
|
|
origins: Vec3[];
|
|
/** World-space axes of the active orientation. */
|
|
axes: [Vec3, Vec3, Vec3];
|
|
/** Constrained axis, highlighted like the desktop's active-axis line. */
|
|
axis: 0 | 1 | 2 | null;
|
|
/** Camera forward (unit), used to face the pivot ring at the viewer. */
|
|
viewForward: Vec3;
|
|
/** World length of one axis: a fixed screen length at the pivot's depth. */
|
|
axisLength: number;
|
|
}
|
|
|
|
export const AXIS_COLORS: [Vec3, Vec3, Vec3] = [
|
|
[1.0, 0.2, 0.33],
|
|
[0.6, 1.0, 0.13],
|
|
[0.157, 0.635, 0.969],
|
|
];
|
|
const PIVOT_COLOR: Vec3 = [0.85, 0.85, 0.9];
|
|
|
|
/** World length that projects to `pixels`: the distance/fov rule for a
|
|
* perspective view, the visible world height for an orthographic one (axis
|
|
* views are orthographic — `view3d_navigate` auto perspective). */
|
|
export function axisWorldLength(distance: number, fovDeg: number, viewportHeightPx: number, pixels = 90, orthoHeight = 0): number {
|
|
if (!(viewportHeightPx > 0)) return 0;
|
|
if (orthoHeight > 0) return (orthoHeight * pixels) / viewportHeightPx;
|
|
if (!(distance > 0)) return 0;
|
|
return (2 * distance * Math.tan((fovDeg * Math.PI) / 360) * pixels) / viewportHeightPx;
|
|
}
|
|
|
|
const add = (a: Vec3, b: Vec3): Vec3 => [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
|
|
const scale = (a: Vec3, k: number): Vec3 => [a[0] * k, a[1] * k, a[2] * k];
|
|
const cross = (a: Vec3, b: Vec3): Vec3 => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
|
|
const normalize = (a: Vec3): Vec3 => {
|
|
const length = Math.hypot(...a);
|
|
return length < 1e-12 ? [0, 0, 1] : [a[0] / length, a[1] / length, a[2] / length];
|
|
};
|
|
|
|
/** Ring of `segments` points around `center` in the plane perpendicular to `normal`. */
|
|
export function ringPoints(center: Vec3, normal: Vec3, radius: number, segments = 20): Vec3[] {
|
|
const n = normalize(normal);
|
|
const helper: Vec3 = Math.abs(n[0]) < 0.9 ? [1, 0, 0] : [0, 1, 0];
|
|
const u = normalize(cross(n, helper));
|
|
const v = normalize(cross(n, u));
|
|
const points: Vec3[] = [];
|
|
for (let index = 0; index < segments; index += 1) {
|
|
const angle = (index / segments) * Math.PI * 2;
|
|
const offset = add(scale(u, Math.cos(angle) * radius), scale(v, Math.sin(angle) * radius));
|
|
points.push(add(center, offset));
|
|
}
|
|
return points;
|
|
}
|
|
|
|
/** Flat position/color arrays for one guide frame. */
|
|
export function buildGuideGeometry(state: GuideState): { positions: number[]; colors: number[] } {
|
|
const positions: number[] = [];
|
|
const colors: number[] = [];
|
|
const push = (a: Vec3, b: Vec3, color: Vec3) => {
|
|
positions.push(a[0], a[1], a[2], b[0], b[1], b[2]);
|
|
colors.push(color[0], color[1], color[2], color[0], color[1], color[2]);
|
|
};
|
|
const pivots = state.pivot ? [state.pivot] : state.origins;
|
|
if (!pivots.length || state.axisLength <= 0) return { positions, colors };
|
|
|
|
/* An axis line from the pivot, plus its arrowhead as two short lines. The
|
|
* constrained axis is drawn twice as thick by repeating it slightly offset
|
|
* (a line cannot change width) and in a brighter tint. */
|
|
const highlight = (color: Vec3): Vec3 => [Math.min(1, color[0] * 1.35 + 0.1), Math.min(1, color[1] * 1.35 + 0.1), Math.min(1, color[2] * 1.35 + 0.1)];
|
|
for (const pivot of pivots) {
|
|
for (const axis of [0, 1, 2] as const) {
|
|
const direction = normalize(state.axes[axis]);
|
|
const end = add(pivot, scale(direction, state.axisLength));
|
|
const color = state.axis === axis ? highlight(AXIS_COLORS[axis]) : AXIS_COLORS[axis];
|
|
push(pivot, end, color);
|
|
if (state.axis === axis) {
|
|
const side = normalize(cross(direction, state.viewForward));
|
|
const back = add(end, scale(direction, -state.axisLength * 0.12));
|
|
push(end, add(back, scale(side, state.axisLength * 0.05)), color);
|
|
push(end, add(back, scale(side, -state.axisLength * 0.05)), color);
|
|
}
|
|
}
|
|
const radius = state.axisLength * 0.07;
|
|
const ring = ringPoints(pivot, state.viewForward, radius);
|
|
for (let index = 0; index < ring.length; index += 1) push(ring[index]!, ring[(index + 1) % ring.length]!, PIVOT_COLOR);
|
|
/* Desktop pivot marker is a ring plus a cross, facing the viewer. */
|
|
const normal = normalize(state.viewForward);
|
|
const helper: Vec3 = Math.abs(normal[0]) < 0.9 ? [1, 0, 0] : [0, 1, 0];
|
|
const u = normalize(cross(normal, helper));
|
|
const v = normalize(cross(normal, u));
|
|
const arm = radius * 2.2;
|
|
push(add(pivot, scale(u, -arm)), add(pivot, scale(u, arm)), PIVOT_COLOR);
|
|
push(add(pivot, scale(v, -arm)), add(pivot, scale(v, arm)), PIVOT_COLOR);
|
|
}
|
|
return { positions, colors };
|
|
}
|
|
|
|
/** Line-segment positions for a mesh's Blender edge list (`ME_edge.verts`
|
|
* exported as `edgeVertexIndices`). Desktop wireframe display draws polygon
|
|
* edges, not render-triangle diagonals, so the modal wireframe uses this. */
|
|
export function edgeWireframePositions(positions: ArrayLike<number>, edges: ArrayLike<number>): Float32Array {
|
|
const segments = Math.floor(edges.length / 2);
|
|
const out = new Float32Array(segments * 6);
|
|
for (let segment = 0; segment < segments; segment += 1) {
|
|
for (let end = 0; end < 2; end += 1) {
|
|
const vertex = edges[segment * 2 + end]!;
|
|
const at = segment * 6 + end * 3;
|
|
out[at] = positions[vertex * 3] ?? 0;
|
|
out[at + 1] = positions[vertex * 3 + 1] ?? 0;
|
|
out[at + 2] = positions[vertex * 3 + 2] ?? 0;
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/** Owns the guide's LineSegments; cheap because the buffers are small. */
|
|
export class TransformGuide {
|
|
private readonly lines: LineSegments;
|
|
private readonly geometry = new BufferGeometry();
|
|
constructor(scene: Scene) {
|
|
this.lines = new LineSegments(
|
|
this.geometry,
|
|
new LineBasicMaterial({ vertexColors: true, depthTest: false, transparent: true, opacity: 0.95 }),
|
|
);
|
|
/* Overlay semantics: never depth-tested, never depth-written, so the guide
|
|
* is visible through geometry exactly like the desktop overlay engine. */
|
|
this.lines.renderOrder = 10;
|
|
this.lines.frustumCulled = false;
|
|
this.lines.visible = false;
|
|
/* Lines are already excluded from the workbench G-buffer prepass. */
|
|
this.lines.userData.renderHelper = "TRANSFORM_GUIDE";
|
|
scene.add(this.lines);
|
|
}
|
|
update(state: GuideState | null): void {
|
|
if (!state) { this.lines.visible = false; return; }
|
|
const { positions, colors } = buildGuideGeometry(state);
|
|
if (!positions.length) { this.lines.visible = false; return; }
|
|
this.geometry.setAttribute("position", new Float32BufferAttribute(positions, 3));
|
|
this.geometry.setAttribute("color", new Float32BufferAttribute(colors, 3));
|
|
this.geometry.computeBoundingSphere();
|
|
this.lines.visible = true;
|
|
}
|
|
dispose(): void {
|
|
this.lines.removeFromParent();
|
|
this.geometry.dispose();
|
|
(this.lines.material as LineBasicMaterial).dispose();
|
|
}
|
|
}
|