diff --git a/apps/web/src/app/globals.css b/apps/web/src/app/globals.css index 4953243..abd622b 100644 --- a/apps/web/src/app/globals.css +++ b/apps/web/src/app/globals.css @@ -1019,7 +1019,8 @@ button { .toolbar-icon:not(.disabled) .toolbar-sprite-icon, .toolbar-icon:not(.disabled) .vector-toolbar-sprite-icon, .shape-menu-trigger .vector-toolbar-sprite-icon, -.action-buttons button .vector-toolbar-sprite-icon { +.action-buttons button .vector-toolbar-sprite-icon, +.inspector-header-icon .vector-toolbar-sprite-icon { filter: grayscale(0.35) sepia(0.12) saturate(1.7) hue-rotate(165deg) contrast(1.18) brightness(0.78) drop-shadow(0 0 1px rgba(255, 255, 255, 0.95)) drop-shadow(0 0 3px rgba(0, 156, 222, 0.2)); opacity: 0.94; } @@ -1028,13 +1029,15 @@ button { .toolbar-icon:not(.disabled):hover .vector-toolbar-sprite-icon, .shape-menu-trigger:hover .vector-toolbar-sprite-icon, .shape-menu-trigger.active .vector-toolbar-sprite-icon, -.action-buttons button:hover .vector-toolbar-sprite-icon { +.action-buttons button:hover .vector-toolbar-sprite-icon, +.inspector-header-icon:hover .vector-toolbar-sprite-icon { filter: grayscale(0.1) sepia(0.22) saturate(2.1) hue-rotate(164deg) contrast(1.2) brightness(0.72) drop-shadow(0 0 1px rgba(255, 255, 255, 1)) drop-shadow(0 0 5px rgba(0, 156, 222, 0.38)); opacity: 1; } .toolbar-icon.active .toolbar-sprite-icon, -.toolbar-icon.active .vector-toolbar-sprite-icon { +.toolbar-icon.active .vector-toolbar-sprite-icon, +.inspector-header-icon.active .vector-toolbar-sprite-icon { filter: grayscale(0.05) sepia(0.22) saturate(2.35) hue-rotate(164deg) contrast(1.2) brightness(0.7) drop-shadow(0 0 1px rgba(255, 255, 255, 1)) drop-shadow(0 0 5px rgba(0, 156, 222, 0.42)); opacity: 1; } @@ -2056,6 +2059,11 @@ button { cursor: pointer; } +.inspector-header-icon .vector-toolbar-sprite-icon { + transform: scale(0.9); + transform-origin: center; +} + .inspector-header-icon.active { color: #0c9dd3; } diff --git a/apps/web/src/components/SketchForgeEditor.tsx b/apps/web/src/components/SketchForgeEditor.tsx index 3f82b7c..6d98c7a 100644 --- a/apps/web/src/components/SketchForgeEditor.tsx +++ b/apps/web/src/components/SketchForgeEditor.tsx @@ -49,6 +49,7 @@ type ExportFormat = "stl" | "obj"; type Vec3 = [number, number, number]; type MeshData = { name: string; vertices: Vec3[]; faces: [number, number, number][] }; type Cuboid = { minX: number; maxX: number; minY: number; maxY: number; minZ: number; maxZ: number }; +type ShapeUpdatePatch = Partial & { bakeTransform?: boolean }; type ManifoldSolid = ReturnType; type ToolbarShapeAsset = ShapeAsset & { menuIcon: string }; type DownloadResult = { mode: "browser" } | { mode: "folder"; path: string }; @@ -113,6 +114,7 @@ declare global { const CUTTER_PADDING = 0.05; const POINT_TOLERANCE = 0.0001; const CUTTER_RESIDUAL_INSET = CUTTER_PADDING * 0.4; +const MIN_SHAPE_DIMENSION = 0.01; const IMPORTED_EXACT_BOOLEAN_TRIANGLE_LIMIT = 150000; const COPLANAR_BOOLEAN_RESCUE_DEGREES = 0.02; const stlLoader = new STLLoader(); @@ -1430,6 +1432,90 @@ function importedMeshForShape(shape: WorkplaneShape): MeshData { return transformMesh({ name: sanitizeName(shape.name), vertices, faces }, shape); } +function shapeHasTransformToBake(shape: WorkplaneShape) { + return ( + Math.abs(cleanRotationDegrees(shape.rotation ?? 0, 3)) > 0 || + Math.abs(cleanRotationDegrees(shape.rotationX ?? 0, 3)) > 0 || + Math.abs(cleanRotationDegrees(shape.rotationZ ?? 0, 3)) > 0 || + Boolean(shape.mirrorX || shape.mirrorY || shape.mirrorZ) + ); +} + +function bakeShapeTransformIntoMesh(shape: WorkplaneShape): WorkplaneShape { + if (!shapeHasTransformToBake(shape)) { + return shape; + } + + const mesh = meshForShape(shape); + if (mesh.vertices.length < 3 || mesh.faces.length < 1) { + return shape; + } + + let minX = Number.POSITIVE_INFINITY; + let minY = Number.POSITIVE_INFINITY; + let minZ = Number.POSITIVE_INFINITY; + let maxX = Number.NEGATIVE_INFINITY; + let maxY = Number.NEGATIVE_INFINITY; + let maxZ = Number.NEGATIVE_INFINITY; + + mesh.vertices.forEach(([x, y, z]) => { + minX = Math.min(minX, x); + minY = Math.min(minY, y); + minZ = Math.min(minZ, z); + maxX = Math.max(maxX, x); + maxY = Math.max(maxY, y); + maxZ = Math.max(maxZ, z); + }); + + if (![minX, minY, minZ, maxX, maxY, maxZ].every(Number.isFinite)) { + return shape; + } + + const centerX = (minX + maxX) / 2; + const centerZ = (minZ + maxZ) / 2; + const width = Math.max(MIN_SHAPE_DIMENSION, maxX - minX); + const height = Math.max(MIN_SHAPE_DIMENSION, maxY - minY); + const depth = Math.max(MIN_SHAPE_DIMENSION, maxZ - minZ); + const positions: number[] = []; + + mesh.faces.forEach(([ai, bi, ci]) => { + [mesh.vertices[ai], mesh.vertices[bi], mesh.vertices[ci]].forEach(([x, y, z]) => { + positions.push(x - centerX, y - minY, z - centerZ); + }); + }); + + return { + ...shape, + kind: "mesh", + x: cleanNearZero(centerX, 0.0005), + z: cleanNearZero(centerZ, 0.0005), + elevation: cleanNearZero(minY, 0.0005), + width, + depth, + height, + size: Math.max(width, depth), + rotation: 0, + rotationX: 0, + rotationZ: 0, + mirrorX: undefined, + mirrorY: undefined, + mirrorZ: undefined, + importedMesh: { + positions, + baseWidth: width, + baseDepth: depth, + baseHeight: height, + triangleCount: mesh.faces.length, + sourceFormat: "json", + }, + imagePlate: undefined, + groupedShapes: undefined, + groupedBaseWidth: undefined, + groupedBaseDepth: undefined, + groupedBaseHeight: undefined, + }; +} + export function importedShapeFromStl(fileName: string, buffer: ArrayBuffer): WorkplaneShape { const rawGeometry = stlLoader.parse(buffer); const geometry = rawGeometry.index ? rawGeometry.toNonIndexed() : rawGeometry.clone(); @@ -2828,9 +2914,9 @@ function booleanMeshShape(selection: WorkplaneShape[]): WorkplaneShape | null { const centerX = (groupBounds.minX + groupBounds.maxX) / 2; const centerZ = (groupBounds.minZ + groupBounds.maxZ) / 2; const minY = groupBounds.minY; - const width = Math.max(0.1, groupBounds.maxX - groupBounds.minX); - const height = Math.max(0.1, groupBounds.maxY - groupBounds.minY); - const depth = Math.max(0.1, groupBounds.maxZ - groupBounds.minZ); + const width = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxX - groupBounds.minX); + const height = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxY - groupBounds.minY); + const depth = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxZ - groupBounds.minZ); const positions: number[] = []; for (let i = 0; i < resultPositions.length; i += 3) { @@ -2891,9 +2977,9 @@ function resultGeometryToMeshShape( const centerX = (groupBounds.minX + groupBounds.maxX) / 2; const centerZ = (groupBounds.minZ + groupBounds.maxZ) / 2; const minY = groupBounds.minY; - const width = Math.max(0.1, groupBounds.maxX - groupBounds.minX); - const height = Math.max(0.1, groupBounds.maxY - groupBounds.minY); - const depth = Math.max(0.1, groupBounds.maxZ - groupBounds.minZ); + const width = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxX - groupBounds.minX); + const height = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxY - groupBounds.minY); + const depth = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxZ - groupBounds.minZ); const positions: number[] = []; for (let i = 0; i < resultPositions.length; i += 3) { @@ -3220,9 +3306,9 @@ function meshPositionsToGroupShape(selection: WorkplaneShape[], solids: Workplan const centerX = (minX + maxX) / 2; const centerZ = (minZ + maxZ) / 2; - const width = Math.max(0.1, maxX - minX); - const height = Math.max(0.1, maxY - minY); - const depth = Math.max(0.1, maxZ - minZ); + const width = Math.max(MIN_SHAPE_DIMENSION, maxX - minX); + const height = Math.max(MIN_SHAPE_DIMENSION, maxY - minY); + const depth = Math.max(MIN_SHAPE_DIMENSION, maxZ - minZ); const normalizedPositions: number[] = []; for (let i = 0; i < positions.length; i += 3) { normalizedPositions.push(positions[i] - centerX, positions[i + 1] - minY, positions[i + 2] - centerZ); @@ -3481,10 +3567,10 @@ function boxedBooleanMeshShape(selection: WorkplaneShape[]): WorkplaneShape | nu const groupBounds = boundsForCuboids(cuboids); const centerX = (groupBounds.minX + groupBounds.maxX) / 2; const centerZ = (groupBounds.minZ + groupBounds.maxZ) / 2; - const width = Math.max(0.1, groupBounds.maxX - groupBounds.minX); + const width = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxX - groupBounds.minX); const minY = groupBounds.minY; - const height = Math.max(0.1, groupBounds.maxY - groupBounds.minY); - const depth = Math.max(0.1, groupBounds.maxZ - groupBounds.minZ); + const height = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxY - groupBounds.minY); + const depth = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxZ - groupBounds.minZ); const mesh = cuboidsToMesh("Group", cuboids, centerX, centerZ, minY); const positions = mesh.faces.flatMap(([ai, bi, ci]) => [mesh.vertices[ai], mesh.vertices[bi], mesh.vertices[ci]]).flat(); const firstSolid = solids[0]; @@ -3536,10 +3622,10 @@ function aabbBooleanMeshShape(selection: WorkplaneShape[]): WorkplaneShape | nul const groupBounds = boundsForCuboids(cuboids); const centerX = (groupBounds.minX + groupBounds.maxX) / 2; const centerZ = (groupBounds.minZ + groupBounds.maxZ) / 2; - const width = Math.max(0.1, groupBounds.maxX - groupBounds.minX); + const width = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxX - groupBounds.minX); const minY = groupBounds.minY; - const height = Math.max(0.1, groupBounds.maxY - groupBounds.minY); - const depth = Math.max(0.1, groupBounds.maxZ - groupBounds.minZ); + const height = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxY - groupBounds.minY); + const depth = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxZ - groupBounds.minZ); const mesh = cuboidsToMesh("Group", cuboids, centerX, centerZ, minY); const positions = mesh.faces.flatMap(([ai, bi, ci]) => [mesh.vertices[ai], mesh.vertices[bi], mesh.vertices[ci]]).flat(); const firstSolid = solids[0]; @@ -3658,9 +3744,9 @@ function hollowClipMeshShape(selection: WorkplaneShape[]): WorkplaneShape | null const centerX = (minX + maxX) / 2; const centerZ = (minZ + maxZ) / 2; - const width = Math.max(0.1, maxX - minX); - const height = Math.max(0.1, maxY - minY); - const depth = Math.max(0.1, maxZ - minZ); + const width = Math.max(MIN_SHAPE_DIMENSION, maxX - minX); + const height = Math.max(MIN_SHAPE_DIMENSION, maxY - minY); + const depth = Math.max(MIN_SHAPE_DIMENSION, maxZ - minZ); const normalizedPositions: number[] = []; for (let i = 0; i < positions.length; i += 3) { normalizedPositions.push(positions[i] - centerX, positions[i + 1] - minY, positions[i + 2] - centerZ); @@ -3759,9 +3845,9 @@ function mergedMeshShape(selection: WorkplaneShape[]): WorkplaneShape | null { const centerX = (minX + maxX) / 2; const centerZ = (minZ + maxZ) / 2; - const width = Math.max(0.1, maxX - minX); - const height = Math.max(0.1, maxY - minY); - const depth = Math.max(0.1, maxZ - minZ); + const width = Math.max(MIN_SHAPE_DIMENSION, maxX - minX); + const height = Math.max(MIN_SHAPE_DIMENSION, maxY - minY); + const depth = Math.max(MIN_SHAPE_DIMENSION, maxZ - minZ); const positions: number[] = []; faces.forEach(([ai, bi, ci]) => { @@ -3819,9 +3905,9 @@ function groupedShape(selection: WorkplaneShape[]): WorkplaneShape | null { const maxZ = groupBounds.maxZ; const centerX = (minX + maxX) / 2; const centerZ = (minZ + maxZ) / 2; - const width = Math.max(0.1, maxX - minX); - const depth = Math.max(0.1, maxZ - minZ); - const height = Math.max(0.1, maxY - minY); + const width = Math.max(MIN_SHAPE_DIMENSION, maxX - minX); + const depth = Math.max(MIN_SHAPE_DIMENSION, maxZ - minZ); + const height = Math.max(MIN_SHAPE_DIMENSION, maxY - minY); const firstSolid = groupable.find((shape) => !shape.hole) ?? groupable[0]; const holeOnly = groupable.every((shape) => shape.hole); @@ -3872,8 +3958,9 @@ function rotationFromQuaternion(quaternion: THREE.Quaternion) { }; } -function cleanShapePatch(patch: Partial): Partial { - const next = { ...patch }; +function cleanShapePatch(patch: ShapeUpdatePatch): Partial { + const { bakeTransform: _bakeTransform, ...rest } = patch; + const next = { ...rest }; if (typeof next.rotation === "number") { next.rotation = cleanRotationDegrees(next.rotation, 1); } @@ -4313,7 +4400,8 @@ export function SketchForgeEditor({ [commitShapes, placementElevation, shapes], ); - const updateShape = useCallback((id: string, patch: Partial) => { + const updateShape = useCallback((id: string, patch: ShapeUpdatePatch) => { + const bakeTransform = Boolean(patch.bakeTransform); const cleanedPatch = cleanShapePatch(patch); setShapes((current) => { let changed = false; @@ -4323,7 +4411,8 @@ export function SketchForgeEditor({ } const patched = { ...shape, ...cleanedPatch }; - const canonical = canonicalizeShape("hole" in cleanedPatch ? withHoleMode(patched, Boolean(cleanedPatch.hole), cleanedPatch.color) : patched); + const canonicalBase = canonicalizeShape("hole" in cleanedPatch ? withHoleMode(patched, Boolean(cleanedPatch.hole), cleanedPatch.color) : patched); + const canonical = bakeTransform ? canonicalizeShape(bakeShapeTransformIntoMesh(canonicalBase)) : canonicalBase; if (workplaneShapesEqual(shape, canonical)) { return shape; } diff --git a/apps/web/src/components/WorkplaneViewport.tsx b/apps/web/src/components/WorkplaneViewport.tsx index bbdf46e..345a660 100644 --- a/apps/web/src/components/WorkplaneViewport.tsx +++ b/apps/web/src/components/WorkplaneViewport.tsx @@ -1,6 +1,6 @@ "use client"; -import { Box, Check, ChevronDown, ChevronUp, Expand, Home, LightbulbOff, LockKeyhole, LockKeyholeOpen, Minus, Plus, X } from "lucide-react"; +import { Box, Check, ChevronDown, ChevronUp, Expand, Home, LockKeyhole, LockKeyholeOpen, Minus, Plus, X } from "lucide-react"; import { useCallback, useEffect, useMemo, useRef, useState, type CSSProperties, type Dispatch, type DragEvent, type MutableRefObject, type PointerEvent as ReactPointerEvent, type SetStateAction } from "react"; import * as THREE from "three"; import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js"; @@ -13,6 +13,7 @@ import droidSerifBoldFontJson from "three/examples/fonts/droid/droid_serif_bold. import gentilisBoldFontJson from "three/examples/fonts/gentilis_bold.typeface.json"; import helvetikerBoldFontJson from "three/examples/fonts/helvetiker_bold.typeface.json"; import optimerBoldFontJson from "three/examples/fonts/optimer_bold.typeface.json"; +import { ToolbarHideSelectedIcon } from "@/components/icons"; import type { AlignAxis, AlignHandleStatus, AlignTarget, GridSize, ShapeAsset, WorkplaneShape } from "@/types/sketchforge"; const gridSizes: GridSize[] = ["Off", "0.1 mm", "0.25 mm", "0.5 mm", "1.0 mm", "2.0 mm", "5.0 mm", "Brick"]; @@ -47,7 +48,7 @@ const workspaceSizePresets = [ const gridBlockPresets = ["1 mm", "2.5 mm", "5 mm", "10 mm", "20 mm", "50 mm", "100 mm", "Custom"] as const; const CAMERA_HOME = new THREE.Vector3(118, 96, 118); const CAMERA_TARGET = new THREE.Vector3(0, 0, 0); -const MIN_SHAPE_SIZE = 0.1; +const MIN_SHAPE_SIZE = 0.01; const MIN_ELEVATION = -180; const MAX_ELEVATION = 220; const CAMERA_MIN_TARGET_Y = -70; @@ -122,7 +123,7 @@ type WorkplaneViewportProps = { onSelectShape: (id: string | string[] | null, mode?: "replace" | "toggle") => void; onSetPlacementElevation: (elevation: number, source: "shape" | "base") => void; onInteractionActiveChange?: (active: boolean) => void; - onUpdateShape: (id: string, patch: Partial) => void; + onUpdateShape: (id: string, patch: ShapeUpdatePatch) => void; onWorkplaneModeChange: (active: boolean) => void; }; @@ -194,6 +195,8 @@ type TransformHandleKind = "scale" | "height" | "lift" | "rotate"; type RotationAxis = "x" | "y" | "z"; type RotationHandleSide = "near" | "right" | "far" | "left"; type RotationHandleSides = Record; +type ShapeUpdatePatch = Partial & { bakeTransform?: boolean }; +type ResizeSigns = { x: number; z: number }; type TransformDragState = { id: string; ids: string[]; @@ -212,6 +215,10 @@ type TransformDragState = { handleWorldOffset: number; screenYPerWorldUnit: number; scalePlaneY: number; + scalePlane?: THREE.Plane; + scaleSigns?: ResizeSigns; + scaleAnchorPoint?: THREE.Vector3; + scaleStartPoint?: THREE.Vector3; rotationAxisVector?: THREE.Vector3; rotationPivot?: THREE.Vector3; rotationPlaneCenter?: THREE.Vector3; @@ -350,9 +357,6 @@ function measureKeyForHandle(kind: TransformHandleKind, handleKey: string, overl if (kind === "lift") { return getElevationMeasureKey(overlay) ?? handleKey; } - if (kind === "height") { - return null; - } return handleKey; } @@ -616,6 +620,11 @@ function frameLocalPoint(frame: SelectionFrame, point: THREE.Vector3) { return new THREE.Vector3(offset.dot(frame.xAxis), offset.dot(frame.yAxis), offset.dot(frame.zAxis)); } +function frameLocalDelta(frame: SelectionFrame, start: THREE.Vector3, current: THREE.Vector3) { + const offset = current.clone().sub(start); + return new THREE.Vector3(offset.dot(frame.xAxis), offset.dot(frame.yAxis), offset.dot(frame.zAxis)); +} + function selectionFrameCorners(frame: SelectionFrame) { const corners: THREE.Vector3[] = []; [-1, 1].forEach((xSign) => { @@ -639,6 +648,38 @@ function selectionWorldYBounds(frame: SelectionFrame) { return { min, max, height: Math.max(MIN_SHAPE_SIZE, max - min) }; } +function localResizePlaneForFrame(frame: SelectionFrame) { + return new THREE.Plane().setFromNormalAndCoplanarPoint( + frame.yAxis.clone().normalize(), + framePoint(frame, 0, frame.min.y, 0), + ); +} + +function resizeSignsForHandle(handleKey: string): ResizeSigns { + const key = handleKey.toLowerCase(); + return { + x: key.includes("right") ? 1 : key.includes("left") ? -1 : 0, + z: key.includes("near") ? 1 : key.includes("far") ? -1 : 0, + }; +} + +function resizeAnchorPointForFrame(frame: SelectionFrame, signs: ResizeSigns) { + return framePoint( + frame, + signs.x ? (-signs.x * frame.width) / 2 : 0, + frame.min.y, + signs.z ? (-signs.z * frame.depth) / 2 : 0, + ); +} + +function resizeCenterFromAnchor(frame: SelectionFrame, anchor: THREE.Vector3, signs: ResizeSigns, width: number, depth: number) { + return anchor + .clone() + .add(frame.yAxis.clone().multiplyScalar(frame.height / 2)) + .add(frame.xAxis.clone().multiplyScalar(signs.x ? (signs.x * width) / 2 : 0)) + .add(frame.zAxis.clone().multiplyScalar(signs.z ? (signs.z * depth) / 2 : 0)); +} + function shapeScreenBounds(state: ThreeState, shape: WorkplaneShape) { const frame = selectionFrameForShapes([shape], [shape.id]); if (!frame) { @@ -678,12 +719,10 @@ function signedAngleAroundAxis(start: THREE.Vector3, current: THREE.Vector3, axi return Math.atan2(axis.clone().normalize().dot(a.clone().cross(b)), clamp(a.dot(b), -1, 1)); } -function screenVectorAngle(from: { x: number; y: number }, to: { x: number; y: number }) { - return THREE.MathUtils.radToDeg(Math.atan2(to.y - from.y, to.x - from.x)); -} - const ROTATION_HANDLE_SIDE_HYSTERESIS = 0.22; const ROTATION_HANDLE_DOMINANCE_HYSTERESIS = 0.18; +const ROTATION_UPPER_HANDLE_ICON_ANGLE = 0; +const ROTATION_BOTTOM_HANDLE_ICON_ANGLE = 0; function signedRotationSide(value: number, previous: RotationHandleSide | undefined, positiveSide: RotationHandleSide, negativeSide: RotationHandleSide) { if (previous === positiveSide && value > -ROTATION_HANDLE_SIDE_HYSTERESIS) { @@ -757,67 +796,71 @@ function projectedWorldYForScreenY(state: ThreeState, shape: WorkplaneShape, tar return nextWorldY; } -function resizeFromHandle( - shape: WorkplaneShape, - point: { x: number; z: number }, +function patchWithPreservedWorldBottom(shape: WorkplaneShape, patch: Partial) { + const startFrame = selectionFrameForShapes([shape], [shape.id]); + if (!startFrame) { + return patch; + } + const startBounds = selectionWorldYBounds(startFrame); + const draftShape = { ...shape, ...patch }; + const draftFrame = selectionFrameForShapes([draftShape], [shape.id]); + if (!draftFrame) { + return patch; + } + const draftBounds = selectionWorldYBounds(draftFrame); + return { + ...patch, + elevation: cleanNearZero(clamp((draftShape.elevation ?? 0) + startBounds.min - draftBounds.min, MIN_ELEVATION, MAX_ELEVATION), 0.0005), + }; +} + +function resizeShapeFromFrameHandle( + transform: TransformDragState, + point: THREE.Vector3, handleKey: string, shiftKey: boolean, altKey: boolean, step: number, ): Partial { - const width = shapeWidth(shape); - const depth = shapeDepth(shape); - const angle = THREE.MathUtils.degToRad(shape.rotation); - const xAxis = { x: Math.cos(angle), z: Math.sin(angle) }; - const zAxis = { x: -Math.sin(angle), z: Math.cos(angle) }; - const dx = point.x - shape.x; - const dz = point.z - shape.z; - const localX = dx * xAxis.x + dz * xAxis.z; - const localZ = dx * zAxis.x + dz * zAxis.z; + const shape = transform.startShape; + const frame = transform.selectionFrame; + const width = frame.width; + const depth = frame.depth; + const localDelta = transform.scaleStartPoint ? frameLocalDelta(frame, transform.scaleStartPoint, point) : new THREE.Vector3(); - const xSign = handleKey.includes("right") ? 1 : handleKey.includes("left") ? -1 : 0; - const zSign = handleKey.includes("near") ? 1 : handleKey.includes("far") ? -1 : 0; + const signs = transform.scaleSigns ?? resizeSignsForHandle(handleKey); const maxSize = 220; - const axisResize = (current: number, local: number, sign: number) => { + const axisResize = (current: number, delta: number, sign: number) => { if (!sign) { - return { size: current, center: 0 }; + return current; } + const signedDelta = sign * delta; if (altKey) { - return { size: snapDimension(Math.abs(local) * 2, step, MIN_SHAPE_SIZE, maxSize), center: 0 }; + return snapDimension(current + signedDelta * 2, step, MIN_SHAPE_SIZE, maxSize); } - const fixedEdge = (-sign * current) / 2; - const rawSize = sign * (local - fixedEdge); - const nextSize = snapDimension(rawSize, step, MIN_SHAPE_SIZE, maxSize); - return { - size: nextSize, - center: fixedEdge + (sign * nextSize) / 2, - }; + return snapDimension(current + signedDelta, step, MIN_SHAPE_SIZE, maxSize); }; - let nextX = axisResize(width, localX, xSign); - let nextZ = axisResize(depth, localZ, zSign); + let nextWidth = axisResize(width, localDelta.x, signs.x); + let nextDepth = axisResize(depth, localDelta.z, signs.z); - if (shiftKey && xSign && zSign) { - const uniform = Math.max(nextX.size, nextZ.size); - nextX = { - size: uniform, - center: altKey ? 0 : (-xSign * width) / 2 + (xSign * uniform) / 2, - }; - nextZ = { - size: uniform, - center: altKey ? 0 : (-zSign * depth) / 2 + (zSign * uniform) / 2, - }; + if (shiftKey && signs.x && signs.z) { + const uniform = Math.max(nextWidth, nextDepth); + nextWidth = uniform; + nextDepth = uniform; } - const worldShiftX = xAxis.x * nextX.center + zAxis.x * nextZ.center; - const worldShiftZ = xAxis.z * nextX.center + zAxis.z * nextZ.center; + const nextCenter = altKey + ? frame.center.clone() + : resizeCenterFromAnchor(frame, transform.scaleAnchorPoint ?? resizeAnchorPointForFrame(frame, signs), signs, nextWidth, nextDepth); return { - x: shape.x + worldShiftX, - z: shape.z + worldShiftZ, - width: nextX.size, - depth: nextZ.size, - size: (nextX.size + nextZ.size) / 2, + x: nextCenter.x, + z: nextCenter.z, + elevation: cleanNearZero(nextCenter.y - shape.height / 2, 0.0005), + width: nextWidth, + depth: nextDepth, + size: (nextWidth + nextDepth) / 2, }; } @@ -830,47 +873,42 @@ function resizeSelectionFromHandle( step: number, ) { const frame = transform.selectionFrame; - const local = frameLocalPoint(frame, point); - const xSign = handleKey.includes("right") ? 1 : handleKey.includes("left") ? -1 : 0; - const zSign = handleKey.includes("near") ? 1 : handleKey.includes("far") ? -1 : 0; - const axisResize = (current: number, value: number, sign: number) => { + const localDelta = transform.scaleStartPoint ? frameLocalDelta(frame, transform.scaleStartPoint, point) : new THREE.Vector3(); + const signs = transform.scaleSigns ?? resizeSignsForHandle(handleKey); + const axisResize = (current: number, delta: number, sign: number) => { if (!sign) { - return { size: current, center: 0, scale: 1 }; + return { size: current, scale: 1 }; } + const signedDelta = sign * delta; if (altKey) { - const size = snapDimension(Math.abs(value) * 2, step, MIN_SHAPE_SIZE, 260); - return { size, center: 0, scale: size / Math.max(MIN_SHAPE_SIZE, current) }; + const size = snapDimension(current + signedDelta * 2, step, MIN_SHAPE_SIZE, 260); + return { size, scale: size / Math.max(MIN_SHAPE_SIZE, current) }; } - const fixedEdge = (-sign * current) / 2; - const rawSize = sign * (value - fixedEdge); + const rawSize = current + signedDelta; const size = snapDimension(rawSize, step, MIN_SHAPE_SIZE, 260); return { size, - center: fixedEdge + (sign * size) / 2, scale: size / Math.max(MIN_SHAPE_SIZE, current), }; }; - let nextX = axisResize(frame.width, local.x, xSign); - let nextZ = axisResize(frame.depth, local.z, zSign); - if (shiftKey && xSign && zSign) { + let nextX = axisResize(frame.width, localDelta.x, signs.x); + let nextZ = axisResize(frame.depth, localDelta.z, signs.z); + if (shiftKey && signs.x && signs.z) { const uniform = Math.max(nextX.size, nextZ.size); nextX = { size: uniform, - center: altKey ? 0 : (-xSign * frame.width) / 2 + (xSign * uniform) / 2, scale: uniform / Math.max(MIN_SHAPE_SIZE, frame.width), }; nextZ = { size: uniform, - center: altKey ? 0 : (-zSign * frame.depth) / 2 + (zSign * uniform) / 2, scale: uniform / Math.max(MIN_SHAPE_SIZE, frame.depth), }; } - const nextCenter = frame.center - .clone() - .add(frame.xAxis.clone().multiplyScalar(nextX.center)) - .add(frame.zAxis.clone().multiplyScalar(nextZ.center)); + const nextCenter = altKey + ? frame.center.clone() + : resizeCenterFromAnchor(frame, transform.scaleAnchorPoint ?? resizeAnchorPointForFrame(frame, signs), signs, nextX.size, nextZ.size); return transform.items.map((item) => { const localCenter = frameLocalPoint(frame, item.startCenter); @@ -881,15 +919,17 @@ 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 patch = { + x: nextItemCenter.x, + z: nextItemCenter.z, + elevation: cleanNearZero(nextItemCenter.y - item.startShape.height / 2, 0.0005), + width, + depth, + size: (width + depth) / 2, + } satisfies Partial; return { id: item.id, - patch: { - x: nextItemCenter.x, - z: nextItemCenter.z, - width, - depth, - size: (width + depth) / 2, - } satisfies Partial, + patch, }; }); } @@ -1170,7 +1210,7 @@ export function WorkplaneViewport({ }; }, []); - const toPlanePointAtY = useCallback((clientX: number, clientY: number, planeY = 0) => { + const toRawPlanePoint = useCallback((clientX: number, clientY: number, plane: THREE.Plane) => { const state = threeRef.current; if (!state) { return null; @@ -1182,18 +1222,27 @@ export function WorkplaneViewport({ state.raycaster.setFromCamera(state.pointer, state.camera); const hit = new THREE.Vector3(); - const plane = planeY === 0 ? state.dragPlane : new THREE.Plane(new THREE.Vector3(0, 1, 0), -planeY); if (!state.raycaster.ray.intersectPlane(plane, hit)) { return null; } + return hit; + }, []); + + const toPlanePointAtY = useCallback((clientX: number, clientY: number, planeY = 0) => { + const state = threeRef.current; + const hit = toRawPlanePoint(clientX, clientY, planeY === 0 ? state?.dragPlane ?? new THREE.Plane(new THREE.Vector3(0, 1, 0), 0) : new THREE.Plane(new THREE.Vector3(0, 1, 0), -planeY)); + if (!state || !hit) { + return null; + } + const step = snapStep(snapRef.current); const bounds = workspaceRef.current; return { x: clamp(snapValue(hit.x, step), -bounds.width / 2 + 6, bounds.width / 2 - 6), z: clamp(snapValue(hit.z, step), -bounds.depth / 2 + 6, bounds.depth / 2 - 6), }; - }, []); + }, [toRawPlanePoint]); const toPlanePoint = useCallback((clientX: number, clientY: number) => toPlanePointAtY(clientX, clientY, 0), [toPlanePointAtY]); const setMarqueeFromState = useCallback((marquee: MarqueeState | null) => { @@ -1235,6 +1284,7 @@ export function WorkplaneViewport({ } const rotationAxis = rotationAxisForHandle(handleKey); + const resizeHandleKey = handleKey; const state = threeRef.current; const yBounds = selectionWorldYBounds(frame); const handlesLowerSide = handleKey === "bottom-height" || handleKey === "lower-shape"; @@ -1255,6 +1305,13 @@ export function WorkplaneViewport({ const rotationCenter = kind === "rotate" ? wheel ?? (state ? projectToScreen(pivot, state) : { x: localClientX, y: localClientY }) : undefined; const rotationStartPoint = kind === "rotate" && state ? rayPointOnRotationPlane(state, event.clientX, event.clientY, rotationPlaneCenter, axisVector) : null; const rotationStartVector = rotationStartPoint ? rotationStartPoint.sub(rotationPlaneCenter) : undefined; + const scalePlane = kind === "scale" ? localResizePlaneForFrame(frame) : undefined; + const scaleStartPoint = scalePlane ? toRawPlanePoint(event.clientX, event.clientY, scalePlane) ?? undefined : undefined; + const scaleSigns = kind === "scale" ? resizeSignsForHandle(resizeHandleKey) : undefined; + const scaleAnchorPoint = kind === "scale" && scaleSigns ? resizeAnchorPointForFrame(frame, scaleSigns) : undefined; + if (kind === "scale" && !scaleStartPoint) { + return; + } event.preventDefault(); event.stopPropagation(); event.currentTarget.setPointerCapture(event.pointerId); @@ -1272,7 +1329,7 @@ export function WorkplaneViewport({ id: shape.id, ids: frame.ids, kind, - handleKey, + handleKey: resizeHandleKey, rotationAxis, pointerId: event.pointerId, startShape: { ...shape }, @@ -1294,6 +1351,10 @@ export function WorkplaneViewport({ handleWorldOffset: liftOffset, screenYPerWorldUnit: state ? projectedScreenYPerWorldUnitAt(state, frame.center.x, frame.center.z, startWorldY) : -3.2, scalePlaneY: kind === "scale" ? yBounds.min : 0, + scalePlane, + scaleSigns, + scaleAnchorPoint, + scaleStartPoint, rotationAxisVector: kind === "rotate" ? axisVector : undefined, rotationPivot: kind === "rotate" ? pivot : undefined, rotationPlaneCenter: kind === "rotate" ? rotationPlaneCenter : undefined, @@ -1326,7 +1387,7 @@ export function WorkplaneViewport({ } onInteractionActiveChange?.(true); }, - [onInteractionActiveChange], + [onInteractionActiveChange, toRawPlanePoint], ); const updateTransform = useCallback( @@ -1406,25 +1467,25 @@ export function WorkplaneViewport({ return true; } - const point = transform.kind === "scale" ? toPlanePointAtY(clientX, clientY, transform.scalePlaneY) : toPlanePoint(clientX, clientY); - if (!point && transform.kind !== "rotate") { - return true; - } - if (transform.kind === "scale") { - if (!point) { + const worldPoint = transform.scalePlane ? toRawPlanePoint(clientX, clientY, transform.scalePlane) : null; + if (!worldPoint) { return true; } if (transform.items.length === 1) { - const next = resizeFromHandle(shape, point, transform.handleKey, shiftKey, altKey, step); + const next = resizeShapeFromFrameHandle(transform, worldPoint, transform.handleKey, shiftKey, altKey, step); onUpdateShape(transform.id, next); } else { - const worldPoint = new THREE.Vector3(point.x, transform.scalePlaneY, point.z); resizeSelectionFromHandle(transform, worldPoint, transform.handleKey, shiftKey, altKey, step).forEach(({ id, patch }) => onUpdateShape(id, patch)); } return true; } + const point = toPlanePoint(clientX, clientY); + if (!point && transform.kind !== "rotate") { + return true; + } + const state = threeRef.current; const rotationCenter = transform.rotationScreenCenter ?? transform.wheelCenter; if (!state || !rotationCenter) { @@ -1474,7 +1535,7 @@ export function WorkplaneViewport({ }); return true; }, - [onUpdateShape, toPlanePoint, toPlanePointAtY], + [onUpdateShape, toPlanePoint, toRawPlanePoint], ); const suppressLiftEditAfterDrag = useCallback(() => { @@ -1492,11 +1553,9 @@ export function WorkplaneViewport({ if (event.currentTarget.hasPointerCapture(transform.pointerId)) { event.currentTarget.releasePointerCapture(transform.pointerId); } + const bakeRotatedShapes = transform.kind === "rotate" && transform.hasMoved ? transform.ids : []; if (transform.kind === "lift") { setPinnedMeasureKey(getElevationMeasureKey(transformOverlayRef.current)); - } else if (transform.kind === "height") { - setPinnedMeasureKey(null); - setHoverMeasureKey(null); } if (transform.kind === "lift" && transform.hasMoved) { suppressLiftEditAfterDrag(); @@ -1509,13 +1568,10 @@ export function WorkplaneViewport({ threeRef.current.controls.enabled = true; } onInteractionActiveChange?.(false); - }, [onInteractionActiveChange, suppressLiftEditAfterDrag]); + bakeRotatedShapes.forEach((id) => onUpdateShape(id, { bakeTransform: true })); + }, [onInteractionActiveChange, onUpdateShape, suppressLiftEditAfterDrag]); const beginDimensionEdit = useCallback((mark: DimensionMark) => { - if (mark.axis === "height") { - setPinnedMeasureKey(getElevationMeasureKey(transformOverlayRef.current)); - return; - } setPinnedMeasureKey(mark.handleKey); setEditingDimension({ key: mark.key, axis: mark.axis, x: mark.labelX, y: mark.labelY, value: mark.label }); }, []); @@ -1575,11 +1631,11 @@ export function WorkplaneViewport({ if (Number.isFinite(value) && value > 0) { const nextValue = Math.max(MIN_SHAPE_SIZE, value); if (edit.axis === "width") { - onUpdateShape(id, { width: nextValue, size: (nextValue + shapeDepth(shape)) / 2 }); + onUpdateShape(id, patchWithPreservedWorldBottom(shape, { width: nextValue, size: (nextValue + shapeDepth(shape)) / 2 })); } else if (edit.axis === "depth") { - onUpdateShape(id, { depth: nextValue, size: (shapeWidth(shape) + nextValue) / 2 }); + onUpdateShape(id, patchWithPreservedWorldBottom(shape, { depth: nextValue, size: (shapeWidth(shape) + nextValue) / 2 })); } else { - onUpdateShape(id, { height: nextValue }); + onUpdateShape(id, patchWithPreservedWorldBottom(shape, { height: nextValue })); } } setEditingDimension(null); @@ -1613,7 +1669,7 @@ export function WorkplaneViewport({ } const value = Number.parseFloat(edit.value); if (Number.isFinite(value)) { - selectedIdsRef.current.forEach((id) => onUpdateShape(id, rotationPatchForAxis(edit.axis, value))); + selectedIdsRef.current.forEach((id) => onUpdateShape(id, { ...rotationPatchForAxis(edit.axis, value), bakeTransform: true })); } setEditingRotation(null); setActiveRotationWheel(false); @@ -1713,7 +1769,9 @@ export function WorkplaneViewport({ if (handle) { const shape = shapesRef.current.find((entry) => entry.id === handle.id); const frame = selectionFrameForShapes(shapesRef.current, selectedIdsRef.current); - const point = handle.kind === "scale" ? toPlanePointAtY(event.clientX, event.clientY, handle.planeY) : toPlanePoint(event.clientX, event.clientY); + const scalePlane = handle.kind === "scale" && frame ? localResizePlaneForFrame(frame) : undefined; + const scaleStartPoint = scalePlane ? toRawPlanePoint(event.clientX, event.clientY, scalePlane) ?? undefined : undefined; + const point = scalePlane ? scaleStartPoint : toPlanePoint(event.clientX, event.clientY); if (!shape || !frame || shape.locked || (!point && handle.kind !== "height" && handle.kind !== "lift" && handle.kind !== "rotate")) { return; } @@ -1724,6 +1782,9 @@ export function WorkplaneViewport({ const startWorldY = yStart + liftOffset; const overlay = transformOverlayRef.current; const rotationAxis = rotationAxisForHandle(handle.handleKey); + const resizeHandleKey = handle.handleKey; + const scaleSigns = handle.kind === "scale" ? resizeSignsForHandle(resizeHandleKey) : undefined; + const scaleAnchorPoint = handle.kind === "scale" && scaleSigns ? resizeAnchorPointForFrame(frame, scaleSigns) : undefined; const wheel = handle.kind === "rotate" ? (overlay?.rotationWheels[rotationAxis] ?? overlay?.rotationWheel ?? undefined) : undefined; const rotationPlaneCenterData = handle.kind === "rotate" ? overlay?.rotationPlaneCenters[rotationAxis] : undefined; const rotationPlaneCenter = rotationPlaneCenterData @@ -1752,7 +1813,7 @@ export function WorkplaneViewport({ id: handle.id, ids: frame.ids, kind: handle.kind, - handleKey: handle.handleKey, + handleKey: resizeHandleKey, rotationAxis, pointerId: event.pointerId, startShape: { ...shape }, @@ -1774,6 +1835,10 @@ export function WorkplaneViewport({ handleWorldOffset: liftOffset, screenYPerWorldUnit: projectedScreenYPerWorldUnitAt(state, frame.center.x, frame.center.z, startWorldY), scalePlaneY: handle.kind === "scale" ? handle.planeY : 0, + scalePlane, + scaleSigns, + scaleAnchorPoint, + scaleStartPoint, rotationAxisVector: handle.kind === "rotate" ? axisVector : undefined, rotationPivot: handle.kind === "rotate" ? pivot : undefined, rotationPlaneCenter: handle.kind === "rotate" ? rotationPlaneCenter : undefined, @@ -1900,6 +1965,7 @@ export function WorkplaneViewport({ setMarqueeFromState, toPlanePoint, toPlanePointAtY, + toRawPlanePoint, ], ); @@ -2188,11 +2254,7 @@ export function WorkplaneViewport({ editingRotation={editingRotation} rotationReadout={rotationReadout} showRotationWheel={activeRotationWheel} - hideDimensionMarks={ - activeTransformKind === "scale" || - activeTransformKind === "height" || - editingDimension?.axis === "height" - } + hideDimensionMarks={activeTransformKind === "scale"} rotationWheelAxis={rotationWheelAxis} onBeginTransform={beginTransform} onMoveTransform={updateTransform} @@ -2281,7 +2343,7 @@ function ShapeInspector({ {locked ? : } @@ -2565,17 +2627,8 @@ function TransformOverlay({ onCommitRotationEdit: () => void; onCancelRotationEdit: () => void; }) { - const elevationMarks = Object.values(box.dimensions) - .flat() - .filter((mark) => mark.axis === "elevation"); - const elevationHandleKey = elevationMarks[0]?.handleKey; - const rawMarks = measureKey ? (box.dimensions[measureKey] ?? []) : []; - const requestedHeightMeasurement = rawMarks.some((mark) => mark.axis === "height"); - const resolvedMeasureKey = requestedHeightMeasurement && elevationHandleKey ? elevationHandleKey : measureKey; - const marks = resolvedMeasureKey ? (box.dimensions[resolvedMeasureKey] ?? []) : []; - const visibleMarks = (hideDimensionMarks ? [] : requestedHeightMeasurement ? elevationMarks : marks).filter( - (mark) => mark.axis !== "height" && mark.key !== editingDimension?.key, - ); + const marks = measureKey ? (box.dimensions[measureKey] ?? []) : []; + const visibleMarks = (hideDimensionMarks ? [] : marks).filter((mark) => mark.key !== editingDimension?.key); const handleMeasureKey = (handle: TransformOverlayState["handles"][number]) => measureKeyForHandle(handle.kind, handle.key, box); const protractorTicks = Array.from({ length: 16 }, (_, index) => { const degrees = index * 22.5 - 90; @@ -2662,7 +2715,7 @@ function TransformOverlay({ {mark.label} ))} - {editingDimension && editingDimension.axis !== "height" ? ( + {editingDimension ? ( corner.x)) - Math.min(...corners.map((corner) => corner.x))), - depth: Math.max(MIN_SHAPE_SIZE, Math.max(...corners.map((corner) => corner.z)) - Math.min(...corners.map((corner) => corner.z))), - height: Math.max(MIN_SHAPE_SIZE, Math.max(...corners.map((corner) => corner.y)) - Math.min(...corners.map((corner) => corner.y))), - }; -} - function getShapeProperties(shape: WorkplaneShape, onUpdate: (patch: Partial) => void): ShapePropertyConfig[] { const width = shapeWidth(shape); const depth = shapeDepth(shape); - const worldSize = worldBoundingDimensionsForShape(shape); const setWidth = (value: number) => onUpdate({ width: value, size: (value + depth) / 2 }); const setDepth = (value: number) => onUpdate({ depth: value, size: (width + value) / 2 }); const setBaseRadius = (value: number) => onUpdate({ baseRadius: value, width: value * 2, depth: value * 2, size: value * 2 }); @@ -2826,9 +2859,9 @@ function getShapeProperties(shape: WorkplaneShape, onUpdate: (patch: Partial onUpdate({ radius }) }, { label: "Steps", value: shape.steps ?? 10, min: 1, max: 64, step: 1, onChange: (steps) => onUpdate({ steps: Math.round(steps) }) }, - { label: "Length", value: depth, displayValue: worldSize.depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth }, - { label: "Width", value: width, displayValue: worldSize.width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth }, - { label: "Height", value: shape.height, displayValue: worldSize.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) }, + { label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth }, + { label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth }, + { label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) }, ]; } @@ -2847,9 +2880,9 @@ function getShapeProperties(shape: WorkplaneShape, onUpdate: (patch: Partial onUpdate({ steps: Math.round(steps) }) }, - { label: "Length", value: depth, displayValue: worldSize.depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth }, - { label: "Width", value: width, displayValue: worldSize.width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth }, - { label: "Height", value: shape.height, displayValue: worldSize.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) }, + { label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth }, + { label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth }, + { label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) }, ]; } @@ -2857,7 +2890,7 @@ function getShapeProperties(shape: WorkplaneShape, onUpdate: (patch: Partial onUpdate({ topRadius }) }, { label: "Base Radius", value: shape.baseRadius ?? width / 2, min: MIN_SHAPE_SIZE, max: 80, onChange: setBaseRadius }, - { label: "Height", value: shape.height, displayValue: worldSize.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) }, + { label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) }, { label: "Sides", value: shape.sides ?? 96, min: 3, max: 128, step: 1, onChange: (sides) => onUpdate({ sides: Math.round(sides) }) }, ]; } @@ -2869,18 +2902,18 @@ function getShapeProperties(shape: WorkplaneShape, onUpdate: (patch: Partial onUpdate({ sides: Math.round(sides) }) }, - { label: "Length", value: depth, displayValue: worldSize.depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth }, - { label: "Width", value: width, displayValue: worldSize.width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth }, - { label: "Height", value: shape.height, displayValue: worldSize.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) }, + { label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth }, + { label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth }, + { label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) }, ]; } if (shape.kind === "tube" || shape.kind === "ring") { return [ { label: "Thickness", value: shape.bevel ?? 4, min: 0.5, max: 20, onChange: (bevel) => onUpdate({ bevel }) }, - { label: "Length", value: depth, displayValue: worldSize.depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth }, - { label: "Width", value: width, displayValue: worldSize.width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth }, - { label: "Height", value: shape.height, displayValue: worldSize.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) }, + { label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth }, + { label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth }, + { label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) }, ]; } @@ -2897,22 +2930,21 @@ function getShapeProperties(shape: WorkplaneShape, onUpdate: (patch: Partial onUpdate({ font }) }, - { label: "Height", value: shape.height, displayValue: worldSize.height, min: MIN_SHAPE_SIZE, max: 40, onChange: (height) => onUpdate({ height }) }, + { label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 40, onChange: (height) => onUpdate({ height }) }, { label: "Bevel", value: shape.bevel ?? 0, min: 0, max: 8, onChange: (bevel) => onUpdate({ bevel }) }, { label: "Segments", value: shape.segments ?? 0, min: 0, max: 24, step: 1, onChange: (segments) => onUpdate({ segments: Math.round(segments) }) }, ]; } return [ - { label: "Length", value: depth, displayValue: worldSize.depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth }, - { label: "Width", value: width, displayValue: worldSize.width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth }, - { label: "Height", value: shape.height, displayValue: worldSize.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) }, + { label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth }, + { label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth }, + { label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) }, ]; } -function RangeProperty({ label, value, displayValue, min, max, step = 0.01, disabled, onChange }: RangePropertyConfig & { disabled?: boolean }) { +function RangeProperty({ label, value, min, max, step = 0.01, disabled, onChange }: RangePropertyConfig & { disabled?: boolean }) { const safeValue = clamp(value, min, max); - const safeDisplayValue = displayValue ?? safeValue; const position = ((safeValue - min) / Math.max(1, max - min)) * 100; const [editing, setEditing] = useState(false); const [draft, setDraft] = useState(formatPanelNumber(safeValue)); @@ -2956,7 +2988,7 @@ function RangeProperty({ label, value, displayValue, min, max, step = 0.01, disa setEditing(true); }} > - {formatPanelNumber(safeDisplayValue)} + {formatPanelNumber(safeValue)} )} { + if (axis === "width") { + const useFarSide = handleKey.includes("far") || handleKey.includes("left"); + return makeDimensionMark( + `${handleKey}-width`, + handleKey, + "width", + widthLabel, + useFarSide ? footprintWorld.farLeft : footprintWorld.nearLeft, + useFarSide ? footprintWorld.farRight : footprintWorld.nearRight, + useFarSide ? farOut : nearOut, + project, + ); + } + const useLeftSide = handleKey.includes("left") || handleKey.includes("far"); + return makeDimensionMark( + `${handleKey}-depth`, + handleKey, + "depth", + depthLabel, + useLeftSide ? footprintWorld.nearLeft : footprintWorld.nearRight, + useLeftSide ? footprintWorld.farLeft : footprintWorld.farRight, + useLeftSide ? leftOut : rightOut, + project, + ); }; + const footprintHandleKeys = ["near-left", "near-right", "far-right", "far-left", "near-mid", "right-mid", "far-mid", "left-mid"]; + const footprintDimensionMarks = Object.fromEntries( + footprintHandleKeys.map((handleKey) => { + const axes = new Set<"width" | "depth">(); + if (handleKey.includes("left") || handleKey.includes("right")) { + axes.add("width"); + } + if (handleKey.includes("near") || handleKey.includes("far")) { + axes.add("depth"); + } + return [handleKey, Array.from(axes).map((axis) => makeFootprintDimensionMark(handleKey, axis))]; + }), + ); const dimensionMarks = { ...footprintDimensionMarks, + [heightHandleKey]: [makeDimensionMark("height", heightHandleKey, "height", heightLabel, bottomCenterWorld, topCenterWorld, rightOut, project)], [liftHandleKey]: [makeDimensionMark("elevation", liftHandleKey, "elevation", liftLabel, workplaneAnchor, verticalBase, rightOut, project)], }; const screenOffsetFromCenter = (point: { x: number; y: number }, distance: number) => { @@ -3763,7 +3810,7 @@ function syncTransformOverlay( const rotateBottom = screenOffsetFromCenter(project(sidePoint(rotationSides.y, worldMinY)), 34); const xFaceCenter = sidePoint(rotationSides.x, worldCenterY); const zFaceCenter = sidePoint(rotationSides.z, worldCenterY); - const yFaceCenter = sidePoint(rotationSides.y, worldMinY); + const yFaceCenter = verticalBase; const planeRadius = 154; const planeWorldStep = Math.max(12, Math.max(frame.width, frame.depth, frame.height) * 0.78); const makePlaneView = (centerWorld: THREE.Vector3, uAxis: THREE.Vector3, vAxis: THREE.Vector3): RotationPlaneView => { @@ -3825,9 +3872,9 @@ function syncTransformOverlay( { key: liftHandleKey, className: showLowerHandles ? "height-lift lower" : "height-lift", kind: "lift" as const, x: liftPoint.x, y: liftPoint.y, title: "Lift" }, ], rotateHandles: [ - { key: "rotate-left", className: "screen-left", x: rotateLeft.x, y: rotateLeft.y, angle: screenVectorAngle(centerPoint, rotateLeft) + 90 }, - { key: "rotate-right", className: "screen-right", x: rotateRight.x, y: rotateRight.y, angle: screenVectorAngle(centerPoint, rotateRight) - 90 }, - { key: "rotate-bottom", className: "screen-bottom", x: rotateBottom.x, y: rotateBottom.y, angle: screenVectorAngle(centerPoint, rotateBottom) + 90 }, + { key: "rotate-left", className: "screen-left", x: rotateLeft.x, y: rotateLeft.y, angle: ROTATION_UPPER_HANDLE_ICON_ANGLE }, + { key: "rotate-right", className: "screen-right", x: rotateRight.x, y: rotateRight.y, angle: ROTATION_UPPER_HANDLE_ICON_ANGLE }, + { key: "rotate-bottom", className: "screen-bottom", x: rotateBottom.x, y: rotateBottom.y, angle: ROTATION_BOTTOM_HANDLE_ICON_ANGLE }, ], dimensions: dimensionMarks, rotationWheel: rotationWheels.y,