feat: improve modeling, exports, and hosted deployment

This commit is contained in:
Formsmith746
2026-08-23 10:20:57 +03:00
parent 4f3859f215
commit 95b029e274
19 changed files with 1643 additions and 126 deletions
+225 -30
View File
@@ -44,7 +44,7 @@ import {
import { regularPolygonFootprintScale } from "@/lib/regularPolygonFootprint";
import { DEFAULT_SNAP_GRID, DEFAULT_WORKPLANE_WORKSPACE, normalizeSnapGrid, normalizeWorkspaceSettings, workplaneSettingsFingerprint, workspaceHydrationSyncDecision } from "@/lib/workplaneSettings";
import { interiorWorkplaneGridCoordinates, workplaneThemePalette, WORKPLANE_LINE_ELEVATION, WORKPLANE_MAJOR_GRID_INTERVAL } from "@/lib/workplaneGrid";
import { cleanNearZero, cleanRotationDegrees, fallbackSolidColor, mirroredAxisCount, mirrorSign, preservesEdgeTreatmentSize, proportionalResizeScale, resizedImportedCoordinates, resizedImportedMeshPositions, resizedShapeSize, shapeDepth, shapeWidth } from "@/lib/workplaneShapes";
import { cleanNearZero, cleanRotationDegrees, fallbackSolidColor, mirroredAxisCount, mirrorSign, preservesEdgeTreatmentSize, proportionalResizeScale, resizedImportedCoordinates, resizedImportedMeshPositions, resizedShapeSize, shapeDepth, shapeHasTaper, shapeOverallFootprintDimensions, shapeTaperDimensions, shapeTaperScaleAt, shapeWidth } from "@/lib/workplaneShapes";
import { sphereTessellation } from "@/lib/sphereTessellation";
import type { SketchForgeMcpViewFace } from "@/lib/sketchforgeMcpProtocol";
import {
@@ -214,6 +214,15 @@ type WorkplaneViewportProps = {
type WorkspaceSettings = WorkplaneWorkspaceSettings;
type ViewCubeFace = "top" | "bottom" | "front" | "back" | "right" | "left";
const VIEW_FACE_SHORTCUTS: Readonly<Record<string, ViewCubeFace>> = {
"1": "front",
"2": "back",
"3": "left",
"4": "right",
"5": "top",
"6": "bottom",
};
function readSavedWorkspaceDefault(key: string | null) {
if (!key || typeof window === "undefined") {
return null;
@@ -816,6 +825,12 @@ function rulerShapeTopologyKey(shape: WorkplaneShape): string {
segments: shape.segments,
topRadius: shape.topRadius,
baseRadius: shape.baseRadius,
taperTopWidth: shape.taperTopWidth,
taperTopDepth: shape.taperTopDepth,
taperBottomWidth: shape.taperBottomWidth,
taperBottomDepth: shape.taperBottomDepth,
taperTopScale: shape.taperTopScale,
taperBottomScale: shape.taperBottomScale,
teeth: shape.teeth,
toothSize: shape.toothSize,
toothWidth: shape.toothWidth,
@@ -872,12 +887,16 @@ function shapeMaterialSignature(shape: WorkplaneShape): string {
}
function shapeGeometrySignature(shape: WorkplaneShape): string {
const taper = shape.kind === "gear" || !shapeHasTaper(shape)
? null
: { ...shapeTaperDimensions(shape), baseWidth: shapeWidth(shape), baseDepth: shapeDepth(shape) };
if (shape.groupedShapes?.length && !shape.importedMesh) {
return JSON.stringify({
kind: "group",
width: shapeWidth(shape),
depth: shapeDepth(shape),
height: shape.height,
taper,
children: shape.groupedShapes.map((child) => [
child.id,
child.hidden,
@@ -894,6 +913,7 @@ function shapeGeometrySignature(shape: WorkplaneShape): string {
return JSON.stringify({
kind: "mesh",
mesh: shapeResourceId(shape.importedMesh),
taper,
preserve: preservesEdgeTreatmentSize(shape)
? [shapeWidth(shape), shapeDepth(shape), shape.height, shape.edgeTreatments]
: false,
@@ -901,16 +921,16 @@ function shapeGeometrySignature(shape: WorkplaneShape): string {
}
if (shape.kind === "box" && !(shape.radius && shape.radius > 0)) {
return JSON.stringify({ kind: "box" });
return JSON.stringify({ kind: "box", taper });
}
if (shape.kind === "cylinder") {
return JSON.stringify({ kind: "cylinder", sides: shape.sides, segments: shape.segments });
return JSON.stringify({ kind: "cylinder", sides: shape.sides, segments: shape.segments, taper });
}
if (shape.kind === "sphere") {
return JSON.stringify({ kind: "sphere", steps: shape.steps });
return JSON.stringify({ kind: "sphere", steps: shape.steps, taper });
}
if (shape.kind === "polygon") {
return JSON.stringify({ kind: "polygon" });
return JSON.stringify({ kind: "polygon", taper });
}
return JSON.stringify({
@@ -926,6 +946,12 @@ function shapeGeometrySignature(shape: WorkplaneShape): string {
segments: shape.segments,
topRadius: shape.topRadius,
baseRadius: shape.baseRadius,
taperTopWidth: shape.taperTopWidth,
taperTopDepth: shape.taperTopDepth,
taperBottomWidth: shape.taperBottomWidth,
taperBottomDepth: shape.taperBottomDepth,
taperTopScale: shape.taperTopScale,
taperBottomScale: shape.taperBottomScale,
teeth: shape.teeth,
toothSize: shape.toothSize,
toothWidth: shape.toothWidth,
@@ -1442,10 +1468,11 @@ function shapeCenter(shape: WorkplaneShape) {
}
function shapeLocalExtents(shape: WorkplaneShape) {
const footprint = shapeOverallFootprintDimensions(shape);
return {
x: shapeWidth(shape) / 2,
x: footprint.width / 2,
y: shape.height / 2,
z: shapeDepth(shape) / 2,
z: footprint.depth / 2,
};
}
@@ -1490,13 +1517,28 @@ function importedShapeProjectionBounds(
const min = new THREE.Vector3(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
const max = new THREE.Vector3(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY);
const point = new THREE.Vector3();
const tapered = shapeHasTaper(shape);
let taperMinY = Number.POSITIVE_INFINITY;
let taperMaxY = Number.NEGATIVE_INFINITY;
if (tapered) {
for (let index = 1; index < positions.length; index += 3) {
const localY = positions[index] * scaleY;
taperMinY = Math.min(taperMinY, localY);
taperMaxY = Math.max(taperMaxY, localY);
}
}
const taperHeight = Math.max(1e-6, taperMaxY - taperMinY);
for (let index = 0; index + 2 < positions.length; index += 3) {
const localY = positions[index + 1] * scaleY;
const normalizedHeight = tapered ? (localY - taperMinY) / taperHeight : 0;
const widthScale = tapered ? shapeTaperScaleAt(shape, normalizedHeight, "width") : 1;
const depthScale = tapered ? shapeTaperScaleAt(shape, normalizedHeight, "depth") : 1;
point
.set(
positions[index] * scaleX,
positions[index + 1] * scaleY - shape.height / 2,
positions[index + 2] * scaleZ,
positions[index] * scaleX * widthScale,
localY - shape.height / 2,
positions[index + 2] * scaleZ * depthScale,
)
.applyQuaternion(quaternion)
.add(center);
@@ -1713,6 +1755,27 @@ function resizedShapePatchFromFrame(shape: WorkplaneShape, center: THREE.Vector3
return patch;
}
function scaledHorizontalShapePatch(shape: WorkplaneShape, scaleX: number, scaleZ: number): Partial<WorkplaneShape> {
const width = Math.max(MIN_SHAPE_SIZE, shapeWidth(shape) * scaleX);
const depth = Math.max(MIN_SHAPE_SIZE, shapeDepth(shape) * scaleZ);
const patch: Partial<WorkplaneShape> = {
width,
depth,
size: resizedShapeSize(width, depth),
};
if (shape.kind === "cone") {
patch.baseRadius = width / 2;
}
if (shapeHasTaper(shape)) {
const taper = shapeTaperDimensions(shape);
patch.taperTopWidth = Math.max(MIN_SHAPE_SIZE, taper.topWidth * scaleX);
patch.taperBottomWidth = Math.max(MIN_SHAPE_SIZE, taper.bottomWidth * scaleX);
patch.taperTopDepth = Math.max(MIN_SHAPE_SIZE, taper.topDepth * scaleZ);
patch.taperBottomDepth = Math.max(MIN_SHAPE_SIZE, taper.bottomDepth * scaleZ);
}
return patch;
}
function shapeScreenBounds(state: ThreeState, shape: WorkplaneShape) {
const frame = selectionFrameForShapes([shape], [shape.id]);
if (!frame) {
@@ -1895,12 +1958,16 @@ function patchWithResizeAnchor(
return patchWithPreservedWorldBottom(shape, patch);
}
const width = Math.max(MIN_SHAPE_SIZE, patch.width ?? shapeWidth(shape));
const depth = Math.max(MIN_SHAPE_SIZE, patch.depth ?? shapeDepth(shape));
const draftShape = { ...shape, ...patch };
const draftFrame = selectionFrameForShapes([draftShape], [shape.id]);
const width = Math.max(MIN_SHAPE_SIZE, draftFrame?.width ?? patch.width ?? shapeWidth(shape));
const depth = Math.max(MIN_SHAPE_SIZE, draftFrame?.depth ?? patch.depth ?? shapeDepth(shape));
const center = resizeCenterFromAnchor(frame, resizeAnchorPointForFrame(frame, signs), signs, width, depth);
return patchWithPreservedWorldBottom(shape, {
...patch,
...resizedShapePatchFromFrame(shape, center, width, depth),
x: cleanNearZero(center.x, 0.0005),
z: cleanNearZero(center.z, 0.0005),
elevation: cleanNearZero(center.y - shape.height / 2, 0.0005),
});
}
@@ -1945,6 +2012,17 @@ function resizeShapeFromFrameHandle(
const nextCenter = altKey
? frame.center.clone()
: resizeCenterFromAnchor(frame, transform.scaleAnchorPoint ?? resizeAnchorPointForFrame(frame, signs), signs, nextWidth, nextDepth);
if (shapeHasTaper(shape)) {
const scaleX = nextWidth / Math.max(MIN_SHAPE_SIZE, width);
const scaleZ = nextDepth / Math.max(MIN_SHAPE_SIZE, depth);
const scaled = scaledHorizontalShapePatch(shape, scaleX, scaleZ);
return {
...scaled,
x: cleanNearZero(nextCenter.x, 0.0005),
z: cleanNearZero(nextCenter.z, 0.0005),
elevation: cleanNearZero(nextCenter.y - shape.height / 2, 0.0005),
};
}
return resizedShapePatchFromFrame(shape, nextCenter, nextWidth, nextDepth);
}
@@ -2176,13 +2254,13 @@ function resizeSelectionFromHandle(
.add(frame.zAxis.clone().multiplyScalar(localCenter.z * nextZ.scale));
const width = snapDimension(shapeWidth(item.startShape) * nextX.scale, step, MIN_SHAPE_SIZE, 260);
const depth = snapDimension(shapeDepth(item.startShape) * nextZ.scale, step, MIN_SHAPE_SIZE, 260);
const actualScaleX = width / Math.max(MIN_SHAPE_SIZE, shapeWidth(item.startShape));
const actualScaleZ = depth / Math.max(MIN_SHAPE_SIZE, shapeDepth(item.startShape));
const patch = {
...scaledHorizontalShapePatch(item.startShape, actualScaleX, actualScaleZ),
x: nextItemCenter.x,
z: nextItemCenter.z,
elevation: cleanNearZero(nextItemCenter.y - item.startShape.height / 2, 0.0005),
width,
depth,
size: resizedShapeSize(width, depth),
} satisfies Partial<WorkplaneShape>;
return {
id: item.id,
@@ -3722,13 +3800,45 @@ export function WorkplaneViewport({
if (Number.isFinite(value) && value > 0) {
const nextValue = Math.max(MIN_SHAPE_SIZE, value);
if (edit.axis === "width") {
const patch: Partial<WorkplaneShape> = { width: nextValue, size: resizedShapeSize(nextValue, shapeDepth(shape)) };
if (shape.kind === "cone") {
patch.baseRadius = nextValue / 2;
const frame = selectionFrameForShapes([shape], [shape.id]);
if (shapeHasTaper(shape) && frame) {
const scaleX = nextValue / Math.max(MIN_SHAPE_SIZE, frame.width);
const anchor = lastResizeAnchorRef.current;
const signs = anchor?.shapeId === shape.id ? resizeSignsForDimension(anchor.signs, "width") : { x: 0, z: 0 };
const nextCenter = signs.x
? resizeCenterFromAnchor(frame, resizeAnchorPointForFrame(frame, signs), signs, nextValue, frame.depth)
: frame.center.clone();
onUpdateShape(id, {
...scaledHorizontalShapePatch(shape, scaleX, 1),
x: cleanNearZero(nextCenter.x, 0.0005),
z: cleanNearZero(nextCenter.z, 0.0005),
elevation: cleanNearZero(nextCenter.y - shape.height / 2, 0.0005),
});
} else {
const patch: Partial<WorkplaneShape> = { width: nextValue, size: resizedShapeSize(nextValue, shapeDepth(shape)) };
if (shape.kind === "cone") {
patch.baseRadius = nextValue / 2;
}
onUpdateShape(id, patchWithResizeAnchor(shape, patch, edit.axis, lastResizeAnchorRef.current));
}
onUpdateShape(id, patchWithResizeAnchor(shape, patch, edit.axis, lastResizeAnchorRef.current));
} else if (edit.axis === "depth") {
onUpdateShape(id, patchWithResizeAnchor(shape, { depth: nextValue, size: resizedShapeSize(shapeWidth(shape), nextValue) }, edit.axis, lastResizeAnchorRef.current));
const frame = selectionFrameForShapes([shape], [shape.id]);
if (shapeHasTaper(shape) && frame) {
const scaleZ = nextValue / Math.max(MIN_SHAPE_SIZE, frame.depth);
const anchor = lastResizeAnchorRef.current;
const signs = anchor?.shapeId === shape.id ? resizeSignsForDimension(anchor.signs, "depth") : { x: 0, z: 0 };
const nextCenter = signs.z
? resizeCenterFromAnchor(frame, resizeAnchorPointForFrame(frame, signs), signs, frame.width, nextValue)
: frame.center.clone();
onUpdateShape(id, {
...scaledHorizontalShapePatch(shape, 1, scaleZ),
x: cleanNearZero(nextCenter.x, 0.0005),
z: cleanNearZero(nextCenter.z, 0.0005),
elevation: cleanNearZero(nextCenter.y - shape.height / 2, 0.0005),
});
} else {
onUpdateShape(id, patchWithResizeAnchor(shape, { depth: nextValue, size: resizedShapeSize(shapeWidth(shape), nextValue) }, edit.axis, lastResizeAnchorRef.current));
}
} else {
onUpdateShape(id, patchWithResizeAnchor(shape, { height: nextValue }, edit.axis, lastResizeAnchorRef.current));
}
@@ -4740,6 +4850,9 @@ export function WorkplaneViewport({
}
const key = event.key.toLowerCase();
const shortcutView = !event.ctrlKey && !event.metaKey && !event.altKey
? VIEW_FACE_SHORTCUTS[event.key]
: undefined;
if (event.key === "Escape" && workplaneModeRef.current) {
event.preventDefault();
onWorkplaneModeChange(false);
@@ -4752,6 +4865,9 @@ export function WorkplaneViewport({
setRulerMoveMode(false);
rulerPointDragRef.current = null;
setRulerToolsOpen(false);
} else if (shortcutView) {
event.preventDefault();
setViewCubeFace(shortcutView);
} else if (key === "w") {
event.preventDefault();
if (!event.shiftKey || !setPlacementWorkplaneAtSelection()) {
@@ -4774,18 +4890,18 @@ export function WorkplaneViewport({
window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
}, [onWorkplaneModeChange, resetView, rulerToolsOpen, setPlacementWorkplaneAtSelection, setRulerActive, togglePlacementWorkplane, toggleProjection, zoomCamera]);
}, [onWorkplaneModeChange, resetView, rulerToolsOpen, setPlacementWorkplaneAtSelection, setRulerActive, setViewCubeFace, togglePlacementWorkplane, toggleProjection, zoomCamera]);
return (
<main className={`workplane-stage ${challengeTutorial ? `key-tag-tutorial-active ${challengeTutorialCollapsed ? "key-tag-tutorial-collapsed" : ""}` : ""}`}>
<div className="view-cube" aria-label="View orientation cube" onPointerDown={(event) => event.stopPropagation()}>
<div className="view-cube-inner" ref={viewCubeRef}>
<button type="button" className="cube-face cube-top" aria-label="Bottom view" onClick={() => setViewCubeFace("bottom")}>BOTTOM</button>
<button type="button" className="cube-face cube-bottom" aria-label="Top view" onClick={() => setViewCubeFace("top")}>TOP</button>
<button type="button" className="cube-face cube-front" aria-label="Front view" onClick={() => setViewCubeFace("front")}>FRONT</button>
<button type="button" className="cube-face cube-back" aria-label="Back view" onClick={() => setViewCubeFace("back")}>BACK</button>
<button type="button" className="cube-face cube-right" aria-label="Right view" onClick={() => setViewCubeFace("right")}>RIGHT</button>
<button type="button" className="cube-face cube-left" aria-label="Left view" onClick={() => setViewCubeFace("left")}>LEFT</button>
<button type="button" className="cube-face cube-top" aria-label="Bottom view" aria-keyshortcuts="6" title="Bottom view (6)" onClick={() => setViewCubeFace("bottom")}>BOTTOM</button>
<button type="button" className="cube-face cube-bottom" aria-label="Top view" aria-keyshortcuts="5" title="Top view (5)" onClick={() => setViewCubeFace("top")}>TOP</button>
<button type="button" className="cube-face cube-front" aria-label="Front view" aria-keyshortcuts="1" title="Front view (1)" onClick={() => setViewCubeFace("front")}>FRONT</button>
<button type="button" className="cube-face cube-back" aria-label="Back view" aria-keyshortcuts="2" title="Back view (2)" onClick={() => setViewCubeFace("back")}>BACK</button>
<button type="button" className="cube-face cube-right" aria-label="Right view" aria-keyshortcuts="4" title="Right view (4)" onClick={() => setViewCubeFace("right")}>RIGHT</button>
<button type="button" className="cube-face cube-left" aria-label="Left view" aria-keyshortcuts="3" title="Left view (3)" onClick={() => setViewCubeFace("left")}>LEFT</button>
</div>
</div>
@@ -7088,6 +7204,7 @@ function createShapeObject(
);
content.position.y = -shape.height / 2;
group.add(content);
applyGroupedContentTaper(content, shape, contentBox);
group.traverse((child) => {
child.userData.shapeId = shape.id;
});
@@ -7259,6 +7376,83 @@ function createImagePlateMaterials(shape: WorkplaneShape, sideMaterial: THREE.Me
];
}
function taperGeometryForShape(geometry: THREE.BufferGeometry, shape: WorkplaneShape) {
if (!shapeHasTaper(shape)) return geometry;
const tapered = geometry.userData.cached ? geometry.clone() : geometry;
if (tapered !== geometry) {
tapered.userData = {};
}
tapered.computeBoundingBox();
const box = tapered.boundingBox;
const position = tapered.getAttribute("position");
if (!box || !position) return tapered;
const height = Math.max(1e-6, box.max.y - box.min.y);
const centerX = (box.min.x + box.max.x) / 2;
const centerZ = (box.min.z + box.max.z) / 2;
for (let index = 0; index < position.count; index += 1) {
const y = position.getY(index);
const normalizedHeight = (y - box.min.y) / height;
const widthScale = shapeTaperScaleAt(shape, normalizedHeight, "width");
const depthScale = shapeTaperScaleAt(shape, normalizedHeight, "depth");
position.setXYZ(
index,
centerX + (position.getX(index) - centerX) * widthScale,
y,
centerZ + (position.getZ(index) - centerZ) * depthScale,
);
}
position.needsUpdate = true;
tapered.computeVertexNormals();
tapered.computeBoundingBox();
tapered.computeBoundingSphere();
return tapered;
}
function applyGroupedContentTaper(content: THREE.Group, shape: WorkplaneShape, baseBounds: THREE.Box3) {
if (!shapeHasTaper(shape)) return;
const height = Math.max(1e-6, baseBounds.max.y - baseBounds.min.y);
const centerX = (baseBounds.min.x + baseBounds.max.x) / 2;
const centerZ = (baseBounds.min.z + baseBounds.max.z) / 2;
content.updateMatrixWorld(true);
const contentWorldInverse = content.matrixWorld.clone().invert();
content.traverse((object) => {
if (!(object instanceof THREE.Mesh || object instanceof THREE.Line || object instanceof THREE.LineSegments)) return;
if (!(object.geometry instanceof THREE.BufferGeometry)) return;
const sourcePosition = object.geometry.getAttribute("position");
if (!sourcePosition) return;
object.updateMatrixWorld(true);
const localToContent = new THREE.Matrix4().multiplyMatrices(contentWorldInverse, object.matrixWorld);
const contentToLocal = localToContent.clone().invert();
if (object instanceof THREE.Mesh) {
releaseSharedShapeGeometry(object);
}
const nextGeometry = object.geometry.clone();
nextGeometry.userData = {};
const position = nextGeometry.getAttribute("position");
const point = new THREE.Vector3();
for (let index = 0; index < position.count; index += 1) {
point.fromBufferAttribute(position, index).applyMatrix4(localToContent);
const normalizedHeight = (point.y - baseBounds.min.y) / height;
const widthScale = shapeTaperScaleAt(shape, normalizedHeight, "width");
const depthScale = shapeTaperScaleAt(shape, normalizedHeight, "depth");
point.x = centerX + (point.x - centerX) * widthScale;
point.z = centerZ + (point.z - centerZ) * depthScale;
point.applyMatrix4(contentToLocal);
position.setXYZ(index, point.x, point.y, point.z);
}
position.needsUpdate = true;
if (object instanceof THREE.Mesh) {
nextGeometry.computeVertexNormals();
}
nextGeometry.computeBoundingBox();
nextGeometry.computeBoundingSphere();
object.geometry = nextGeometry;
if (object instanceof THREE.Mesh && content.userData.acceleratedPicking !== false) {
enableAcceleratedMeshPicking(object, nextGeometry, Boolean(shape.importedMesh));
}
});
}
function addMesh(
group: THREE.Group,
geometry: THREE.BufferGeometry,
@@ -7268,7 +7462,8 @@ function addMesh(
rotation?: THREE.Euler,
scale?: THREE.Vector3,
) {
const prepared = geometry.userData.cached ? geometry : putGeometryOnBase(geometry);
const based = geometry.userData.cached ? geometry : putGeometryOnBase(geometry);
const prepared = taperGeometryForShape(based, shape);
const mesh = new THREE.Mesh(prepared, material);
mesh.userData.shapeSurface = true;
retainSharedShapeGeometry(mesh, prepared);
@@ -7375,7 +7570,7 @@ function getPreservedImportedMeshGeometry(shape: WorkplaneShape) {
}
function getEdgesGeometry(shape: WorkplaneShape, geometry: THREE.BufferGeometry, threshold: number) {
const importedCache = shape.importedMesh && !preservesEdgeTreatmentSize(shape)
const importedCache = shape.importedMesh && !preservesEdgeTreatmentSize(shape) && !shapeHasTaper(shape)
? getImportedMeshCache(shape.importedMesh).edges
: null;
let cache = importedCache ?? sharedEdgesGeometryCache.get(geometry);