Files
workinf_Blender_Wasm/web/tests/unit/transform-guide.test.mjs
mes123456 c47fa79102
Some checks failed
M6 deployable RC / quick (push) Has been cancelled
M6 deployable RC / chromium (push) Has been cancelled
M6 deployable RC / release (push) Has been cancelled
feat(web): viewport transform parity — offscreen backend, per-island pivots, ortho sizing
- 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>
2026-11-28 07:48:17 +08:00

81 lines
3.9 KiB
JavaScript

/* Transform guide geometry: orientation axes, pivot marker and constraint
* highlight. Desktop reference: transform.cc drawTransform_* — a fixed-size
* triad in the active orientation plus a ring-and-cross pivot marker. */
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import test from "node:test";
import ts from "typescript";
const repoRoot = path.resolve(import.meta.dirname, "../../..");
const sourcePath = path.join(repoRoot, "web/app/src/three-adapter/transform-guide.ts");
const transpiled = ts.transpileModule(fs.readFileSync(sourcePath, "utf8"), {
compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 },
fileName: sourcePath,
reportDiagnostics: true,
});
assert.deepEqual(transpiled.diagnostics, []);
/* The geometry functions never touch three at runtime, but the module imports
* its display classes; stub them so the pure builders can be imported. */
const threeStubs = `
export class LineSegments { constructor(geometry, material) { this.geometry = geometry; this.material = material; this.userData = {}; } removeFromParent() {} }
export class LineBasicMaterial { constructor() {} dispose() {} }
export class BufferGeometry { setAttribute() {} computeBoundingSphere() {} dispose() {} }
export class Float32BufferAttribute { constructor() {} }
export class Scene { add() {} }
`;
const output = transpiled.outputText.replace(/import\s*\{[^}]*\}\s*from\s*"\.\.\/vendor\/three\/three\.module\.js";/, threeStubs);
const moduleUrl = "data:text/javascript;base64," + Buffer.from(output).toString("base64");
const { AXIS_COLORS, axisWorldLength, buildGuideGeometry, edgeWireframePositions } = await import(moduleUrl);
const base = {
pivot: [0, 0, 0], origins: [], axes: [[1, 0, 0], [0, 1, 0], [0, 0, 1]],
axis: null, viewForward: [0, 0, -1], axisLength: 1,
};
const segments = (result) => result.positions.length / 6;
test("one pivot draws three axes, a ring and a cross", () => {
const geometry = buildGuideGeometry(base);
assert.equal(segments(geometry), 3 + 20 + 2);
assert.equal(geometry.colors.length, geometry.positions.length);
/* The first three segments are the axes, coloured like the desktop triad. */
const colours = geometry.colors;
assert.deepEqual(colours.slice(0, 3), AXIS_COLORS[0]);
assert.deepEqual(colours.slice(6, 9), AXIS_COLORS[1]);
assert.deepEqual(colours.slice(12, 15), AXIS_COLORS[2]);
});
test("a constrained axis adds its two arrowhead strokes", () => {
assert.equal(segments(buildGuideGeometry({ ...base, axis: 0 })), 3 + 2 + 20 + 2);
});
test("INDIVIDUAL_ORIGINS draws one marker per origin", () => {
const geometry = buildGuideGeometry({ ...base, pivot: null, origins: [[1, 0, 0], [-2, 0, 0]] });
assert.equal(segments(geometry), 2 * (3 + 20 + 2));
});
test("zero length or no pivots draw nothing", () => {
assert.equal(segments(buildGuideGeometry({ ...base, axisLength: 0 })), 0);
assert.equal(segments(buildGuideGeometry({ ...base, pivot: null, origins: [] })), 0);
});
test("fixed screen length matches the perspective rule", () => {
const length = axisWorldLength(1, 45, 900, 90);
assert.ok(Math.abs(length - (2 * Math.tan(Math.PI / 8) * 0.1)) < 1e-9);
assert.equal(axisWorldLength(0, 45, 900), 0);
});
test("orthographic views size the axes from the visible height", () => {
/* Axis views force orthographic, where a world unit is orthoHeight/height. */
assert.equal(axisWorldLength(5, 45, 678, 90, 4.1569), (4.1569 * 90) / 678);
assert.equal(axisWorldLength(0, 45, 678, 90, 4.1569), (4.1569 * 90) / 678);
assert.equal(axisWorldLength(5, 45, 0, 90, 4.1569), 0);
});
test("edge wireframe maps the Blender edge list into line segments", () => {
const positions = [0, 0, 0, 1, 0, 0, 0, 1, 0];
const out = edgeWireframePositions(positions, [0, 1, 1, 2]);
assert.equal(out.length, 2 * 6);
assert.deepEqual(Array.from(out), [0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0]);
});