Files
snapcad-studio/apps/web/src/components/WorkplaneViewport.tsx
T

3970 lines
155 KiB
TypeScript
Raw Normal View History

2026-06-06 23:10:29 +03:00
"use client";
2026-06-14 22:05:58 +03:00
import { Box, Check, Expand, Home, Minus, Plus } from "lucide-react";
import { useCallback, useEffect, useMemo, useRef, useState, type Dispatch, type DragEvent, type MutableRefObject, type PointerEvent as ReactPointerEvent, type SetStateAction } from "react";
2026-06-06 23:10:29 +03:00
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import { RoundedBoxGeometry } from "three/examples/jsm/geometries/RoundedBoxGeometry.js";
import { TextGeometry } from "three/examples/jsm/geometries/TextGeometry.js";
import { FontLoader, type Font, type FontData } from "three/examples/jsm/loaders/FontLoader.js";
import droidMonoFontJson from "three/examples/fonts/droid/droid_sans_mono_regular.typeface.json";
import droidSansBoldFontJson from "three/examples/fonts/droid/droid_sans_bold.typeface.json";
import droidSerifBoldFontJson from "three/examples/fonts/droid/droid_serif_bold.typeface.json";
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";
2026-06-13 09:33:10 +03:00
import { AlignOverlay, MirrorOverlay, type AlignOverlayState, type MirrorOverlayState } from "@/components/workplane/ActionOverlays";
2026-06-15 12:56:23 +03:00
import { ShapeInspector, SnapGridControl, type ShapeInspectorUpdateOptions } from "@/components/workplane/ShapeInspector";
2026-06-14 22:05:58 +03:00
import { WorkspaceSettingsModal } from "@/components/workplane/WorkspaceSettingsModal";
2026-06-13 10:26:40 +03:00
import { DEFAULT_SNAP_GRID, DEFAULT_WORKPLANE_WORKSPACE, normalizeSnapGrid, normalizeWorkspaceSettings, workplaneSettingsFingerprint } from "@/lib/workplaneSettings";
2026-06-14 22:05:58 +03:00
import { cleanNearZero, cleanRotationDegrees, fallbackSolidColor, mirroredAxisCount, mirrorSign, proportionalResizeScale, resizedShapeSize, shapeDepth, shapeWidth } from "@/lib/workplaneShapes";
2026-06-13 09:33:10 +03:00
import {
TransformOverlay,
getElevationMeasureKey,
measureKeyForHandle,
type DimensionMark,
type EditingDimension,
type EditingRotation,
type PinnedRotationWheelView,
type RotationAxis,
type RotationPlaneView,
type RotationReadout,
type RotationWheelView,
type TransformHandleKind,
type TransformOverlayState,
} from "@/components/workplane/TransformOverlay";
2026-06-13 10:26:40 +03:00
import type { AlignAxis, AlignHandleStatus, AlignTarget, GridSize, ShapeAsset, WorkplaneShape, WorkplaneWorkspaceSettings } from "@/types/sketchforge";
2026-06-06 23:10:29 +03:00
const WORKPLANE_WIDTH = 200;
const WORKPLANE_DEPTH = 140;
const MIN_GRID_BLOCK_SIZE = 1;
const MAX_GRID_BLOCK_SIZE = 200;
2026-06-13 10:26:40 +03:00
const DEFAULT_WORKSPACE = DEFAULT_WORKPLANE_WORKSPACE;
2026-06-06 23:10:29 +03:00
const CAMERA_HOME = new THREE.Vector3(118, 96, 118);
const CAMERA_TARGET = new THREE.Vector3(0, 0, 0);
2026-06-12 14:05:36 +03:00
const MIN_SHAPE_SIZE = 0.01;
2026-06-06 23:10:29 +03:00
const MIN_ELEVATION = -180;
const MAX_ELEVATION = 220;
const CAMERA_MIN_TARGET_Y = -70;
const CAMERA_MAX_TARGET_Y = 120;
2026-06-14 22:05:58 +03:00
const SHAPE_KINDS = new Set<ShapeAsset["kind"]>([
"box",
"cylinder",
"sphere",
"sketch",
"scribble",
"cone",
"pyramid",
"roof",
"text",
"roundRoof",
"halfSphere",
"torus",
"tube",
"ring",
"wedge",
"polygon",
"icosahedron",
"mesh",
]);
2026-06-06 23:10:29 +03:00
const fontLoader = new FontLoader();
const textFonts: Record<string, Font> = {
Multilanguage: fontLoader.parse(helvetikerBoldFontJson as FontData),
Sans: fontLoader.parse(droidSansBoldFontJson as FontData),
Serif: fontLoader.parse(droidSerifBoldFontJson as FontData),
Script: fontLoader.parse(gentilisBoldFontJson as FontData),
Monospace: fontLoader.parse(droidMonoFontJson as FontData),
Rounded: fontLoader.parse(optimerBoldFontJson as FontData),
Stencil: fontLoader.parse(helvetikerBoldFontJson as FontData),
};
const importedGeometryCache = new WeakMap<
NonNullable<WorkplaneShape["importedMesh"]>,
{ geometry: THREE.BufferGeometry; edges: Map<number, THREE.EdgesGeometry> }
>();
const imageTextureLoader = new THREE.TextureLoader();
const IMPORTED_SELECTED_EDGE_TRIANGLE_LIMIT = 40000;
2026-06-14 22:05:58 +03:00
function parseDroppedShapeAsset(raw: string): ShapeAsset | null {
try {
const value: unknown = JSON.parse(raw);
if (!value || typeof value !== "object") {
return null;
}
const asset = value as Partial<ShapeAsset>;
if (
typeof asset.id !== "string" ||
typeof asset.name !== "string" ||
typeof asset.src !== "string" ||
typeof asset.color !== "string" ||
!SHAPE_KINDS.has(asset.kind as ShapeAsset["kind"]) ||
(asset.hole !== undefined && typeof asset.hole !== "boolean")
) {
return null;
}
return {
id: asset.id,
name: asset.name,
src: asset.src,
kind: asset.kind as ShapeAsset["kind"],
color: asset.color,
hole: asset.hole,
};
} catch {
return null;
}
}
2026-06-06 23:10:29 +03:00
type WorkplaneViewportProps = {
shapes: WorkplaneShape[];
selectedIds: string[];
alignMode: boolean;
alignAnchorId: string | null;
alignHandles: AlignHandleStatus[];
alignReferenceShapes: WorkplaneShape[];
mirrorMode: boolean;
mirrorReferenceShapes: WorkplaneShape[];
placementElevation: number;
workplaneMode: boolean;
2026-06-13 10:26:40 +03:00
initialSnap?: GridSize;
initialWorkspace?: WorkplaneWorkspaceSettings;
workspaceSettingsKey?: string | null;
2026-06-06 23:10:29 +03:00
onAddShape: (shape: ShapeAsset, point?: { x: number; z: number; elevation?: number }) => void;
onAlignAnchorChange: (id: string) => void;
onAlignPreview: (axis: AlignAxis, target: AlignTarget) => void;
onAlignPreviewClear: () => void;
onAlignSelection: (axis: AlignAxis, target: AlignTarget) => void;
onMirrorPreview: (axis: AlignAxis) => void;
onMirrorPreviewClear: () => void;
onMirrorSelection: (axis: AlignAxis) => void;
onSelectShape: (id: string | string[] | null, mode?: "replace" | "toggle") => void;
onSetPlacementElevation: (elevation: number, source: "shape" | "base") => void;
onInteractionActiveChange?: (active: boolean) => void;
2026-06-12 14:05:36 +03:00
onUpdateShape: (id: string, patch: ShapeUpdatePatch) => void;
2026-06-13 10:26:40 +03:00
onWorkspaceSettingsChange?: (settings: { workspace: WorkplaneWorkspaceSettings; snap: GridSize }) => void;
2026-06-06 23:10:29 +03:00
onWorkplaneModeChange: (active: boolean) => void;
};
2026-06-13 10:26:40 +03:00
type WorkspaceSettings = WorkplaneWorkspaceSettings;
2026-06-06 23:10:29 +03:00
type ThreeState = {
renderer: THREE.WebGLRenderer;
scene: THREE.Scene;
camera: THREE.PerspectiveCamera;
controls: OrbitControls;
workplaneLayer: THREE.Group;
shapeLayer: THREE.Group;
helperLayer: THREE.Group;
raycaster: THREE.Raycaster;
pointer: THREE.Vector2;
dragPlane: THREE.Plane;
animationId: number;
needsRender: boolean;
wasCameraMoving: boolean;
lastOverlaySync: number;
lastViewCubeSync: number;
rotationHandleSides: RotationHandleSides | null;
2026-06-06 23:10:29 +03:00
disposeInteractionListeners: () => void;
resize: () => void;
};
type ViewportPerfStats = {
fps: number;
frameMs: number;
maxFrameMs: number;
drawCalls: number;
triangles: number;
points: number;
lines: number;
shapeCount: number;
};
declare global {
interface Window {
sketchforgePerf?: {
get: () => ViewportPerfStats;
};
sketchforgeCaptureCanvas?: () => string;
}
}
type DragState = {
primaryId: string;
offsetX: number;
offsetZ: number;
planeY: number;
pointerId: number;
primaryStartX: number;
primaryStartZ: number;
items: DragItem[];
};
type MarqueeState = {
pointerId: number;
startX: number;
startY: number;
currentX: number;
currentY: number;
additive: boolean;
hasMoved: boolean;
};
type RotationHandleSide = "near" | "right" | "far" | "left";
type RotationHandleSides = Record<RotationAxis, RotationHandleSide>;
2026-06-12 14:05:36 +03:00
type ShapeUpdatePatch = Partial<WorkplaneShape> & { bakeTransform?: boolean };
type ResizeSigns = { x: number; z: number };
2026-06-15 12:56:23 +03:00
type ResizeAnchorMemory = {
shapeId: string;
handleKey: string;
signs: ResizeSigns;
pressedY: "top" | "bottom" | null;
};
2026-06-06 23:10:29 +03:00
type TransformDragState = {
id: string;
ids: string[];
kind: TransformHandleKind;
handleKey: string;
rotationAxis: RotationAxis;
pointerId: number;
startShape: WorkplaneShape;
items: TransformDragItem[];
selectionFrame: SelectionFrame;
startScreenAngle: number;
startClientX: number;
startClientY: number;
startScreenY: number;
startWorldY: number;
handleWorldOffset: number;
screenYPerWorldUnit: number;
scalePlaneY: number;
2026-06-12 14:05:36 +03:00
scalePlane?: THREE.Plane;
scaleSigns?: ResizeSigns;
scaleAnchorPoint?: THREE.Vector3;
scaleStartPoint?: THREE.Vector3;
2026-06-06 23:10:29 +03:00
rotationAxisVector?: THREE.Vector3;
rotationPivot?: THREE.Vector3;
rotationPlaneCenter?: THREE.Vector3;
2026-06-06 23:10:29 +03:00
rotationStartVector?: THREE.Vector3;
rotationScreenCenter?: { x: number; y: number };
rotationScreenSign?: number;
rotationStartQuaternion?: THREE.Quaternion;
2026-06-13 09:13:15 +03:00
wheelCenter?: RotationWheelView;
2026-06-06 23:10:29 +03:00
hasMoved?: boolean;
};
type TransformDragItem = {
id: string;
startShape: WorkplaneShape;
startCenter: THREE.Vector3;
startQuaternion: THREE.Quaternion;
};
type SelectionFrame = {
ids: string[];
center: THREE.Vector3;
quaternion: THREE.Quaternion;
xAxis: THREE.Vector3;
yAxis: THREE.Vector3;
zAxis: THREE.Vector3;
width: number;
height: number;
depth: number;
min: THREE.Vector3;
max: THREE.Vector3;
singleShape: WorkplaneShape | null;
};
type DragItem = {
id: string;
startX: number;
startZ: number;
nextX: number;
nextZ: number;
visual: THREE.Object3D | null;
helper: THREE.Box3Helper | null;
helperBox: THREE.Box3 | null;
hadPreviewSimplified: boolean;
};
function isVerticalMeasureHandleKind(kind: TransformHandleKind) {
return kind === "height" || kind === "lift";
}
function previewShapesForDrag(shapes: WorkplaneShape[], drag: DragState | null) {
if (!drag) {
return shapes;
}
const previewById = new Map(drag.items.map((item) => [item.id, item]));
return shapes.map((shape) => {
const preview = previewById.get(shape.id);
return preview ? { ...shape, x: preview.nextX, z: preview.nextZ } : shape;
});
}
function clamp(value: number, min: number, max: number) {
return Math.min(max, Math.max(min, value));
}
function snapStep(size: GridSize) {
if (size === "Off") {
return 0;
}
if (size === "Brick") {
return 8;
}
return Number.parseFloat(size) || 1;
}
function snapValue(value: number, step: number) {
return step > 0 ? Math.round(value / step) * step : value;
}
function snapDimension(value: number, step: number, min = MIN_SHAPE_SIZE, max = 220) {
const snapped = step > 0 ? snapValue(value, step) : value;
const effectiveMin = step > 0 ? Math.max(min, Math.min(step, max)) : min;
return clamp(snapped, effectiveMin, max);
}
function snapPositionValue(value: number, step: number, min: number, max: number) {
return clamp(step > 0 ? snapValue(value, step) : value, min, max);
}
function projectedScreenY(state: ThreeState, shape: WorkplaneShape, y: number) {
return projectedScreenYAt(state, shape.x, shape.z, y);
}
function projectedScreenYAt(state: ThreeState, x: number, z: number, y: number) {
const rect = state.renderer.domElement.getBoundingClientRect();
state.camera.updateMatrixWorld();
const projected = new THREE.Vector3(x, y, z).project(state.camera);
return ((1 - projected.y) / 2) * rect.height;
}
function projectedScreenYPerWorldUnit(state: ThreeState, shape: WorkplaneShape, y: number) {
return projectedScreenYPerWorldUnitAt(state, shape.x, shape.z, y);
}
function projectedScreenYPerWorldUnitAt(state: ThreeState, x: number, z: number, y: number) {
const sample = 8;
const start = projectedScreenYAt(state, x, z, y);
const end = projectedScreenYAt(state, x, z, y + sample);
const slope = (end - start) / sample;
return Math.abs(slope) > 0.01 ? slope : -3.2;
}
function screenAngle(clientX: number, clientY: number, center: { x: number; y: number }) {
return Math.atan2(clientY - center.y, clientX - center.x);
}
function unwrapRadians(value: number) {
if (value > Math.PI) {
return value - Math.PI * 2;
}
if (value < -Math.PI) {
return value + Math.PI * 2;
}
return value;
}
function rotationAxisForHandle(handleKey: string): RotationAxis {
if (handleKey.endsWith("-x") || handleKey === "rotate-left") {
2026-06-06 23:10:29 +03:00
return "x";
}
if (handleKey.endsWith("-z") || handleKey === "rotate-right") {
2026-06-06 23:10:29 +03:00
return "z";
}
return "y";
}
function rotationValueForAxis(shape: WorkplaneShape, axis: RotationAxis) {
if (axis === "x") {
return shape.rotationX ?? 0;
}
if (axis === "z") {
return shape.rotationZ ?? 0;
}
return shape.rotation;
}
function rotationPatchForAxis(axis: RotationAxis, value: number): Partial<WorkplaneShape> {
const normalized = cleanRotationDegrees(value);
if (axis === "x") {
return { rotationX: normalized };
}
if (axis === "z") {
return { rotationZ: normalized };
}
return { rotation: normalized };
}
function rotationAxisVector(axis: RotationAxis) {
if (axis === "x") {
return new THREE.Vector3(1, 0, 0);
}
if (axis === "z") {
return new THREE.Vector3(0, 0, 1);
}
return new THREE.Vector3(0, 1, 0);
}
function quaternionForShape(shape: WorkplaneShape) {
return new THREE.Quaternion().setFromEuler(
new THREE.Euler(
THREE.MathUtils.degToRad(shape.rotationX ?? 0),
THREE.MathUtils.degToRad(shape.rotation),
THREE.MathUtils.degToRad(shape.rotationZ ?? 0),
"XYZ",
),
);
}
function rotationPatchFromQuaternion(quaternion: THREE.Quaternion): Partial<WorkplaneShape> {
const euler = new THREE.Euler().setFromQuaternion(quaternion, "XYZ");
return {
rotationX: cleanRotationDegrees(THREE.MathUtils.radToDeg(euler.x)),
rotation: cleanRotationDegrees(THREE.MathUtils.radToDeg(euler.y)),
rotationZ: cleanRotationDegrees(THREE.MathUtils.radToDeg(euler.z)),
};
}
function shouldPreserveDrawingBufferForLocalAutomation() {
return typeof window !== "undefined";
}
function rotationScreenSign(axisVector: THREE.Vector3, camera: THREE.Camera) {
const cameraForward = camera.getWorldDirection(new THREE.Vector3());
return axisVector.dot(cameraForward) >= 0 ? 1 : -1;
}
function projectToScreen(point: THREE.Vector3, state: ThreeState) {
const rect = state.renderer.domElement.getBoundingClientRect();
state.camera.updateMatrixWorld();
const projected = point.clone().project(state.camera);
return {
x: ((projected.x + 1) / 2) * rect.width,
y: ((1 - projected.y) / 2) * rect.height,
};
}
function shapeCenter(shape: WorkplaneShape) {
return new THREE.Vector3(shape.x, (shape.elevation ?? 0) + shape.height / 2, shape.z);
}
function shapeLocalExtents(shape: WorkplaneShape) {
return {
x: shapeWidth(shape) / 2,
y: shape.height / 2,
z: shapeDepth(shape) / 2,
};
}
function selectionFrameForShapes(shapes: WorkplaneShape[], selectedIds: string[]): SelectionFrame | null {
const selected = selectedIds.map((id) => shapes.find((shape) => shape.id === id)).filter((shape): shape is WorkplaneShape => Boolean(shape && !shape.hidden));
if (selected.length === 0) {
return null;
}
const singleShape = selected.length === 1 ? selected[0] : null;
const quaternion = singleShape ? quaternionForShape(singleShape) : new THREE.Quaternion();
const inverse = quaternion.clone().invert();
const localMin = new THREE.Vector3(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
const localMax = new THREE.Vector3(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY);
const origin = singleShape ? shapeCenter(singleShape) : new THREE.Vector3();
if (!singleShape) {
selected.forEach((shape) => origin.add(shapeCenter(shape)));
origin.multiplyScalar(1 / selected.length);
}
selected.forEach((shape) => {
const center = shapeCenter(shape);
const extents = shapeLocalExtents(shape);
const shapeQuaternion = quaternionForShape(shape);
[-1, 1].forEach((xSign) => {
[-1, 1].forEach((ySign) => {
[-1, 1].forEach((zSign) => {
const point = new THREE.Vector3(xSign * extents.x, ySign * extents.y, zSign * extents.z).applyQuaternion(shapeQuaternion).add(center);
const local = point.sub(origin).applyQuaternion(inverse);
localMin.min(local);
localMax.max(local);
});
});
});
});
const localCenter = localMin.clone().add(localMax).multiplyScalar(0.5);
const center = origin.clone().add(localCenter.clone().applyQuaternion(quaternion));
const width = Math.max(MIN_SHAPE_SIZE, localMax.x - localMin.x);
const height = Math.max(MIN_SHAPE_SIZE, localMax.y - localMin.y);
const depth = Math.max(MIN_SHAPE_SIZE, localMax.z - localMin.z);
const xAxis = new THREE.Vector3(1, 0, 0).applyQuaternion(quaternion).normalize();
const yAxis = new THREE.Vector3(0, 1, 0).applyQuaternion(quaternion).normalize();
const zAxis = new THREE.Vector3(0, 0, 1).applyQuaternion(quaternion).normalize();
return {
ids: selected.map((shape) => shape.id),
center,
quaternion,
xAxis,
yAxis,
zAxis,
width,
height,
depth,
min: new THREE.Vector3(-width / 2, -height / 2, -depth / 2),
max: new THREE.Vector3(width / 2, height / 2, depth / 2),
singleShape,
};
}
function framePoint(frame: SelectionFrame, x: number, y: number, z: number) {
return frame.center
.clone()
.add(frame.xAxis.clone().multiplyScalar(x))
.add(frame.yAxis.clone().multiplyScalar(y))
.add(frame.zAxis.clone().multiplyScalar(z));
}
function frameLocalPoint(frame: SelectionFrame, point: THREE.Vector3) {
const offset = point.clone().sub(frame.center);
return new THREE.Vector3(offset.dot(frame.xAxis), offset.dot(frame.yAxis), offset.dot(frame.zAxis));
}
2026-06-12 14:05:36 +03:00
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));
}
2026-06-06 23:10:29 +03:00
function selectionFrameCorners(frame: SelectionFrame) {
const corners: THREE.Vector3[] = [];
[-1, 1].forEach((xSign) => {
[-1, 1].forEach((ySign) => {
[-1, 1].forEach((zSign) => {
corners.push(framePoint(frame, (xSign * frame.width) / 2, (ySign * frame.height) / 2, (zSign * frame.depth) / 2));
});
});
});
return corners;
}
function selectionWorldYBounds(frame: SelectionFrame) {
const corners = selectionFrameCorners(frame);
const min = cleanNearZero(Math.min(...corners.map((corner) => corner.y)));
const max = cleanNearZero(Math.max(...corners.map((corner) => corner.y)));
return { min, max, height: Math.max(MIN_SHAPE_SIZE, max - min) };
}
2026-06-12 14:05:36 +03:00
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));
}
2026-06-12 20:33:19 +03:00
function resizedShapePatchFromFrame(shape: WorkplaneShape, center: THREE.Vector3, width: number, depth: number): Partial<WorkplaneShape> {
2026-06-13 15:56:06 +03:00
const patch: Partial<WorkplaneShape> = {
2026-06-12 20:33:19 +03:00
x: cleanNearZero(center.x, 0.0005),
z: cleanNearZero(center.z, 0.0005),
elevation: cleanNearZero(center.y - shape.height / 2, 0.0005),
width,
depth,
size: resizedShapeSize(width, depth),
};
2026-06-13 15:56:06 +03:00
if (shape.kind === "cone") {
patch.baseRadius = width / 2;
}
return patch;
2026-06-12 20:33:19 +03:00
}
2026-06-06 23:10:29 +03:00
function shapeScreenBounds(state: ThreeState, shape: WorkplaneShape) {
const frame = selectionFrameForShapes([shape], [shape.id]);
if (!frame) {
return null;
}
const points = selectionFrameCorners(frame).map((corner) => projectToScreen(corner, state));
return {
minX: Math.min(...points.map((point) => point.x)),
maxX: Math.max(...points.map((point) => point.x)),
minY: Math.min(...points.map((point) => point.y)),
maxY: Math.max(...points.map((point) => point.y)),
};
}
function boundsIntersectRect(bounds: NonNullable<ReturnType<typeof shapeScreenBounds>>, rect: { left: number; top: number; right: number; bottom: number }) {
return bounds.maxX >= rect.left && bounds.minX <= rect.right && bounds.maxY >= rect.top && bounds.minY <= rect.bottom;
}
function rotationAxisVectorForFrame(handleKey: string, frame: SelectionFrame) {
const axis = rotationAxisForHandle(handleKey);
void frame;
return rotationAxisVector(axis);
2026-06-06 23:10:29 +03:00
}
function rayPointOnRotationPlane(state: ThreeState, clientX: number, clientY: number, pivot: THREE.Vector3, axis: THREE.Vector3) {
const rect = state.renderer.domElement.getBoundingClientRect();
state.pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
state.pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
state.raycaster.setFromCamera(state.pointer, state.camera);
const plane = new THREE.Plane().setFromNormalAndCoplanarPoint(axis.clone().normalize(), pivot);
return state.raycaster.ray.intersectPlane(plane, new THREE.Vector3());
}
function signedAngleAroundAxis(start: THREE.Vector3, current: THREE.Vector3, axis: THREE.Vector3) {
const a = start.clone().normalize();
const b = current.clone().normalize();
return Math.atan2(axis.clone().normalize().dot(a.clone().cross(b)), clamp(a.dot(b), -1, 1));
}
const ROTATION_HANDLE_SIDE_HYSTERESIS = 0.22;
const ROTATION_HANDLE_DOMINANCE_HYSTERESIS = 0.18;
2026-06-12 14:05:36 +03:00
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) {
return previous;
}
if (previous === negativeSide && value < ROTATION_HANDLE_SIDE_HYSTERESIS) {
return previous;
}
return value >= 0 ? positiveSide : negativeSide;
}
function rotationSideScore(side: RotationHandleSide, viewX: number, viewZ: number) {
if (side === "right") {
return viewX;
}
if (side === "left") {
return -viewX;
}
if (side === "near") {
return viewZ;
}
return -viewZ;
}
function dominantRotationSide(viewX: number, viewZ: number, previous: RotationHandleSide | undefined) {
const sides: RotationHandleSide[] = ["near", "right", "far", "left"];
const best = sides.reduce(
(current, side) => {
const score = rotationSideScore(side, viewX, viewZ);
return score > current.score ? { side, score } : current;
},
{ side: "near" as RotationHandleSide, score: Number.NEGATIVE_INFINITY },
);
if (previous && rotationSideScore(previous, viewX, viewZ) >= best.score - ROTATION_HANDLE_DOMINANCE_HYSTERESIS) {
return previous;
}
return best.side;
}
function rotationHandleSidesForCamera(state: ThreeState, center: THREE.Vector3) {
const view = state.camera.position.clone().sub(center);
view.y = 0;
const length = view.length();
if (length < 0.0001) {
return state.rotationHandleSides ?? { x: "right", y: "near", z: "near" };
}
const viewX = view.x / length;
const viewZ = view.z / length;
const previous = state.rotationHandleSides ?? undefined;
const next: RotationHandleSides = {
x: signedRotationSide(viewX, previous?.x, "right", "left"),
y: dominantRotationSide(viewX, viewZ, previous?.y),
z: signedRotationSide(viewZ, previous?.z, "near", "far"),
};
state.rotationHandleSides = next;
return next;
}
2026-06-06 23:10:29 +03:00
function projectedWorldYForScreenY(state: ThreeState, shape: WorkplaneShape, targetScreenY: number, startWorldY: number) {
let nextWorldY = startWorldY;
for (let index = 0; index < 8; index += 1) {
const currentScreenY = projectedScreenY(state, shape, nextWorldY);
const screenSlope = projectedScreenYPerWorldUnit(state, shape, nextWorldY);
if (Math.abs(screenSlope) < 0.01) {
break;
}
nextWorldY = clamp(nextWorldY - (currentScreenY - targetScreenY) / screenSlope, MIN_ELEVATION - 80, MAX_ELEVATION + 80);
}
return nextWorldY;
}
2026-06-15 12:56:23 +03:00
function patchWithPreservedWorldYEdge(shape: WorkplaneShape, patch: Partial<WorkplaneShape>, edge: "bottom" | "top") {
2026-06-12 14:05:36 +03:00
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);
2026-06-15 12:56:23 +03:00
const delta = edge === "bottom" ? startBounds.min - draftBounds.min : startBounds.max - draftBounds.max;
2026-06-12 14:05:36 +03:00
return {
...patch,
2026-06-15 12:56:23 +03:00
elevation: cleanNearZero(clamp((draftShape.elevation ?? 0) + delta, MIN_ELEVATION, MAX_ELEVATION), 0.0005),
2026-06-12 14:05:36 +03:00
};
}
2026-06-15 12:56:23 +03:00
function patchWithPreservedWorldBottom(shape: WorkplaneShape, patch: Partial<WorkplaneShape>) {
return patchWithPreservedWorldYEdge(shape, patch, "bottom");
}
function resizeSignsForDimension(signs: ResizeSigns, axis: "width" | "depth") {
return axis === "width" ? { x: signs.x, z: 0 } : { x: 0, z: signs.z };
}
function patchWithResizeAnchor(
shape: WorkplaneShape,
patch: Partial<WorkplaneShape>,
axis: ShapeInspectorUpdateOptions["resizeAxis"] | DimensionMark["axis"],
anchor: ResizeAnchorMemory | null,
) {
if (axis === "height") {
return patchWithPreservedWorldYEdge(shape, patch, anchor?.shapeId === shape.id && anchor.pressedY === "bottom" ? "top" : "bottom");
}
if (axis !== "width" && axis !== "depth") {
return patchWithPreservedWorldBottom(shape, patch);
}
if (!anchor || anchor.shapeId !== shape.id) {
return patchWithPreservedWorldBottom(shape, patch);
}
const signs = resizeSignsForDimension(anchor.signs, axis);
if (!signs.x && !signs.z) {
return patchWithPreservedWorldBottom(shape, patch);
}
const frame = selectionFrameForShapes([shape], [shape.id]);
if (!frame) {
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 center = resizeCenterFromAnchor(frame, resizeAnchorPointForFrame(frame, signs), signs, width, depth);
return patchWithPreservedWorldBottom(shape, {
...patch,
...resizedShapePatchFromFrame(shape, center, width, depth),
});
}
2026-06-12 14:05:36 +03:00
function resizeShapeFromFrameHandle(
transform: TransformDragState,
point: THREE.Vector3,
2026-06-06 23:10:29 +03:00
handleKey: string,
shiftKey: boolean,
altKey: boolean,
step: number,
): Partial<WorkplaneShape> {
2026-06-12 14:05:36 +03:00
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();
2026-06-06 23:10:29 +03:00
2026-06-12 14:05:36 +03:00
const signs = transform.scaleSigns ?? resizeSignsForHandle(handleKey);
2026-06-06 23:10:29 +03:00
const maxSize = 220;
2026-06-12 14:05:36 +03:00
const axisResize = (current: number, delta: number, sign: number) => {
2026-06-06 23:10:29 +03:00
if (!sign) {
2026-06-12 14:05:36 +03:00
return current;
2026-06-06 23:10:29 +03:00
}
2026-06-12 14:05:36 +03:00
const signedDelta = sign * delta;
2026-06-06 23:10:29 +03:00
if (altKey) {
2026-06-12 14:05:36 +03:00
return snapDimension(current + signedDelta * 2, step, MIN_SHAPE_SIZE, maxSize);
2026-06-06 23:10:29 +03:00
}
2026-06-12 14:05:36 +03:00
return snapDimension(current + signedDelta, step, MIN_SHAPE_SIZE, maxSize);
2026-06-06 23:10:29 +03:00
};
2026-06-12 14:05:36 +03:00
let nextWidth = axisResize(width, localDelta.x, signs.x);
let nextDepth = axisResize(depth, localDelta.z, signs.z);
2026-06-06 23:10:29 +03:00
2026-06-12 14:05:36 +03:00
if (shiftKey && signs.x && signs.z) {
2026-06-14 22:05:58 +03:00
const scale = proportionalResizeScale(width, depth, nextWidth, nextDepth);
const limitedScale = clamp(scale, MIN_SHAPE_SIZE / Math.max(MIN_SHAPE_SIZE, Math.min(width, depth)), maxSize / Math.max(width, depth));
nextWidth = snapDimension(width * limitedScale, step, MIN_SHAPE_SIZE, maxSize);
nextDepth = snapDimension(depth * limitedScale, step, MIN_SHAPE_SIZE, maxSize);
2026-06-06 23:10:29 +03:00
}
2026-06-12 14:05:36 +03:00
const nextCenter = altKey
? frame.center.clone()
: resizeCenterFromAnchor(frame, transform.scaleAnchorPoint ?? resizeAnchorPointForFrame(frame, signs), signs, nextWidth, nextDepth);
2026-06-12 20:33:19 +03:00
return resizedShapePatchFromFrame(shape, nextCenter, nextWidth, nextDepth);
2026-06-06 23:10:29 +03:00
}
function resizeSelectionFromHandle(
transform: TransformDragState,
point: THREE.Vector3,
handleKey: string,
shiftKey: boolean,
altKey: boolean,
step: number,
) {
const frame = transform.selectionFrame;
2026-06-12 14:05:36 +03:00
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) => {
2026-06-06 23:10:29 +03:00
if (!sign) {
2026-06-12 14:05:36 +03:00
return { size: current, scale: 1 };
2026-06-06 23:10:29 +03:00
}
2026-06-12 14:05:36 +03:00
const signedDelta = sign * delta;
2026-06-06 23:10:29 +03:00
if (altKey) {
2026-06-12 14:05:36 +03:00
const size = snapDimension(current + signedDelta * 2, step, MIN_SHAPE_SIZE, 260);
return { size, scale: size / Math.max(MIN_SHAPE_SIZE, current) };
2026-06-06 23:10:29 +03:00
}
2026-06-12 14:05:36 +03:00
const rawSize = current + signedDelta;
2026-06-06 23:10:29 +03:00
const size = snapDimension(rawSize, step, MIN_SHAPE_SIZE, 260);
return {
size,
scale: size / Math.max(MIN_SHAPE_SIZE, current),
};
};
2026-06-12 14:05:36 +03:00
let nextX = axisResize(frame.width, localDelta.x, signs.x);
let nextZ = axisResize(frame.depth, localDelta.z, signs.z);
if (shiftKey && signs.x && signs.z) {
2026-06-14 22:05:58 +03:00
const scale = proportionalResizeScale(frame.width, frame.depth, nextX.size, nextZ.size);
const limitedScale = clamp(scale, MIN_SHAPE_SIZE / Math.max(MIN_SHAPE_SIZE, Math.min(frame.width, frame.depth)), 260 / Math.max(frame.width, frame.depth));
const width = snapDimension(frame.width * limitedScale, step, MIN_SHAPE_SIZE, 260);
const depth = snapDimension(frame.depth * limitedScale, step, MIN_SHAPE_SIZE, 260);
2026-06-06 23:10:29 +03:00
nextX = {
2026-06-14 22:05:58 +03:00
size: width,
scale: width / Math.max(MIN_SHAPE_SIZE, frame.width),
2026-06-06 23:10:29 +03:00
};
nextZ = {
2026-06-14 22:05:58 +03:00
size: depth,
scale: depth / Math.max(MIN_SHAPE_SIZE, frame.depth),
2026-06-06 23:10:29 +03:00
};
}
2026-06-12 14:05:36 +03:00
const nextCenter = altKey
? frame.center.clone()
: resizeCenterFromAnchor(frame, transform.scaleAnchorPoint ?? resizeAnchorPointForFrame(frame, signs), signs, nextX.size, nextZ.size);
2026-06-06 23:10:29 +03:00
return transform.items.map((item) => {
const localCenter = frameLocalPoint(frame, item.startCenter);
const nextItemCenter = nextCenter
.clone()
.add(frame.xAxis.clone().multiplyScalar(localCenter.x * nextX.scale))
.add(frame.yAxis.clone().multiplyScalar(localCenter.y))
.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);
2026-06-12 14:05:36 +03:00
const patch = {
x: nextItemCenter.x,
z: nextItemCenter.z,
elevation: cleanNearZero(nextItemCenter.y - item.startShape.height / 2, 0.0005),
width,
depth,
2026-06-12 20:33:19 +03:00
size: resizedShapeSize(width, depth),
2026-06-12 14:05:36 +03:00
} satisfies Partial<WorkplaneShape>;
2026-06-06 23:10:29 +03:00
return {
id: item.id,
2026-06-12 14:05:36 +03:00
patch,
2026-06-06 23:10:29 +03:00
};
});
}
export function WorkplaneViewport({
shapes,
selectedIds,
alignMode,
alignAnchorId,
alignHandles,
alignReferenceShapes,
mirrorMode,
mirrorReferenceShapes,
placementElevation,
workplaneMode,
2026-06-13 10:26:40 +03:00
initialSnap,
initialWorkspace,
workspaceSettingsKey,
2026-06-06 23:10:29 +03:00
onAddShape,
onAlignAnchorChange,
onAlignPreview,
onAlignPreviewClear,
onAlignSelection,
onMirrorPreview,
onMirrorPreviewClear,
onMirrorSelection,
onSelectShape,
onSetPlacementElevation,
onInteractionActiveChange,
2026-06-06 23:10:29 +03:00
onUpdateShape,
2026-06-13 10:26:40 +03:00
onWorkspaceSettingsChange,
2026-06-06 23:10:29 +03:00
onWorkplaneModeChange,
}: WorkplaneViewportProps) {
const [snapOpen, setSnapOpen] = useState(false);
2026-06-13 10:26:40 +03:00
const [snap, setSnap] = useState<GridSize>(() => normalizeSnapGrid(initialSnap, DEFAULT_SNAP_GRID));
2026-06-06 23:10:29 +03:00
const [settingsOpen, setSettingsOpen] = useState(false);
2026-06-13 10:26:40 +03:00
const [workspace, setWorkspace] = useState<WorkspaceSettings>(() => normalizeWorkspaceSettings(initialWorkspace));
2026-06-06 23:10:29 +03:00
const [transformOverlay, setTransformOverlay] = useState<TransformOverlayState | null>(null);
const [alignOverlay, setAlignOverlay] = useState<AlignOverlayState | null>(null);
const [mirrorOverlay, setMirrorOverlay] = useState<MirrorOverlayState | null>(null);
const [marqueeRect, setMarqueeRect] = useState<{ left: number; top: number; width: number; height: number } | null>(null);
const [hoverMeasureKey, setHoverMeasureKey] = useState<string | null>(null);
const [pinnedMeasureKey, setPinnedMeasureKey] = useState<string | null>(null);
const [rotationReadout, setRotationReadout] = useState<RotationReadout>(null);
const [activeRotationWheel, setActiveRotationWheel] = useState(false);
const [activeTransformKind, setActiveTransformKind] = useState<TransformHandleKind | null>(null);
const [rotationWheelAxis, setRotationWheelAxis] = useState<RotationAxis>("y");
2026-06-13 09:13:15 +03:00
const [pinnedRotationWheelView, setPinnedRotationWheelView] = useState<PinnedRotationWheelView | null>(null);
2026-06-06 23:10:29 +03:00
const [editingDimension, setEditingDimension] = useState<EditingDimension>(null);
const [editingRotation, setEditingRotation] = useState<EditingRotation>(null);
const hostRef = useRef<HTMLDivElement | null>(null);
const threeRef = useRef<ThreeState | null>(null);
const shapesRef = useRef(shapes);
const alignReferenceShapesRef = useRef(alignReferenceShapes);
const mirrorReferenceShapesRef = useRef(mirrorReferenceShapes);
const selectedIdsRef = useRef(selectedIds);
const dragRef = useRef<DragState | null>(null);
const marqueeRef = useRef<MarqueeState | null>(null);
const transformRef = useRef<TransformDragState | null>(null);
2026-06-15 12:56:23 +03:00
const lastResizeAnchorRef = useRef<ResizeAnchorMemory | null>(null);
2026-06-06 23:10:29 +03:00
const suppressNextLiftEditRef = useRef(false);
const snapRef = useRef(snap);
const workspaceRef = useRef(workspace);
2026-06-13 10:26:40 +03:00
const workspaceSettingsKeyRef = useRef(workspaceSettingsKey ?? null);
const lastWorkspaceSettingsSyncRef = useRef("");
2026-06-06 23:10:29 +03:00
const viewCubeRef = useRef<HTMLDivElement | null>(null);
const transformOverlayRef = useRef<TransformOverlayState | null>(null);
const alignOverlayRef = useRef<AlignOverlayState | null>(null);
const mirrorOverlayRef = useRef<MirrorOverlayState | null>(null);
const alignModeRef = useRef(alignMode);
const alignAnchorIdRef = useRef(alignAnchorId);
const alignHandlesRef = useRef(alignHandles);
const mirrorModeRef = useRef(mirrorMode);
const selectedIdsKeyRef = useRef(selectedIds.join("|"));
const perfRef = useRef({
fps: 0,
frameMs: 0,
maxFrameMs: 0,
frames: 0,
lastSample: 0,
});
const selectedShape = useMemo(() => (selectedIds.length === 1 ? shapes.find((shape) => shape.id === selectedIds[0]) ?? null : null), [selectedIds, shapes]);
const placementElevationRef = useRef(placementElevation);
const workplaneModeRef = useRef(workplaneMode);
2026-06-15 12:56:23 +03:00
const rememberResizeAnchor = useCallback((shapeId: string, kind: TransformHandleKind, handleKey: string) => {
if (kind === "scale") {
const signs = resizeSignsForHandle(handleKey);
if (signs.x || signs.z) {
lastResizeAnchorRef.current = { shapeId, handleKey, signs, pressedY: null };
}
return;
}
if (kind === "height") {
lastResizeAnchorRef.current = {
shapeId,
handleKey,
signs: { x: 0, z: 0 },
pressedY: handleKey === "bottom-height" ? "bottom" : "top",
};
}
}, []);
2026-06-13 10:26:40 +03:00
useEffect(() => {
const nextKey = workspaceSettingsKey ?? null;
if (workspaceSettingsKeyRef.current !== nextKey) {
workspaceSettingsKeyRef.current = nextKey;
lastWorkspaceSettingsSyncRef.current = "";
}
setSnap(normalizeSnapGrid(initialSnap, DEFAULT_SNAP_GRID));
setWorkspace(normalizeWorkspaceSettings(initialWorkspace));
}, [initialSnap, initialWorkspace, workspaceSettingsKey]);
useEffect(() => {
const normalizedWorkspace = normalizeWorkspaceSettings(workspace);
const normalizedSnap = normalizeSnapGrid(snap, DEFAULT_SNAP_GRID);
const fingerprint = workplaneSettingsFingerprint(normalizedWorkspace, normalizedSnap);
if (lastWorkspaceSettingsSyncRef.current === fingerprint) {
return;
}
lastWorkspaceSettingsSyncRef.current = fingerprint;
onWorkspaceSettingsChange?.({ workspace: normalizedWorkspace, snap: normalizedSnap });
}, [onWorkspaceSettingsChange, snap, workspace]);
2026-06-06 23:10:29 +03:00
useEffect(() => {
const openWorkspaceSettings = () => setSettingsOpen(true);
window.addEventListener("sketchforge:open-workspace-settings", openWorkspaceSettings);
return () => window.removeEventListener("sketchforge:open-workspace-settings", openWorkspaceSettings);
}, []);
useEffect(() => {
shapesRef.current = shapes;
rebuildShapes(threeRef.current, shapes, selectedIdsRef.current);
if (threeRef.current) {
syncTransformOverlay(threeRef.current, shapes, selectedIdsRef.current, transformOverlayRef, setTransformOverlay, Boolean(transformRef.current || dragRef.current));
syncAlignOverlay(threeRef.current, alignReferenceShapesRef.current, selectedIdsRef.current, alignModeRef.current, alignAnchorIdRef.current, alignHandlesRef.current, alignOverlayRef, setAlignOverlay);
syncMirrorOverlay(threeRef.current, mirrorReferenceShapesRef.current, selectedIdsRef.current, mirrorModeRef.current, mirrorOverlayRef, setMirrorOverlay);
threeRef.current.needsRender = true;
}
}, [shapes]);
useEffect(() => {
alignReferenceShapesRef.current = alignReferenceShapes;
if (threeRef.current) {
syncAlignOverlay(threeRef.current, alignReferenceShapes, selectedIdsRef.current, alignModeRef.current, alignAnchorIdRef.current, alignHandlesRef.current, alignOverlayRef, setAlignOverlay);
threeRef.current.needsRender = true;
}
}, [alignReferenceShapes]);
useEffect(() => {
mirrorReferenceShapesRef.current = mirrorReferenceShapes;
if (threeRef.current) {
syncMirrorOverlay(threeRef.current, mirrorReferenceShapes, selectedIdsRef.current, mirrorModeRef.current, mirrorOverlayRef, setMirrorOverlay);
threeRef.current.needsRender = true;
}
}, [mirrorReferenceShapes]);
useEffect(() => {
const nextSelectedIdsKey = selectedIds.join("|");
if (nextSelectedIdsKey !== selectedIdsKeyRef.current) {
selectedIdsKeyRef.current = nextSelectedIdsKey;
2026-06-15 12:56:23 +03:00
lastResizeAnchorRef.current = null;
2026-06-06 23:10:29 +03:00
setHoverMeasureKey(null);
setPinnedMeasureKey(null);
setEditingDimension(null);
setEditingRotation(null);
setRotationReadout(null);
setActiveRotationWheel(false);
setActiveTransformKind(null);
}
selectedIdsRef.current = selectedIds;
rebuildShapes(threeRef.current, shapesRef.current, selectedIds);
if (threeRef.current) {
syncTransformOverlay(threeRef.current, shapesRef.current, selectedIds, transformOverlayRef, setTransformOverlay, Boolean(transformRef.current || dragRef.current));
syncAlignOverlay(threeRef.current, alignReferenceShapesRef.current, selectedIds, alignModeRef.current, alignAnchorIdRef.current, alignHandlesRef.current, alignOverlayRef, setAlignOverlay);
syncMirrorOverlay(threeRef.current, mirrorReferenceShapesRef.current, selectedIds, mirrorModeRef.current, mirrorOverlayRef, setMirrorOverlay);
threeRef.current.needsRender = true;
}
}, [selectedIds]);
useEffect(() => {
alignModeRef.current = alignMode;
alignAnchorIdRef.current = alignAnchorId;
alignHandlesRef.current = alignHandles;
if (threeRef.current) {
syncAlignOverlay(threeRef.current, alignReferenceShapesRef.current, selectedIdsRef.current, alignMode, alignAnchorId, alignHandles, alignOverlayRef, setAlignOverlay);
threeRef.current.needsRender = true;
}
}, [alignAnchorId, alignHandles, alignMode]);
useEffect(() => {
mirrorModeRef.current = mirrorMode;
if (threeRef.current) {
syncMirrorOverlay(threeRef.current, mirrorReferenceShapesRef.current, selectedIdsRef.current, mirrorMode, mirrorOverlayRef, setMirrorOverlay);
threeRef.current.needsRender = true;
}
}, [mirrorMode]);
useEffect(() => {
snapRef.current = snap;
}, [snap]);
useEffect(() => {
placementElevationRef.current = placementElevation;
}, [placementElevation]);
useEffect(() => {
workplaneModeRef.current = workplaneMode;
}, [workplaneMode]);
useEffect(() => {
workspaceRef.current = workspace;
rebuildWorkplane(threeRef.current, workspace);
if (threeRef.current) {
threeRef.current.needsRender = true;
}
}, [workspace]);
2026-06-13 10:26:40 +03:00
useEffect(() => {
setSelectionHelpersVisible(threeRef.current, activeTransformKind !== "rotate");
}, [activeTransformKind]);
2026-06-06 23:10:29 +03:00
useEffect(() => {
const host = hostRef.current;
if (!host) {
return;
}
const state = createThreeScene(host);
threeRef.current = state;
window.sketchforgeCaptureCanvas = () => {
state.camera.updateMatrixWorld();
state.renderer.render(state.scene, state.camera);
return state.renderer.domElement.toDataURL("image/png");
};
perfRef.current.lastSample = performance.now();
resetCamera(state);
rebuildShapes(state, shapesRef.current, selectedIdsRef.current);
const animate = () => {
state.animationId = window.requestAnimationFrame(animate);
const now = performance.now();
const controlsChanged = state.controls.update();
const cameraSettled = state.wasCameraMoving && !controlsChanged;
if (!controlsChanged && !state.needsRender && !cameraSettled) {
return;
}
constrainCamera(state, workspaceRef.current);
// Future edits: keep this before any view cube or transform-overlay projection.
// OrbitControls changes camera position/quaternion, but manual Vector3.project()
// can read the previous matrix unless we force the matrix world current here.
// Removing this brings back the one-frame-late handle/line lag during camera motion.
state.camera.updateMatrixWorld();
if (now - state.lastViewCubeSync > 48 || cameraSettled || state.needsRender) {
syncViewCube(state, viewCubeRef.current);
state.lastViewCubeSync = now;
}
if (controlsChanged || cameraSettled || state.needsRender || now - state.lastOverlaySync > 96) {
const previewShapes = previewShapesForDrag(shapesRef.current, dragRef.current);
syncTransformOverlay(
state,
previewShapes,
selectedIdsRef.current,
transformOverlayRef,
setTransformOverlay,
Boolean(transformRef.current || dragRef.current),
);
syncAlignOverlay(state, alignReferenceShapesRef.current, selectedIdsRef.current, alignModeRef.current, alignAnchorIdRef.current, alignHandlesRef.current, alignOverlayRef, setAlignOverlay);
syncMirrorOverlay(state, mirrorReferenceShapesRef.current, selectedIdsRef.current, mirrorModeRef.current, mirrorOverlayRef, setMirrorOverlay);
state.lastOverlaySync = now;
}
const renderStart = performance.now();
state.renderer.render(state.scene, state.camera);
const frameMs = performance.now() - renderStart;
const perf = perfRef.current;
perf.frameMs = frameMs;
perf.maxFrameMs = Math.max(perf.maxFrameMs, frameMs);
perf.frames += 1;
if (now - perf.lastSample >= 1000) {
perf.fps = (perf.frames * 1000) / Math.max(1, now - perf.lastSample);
perf.frames = 0;
perf.lastSample = now;
perf.maxFrameMs = frameMs;
}
state.wasCameraMoving = controlsChanged;
state.needsRender = false;
};
animate();
window.addEventListener("resize", state.resize);
return () => {
window.cancelAnimationFrame(state.animationId);
window.removeEventListener("resize", state.resize);
state.disposeInteractionListeners();
state.controls.dispose();
disposeChildren(state.workplaneLayer);
disposeChildren(state.shapeLayer);
disposeChildren(state.helperLayer);
state.renderer.dispose();
host.replaceChildren();
if (window.sketchforgeCaptureCanvas) {
delete window.sketchforgeCaptureCanvas;
}
threeRef.current = null;
};
}, []);
useEffect(() => {
window.sketchforgePerf = {
get: () => {
const state = threeRef.current;
const info = state?.renderer.info.render;
return {
fps: Number(perfRef.current.fps.toFixed(1)),
frameMs: Number(perfRef.current.frameMs.toFixed(2)),
maxFrameMs: Number(perfRef.current.maxFrameMs.toFixed(2)),
drawCalls: info?.calls ?? 0,
triangles: info?.triangles ?? 0,
points: info?.points ?? 0,
lines: info?.lines ?? 0,
shapeCount: shapesRef.current.filter((shape) => !shape.hidden).length,
};
},
};
return () => {
delete window.sketchforgePerf;
};
}, []);
2026-06-12 14:05:36 +03:00
const toRawPlanePoint = useCallback((clientX: number, clientY: number, plane: THREE.Plane) => {
2026-06-06 23:10:29 +03:00
const state = threeRef.current;
if (!state) {
return null;
}
const rect = state.renderer.domElement.getBoundingClientRect();
state.pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
state.pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
state.raycaster.setFromCamera(state.pointer, state.camera);
const hit = new THREE.Vector3();
if (!state.raycaster.ray.intersectPlane(plane, hit)) {
return null;
}
2026-06-12 14:05:36 +03:00
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;
}
2026-06-06 23:10:29 +03:00
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),
};
2026-06-12 14:05:36 +03:00
}, [toRawPlanePoint]);
2026-06-06 23:10:29 +03:00
const toPlanePoint = useCallback((clientX: number, clientY: number) => toPlanePointAtY(clientX, clientY, 0), [toPlanePointAtY]);
const setMarqueeFromState = useCallback((marquee: MarqueeState | null) => {
if (!marquee) {
setMarqueeRect(null);
return;
}
const left = Math.min(marquee.startX, marquee.currentX);
const top = Math.min(marquee.startY, marquee.currentY);
setMarqueeRect({
left,
top,
width: Math.abs(marquee.currentX - marquee.startX),
height: Math.abs(marquee.currentY - marquee.startY),
});
}, []);
const shapesInMarquee = useCallback((rect: { left: number; top: number; right: number; bottom: number }) => {
const state = threeRef.current;
if (!state) {
return [];
}
return shapesRef.current
.filter((shape) => !shape.hidden)
.filter((shape) => {
const bounds = shapeScreenBounds(state, shape);
return bounds ? boundsIntersectRect(bounds, rect) : false;
})
.map((shape) => shape.id);
}, []);
const beginTransform = useCallback(
(kind: TransformHandleKind, handleKey: string, event: ReactPointerEvent<Element>) => {
const ids = selectedIdsRef.current;
const frame = selectionFrameForShapes(shapesRef.current, ids);
const shape = frame?.singleShape ?? shapesRef.current.find((entry) => entry.id === ids[0]);
if (!frame || !shape || ids.length === 0 || ids.some((id) => shapesRef.current.find((entry) => entry.id === id)?.locked)) {
return;
}
const rotationAxis = rotationAxisForHandle(handleKey);
2026-06-12 14:05:36 +03:00
const resizeHandleKey = handleKey;
2026-06-06 23:10:29 +03:00
const state = threeRef.current;
const yBounds = selectionWorldYBounds(frame);
const handlesLowerSide = handleKey === "bottom-height" || handleKey === "lower-shape";
const yStart = handlesLowerSide ? yBounds.min : yBounds.max;
const liftOffset = kind === "lift" ? Math.max(2, yBounds.height * 0.08) * (handlesLowerSide ? -1 : 1) : 0;
const startWorldY = yStart + liftOffset;
const overlay = transformOverlayRef.current;
const wheel = kind === "rotate" ? (overlay?.rotationWheels[rotationAxis] ?? overlay?.rotationWheel ?? undefined) : undefined;
2026-06-13 09:13:15 +03:00
const rotationPlane = kind === "rotate" ? overlay?.rotationPlanes[rotationAxis] : undefined;
const rotationPlaneCenterData = kind === "rotate" ? overlay?.rotationPlaneCenters[rotationAxis] : undefined;
const rotationPlaneCenter = rotationPlaneCenterData
? new THREE.Vector3(rotationPlaneCenterData.x, rotationPlaneCenterData.y, rotationPlaneCenterData.z)
: frame.center.clone();
2026-06-06 23:10:29 +03:00
const rect = state?.renderer.domElement.getBoundingClientRect();
const localClientX = rect ? event.clientX - rect.left : event.clientX;
const localClientY = rect ? event.clientY - rect.top : event.clientY;
const axisVector = rotationAxisVectorForFrame(handleKey, frame);
const pivot = frame.center.clone();
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;
2026-06-12 14:05:36 +03:00
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;
}
2026-06-15 12:56:23 +03:00
rememberResizeAnchor(shape.id, kind, resizeHandleKey);
2026-06-06 23:10:29 +03:00
event.preventDefault();
event.stopPropagation();
event.currentTarget.setPointerCapture(event.pointerId);
setEditingRotation(null);
setPinnedMeasureKey(measureKeyForHandle(kind, handleKey, transformOverlayRef.current));
if (kind === "height") {
setHoverMeasureKey(null);
}
2026-06-06 23:10:29 +03:00
setActiveRotationWheel(kind === "rotate");
setActiveTransformKind(kind);
2026-06-13 10:26:40 +03:00
setSelectionHelpersVisible(state ?? null, kind !== "rotate");
2026-06-06 23:10:29 +03:00
if (kind === "rotate") {
setRotationWheelAxis(rotationAxis);
2026-06-13 09:13:15 +03:00
setPinnedRotationWheelView(wheel && rotationPlane ? { axis: rotationAxis, wheel: { ...wheel }, plane: { ...rotationPlane } } : null);
} else {
setPinnedRotationWheelView(null);
2026-06-06 23:10:29 +03:00
}
transformRef.current = {
id: shape.id,
ids: frame.ids,
kind,
2026-06-12 14:05:36 +03:00
handleKey: resizeHandleKey,
2026-06-06 23:10:29 +03:00
rotationAxis,
pointerId: event.pointerId,
startShape: { ...shape },
items: frame.ids
.map((id) => shapesRef.current.find((entry) => entry.id === id))
.filter((entry): entry is WorkplaneShape => Boolean(entry))
.map((entry) => ({
id: entry.id,
startShape: { ...entry },
startCenter: shapeCenter(entry),
startQuaternion: quaternionForShape(entry),
})),
selectionFrame: frame,
startScreenAngle: rotationCenter ? screenAngle(localClientX, localClientY, rotationCenter) : 0,
startClientX: event.clientX,
startClientY: event.clientY,
startScreenY: state ? projectedScreenYAt(state, frame.center.x, frame.center.z, startWorldY) : event.clientY,
startWorldY,
handleWorldOffset: liftOffset,
screenYPerWorldUnit: state ? projectedScreenYPerWorldUnitAt(state, frame.center.x, frame.center.z, startWorldY) : -3.2,
scalePlaneY: kind === "scale" ? yBounds.min : 0,
2026-06-12 14:05:36 +03:00
scalePlane,
scaleSigns,
scaleAnchorPoint,
scaleStartPoint,
2026-06-06 23:10:29 +03:00
rotationAxisVector: kind === "rotate" ? axisVector : undefined,
rotationPivot: kind === "rotate" ? pivot : undefined,
rotationPlaneCenter: kind === "rotate" ? rotationPlaneCenter : undefined,
2026-06-06 23:10:29 +03:00
rotationStartVector: kind === "rotate" ? rotationStartVector : undefined,
rotationScreenCenter: rotationCenter,
rotationScreenSign: kind === "rotate" && state ? rotationScreenSign(axisVector, state.camera) : 1,
rotationStartQuaternion: kind === "rotate" ? quaternionForShape(shape) : undefined,
wheelCenter: wheel,
};
if (kind === "rotate" && state) {
const renderRect = state.renderer.domElement.getBoundingClientRect();
setRotationReadout({
x: event.clientX - renderRect.left + 18,
y: event.clientY - renderRect.top - 18,
text: `${Math.round(rotationValueForAxis(shape, rotationAxis))}°`,
angle: 0,
});
} else if (kind === "lift" && state) {
const renderRect = state.renderer.domElement.getBoundingClientRect();
setRotationReadout({
x: event.clientX - renderRect.left + 22,
y: event.clientY - renderRect.top - 34,
text: formatMeasure(yBounds.min),
});
} else {
setRotationReadout(null);
}
if (state) {
state.controls.enabled = false;
}
onInteractionActiveChange?.(true);
2026-06-06 23:10:29 +03:00
},
2026-06-15 12:56:23 +03:00
[onInteractionActiveChange, rememberResizeAnchor, toRawPlanePoint],
2026-06-06 23:10:29 +03:00
);
const updateTransform = useCallback(
(clientX: number, clientY: number, shiftKey = false, altKey = false) => {
const transform = transformRef.current;
if (!transform) {
return false;
}
if (Math.hypot(clientX - transform.startClientX, clientY - transform.startClientY) > 3) {
transform.hasMoved = true;
}
const shape = transform.startShape;
const step = snapStep(snapRef.current);
if (transform.kind === "height") {
const state = threeRef.current;
const yBounds = selectionWorldYBounds(transform.selectionFrame);
const draggedWorldY = state
? projectedWorldYForScreenY(state, shape, transform.startScreenY + clientY - transform.startClientY, transform.startWorldY)
: transform.startWorldY + (clientY - transform.startClientY) / transform.screenYPerWorldUnit;
const resizingFromBottom = transform.handleKey === "bottom-height";
const rawWorldHeight = resizingFromBottom ? yBounds.max - draggedWorldY : draggedWorldY - yBounds.min;
const nextWorldHeight = clamp(yBounds.height + snapValue(rawWorldHeight - yBounds.height, step), MIN_SHAPE_SIZE, 180);
const scaleY = nextWorldHeight / Math.max(MIN_SHAPE_SIZE, yBounds.height);
transform.items.forEach((item) => {
const localCenter = frameLocalPoint(transform.selectionFrame, item.startCenter);
const nextCenterY = resizingFromBottom
? transform.selectionFrame.center.y + transform.selectionFrame.height / 2 - (transform.selectionFrame.height / 2 - localCenter.y) * scaleY
: transform.selectionFrame.center.y - transform.selectionFrame.height / 2 + (localCenter.y + transform.selectionFrame.height / 2) * scaleY;
const height = clamp(item.startShape.height * scaleY, MIN_SHAPE_SIZE, 180);
let elevation = nextCenterY - height / 2;
if (transform.items.length === 1) {
const draftShape = { ...item.startShape, height, elevation };
const draftFrame = selectionFrameForShapes([draftShape], [item.id]);
if (draftFrame) {
const draftBounds = selectionWorldYBounds(draftFrame);
elevation += resizingFromBottom ? yBounds.max - draftBounds.max : yBounds.min - draftBounds.min;
}
}
onUpdateShape(item.id, {
height,
elevation: cleanNearZero(clamp(elevation, MIN_ELEVATION, MAX_ELEVATION), 0.0005),
});
});
return true;
}
if (transform.kind === "lift") {
const state = threeRef.current;
const yBounds = selectionWorldYBounds(transform.selectionFrame);
const handleWorldY = state
? projectedWorldYForScreenY(state, shape, transform.startScreenY + clientY - transform.startClientY, transform.startWorldY)
: transform.startWorldY + (clientY - transform.startClientY) / transform.screenYPerWorldUnit;
const handlesLowerSide = transform.handleKey === "lower-shape";
const rawBottom = handlesLowerSide ? handleWorldY - transform.handleWorldOffset : handleWorldY - yBounds.height - transform.handleWorldOffset;
const nextBottom = cleanNearZero(
clamp(yBounds.min + snapValue(rawBottom - yBounds.min, step), MIN_ELEVATION, MAX_ELEVATION),
0.0005,
);
const delta = nextBottom - yBounds.min;
transform.items.forEach((item) =>
onUpdateShape(item.id, {
elevation: cleanNearZero(
clamp((item.startShape.elevation ?? 0) + delta, MIN_ELEVATION, MAX_ELEVATION),
0.0005,
),
}),
);
if (state) {
const readoutPoint = projectToScreen(new THREE.Vector3(transform.selectionFrame.center.x, handleWorldY, transform.selectionFrame.center.z), state);
setRotationReadout({
x: readoutPoint.x + 28,
y: readoutPoint.y - 30,
text: formatMeasure(nextBottom),
});
}
return true;
}
if (transform.kind === "scale") {
2026-06-12 14:05:36 +03:00
const worldPoint = transform.scalePlane ? toRawPlanePoint(clientX, clientY, transform.scalePlane) : null;
if (!worldPoint) {
2026-06-06 23:10:29 +03:00
return true;
}
if (transform.items.length === 1) {
2026-06-12 14:05:36 +03:00
const next = resizeShapeFromFrameHandle(transform, worldPoint, transform.handleKey, shiftKey, altKey, step);
2026-06-06 23:10:29 +03:00
onUpdateShape(transform.id, next);
} else {
resizeSelectionFromHandle(transform, worldPoint, transform.handleKey, shiftKey, altKey, step).forEach(({ id, patch }) => onUpdateShape(id, patch));
}
return true;
}
2026-06-12 14:05:36 +03:00
const point = toPlanePoint(clientX, clientY);
if (!point && transform.kind !== "rotate") {
return true;
}
2026-06-06 23:10:29 +03:00
const state = threeRef.current;
const rotationCenter = transform.rotationScreenCenter ?? transform.wheelCenter;
if (!state || !rotationCenter) {
return true;
}
const rect = state.renderer.domElement.getBoundingClientRect();
const localClientX = clientX - rect.left;
const localClientY = clientY - rect.top;
const axisVector = (transform.rotationAxisVector ?? rotationAxisVectorForFrame(transform.handleKey, transform.selectionFrame)).clone().normalize();
const pivot = transform.rotationPivot ?? transform.selectionFrame.center;
const planeCenter = transform.rotationPlaneCenter ?? pivot;
const currentPoint = rayPointOnRotationPlane(state, clientX, clientY, planeCenter, axisVector);
2026-06-06 23:10:29 +03:00
const rawDelta =
currentPoint && transform.rotationStartVector && transform.rotationStartVector.lengthSq() > 0.000001
? THREE.MathUtils.radToDeg(signedAngleAroundAxis(transform.rotationStartVector, currentPoint.sub(planeCenter), axisVector))
2026-06-06 23:10:29 +03:00
: THREE.MathUtils.radToDeg(unwrapRadians(screenAngle(localClientX, localClientY, rotationCenter) - transform.startScreenAngle)) * (transform.rotationScreenSign ?? 1);
const distance = transform.wheelCenter ? Math.hypot(localClientX - transform.wheelCenter.x, localClientY - transform.wheelCenter.y) : Number.POSITIVE_INFINITY;
let delta: number;
if (shiftKey) {
delta = Math.round(rawDelta / 45) * 45;
} else if (transform.wheelCenter && distance <= transform.wheelCenter.radius) {
delta = Math.round(rawDelta / 22.5) * 22.5;
} else {
delta = Math.round(rawDelta);
}
const deltaQuaternion = new THREE.Quaternion().setFromAxisAngle(axisVector, THREE.MathUtils.degToRad(delta));
const rotationDelta = deltaQuaternion.clone();
if (state) {
setRotationReadout({
x: transform.wheelCenter ? transform.wheelCenter.x : localClientX + 18,
y: transform.wheelCenter ? transform.wheelCenter.y - 92 : localClientY - 18,
text: `${Number(delta.toFixed(1))}°`,
angle: delta,
});
}
transform.items.forEach((item) => {
const nextQuaternion = rotationDelta.clone().multiply(item.startQuaternion);
const patch: Partial<WorkplaneShape> = rotationPatchFromQuaternion(nextQuaternion);
if (transform.items.length > 1) {
const nextCenter = pivot.clone().add(item.startCenter.clone().sub(pivot).applyQuaternion(rotationDelta));
patch.x = snapPositionValue(nextCenter.x, step, -workspaceRef.current.width / 2 + 6, workspaceRef.current.width / 2 - 6);
patch.z = snapPositionValue(nextCenter.z, step, -workspaceRef.current.depth / 2 + 6, workspaceRef.current.depth / 2 - 6);
patch.elevation = snapPositionValue(nextCenter.y - item.startShape.height / 2, step, MIN_ELEVATION, MAX_ELEVATION);
}
onUpdateShape(item.id, patch);
});
return true;
},
2026-06-12 14:05:36 +03:00
[onUpdateShape, toPlanePoint, toRawPlanePoint],
2026-06-06 23:10:29 +03:00
);
const suppressLiftEditAfterDrag = useCallback(() => {
suppressNextLiftEditRef.current = true;
window.setTimeout(() => {
suppressNextLiftEditRef.current = false;
}, 250);
}, []);
const finishTransform = useCallback((event: ReactPointerEvent<Element>) => {
const transform = transformRef.current;
if (!transform) {
return;
}
if (event.currentTarget.hasPointerCapture(transform.pointerId)) {
event.currentTarget.releasePointerCapture(transform.pointerId);
}
2026-06-12 14:05:36 +03:00
const bakeRotatedShapes = transform.kind === "rotate" && transform.hasMoved ? transform.ids : [];
if (transform.kind === "lift") {
2026-06-06 23:10:29 +03:00
setPinnedMeasureKey(getElevationMeasureKey(transformOverlayRef.current));
}
if (transform.kind === "lift" && transform.hasMoved) {
suppressLiftEditAfterDrag();
}
transformRef.current = null;
setActiveRotationWheel(false);
setActiveTransformKind(null);
2026-06-13 09:13:15 +03:00
setPinnedRotationWheelView(null);
2026-06-06 23:10:29 +03:00
setRotationReadout(null);
if (threeRef.current) {
2026-06-13 10:26:40 +03:00
setSelectionHelpersVisible(threeRef.current, true);
2026-06-06 23:10:29 +03:00
threeRef.current.controls.enabled = true;
}
onInteractionActiveChange?.(false);
2026-06-12 14:05:36 +03:00
bakeRotatedShapes.forEach((id) => onUpdateShape(id, { bakeTransform: true }));
}, [onInteractionActiveChange, onUpdateShape, suppressLiftEditAfterDrag]);
2026-06-06 23:10:29 +03:00
const beginDimensionEdit = useCallback((mark: DimensionMark) => {
2026-06-15 12:56:23 +03:00
const id = selectedIdsRef.current[0];
if (id && (mark.axis === "width" || mark.axis === "depth" || mark.axis === "height")) {
rememberResizeAnchor(id, mark.axis === "height" ? "height" : "scale", mark.handleKey);
}
2026-06-06 23:10:29 +03:00
setPinnedMeasureKey(mark.handleKey);
setEditingDimension({ key: mark.key, axis: mark.axis, x: mark.labelX, y: mark.labelY, value: mark.label });
2026-06-15 12:56:23 +03:00
}, [rememberResizeAnchor]);
2026-06-06 23:10:29 +03:00
const beginLiftEdit = useCallback((handleKey: string, x: number, y: number) => {
if (suppressNextLiftEditRef.current) {
suppressNextLiftEditRef.current = false;
return;
}
const frame = selectionFrameForShapes(shapesRef.current, selectedIdsRef.current);
if (!frame) {
return;
}
const yBounds = selectionWorldYBounds(frame);
const elevationMark = Object.values(transformOverlayRef.current?.dimensions ?? {})
.flat()
.find((entry) => entry.axis === "elevation");
const editX = elevationMark?.labelX ?? x;
const editY = elevationMark?.labelY ?? y;
setPinnedMeasureKey(elevationMark?.handleKey ?? handleKey);
setActiveRotationWheel(false);
setRotationReadout(null);
setEditingDimension({
key: "elevation",
axis: "elevation",
x: clamp(editX, 44, Math.max(44, (transformOverlayRef.current?.width ?? 900) - 44)),
y: clamp(editY, 34, Math.max(34, (transformOverlayRef.current?.height ?? 600) - 34)),
value: formatMeasure(yBounds.min),
});
}, []);
const commitDimensionEdit = useCallback(() => {
const edit = editingDimension;
const id = selectedIdsRef.current[0];
const shape = shapesRef.current.find((entry) => entry.id === id);
if (!edit || !shape) {
setEditingDimension(null);
return;
}
const value = Number.parseFloat(edit.value);
if (edit.axis === "elevation") {
if (Number.isFinite(value)) {
const frame = selectionFrameForShapes(shapesRef.current, selectedIdsRef.current);
const currentMin = frame ? selectionWorldYBounds(frame).min : shape.elevation ?? 0;
const targetMin = cleanNearZero(clamp(value, MIN_ELEVATION, MAX_ELEVATION), 0.0005);
const delta = targetMin - currentMin;
selectedIdsRef.current.forEach((selectedId) => {
const selectedShape = shapesRef.current.find((entry) => entry.id === selectedId);
if (selectedShape) {
onUpdateShape(selectedId, { elevation: cleanNearZero(clamp((selectedShape.elevation ?? 0) + delta, MIN_ELEVATION, MAX_ELEVATION), 0.0005) });
}
});
}
setEditingDimension(null);
return;
}
if (Number.isFinite(value) && value > 0) {
const nextValue = Math.max(MIN_SHAPE_SIZE, value);
if (edit.axis === "width") {
2026-06-13 15:56:06 +03:00
const patch: Partial<WorkplaneShape> = { width: nextValue, size: resizedShapeSize(nextValue, shapeDepth(shape)) };
if (shape.kind === "cone") {
patch.baseRadius = nextValue / 2;
}
2026-06-15 12:56:23 +03:00
onUpdateShape(id, patchWithResizeAnchor(shape, patch, edit.axis, lastResizeAnchorRef.current));
2026-06-06 23:10:29 +03:00
} else if (edit.axis === "depth") {
2026-06-15 12:56:23 +03:00
onUpdateShape(id, patchWithResizeAnchor(shape, { depth: nextValue, size: resizedShapeSize(shapeWidth(shape), nextValue) }, edit.axis, lastResizeAnchorRef.current));
2026-06-06 23:10:29 +03:00
} else {
2026-06-15 12:56:23 +03:00
onUpdateShape(id, patchWithResizeAnchor(shape, { height: nextValue }, edit.axis, lastResizeAnchorRef.current));
2026-06-06 23:10:29 +03:00
}
}
setEditingDimension(null);
}, [editingDimension, onUpdateShape]);
const cancelDimensionEdit = useCallback(() => {
setEditingDimension(null);
}, []);
const beginRotationEdit = useCallback((handleKey: string, x: number, y: number) => {
const axis = rotationAxisForHandle(handleKey);
const shape = selectedIdsRef.current.length === 1 ? shapesRef.current.find((entry) => entry.id === selectedIdsRef.current[0]) : null;
const currentValue = shape ? rotationValueForAxis(shape, axis) : 0;
setPinnedMeasureKey(handleKey);
setActiveRotationWheel(true);
setRotationWheelAxis(axis);
setRotationReadout(null);
setEditingRotation({
axis,
handleKey,
x: clamp(x, 38, Math.max(38, (transformOverlayRef.current?.width ?? 900) - 38)),
y: clamp(y, 38, Math.max(38, (transformOverlayRef.current?.height ?? 600) - 38)),
value: String(Number(currentValue.toFixed(1))),
});
}, []);
const commitRotationEdit = useCallback(() => {
const edit = editingRotation;
if (!edit) {
return;
}
const value = Number.parseFloat(edit.value);
if (Number.isFinite(value)) {
2026-06-12 14:05:36 +03:00
selectedIdsRef.current.forEach((id) => onUpdateShape(id, { ...rotationPatchForAxis(edit.axis, value), bakeTransform: true }));
2026-06-06 23:10:29 +03:00
}
setEditingRotation(null);
setActiveRotationWheel(false);
}, [editingRotation, onUpdateShape]);
const cancelRotationEdit = useCallback(() => {
setEditingRotation(null);
setActiveRotationWheel(false);
}, []);
const pickShape = useCallback((clientX: number, clientY: number) => {
const state = threeRef.current;
if (!state) {
return null;
}
const rect = state.renderer.domElement.getBoundingClientRect();
state.pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
state.pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
state.raycaster.setFromCamera(state.pointer, state.camera);
const intersections = state.raycaster.intersectObjects(state.shapeLayer.children, true);
const hit = intersections.find((entry) => typeof entry.object.userData.shapeId === "string");
if (hit) {
return hit.object.userData.shapeId as string;
}
let nearestId: string | null = null;
let nearestDistance = Number.POSITIVE_INFINITY;
shapesRef.current.forEach((shape) => {
const center = new THREE.Vector3(shape.x, (shape.elevation ?? 0) + shape.height / 2, shape.z).project(state.camera);
const screenX = rect.left + ((center.x + 1) / 2) * rect.width;
const screenY = rect.top + ((1 - center.y) / 2) * rect.height;
const distance = Math.hypot(clientX - screenX, clientY - screenY);
const hitRadius = clamp(Math.max(shapeWidth(shape), shapeDepth(shape)) * 2.6, 48, 112);
if (distance <= hitRadius && distance < nearestDistance) {
nearestId = shape.id;
nearestDistance = distance;
}
});
return nearestId;
}, []);
const pickTransformHandle = useCallback((clientX: number, clientY: number) => {
const state = threeRef.current;
if (!state || selectedIdsRef.current.length !== 1) {
return null;
}
const rect = state.renderer.domElement.getBoundingClientRect();
state.pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
state.pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
state.raycaster.setFromCamera(state.pointer, state.camera);
const intersections = state.raycaster.intersectObjects(state.helperLayer.children, true);
const hit = intersections.find((entry) => typeof entry.object.userData.transformHandle === "string");
if (!hit) {
return null;
}
return {
id: hit.object.userData.shapeId as string,
kind: hit.object.userData.transformHandle as TransformHandleKind,
handleKey: (hit.object.userData.transformHandleKey as string | undefined) ?? (hit.object.userData.transformHandle as string),
planeY: typeof hit.object.userData.transformPlaneY === "number" ? (hit.object.userData.transformPlaneY as number) : 0,
};
}, []);
const handlePointerDown = useCallback(
(event: ReactPointerEvent<HTMLDivElement>) => {
const state = threeRef.current;
if (!state) {
return;
}
if (event.button !== 0 || event.ctrlKey || event.metaKey) {
return;
}
const rect = state.renderer.domElement.getBoundingClientRect();
if (workplaneModeRef.current) {
event.preventDefault();
const id = pickShape(event.clientX, event.clientY);
if (id) {
const frame = selectionFrameForShapes(shapesRef.current, [id]);
const top = frame ? selectionWorldYBounds(frame).max : 0;
onSetPlacementElevation(snapPositionValue(top, snapStep(snapRef.current), MIN_ELEVATION, MAX_ELEVATION), "shape");
onSelectShape(id);
} else {
onSetPlacementElevation(0, "base");
}
onWorkplaneModeChange(false);
return;
}
const handle = pickTransformHandle(event.clientX, event.clientY);
if (handle) {
const shape = shapesRef.current.find((entry) => entry.id === handle.id);
const frame = selectionFrameForShapes(shapesRef.current, selectedIdsRef.current);
2026-06-12 14:05:36 +03:00
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);
2026-06-06 23:10:29 +03:00
if (!shape || !frame || shape.locked || (!point && handle.kind !== "height" && handle.kind !== "lift" && handle.kind !== "rotate")) {
return;
}
const yBounds = selectionWorldYBounds(frame);
const handlesLowerSide = handle.handleKey === "bottom-height" || handle.handleKey === "lower-shape";
const yStart = handlesLowerSide ? yBounds.min : yBounds.max;
const liftOffset = handle.kind === "lift" ? Math.max(2, yBounds.height * 0.08) * (handlesLowerSide ? -1 : 1) : 0;
const startWorldY = yStart + liftOffset;
const overlay = transformOverlayRef.current;
2026-06-06 23:10:29 +03:00
const rotationAxis = rotationAxisForHandle(handle.handleKey);
2026-06-12 14:05:36 +03:00
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;
2026-06-13 09:13:15 +03:00
const rotationPlane = handle.kind === "rotate" ? overlay?.rotationPlanes[rotationAxis] : undefined;
const rotationPlaneCenterData = handle.kind === "rotate" ? overlay?.rotationPlaneCenters[rotationAxis] : undefined;
const rotationPlaneCenter = rotationPlaneCenterData
? new THREE.Vector3(rotationPlaneCenterData.x, rotationPlaneCenterData.y, rotationPlaneCenterData.z)
: frame.center.clone();
2026-06-06 23:10:29 +03:00
const localClientX = event.clientX - rect.left;
const localClientY = event.clientY - rect.top;
const axisVector = rotationAxisVectorForFrame(handle.handleKey, frame);
const pivot = frame.center.clone();
const rotationCenter = handle.kind === "rotate" ? wheel ?? projectToScreen(pivot, state) : undefined;
const rotationStartPoint = handle.kind === "rotate" ? rayPointOnRotationPlane(state, event.clientX, event.clientY, rotationPlaneCenter, axisVector) : null;
const rotationStartVector = rotationStartPoint ? rotationStartPoint.sub(rotationPlaneCenter) : undefined;
2026-06-15 12:56:23 +03:00
rememberResizeAnchor(handle.id, handle.kind, resizeHandleKey);
2026-06-06 23:10:29 +03:00
event.preventDefault();
event.currentTarget.setPointerCapture(event.pointerId);
setEditingRotation(null);
setPinnedMeasureKey(measureKeyForHandle(handle.kind, handle.handleKey, transformOverlayRef.current));
if (handle.kind === "height") {
setHoverMeasureKey(null);
}
2026-06-06 23:10:29 +03:00
setActiveRotationWheel(handle.kind === "rotate");
setActiveTransformKind(handle.kind);
2026-06-13 10:26:40 +03:00
setSelectionHelpersVisible(state, handle.kind !== "rotate");
2026-06-06 23:10:29 +03:00
if (handle.kind === "rotate") {
setRotationWheelAxis(rotationAxis);
2026-06-13 09:13:15 +03:00
setPinnedRotationWheelView(wheel && rotationPlane ? { axis: rotationAxis, wheel: { ...wheel }, plane: { ...rotationPlane } } : null);
} else {
setPinnedRotationWheelView(null);
2026-06-06 23:10:29 +03:00
}
transformRef.current = {
id: handle.id,
ids: frame.ids,
kind: handle.kind,
2026-06-12 14:05:36 +03:00
handleKey: resizeHandleKey,
2026-06-06 23:10:29 +03:00
rotationAxis,
pointerId: event.pointerId,
startShape: { ...shape },
items: frame.ids
.map((id) => shapesRef.current.find((entry) => entry.id === id))
.filter((entry): entry is WorkplaneShape => Boolean(entry))
.map((entry) => ({
id: entry.id,
startShape: { ...entry },
startCenter: shapeCenter(entry),
startQuaternion: quaternionForShape(entry),
})),
selectionFrame: frame,
startScreenAngle: rotationCenter ? screenAngle(localClientX, localClientY, rotationCenter) : 0,
startClientX: event.clientX,
startClientY: event.clientY,
startScreenY: projectedScreenYAt(state, frame.center.x, frame.center.z, startWorldY),
startWorldY,
handleWorldOffset: liftOffset,
screenYPerWorldUnit: projectedScreenYPerWorldUnitAt(state, frame.center.x, frame.center.z, startWorldY),
scalePlaneY: handle.kind === "scale" ? handle.planeY : 0,
2026-06-12 14:05:36 +03:00
scalePlane,
scaleSigns,
scaleAnchorPoint,
scaleStartPoint,
2026-06-06 23:10:29 +03:00
rotationAxisVector: handle.kind === "rotate" ? axisVector : undefined,
rotationPivot: handle.kind === "rotate" ? pivot : undefined,
rotationPlaneCenter: handle.kind === "rotate" ? rotationPlaneCenter : undefined,
2026-06-06 23:10:29 +03:00
rotationStartVector: handle.kind === "rotate" ? rotationStartVector : undefined,
rotationScreenCenter: rotationCenter,
rotationScreenSign: handle.kind === "rotate" ? rotationScreenSign(axisVector, state.camera) : 1,
rotationStartQuaternion: handle.kind === "rotate" ? quaternionForShape(shape) : undefined,
wheelCenter: wheel,
};
if (handle.kind === "rotate") {
setRotationReadout({
x: event.clientX - rect.left + 18,
y: event.clientY - rect.top - 18,
text: `${Math.round(rotationValueForAxis(shape, rotationAxis))}°`,
angle: 0,
});
} else if (handle.kind === "lift") {
setRotationReadout({
x: event.clientX - rect.left + 22,
y: event.clientY - rect.top - 34,
text: formatMeasure(yBounds.min),
});
} else {
setRotationReadout(null);
}
state.controls.enabled = false;
onInteractionActiveChange?.(true);
2026-06-06 23:10:29 +03:00
return;
}
const id = pickShape(event.clientX, event.clientY);
const additive = event.shiftKey;
if (!id) {
const startX = event.clientX - rect.left;
const startY = event.clientY - rect.top;
event.preventDefault();
event.currentTarget.setPointerCapture(event.pointerId);
marqueeRef.current = {
pointerId: event.pointerId,
startX,
startY,
currentX: startX,
currentY: startY,
additive,
hasMoved: false,
};
setMarqueeFromState(marqueeRef.current);
state.controls.enabled = false;
onInteractionActiveChange?.(true);
2026-06-06 23:10:29 +03:00
return;
}
const shape = shapesRef.current.find((entry) => entry.id === id);
const selectedIdsSnapshot = selectedIdsRef.current;
if (alignModeRef.current && selectedIdsSnapshot.includes(id)) {
event.preventDefault();
onAlignAnchorChange(id);
return;
}
const dragPlaneY = shape ? shape.elevation ?? 0 : 0;
const point = toPlanePointAtY(event.clientX, event.clientY, dragPlaneY);
if (!point || !shape) {
return;
}
event.preventDefault();
const alreadySelected = selectedIdsSnapshot.includes(id);
if (additive) {
onSelectShape(id, "toggle");
return;
}
if (!alreadySelected) {
onSelectShape(id);
}
if (shape.locked) {
return;
}
event.currentTarget.setPointerCapture(event.pointerId);
const dragIds = alreadySelected && selectedIdsSnapshot.length > 1 ? selectedIdsSnapshot : [id];
const items = dragIds
.map((dragId) => {
const dragShape = shapesRef.current.find((entry) => entry.id === dragId);
if (!dragShape || dragShape.locked) {
return null;
}
const helper = findSelectionHelper(state, dragId);
return {
id: dragId,
startX: dragShape.x,
startZ: dragShape.z,
nextX: dragShape.x,
nextZ: dragShape.z,
visual: findShapeObject(state, dragId),
helper,
helperBox: helper ? helper.box.clone() : null,
hadPreviewSimplified: false,
};
})
.filter((item): item is DragItem => Boolean(item));
if (items.length === 0) {
return;
}
dragRef.current = {
primaryId: id,
offsetX: shape.x - point.x,
offsetZ: shape.z - point.z,
planeY: dragPlaneY,
pointerId: event.pointerId,
primaryStartX: shape.x,
primaryStartZ: shape.z,
items,
};
state.controls.enabled = false;
onInteractionActiveChange?.(true);
2026-06-06 23:10:29 +03:00
},
[
onAlignAnchorChange,
onInteractionActiveChange,
onSelectShape,
onSetPlacementElevation,
onWorkplaneModeChange,
pickShape,
pickTransformHandle,
setMarqueeFromState,
toPlanePoint,
toPlanePointAtY,
2026-06-12 14:05:36 +03:00
toRawPlanePoint,
],
2026-06-06 23:10:29 +03:00
);
const handlePointerMove = useCallback(
(event: ReactPointerEvent<HTMLDivElement>) => {
const transform = transformRef.current;
if (transform) {
updateTransform(event.clientX, event.clientY, event.shiftKey, event.altKey);
if (threeRef.current) {
threeRef.current.needsRender = true;
}
return;
}
const marquee = marqueeRef.current;
if (marquee) {
const state = threeRef.current;
if (!state) {
return;
}
const rect = state.renderer.domElement.getBoundingClientRect();
marquee.currentX = event.clientX - rect.left;
marquee.currentY = event.clientY - rect.top;
marquee.hasMoved = marquee.hasMoved || Math.hypot(marquee.currentX - marquee.startX, marquee.currentY - marquee.startY) > 5;
setMarqueeFromState(marquee);
return;
}
const drag = dragRef.current;
if (!drag) {
return;
}
const point = toPlanePointAtY(event.clientX, event.clientY, drag.planeY);
if (!point) {
return;
}
const primaryNextX = clamp(point.x + drag.offsetX, -workspaceRef.current.width / 2 + 6, workspaceRef.current.width / 2 - 6);
const primaryNextZ = clamp(point.z + drag.offsetZ, -workspaceRef.current.depth / 2 + 6, workspaceRef.current.depth / 2 - 6);
const deltaX = primaryNextX - drag.primaryStartX;
const deltaZ = primaryNextZ - drag.primaryStartZ;
drag.items.forEach((item) => {
item.nextX = clamp(item.startX + deltaX, -workspaceRef.current.width / 2 + 6, workspaceRef.current.width / 2 - 6);
item.nextZ = clamp(item.startZ + deltaZ, -workspaceRef.current.depth / 2 + 6, workspaceRef.current.depth / 2 - 6);
if (item.visual) {
if (!item.hadPreviewSimplified) {
setComplexEdgeVisibility(item.visual, false);
item.hadPreviewSimplified = true;
}
item.visual.position.x = item.nextX;
item.visual.position.z = item.nextZ;
}
if (item.helper && item.helperBox) {
item.helper.box.copy(item.helperBox);
item.helper.box.translate(new THREE.Vector3(item.nextX - item.startX, 0, item.nextZ - item.startZ));
item.helper.updateMatrixWorld(true);
}
});
if (threeRef.current) {
const previewShapes = previewShapesForDrag(shapesRef.current, drag);
updateSelectedGroundFootprintPreviews(threeRef.current, drag);
syncTransformOverlay(threeRef.current, previewShapes, selectedIdsRef.current, transformOverlayRef, setTransformOverlay, true);
threeRef.current.lastOverlaySync = performance.now();
threeRef.current.needsRender = true;
}
},
[setMarqueeFromState, toPlanePoint, updateTransform],
);
const finishDrag = useCallback(
(event: ReactPointerEvent<HTMLDivElement>) => {
const state = threeRef.current;
const transform = transformRef.current;
if (transform) {
if (event.currentTarget.hasPointerCapture(transform.pointerId)) {
event.currentTarget.releasePointerCapture(transform.pointerId);
}
if (transform.kind === "lift") {
2026-06-06 23:10:29 +03:00
setPinnedMeasureKey(getElevationMeasureKey(transformOverlayRef.current));
} else if (transform.kind === "height") {
setPinnedMeasureKey(null);
setHoverMeasureKey(null);
2026-06-06 23:10:29 +03:00
}
if (transform.kind === "lift" && transform.hasMoved) {
suppressLiftEditAfterDrag();
}
transformRef.current = null;
setActiveRotationWheel(false);
setActiveTransformKind(null);
setRotationReadout(null);
if (state) {
2026-06-13 10:26:40 +03:00
setSelectionHelpersVisible(state, true);
2026-06-06 23:10:29 +03:00
state.controls.enabled = true;
}
onInteractionActiveChange?.(false);
2026-06-06 23:10:29 +03:00
return;
}
const marquee = marqueeRef.current;
if (marquee) {
if (event.currentTarget.hasPointerCapture(marquee.pointerId)) {
event.currentTarget.releasePointerCapture(marquee.pointerId);
}
marqueeRef.current = null;
setMarqueeFromState(null);
if (marquee.hasMoved) {
const rect = {
left: Math.min(marquee.startX, marquee.currentX),
right: Math.max(marquee.startX, marquee.currentX),
top: Math.min(marquee.startY, marquee.currentY),
bottom: Math.max(marquee.startY, marquee.currentY),
};
const selected = shapesInMarquee(rect);
if (marquee.additive) {
const merged = [...selectedIdsRef.current];
selected.forEach((id) => {
if (!merged.includes(id)) {
merged.push(id);
}
});
onSelectShape(merged);
} else {
onSelectShape(selected);
}
} else if (!marquee.additive) {
onSelectShape(null);
}
if (state) {
state.controls.enabled = true;
}
onInteractionActiveChange?.(false);
2026-06-06 23:10:29 +03:00
return;
}
const drag = dragRef.current;
if (!drag) {
return;
}
if (event.currentTarget.hasPointerCapture(drag.pointerId)) {
event.currentTarget.releasePointerCapture(drag.pointerId);
}
drag.items.forEach((item) => {
if (item.visual && item.hadPreviewSimplified) {
setComplexEdgeVisibility(item.visual, true);
}
const shape = shapesRef.current.find((entry) => entry.id === item.id);
if (shape && (shape.x !== item.nextX || shape.z !== item.nextZ)) {
onUpdateShape(item.id, { x: item.nextX, z: item.nextZ });
}
});
dragRef.current = null;
if (state) {
state.controls.enabled = true;
}
onInteractionActiveChange?.(false);
2026-06-06 23:10:29 +03:00
},
2026-06-15 12:56:23 +03:00
[onInteractionActiveChange, onSelectShape, onUpdateShape, rememberResizeAnchor, setMarqueeFromState, shapesInMarquee, suppressLiftEditAfterDrag],
2026-06-06 23:10:29 +03:00
);
const handleDrop = useCallback(
(event: DragEvent<HTMLDivElement>) => {
event.preventDefault();
const raw = event.dataTransfer.getData("application/x-sketchforge-shape");
if (!raw) {
return;
}
2026-06-14 22:05:58 +03:00
const asset = parseDroppedShapeAsset(raw);
if (!asset) {
return;
}
2026-06-06 23:10:29 +03:00
const point = toPlanePoint(event.clientX, event.clientY);
onAddShape(asset, point ? { ...point, elevation: placementElevationRef.current } : { x: 0, z: 0, elevation: placementElevationRef.current });
},
[onAddShape, toPlanePoint],
);
const resetView = useCallback(() => {
const state = threeRef.current;
if (state) {
resetCamera(state);
state.needsRender = true;
}
}, []);
const zoomCamera = useCallback((scale: number) => {
const state = threeRef.current;
if (!state) {
return;
}
const offset = state.camera.position.clone().sub(state.controls.target);
const distance = clamp(offset.length() * scale, 22, 4200);
offset.setLength(distance);
state.camera.position.copy(state.controls.target).add(offset);
state.camera.updateProjectionMatrix();
state.controls.update();
state.needsRender = true;
}, []);
useEffect(() => {
const isTypingTarget = (target: EventTarget | null) => {
if (!(target instanceof HTMLElement)) {
return false;
}
return target.isContentEditable || ["INPUT", "TEXTAREA", "SELECT"].includes(target.tagName);
};
const handleKeyDown = (event: KeyboardEvent) => {
if (isTypingTarget(event.target)) {
return;
}
const key = event.key.toLowerCase();
if (key === "f" || event.key === "Home") {
event.preventDefault();
resetView();
} else if (event.key === "+" || event.key === "=") {
event.preventDefault();
zoomCamera(0.72);
} else if (event.key === "-" || event.key === "_") {
event.preventDefault();
zoomCamera(1.28);
}
};
window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
}, [resetView, zoomCamera]);
return (
<main className="workplane-stage">
<div className="view-cube" aria-hidden="true">
<div className="view-cube-inner" ref={viewCubeRef}>
<div className="cube-face cube-top">TOP</div>
<div className="cube-face cube-bottom">BOTTOM</div>
<div className="cube-face cube-front">FRONT</div>
<div className="cube-face cube-back">BACK</div>
<div className="cube-face cube-right">RIGHT</div>
<div className="cube-face cube-left">LEFT</div>
</div>
</div>
<div className="camera-controls" aria-label="Camera controls">
<button aria-label="Home" onClick={resetView}>
<Home size={28} />
</button>
<button aria-label="Fit view" onClick={resetView}>
<Expand size={24} />
</button>
<button aria-label="Zoom in" onClick={() => zoomCamera(0.7)}>
<Plus size={33} />
</button>
<button aria-label="Zoom out" onClick={() => zoomCamera(1.35)}>
<Minus size={33} />
</button>
<button aria-label="Camera mode" onClick={resetView}>
<Box size={27} />
</button>
</div>
<section className={`workplane-wrap ${workplaneMode ? "placing-workplane" : ""}`} aria-label="Workplane">
<div className="workplane-plane">
<div
className="three-workplane-host"
ref={hostRef}
onDragOver={(event) => {
event.preventDefault();
event.dataTransfer.dropEffect = "copy";
}}
onDrop={handleDrop}
onPointerDown={handlePointerDown}
onPointerMove={handlePointerMove}
onPointerUp={finishDrag}
onPointerCancel={finishDrag}
/>
{marqueeRect ? <div className="selection-marquee" style={marqueeRect} /> : null}
{transformOverlay && !alignMode && !mirrorMode ? (
<TransformOverlay
box={transformOverlay}
measureKey={pinnedMeasureKey ?? hoverMeasureKey}
editingDimension={editingDimension}
editingRotation={editingRotation}
rotationReadout={rotationReadout}
showRotationWheel={activeRotationWheel}
2026-06-13 10:26:40 +03:00
hideSelectionChrome={activeTransformKind === "rotate"}
2026-06-12 14:05:36 +03:00
hideDimensionMarks={activeTransformKind === "scale"}
2026-06-06 23:10:29 +03:00
rotationWheelAxis={rotationWheelAxis}
2026-06-13 09:13:15 +03:00
pinnedRotationWheelView={pinnedRotationWheelView}
2026-06-06 23:10:29 +03:00
onBeginTransform={beginTransform}
onMoveTransform={updateTransform}
onFinishTransform={finishTransform}
onHoverMeasure={setHoverMeasureKey}
onPinMeasure={setPinnedMeasureKey}
onBeginDimensionEdit={beginDimensionEdit}
onBeginLiftEdit={beginLiftEdit}
onEditingDimensionChange={(value) => setEditingDimension((current) => (current ? { ...current, value } : current))}
onCommitDimensionEdit={commitDimensionEdit}
onCancelDimensionEdit={cancelDimensionEdit}
onBeginRotationEdit={beginRotationEdit}
onEditingRotationChange={(value) => setEditingRotation((current) => (current ? { ...current, value } : current))}
onCommitRotationEdit={commitRotationEdit}
onCancelRotationEdit={cancelRotationEdit}
/>
) : null}
{alignOverlay ? <AlignOverlay overlay={alignOverlay} onAlign={onAlignSelection} onPreview={onAlignPreview} onPreviewClear={onAlignPreviewClear} /> : null}
{mirrorOverlay ? <MirrorOverlay overlay={mirrorOverlay} onMirror={onMirrorSelection} onPreview={onMirrorPreview} onPreviewClear={onMirrorPreviewClear} /> : null}
</div>
</section>
{selectedShape ? (
<ShapeInspector
shape={selectedShape}
snap={snap}
snapOpen={snapOpen}
2026-06-15 12:56:23 +03:00
onUpdate={(patch, options) => onUpdateShape(selectedShape.id, patchWithResizeAnchor(selectedShape, patch, options?.resizeAxis, lastResizeAnchorRef.current))}
2026-06-06 23:10:29 +03:00
onClose={() => onSelectShape(null)}
onSnapChange={setSnap}
onSnapOpenChange={setSnapOpen}
/>
) : null}
{!selectedShape ? (
<div className="grid-settings">
<SnapGridControl snap={snap} snapOpen={snapOpen} onSnapChange={setSnap} onSnapOpenChange={setSnapOpen} />
</div>
) : null}
{settingsOpen ? (
<WorkspaceSettingsModal
workspace={workspace}
snap={snap}
onWorkspaceChange={setWorkspace}
onSnapChange={setSnap}
onClose={() => setSettingsOpen(false)}
/>
) : null}
</main>
);
}
function createThreeScene(host: HTMLDivElement): ThreeState {
const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: "high-performance", preserveDrawingBuffer: shouldPreserveDrawingBufferForLocalAutomation() });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(host.clientWidth, host.clientHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = DEFAULT_WORKSPACE.showShadows;
renderer.shadowMap.type = THREE.PCFShadowMap;
host.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color("#f8fbfc");
const camera = new THREE.PerspectiveCamera(38, host.clientWidth / Math.max(1, host.clientHeight), 0.1, 6000);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.08;
controls.rotateSpeed = 0.58;
controls.zoomSpeed = 0.72;
controls.panSpeed = 0.65;
controls.screenSpacePanning = true;
controls.zoomToCursor = true;
controls.mouseButtons = {
LEFT: null,
MIDDLE: THREE.MOUSE.PAN,
RIGHT: THREE.MOUSE.ROTATE,
};
controls.minDistance = 18;
controls.maxDistance = 4200;
controls.minPolarAngle = 0.06;
controls.maxPolarAngle = Math.PI * 0.68;
controls.target.copy(CAMERA_TARGET);
const ambient = new THREE.HemisphereLight("#ffffff", "#d6edf5", 2.1);
scene.add(ambient);
const key = new THREE.DirectionalLight("#ffffff", 3.1);
key.position.set(70, 130, 75);
key.castShadow = true;
key.shadow.camera.left = -130;
key.shadow.camera.right = 130;
key.shadow.camera.top = 130;
key.shadow.camera.bottom = -130;
key.shadow.mapSize.set(2048, 2048);
key.shadow.bias = -0.00008;
key.shadow.normalBias = 0.045;
scene.add(key);
const fill = new THREE.DirectionalLight("#c8f4ff", 1.2);
fill.position.set(-95, 45, -60);
scene.add(fill);
const workplaneLayer = new THREE.Group();
workplaneLayer.name = "Workplane";
const shapeLayer = new THREE.Group();
shapeLayer.name = "Shapes";
const helperLayer = new THREE.Group();
helperLayer.name = "SelectionHelpers";
scene.add(workplaneLayer, shapeLayer, helperLayer);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
const resize = () => {
const width = Math.max(1, host.clientWidth);
const height = Math.max(1, host.clientHeight);
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
state.needsRender = true;
};
const state = {
renderer,
scene,
camera,
controls,
workplaneLayer,
shapeLayer,
helperLayer,
raycaster,
pointer,
dragPlane,
animationId: 0,
needsRender: true,
wasCameraMoving: false,
lastOverlaySync: 0,
lastViewCubeSync: 0,
rotationHandleSides: null,
2026-06-06 23:10:29 +03:00
disposeInteractionListeners: () => {},
resize,
};
const requestRender = () => {
state.needsRender = true;
};
const configureSketchForgeMouseButtons = (event: PointerEvent) => {
controls.mouseButtons.LEFT = event.button === 0 && (event.ctrlKey || event.metaKey) ? THREE.MOUSE.PAN : null;
controls.mouseButtons.MIDDLE = THREE.MOUSE.PAN;
controls.mouseButtons.RIGHT = THREE.MOUSE.ROTATE;
};
const resetSketchForgeMouseButtons = () => {
controls.mouseButtons.LEFT = null;
controls.mouseButtons.MIDDLE = THREE.MOUSE.PAN;
controls.mouseButtons.RIGHT = THREE.MOUSE.ROTATE;
};
const preventContextMenu = (event: MouseEvent) => {
event.preventDefault();
};
controls.addEventListener("change", requestRender);
renderer.domElement.addEventListener("pointerdown", configureSketchForgeMouseButtons, { capture: true });
renderer.domElement.addEventListener("pointerup", resetSketchForgeMouseButtons);
renderer.domElement.addEventListener("pointercancel", resetSketchForgeMouseButtons);
renderer.domElement.addEventListener("contextmenu", preventContextMenu);
renderer.domElement.addEventListener("wheel", requestRender, { passive: true });
renderer.domElement.addEventListener("pointerdown", requestRender);
state.disposeInteractionListeners = () => {
controls.removeEventListener("change", requestRender);
renderer.domElement.removeEventListener("pointerdown", configureSketchForgeMouseButtons, { capture: true });
renderer.domElement.removeEventListener("pointerup", resetSketchForgeMouseButtons);
renderer.domElement.removeEventListener("pointercancel", resetSketchForgeMouseButtons);
renderer.domElement.removeEventListener("contextmenu", preventContextMenu);
renderer.domElement.removeEventListener("wheel", requestRender);
renderer.domElement.removeEventListener("pointerdown", requestRender);
};
rebuildWorkplane(state, DEFAULT_WORKSPACE);
return state;
}
function resetCamera(state: ThreeState) {
state.camera.position.copy(CAMERA_HOME);
state.controls.target.copy(CAMERA_TARGET);
state.camera.lookAt(CAMERA_TARGET);
state.camera.updateProjectionMatrix();
state.controls.update();
}
function constrainCamera(state: ThreeState, workspace: WorkspaceSettings) {
const target = state.controls.target;
const previousTarget = target.clone();
target.x = clamp(target.x, -workspace.width / 2, workspace.width / 2);
target.y = clamp(target.y, CAMERA_MIN_TARGET_Y, CAMERA_MAX_TARGET_Y);
target.z = clamp(target.z, -workspace.depth / 2, workspace.depth / 2);
const targetShift = target.clone().sub(previousTarget);
if (targetShift.lengthSq() > 0) {
state.camera.position.add(targetShift);
state.camera.updateProjectionMatrix();
}
}
function syncViewCube(state: ThreeState, cube: HTMLDivElement | null) {
if (!cube) {
return;
}
const offset = state.camera.position.clone().sub(state.controls.target);
const horizontalDistance = Math.max(0.001, Math.hypot(offset.x, offset.z));
const pitch = THREE.MathUtils.radToDeg(Math.atan2(offset.y, horizontalDistance));
const yaw = THREE.MathUtils.radToDeg(Math.atan2(offset.x, offset.z));
cube.style.transform = `rotateX(${pitch}deg) rotateY(${-yaw}deg)`;
}
function rebuildWorkplane(state: ThreeState | null, workspace: WorkspaceSettings) {
if (!state) {
return;
}
disposeChildren(state.workplaneLayer);
state.scene.background = new THREE.Color(workspace.background);
state.renderer.shadowMap.enabled = workspace.showShadows;
state.controls.zoomSpeed = 0.28 + workspace.zoomSpeed * 0.09;
const base = new THREE.Mesh(
new THREE.PlaneGeometry(workspace.width, workspace.depth),
new THREE.MeshStandardMaterial({
color: "#ddf8ff",
transparent: true,
opacity: 0.68,
roughness: 0.92,
side: THREE.FrontSide,
}),
);
base.name = "WorkplaneBase";
base.rotation.x = -Math.PI / 2;
base.receiveShadow = workspace.showShadows;
state.workplaneLayer.add(base);
if (workspace.showGrid) {
state.workplaneLayer.add(createGridLines(workspace.width, workspace.depth, workspace.gridBlockSize));
}
}
function createGridLines(width = WORKPLANE_WIDTH, depth = WORKPLANE_DEPTH, blockSize = DEFAULT_WORKSPACE.gridBlockSize) {
const group = new THREE.Group();
const minor = new THREE.LineBasicMaterial({ color: "#91dff0", transparent: true, opacity: 0.55 });
const major = new THREE.LineBasicMaterial({ color: "#4bbddf", transparent: true, opacity: 0.7 });
const axis = new THREE.LineBasicMaterial({ color: "#34aad2", transparent: true, opacity: 0.88 });
const minorPoints: number[] = [];
const majorPoints: number[] = [];
const axisPoints: number[] = [];
const borderPoints: number[] = [];
const pushLine = (points: number[], from: [number, number, number], to: [number, number, number]) => {
points.push(...from, ...to);
};
const step = clamp(blockSize, MIN_GRID_BLOCK_SIZE, MAX_GRID_BLOCK_SIZE);
const majorEvery = 4;
const xCount = Math.floor(width / step);
const zCount = Math.floor(depth / step);
for (let index = 0; index <= xCount; index += 1) {
const x = -width / 2 + index * step;
const centeredX = Math.abs(x) < 0.0001 ? 0 : x;
const points = centeredX === 0 ? axisPoints : index % majorEvery === 0 ? majorPoints : minorPoints;
pushLine(points, [centeredX, 0.04, -depth / 2], [centeredX, 0.04, depth / 2]);
}
for (let index = 0; index <= zCount; index += 1) {
const z = -depth / 2 + index * step;
const centeredZ = Math.abs(z) < 0.0001 ? 0 : z;
const points = centeredZ === 0 ? axisPoints : index % majorEvery === 0 ? majorPoints : minorPoints;
pushLine(points, [-width / 2, 0.04, centeredZ], [width / 2, 0.04, centeredZ]);
}
const border = new THREE.LineBasicMaterial({ color: "#58c5e6", transparent: true, opacity: 0.9 });
pushLine(borderPoints, [-width / 2, 0.08, -depth / 2], [width / 2, 0.08, -depth / 2]);
pushLine(borderPoints, [width / 2, 0.08, -depth / 2], [width / 2, 0.08, depth / 2]);
pushLine(borderPoints, [width / 2, 0.08, depth / 2], [-width / 2, 0.08, depth / 2]);
pushLine(borderPoints, [-width / 2, 0.08, depth / 2], [-width / 2, 0.08, -depth / 2]);
group.add(linesFromPoints(minorPoints, minor));
group.add(linesFromPoints(majorPoints, major));
group.add(linesFromPoints(axisPoints, axis));
group.add(linesFromPoints(borderPoints, border));
return group;
}
function linesFromPoints(points: number[], material: THREE.LineBasicMaterial) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.Float32BufferAttribute(points, 3));
return new THREE.LineSegments(geometry, material);
}
function rebuildShapes(state: ThreeState | null, shapes: WorkplaneShape[], selectedIds: string[]) {
if (!state) {
return;
}
disposeChildren(state.shapeLayer);
const selected = new Set(selectedIds);
shapes.forEach((shape) => {
if (shape.hidden) {
return;
}
const object = createShapeObject(shape, selected.has(shape.id), () => {
state.needsRender = true;
});
state.shapeLayer.add(object);
});
rebuildSelectionHelpers(state, shapes, selectedIds);
}
function rebuildSelectionHelpers(state: ThreeState | null, shapes: WorkplaneShape[], selectedIds: string[]) {
if (!state) {
return;
}
disposeChildren(state.helperLayer);
selectedIds.forEach((id) => {
const shape = shapes.find((entry) => entry.id === id && !entry.hidden);
if (!shape) {
return;
}
const shadow = createSelectedGroundFootprint(shape);
if (shadow) {
state.helperLayer.add(shadow);
}
});
}
2026-06-13 10:26:40 +03:00
function setSelectionHelpersVisible(state: ThreeState | null, visible: boolean) {
if (!state || state.helperLayer.visible === visible) {
return;
}
state.helperLayer.visible = visible;
state.needsRender = true;
}
2026-06-06 23:10:29 +03:00
function formatMeasure(value: number) {
return cleanNearZero(value).toFixed(2);
}
function makeDimensionMark(
key: string,
handleKey: string,
axis: DimensionMark["axis"],
label: string,
fromWorld: THREE.Vector3,
toWorld: THREE.Vector3,
outwardWorld: THREE.Vector3,
project: (point: THREE.Vector3) => { x: number; y: number },
): DimensionMark {
const from = project(fromWorld);
const to = project(toWorld);
const outwardAxis = outwardWorld.clone();
outwardAxis.normalize();
const railOffset = 5.8;
const extensionOverrun = 1.4;
const labelOffset = 3.2;
const railFrom = project(fromWorld.clone().add(outwardAxis.clone().multiplyScalar(railOffset)));
const railTo = project(toWorld.clone().add(outwardAxis.clone().multiplyScalar(railOffset)));
const extensionFrom = project(fromWorld.clone().add(outwardAxis.clone().multiplyScalar(railOffset + extensionOverrun)));
const extensionTo = project(toWorld.clone().add(outwardAxis.clone().multiplyScalar(railOffset + extensionOverrun)));
const labelPoint = project(
fromWorld
.clone()
.lerp(toWorld, 0.5)
.add(outwardAxis.clone().multiplyScalar(railOffset + labelOffset)),
);
return {
key,
handleKey,
axis,
label,
x1: railFrom.x,
y1: railFrom.y,
x2: railTo.x,
y2: railTo.y,
e1x1: from.x,
e1y1: from.y,
e1x2: extensionFrom.x,
e1y2: extensionFrom.y,
e2x1: to.x,
e2y1: to.y,
e2x2: extensionTo.x,
e2y2: extensionTo.y,
labelX: labelPoint.x,
labelY: labelPoint.y,
};
}
function updateTransformOverlayIfChanged(
overlayRef: MutableRefObject<TransformOverlayState | null>,
setOverlay: Dispatch<SetStateAction<TransformOverlayState | null>>,
next: TransformOverlayState,
) {
if (overlayRef.current && JSON.stringify(overlayRef.current) === JSON.stringify(next)) {
return;
}
overlayRef.current = next;
setOverlay(next);
}
2026-06-06 23:10:29 +03:00
function syncTransformOverlay(
state: ThreeState,
shapes: WorkplaneShape[],
selectedIds: string[],
overlayRef: MutableRefObject<TransformOverlayState | null>,
setOverlay: Dispatch<SetStateAction<TransformOverlayState | null>>,
keepVisibleDuringInteraction = false,
) {
if (selectedIds.length < 1) {
if (overlayRef.current) {
overlayRef.current = null;
setOverlay(null);
}
return;
}
const frame = selectionFrameForShapes(shapes, selectedIds);
if (!frame) {
if (overlayRef.current) {
overlayRef.current = null;
setOverlay(null);
}
return;
}
const rect = state.renderer.domElement.getBoundingClientRect();
// Future edits: do not remove this. The transform overlay is projected with
// Vector3.project(), outside Three's renderer. With OrbitControls damping, the
// camera matrix can otherwise be one frame stale, making handles/lines trail.
state.camera.updateMatrixWorld();
const corners = selectionFrameCorners(frame);
const projectedCorners = corners.map((corner) => {
const cameraSpace = corner.clone().applyMatrix4(state.camera.matrixWorldInverse);
const projected = corner.clone().project(state.camera);
return { cameraSpace, projected };
});
const nearPlane = state.camera instanceof THREE.PerspectiveCamera ? state.camera.near : 0.1;
const selectionRadius = Math.max(MIN_SHAPE_SIZE, Math.sqrt(frame.width ** 2 + frame.height ** 2 + frame.depth ** 2) / 2);
const cameraDistance = state.camera.position.distanceTo(frame.center);
// When zoomed into/through a selected object, the projected overlay can span
// thousands of pixels even before any corner crosses the near plane. Hide it
// at that depth instead of drawing misleading dashed lines across the scene.
const cameraInsideSelection = cameraDistance < selectionRadius * 1.12;
const projectionInvalid = projectedCorners.some(({ cameraSpace, projected }) => cameraSpace.z > -nearPlane * 1.5 || !Number.isFinite(projected.x) || !Number.isFinite(projected.y));
const projectedSpanTooLarge = (() => {
const xs = projectedCorners.map(({ projected }) => ((projected.x + 1) / 2) * rect.width);
const ys = projectedCorners.map(({ projected }) => ((1 - projected.y) / 2) * rect.height);
return Math.max(...xs) - Math.min(...xs) > rect.width * 4 || Math.max(...ys) - Math.min(...ys) > rect.height * 4;
})();
const overlayTooClose = projectionInvalid || (!keepVisibleDuringInteraction && (cameraInsideSelection || projectedSpanTooLarge));
if (overlayTooClose) {
if (overlayRef.current) {
overlayRef.current = null;
setOverlay(null);
}
return;
}
const project = (point: THREE.Vector3) => {
const projected = point.clone().project(state.camera);
return {
x: ((projected.x + 1) / 2) * rect.width,
y: ((1 - projected.y) / 2) * rect.height,
};
};
const worldMinY = Math.min(...corners.map((corner) => corner.y));
const worldMaxY = Math.max(...corners.map((corner) => corner.y));
const worldMinX = Math.min(...corners.map((corner) => corner.x));
const worldMaxX = Math.max(...corners.map((corner) => corner.x));
const worldMinZ = Math.min(...corners.map((corner) => corner.z));
const worldMaxZ = Math.max(...corners.map((corner) => corner.z));
const worldCenterX = (worldMinX + worldMaxX) / 2;
const worldCenterY = (worldMinY + worldMaxY) / 2;
2026-06-06 23:10:29 +03:00
const worldCenterZ = (worldMinZ + worldMaxZ) / 2;
const worldCenter = new THREE.Vector3(worldCenterX, worldCenterY, worldCenterZ);
2026-06-06 23:10:29 +03:00
const worldHeight = Math.max(MIN_SHAPE_SIZE, worldMaxY - worldMinY);
const liftOffset = Math.max(2, worldHeight * 0.08);
const verticalBase = new THREE.Vector3(worldCenterX, worldMinY, worldCenterZ);
const verticalTop = new THREE.Vector3(worldCenterX, worldMaxY, worldCenterZ);
const showLowerHandles = state.camera.position.y < frame.center.y;
const liftHandle = new THREE.Vector3(worldCenterX, showLowerHandles ? worldMinY - liftOffset : worldMaxY + liftOffset, worldCenterZ);
2026-06-12 14:05:36 +03:00
const xFootAxis = frame.xAxis.clone().normalize();
const yFootAxis = frame.yAxis.clone().normalize();
const zFootAxis = frame.zAxis.clone().normalize();
const localBottomY = frame.min.y;
const localTopY = frame.max.y;
2026-06-06 23:10:29 +03:00
const footprintWorld = {
2026-06-12 14:05:36 +03:00
nearLeft: framePoint(frame, frame.min.x, localBottomY, frame.max.z),
nearRight: framePoint(frame, frame.max.x, localBottomY, frame.max.z),
farRight: framePoint(frame, frame.max.x, localBottomY, frame.min.z),
farLeft: framePoint(frame, frame.min.x, localBottomY, frame.min.z),
near: framePoint(frame, 0, localBottomY, frame.max.z),
right: framePoint(frame, frame.max.x, localBottomY, 0),
far: framePoint(frame, 0, localBottomY, frame.min.z),
left: framePoint(frame, frame.min.x, localBottomY, 0),
2026-06-06 23:10:29 +03:00
};
2026-06-12 14:05:36 +03:00
const bottomCenterWorld = framePoint(frame, 0, localBottomY, 0);
const topCenterWorld = framePoint(frame, 0, localTopY, 0);
2026-06-06 23:10:29 +03:00
const bottom = {
nearLeft: project(footprintWorld.nearLeft),
nearRight: project(footprintWorld.nearRight),
farRight: project(footprintWorld.farRight),
farLeft: project(footprintWorld.farLeft),
};
const mid = {
near: project(footprintWorld.near),
right: project(footprintWorld.right),
far: project(footprintWorld.far),
left: project(footprintWorld.left),
};
2026-06-12 14:05:36 +03:00
const bottomCenterPoint = project(bottomCenterWorld);
const topPoint = project(topCenterWorld);
const heightPoint = project(showLowerHandles ? bottomCenterWorld : topCenterWorld);
2026-06-06 23:10:29 +03:00
const liftPoint = project(liftHandle);
2026-06-12 14:05:36 +03:00
const centerPoint = project(frame.center);
2026-06-06 23:10:29 +03:00
const footprintGuides = [
{ x1: bottom.nearLeft.x, y1: bottom.nearLeft.y, x2: bottom.nearRight.x, y2: bottom.nearRight.y },
{ x1: bottom.nearRight.x, y1: bottom.nearRight.y, x2: bottom.farRight.x, y2: bottom.farRight.y },
{ x1: bottom.farRight.x, y1: bottom.farRight.y, x2: bottom.farLeft.x, y2: bottom.farLeft.y },
{ x1: bottom.farLeft.x, y1: bottom.farLeft.y, x2: bottom.nearLeft.x, y2: bottom.nearLeft.y },
];
2026-06-12 14:05:36 +03:00
const widthLabel = formatMeasure(frame.width);
const depthLabel = formatMeasure(frame.depth);
const heightLabel = formatMeasure(frame.height);
2026-06-06 23:10:29 +03:00
const nearOut = zFootAxis;
const farOut = zFootAxis.clone().multiplyScalar(-1);
const rightOut = xFootAxis;
const leftOut = xFootAxis.clone().multiplyScalar(-1);
const heightHandleKey = showLowerHandles ? "bottom-height" : "top-height";
const liftHandleKey = showLowerHandles ? "lower-shape" : "lift-shape";
const workplaneAnchor = new THREE.Vector3(worldCenterX, 0, worldCenterZ);
const liftLabel = formatMeasure(worldMinY);
2026-06-12 14:05:36 +03:00
const makeFootprintDimensionMark = (handleKey: string, axis: "width" | "depth") => {
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,
);
2026-06-06 23:10:29 +03:00
};
2026-06-12 14:05:36 +03:00
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))];
}),
);
2026-06-06 23:10:29 +03:00
const dimensionMarks = {
...footprintDimensionMarks,
2026-06-12 14:05:36 +03:00
[heightHandleKey]: [makeDimensionMark("height", heightHandleKey, "height", heightLabel, bottomCenterWorld, topCenterWorld, rightOut, project)],
2026-06-06 23:10:29 +03:00
[liftHandleKey]: [makeDimensionMark("elevation", liftHandleKey, "elevation", liftLabel, workplaneAnchor, verticalBase, rightOut, project)],
};
const screenOffsetFromCenter = (point: { x: number; y: number }, distance: number) => {
const dx = point.x - centerPoint.x;
const dy = point.y - centerPoint.y;
const length = Math.max(1, Math.hypot(dx, dy));
return {
x: point.x + (dx / length) * distance,
y: point.y + (dy / length) * distance,
};
};
const rotationSides = rotationHandleSidesForCamera(state, worldCenter);
const sidePoint = (side: RotationHandleSide, y: number) => {
if (side === "right") {
return new THREE.Vector3(worldMaxX, y, worldCenterZ);
}
if (side === "left") {
return new THREE.Vector3(worldMinX, y, worldCenterZ);
}
if (side === "near") {
return new THREE.Vector3(worldCenterX, y, worldMaxZ);
}
return new THREE.Vector3(worldCenterX, y, worldMinZ);
};
const rotateLeft = screenOffsetFromCenter(project(sidePoint(rotationSides.x, worldMaxY)), 24);
const rotateRight = screenOffsetFromCenter(project(sidePoint(rotationSides.z, worldMaxY)), 28);
const rotateBottom = screenOffsetFromCenter(project(sidePoint(rotationSides.y, worldMinY)), 34);
const xFaceCenter = sidePoint(rotationSides.x, worldCenterY);
const zFaceCenter = sidePoint(rotationSides.z, worldCenterY);
2026-06-12 14:05:36 +03:00
const yFaceCenter = verticalBase;
const planeRadius = 154;
2026-06-06 23:10:29 +03:00
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 => {
const screenCenter = project(centerWorld);
const u = project(centerWorld.clone().add(uAxis.clone().multiplyScalar(planeWorldStep)));
const v = project(centerWorld.clone().add(vAxis.clone().multiplyScalar(planeWorldStep)));
const du = { x: u.x - screenCenter.x, y: u.y - screenCenter.y };
const dv = { x: v.x - screenCenter.x, y: v.y - screenCenter.y };
2026-06-06 23:10:29 +03:00
const longest = Math.max(12, Math.hypot(du.x, du.y), Math.hypot(dv.x, dv.y));
const scale = planeRadius / longest / 100;
return {
x: screenCenter.x,
y: screenCenter.y,
2026-06-06 23:10:29 +03:00
a: du.x * scale,
b: du.y * scale,
c: dv.x * scale,
d: dv.y * scale,
};
};
const makeWheel = (centerWorld: THREE.Vector3) => {
const screenCenter = project(centerWorld);
return { x: screenCenter.x, y: screenCenter.y, radius: planeRadius };
};
const makeWorldPoint = (point: THREE.Vector3) => ({ x: point.x, y: point.y, z: point.z });
const rotationWheels: Record<RotationAxis, { x: number; y: number; radius: number }> = {
x: makeWheel(xFaceCenter),
y: makeWheel(yFaceCenter),
z: makeWheel(zFaceCenter),
};
const rotationPlaneCenters: Record<RotationAxis, { x: number; y: number; z: number }> = {
x: makeWorldPoint(xFaceCenter),
y: makeWorldPoint(yFaceCenter),
z: makeWorldPoint(zFaceCenter),
};
const rotationPlanes: Record<RotationAxis, RotationPlaneView> = {
x: makePlaneView(xFaceCenter, zFootAxis, yFootAxis),
y: makePlaneView(yFaceCenter, xFootAxis, zFootAxis),
z: makePlaneView(zFaceCenter, xFootAxis, yFootAxis),
2026-06-06 23:10:29 +03:00
};
const next = {
id: frame.ids.join("|"),
width: rect.width,
height: rect.height,
guides: [
{ x1: topPoint.x, y1: topPoint.y, x2: bottomCenterPoint.x, y2: bottomCenterPoint.y },
...footprintGuides,
],
handles: [
{ key: "near-left", className: "corner", kind: "scale" as const, x: bottom.nearLeft.x, y: bottom.nearLeft.y, title: "Resize" },
{ key: "near-right", className: "corner", kind: "scale" as const, x: bottom.nearRight.x, y: bottom.nearRight.y, title: "Resize" },
{ key: "far-right", className: "corner", kind: "scale" as const, x: bottom.farRight.x, y: bottom.farRight.y, title: "Resize" },
{ key: "far-left", className: "corner", kind: "scale" as const, x: bottom.farLeft.x, y: bottom.farLeft.y, title: "Resize" },
{ key: "near-mid", className: "edge dark", kind: "scale" as const, x: mid.near.x, y: mid.near.y, title: "Resize" },
{ key: "right-mid", className: "edge dark", kind: "scale" as const, x: mid.right.x, y: mid.right.y, title: "Resize" },
{ key: "far-mid", className: "edge dark", kind: "scale" as const, x: mid.far.x, y: mid.far.y, title: "Resize" },
{ key: "left-mid", className: "edge dark", kind: "scale" as const, x: mid.left.x, y: mid.left.y, title: "Resize" },
{ key: heightHandleKey, className: "height-top", kind: "height" as const, x: heightPoint.x, y: heightPoint.y, title: "Height" },
{ key: liftHandleKey, className: showLowerHandles ? "height-lift lower" : "height-lift", kind: "lift" as const, x: liftPoint.x, y: liftPoint.y, title: "Lift" },
],
rotateHandles: [
2026-06-12 14:05:36 +03:00
{ 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 },
2026-06-06 23:10:29 +03:00
],
dimensions: dimensionMarks,
rotationWheel: rotationWheels.y,
rotationWheels,
rotationPlaneCenters,
2026-06-06 23:10:29 +03:00
rotationPlanes,
};
updateTransformOverlayIfChanged(overlayRef, setOverlay, next);
2026-06-06 23:10:29 +03:00
}
function syncAlignOverlay(
state: ThreeState,
shapes: WorkplaneShape[],
selectedIds: string[],
alignMode: boolean,
alignAnchorId: string | null,
statuses: AlignHandleStatus[],
overlayRef: MutableRefObject<AlignOverlayState | null>,
setOverlay: Dispatch<SetStateAction<AlignOverlayState | null>>,
) {
const clear = () => {
if (overlayRef.current) {
overlayRef.current = null;
setOverlay(null);
}
};
if (!alignMode || selectedIds.length < 2) {
clear();
return;
}
const selectedFrame = selectionFrameForShapes(shapes, selectedIds);
const anchorFrame = alignAnchorId && selectedIds.includes(alignAnchorId) ? selectionFrameForShapes(shapes, [alignAnchorId]) : null;
const frame = anchorFrame ?? selectedFrame;
if (!frame) {
clear();
return;
}
const rect = state.renderer.domElement.getBoundingClientRect();
state.camera.updateMatrixWorld();
const corners = selectionFrameCorners(frame);
const projectedCorners = corners.map((corner) => {
const cameraSpace = corner.clone().applyMatrix4(state.camera.matrixWorldInverse);
const projected = corner.clone().project(state.camera);
return { cameraSpace, projected };
});
const nearPlane = state.camera instanceof THREE.PerspectiveCamera ? state.camera.near : 0.1;
if (projectedCorners.some(({ cameraSpace, projected }) => cameraSpace.z > -nearPlane * 1.5 || !Number.isFinite(projected.x) || !Number.isFinite(projected.y))) {
clear();
return;
}
const project = (point: THREE.Vector3) => {
const projected = point.clone().project(state.camera);
return {
x: ((projected.x + 1) / 2) * rect.width,
y: ((1 - projected.y) / 2) * rect.height,
};
};
const worldMinY = Math.min(...corners.map((corner) => corner.y));
const worldMaxY = Math.max(...corners.map((corner) => corner.y));
const worldMinX = Math.min(...corners.map((corner) => corner.x));
const worldMaxX = Math.max(...corners.map((corner) => corner.x));
const worldMinZ = Math.min(...corners.map((corner) => corner.z));
const worldMaxZ = Math.max(...corners.map((corner) => corner.z));
const worldCenterX = (worldMinX + worldMaxX) / 2;
const worldCenterY = (worldMinY + worldMaxY) / 2;
const worldCenterZ = (worldMinZ + worldMaxZ) / 2;
const offset = Math.max(8, Math.max(worldMaxX - worldMinX, worldMaxY - worldMinY, worldMaxZ - worldMinZ) * 0.16);
const statusByKey = new Map(statuses.map((status) => [`${status.axis}:${status.target}`, status]));
const guidePoints = {
x0: project(new THREE.Vector3(worldMinX, worldMinY, worldMaxZ + offset)),
x1: project(new THREE.Vector3(worldMaxX, worldMinY, worldMaxZ + offset)),
z0: project(new THREE.Vector3(worldMaxX + offset, worldMinY, worldMinZ)),
z1: project(new THREE.Vector3(worldMaxX + offset, worldMinY, worldMaxZ)),
y0: project(new THREE.Vector3(worldMinX - offset, worldMinY, worldMaxZ + offset)),
y1: project(new THREE.Vector3(worldMinX - offset, worldMaxY, worldMaxZ + offset)),
};
const makeHandle = (axis: AlignAxis, target: AlignTarget, point: THREE.Vector3) => {
const status = statusByKey.get(`${axis}:${target}`);
if (!status) {
return null;
}
const screen = project(point);
return {
...status,
key: `${axis}-${target}`,
x: screen.x,
y: screen.y,
};
};
const handles = [
makeHandle("x", "min", new THREE.Vector3(worldMinX, worldMinY, worldMaxZ + offset)),
makeHandle("x", "center", new THREE.Vector3(worldCenterX, worldMinY, worldMaxZ + offset)),
makeHandle("x", "max", new THREE.Vector3(worldMaxX, worldMinY, worldMaxZ + offset)),
makeHandle("z", "min", new THREE.Vector3(worldMaxX + offset, worldMinY, worldMinZ)),
makeHandle("z", "center", new THREE.Vector3(worldMaxX + offset, worldMinY, worldCenterZ)),
makeHandle("z", "max", new THREE.Vector3(worldMaxX + offset, worldMinY, worldMaxZ)),
makeHandle("y", "min", new THREE.Vector3(worldMinX - offset, worldMinY, worldMaxZ + offset)),
makeHandle("y", "center", new THREE.Vector3(worldMinX - offset, worldCenterY, worldMaxZ + offset)),
makeHandle("y", "max", new THREE.Vector3(worldMinX - offset, worldMaxY, worldMaxZ + offset)),
].filter((handle): handle is AlignOverlayState["handles"][number] => Boolean(handle));
const next = {
guides: [
{ key: "x", x1: guidePoints.x0.x, y1: guidePoints.x0.y, x2: guidePoints.x1.x, y2: guidePoints.x1.y },
{ key: "z", x1: guidePoints.z0.x, y1: guidePoints.z0.y, x2: guidePoints.z1.x, y2: guidePoints.z1.y },
{ key: "y", x1: guidePoints.y0.x, y1: guidePoints.y0.y, x2: guidePoints.y1.x, y2: guidePoints.y1.y },
],
handles,
};
overlayRef.current = next;
setOverlay(next);
}
function syncMirrorOverlay(
state: ThreeState,
shapes: WorkplaneShape[],
selectedIds: string[],
mirrorMode: boolean,
overlayRef: MutableRefObject<MirrorOverlayState | null>,
setOverlay: Dispatch<SetStateAction<MirrorOverlayState | null>>,
) {
const clear = () => {
if (overlayRef.current) {
overlayRef.current = null;
setOverlay(null);
}
};
if (!mirrorMode || selectedIds.length < 1) {
clear();
return;
}
const frame = selectionFrameForShapes(shapes, selectedIds);
if (!frame) {
clear();
return;
}
const rect = state.renderer.domElement.getBoundingClientRect();
state.camera.updateMatrixWorld();
const corners = selectionFrameCorners(frame);
const projectedCorners = corners.map((corner) => {
const cameraSpace = corner.clone().applyMatrix4(state.camera.matrixWorldInverse);
const projected = corner.clone().project(state.camera);
return { cameraSpace, projected };
});
const nearPlane = state.camera instanceof THREE.PerspectiveCamera ? state.camera.near : 0.1;
if (projectedCorners.some(({ cameraSpace, projected }) => cameraSpace.z > -nearPlane * 1.5 || !Number.isFinite(projected.x) || !Number.isFinite(projected.y))) {
clear();
return;
}
const project = (point: THREE.Vector3) => {
const projected = point.clone().project(state.camera);
return {
x: ((projected.x + 1) / 2) * rect.width,
y: ((1 - projected.y) / 2) * rect.height,
};
};
const screenAngle = (from: THREE.Vector3, to: THREE.Vector3) => {
const a = project(from);
const b = project(to);
return THREE.MathUtils.radToDeg(Math.atan2(b.y - a.y, b.x - a.x));
};
const worldMinY = Math.min(...corners.map((corner) => corner.y));
const worldMaxY = Math.max(...corners.map((corner) => corner.y));
const worldMinX = Math.min(...corners.map((corner) => corner.x));
const worldMaxX = Math.max(...corners.map((corner) => corner.x));
const worldMinZ = Math.min(...corners.map((corner) => corner.z));
const worldMaxZ = Math.max(...corners.map((corner) => corner.z));
const worldCenterX = (worldMinX + worldMaxX) / 2;
const worldCenterY = (worldMinY + worldMaxY) / 2;
const worldCenterZ = (worldMinZ + worldMaxZ) / 2;
const width = Math.max(MIN_SHAPE_SIZE, worldMaxX - worldMinX);
const height = Math.max(MIN_SHAPE_SIZE, worldMaxY - worldMinY);
const depth = Math.max(MIN_SHAPE_SIZE, worldMaxZ - worldMinZ);
const offset = Math.max(10, Math.max(width, height, depth) * 0.2);
const step = Math.max(10, Math.max(width, height, depth) * 0.28);
const xWorld = new THREE.Vector3(worldCenterX, worldMinY, worldMaxZ + offset);
const zWorld = new THREE.Vector3(worldMaxX + offset, worldMinY, worldCenterZ);
const yWorld = new THREE.Vector3(worldMinX - offset, worldCenterY, worldMaxZ + offset);
const xScreen = project(xWorld);
const zScreen = project(zWorld);
const yScreen = project(yWorld);
const xGuideA = new THREE.Vector3(worldMinX, worldMinY, worldMaxZ + offset);
const xGuideB = new THREE.Vector3(worldMaxX, worldMinY, worldMaxZ + offset);
const zGuideA = new THREE.Vector3(worldMaxX + offset, worldMinY, worldMinZ);
const zGuideB = new THREE.Vector3(worldMaxX + offset, worldMinY, worldMaxZ);
const yGuideA = new THREE.Vector3(worldMinX - offset, worldMinY, worldMaxZ + offset);
const yGuideB = new THREE.Vector3(worldMinX - offset, worldMaxY, worldMaxZ + offset);
const xA = project(xGuideA);
const xB = project(xGuideB);
const zA = project(zGuideA);
const zB = project(zGuideB);
const yA = project(yGuideA);
const yB = project(yGuideB);
const next = {
guides: [
{ key: "x", x1: xA.x, y1: xA.y, x2: xB.x, y2: xB.y },
{ key: "z", x1: zA.x, y1: zA.y, x2: zB.x, y2: zB.y },
{ key: "y", x1: yA.x, y1: yA.y, x2: yB.x, y2: yB.y },
],
handles: [
{
axis: "x" as const,
key: "mirror-x",
x: xScreen.x,
y: xScreen.y,
angle: screenAngle(xWorld.clone().add(new THREE.Vector3(-step, 0, 0)), xWorld.clone().add(new THREE.Vector3(step, 0, 0))),
title: "Mirror left-right",
},
{
axis: "z" as const,
key: "mirror-z",
x: zScreen.x,
y: zScreen.y,
angle: screenAngle(zWorld.clone().add(new THREE.Vector3(0, 0, -step)), zWorld.clone().add(new THREE.Vector3(0, 0, step))),
title: "Mirror front-back",
},
{
axis: "y" as const,
key: "mirror-y",
x: yScreen.x,
y: yScreen.y,
angle: screenAngle(yWorld.clone().add(new THREE.Vector3(0, -step, 0)), yWorld.clone().add(new THREE.Vector3(0, step, 0))),
title: "Mirror top-bottom",
},
],
};
overlayRef.current = next;
setOverlay(next);
}
function findShapeObject(state: ThreeState, id: string) {
return state.shapeLayer.children.find((child) => child.userData.shapeId === id) ?? null;
}
function findSelectionHelper(state: ThreeState, id: string) {
const helper = state.helperLayer.children.find((child) => child.userData.shapeId === id);
return helper instanceof THREE.Box3Helper ? helper : null;
}
function findSelectedGroundFootprint(state: ThreeState, id: string) {
return state.helperLayer.children.find((child) => child.name === "SelectedGroundFootprint" && child.userData.shapeId === id) ?? null;
}
function updateSelectedGroundFootprintPreviews(state: ThreeState, drag: DragState) {
drag.items.forEach((item) => {
const footprint = findSelectedGroundFootprint(state, item.id);
if (!footprint) {
return;
}
footprint.position.x = item.nextX - item.startX;
footprint.position.z = item.nextZ - item.startZ;
footprint.updateMatrixWorld(true);
});
}
function createSelectedGroundFootprint(shape: WorkplaneShape) {
const frame = selectionFrameForShapes([shape], [shape.id]);
if (!frame) {
return null;
}
const corners = selectionFrameCorners(frame);
const minWorldY = Math.min(...corners.map((corner) => corner.y));
if (minWorldY <= 0.08) {
return null;
}
const group = new THREE.Group();
group.name = "SelectedGroundFootprint";
group.userData.shapeId = shape.id;
2026-06-12 20:33:19 +03:00
const y = 0.04;
const footprint = [
framePoint(frame, frame.min.x, frame.min.y, frame.min.z),
framePoint(frame, frame.max.x, frame.min.y, frame.min.z),
framePoint(frame, frame.max.x, frame.min.y, frame.max.z),
framePoint(frame, frame.min.x, frame.min.y, frame.max.z),
].map((point) => new THREE.Vector3(point.x, y, point.z));
const fillGeometry = new THREE.BufferGeometry();
fillGeometry.setAttribute(
"position",
new THREE.BufferAttribute(
new Float32Array([
footprint[0].x, footprint[0].y, footprint[0].z,
footprint[1].x, footprint[1].y, footprint[1].z,
footprint[2].x, footprint[2].y, footprint[2].z,
footprint[0].x, footprint[0].y, footprint[0].z,
footprint[2].x, footprint[2].y, footprint[2].z,
footprint[3].x, footprint[3].y, footprint[3].z,
]),
3,
),
);
fillGeometry.computeVertexNormals();
2026-06-06 23:10:29 +03:00
const fill = new THREE.Mesh(
2026-06-12 20:33:19 +03:00
fillGeometry,
2026-06-06 23:10:29 +03:00
new THREE.MeshBasicMaterial({
color: "#7f8f95",
transparent: true,
opacity: 0.22,
depthWrite: false,
side: THREE.DoubleSide,
}),
);
group.add(fill);
2026-06-12 20:33:19 +03:00
const points = [...footprint, footprint[0].clone()];
2026-06-06 23:10:29 +03:00
const outline = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
new THREE.LineBasicMaterial({ color: "#00aeea", transparent: true, opacity: 0.92 }),
);
outline.userData.shapeId = shape.id;
group.add(outline);
return group;
}
function createTransformHandles(box: THREE.Box3, id: string) {
const group = new THREE.Group();
group.name = "SketchForgeTransformHandles";
group.userData.shapeId = id;
const handleMaterial = new THREE.MeshBasicMaterial({ color: "#e8eef1" });
const darkMaterial = new THREE.MeshBasicMaterial({ color: "#273849" });
const rotateMaterial = new THREE.LineBasicMaterial({ color: "#00aeea", transparent: true, opacity: 0.96 });
const dashMaterial = new THREE.LineDashedMaterial({ color: "#2c3339", dashSize: 2.2, gapSize: 2.4, transparent: true, opacity: 0.72 });
const handleGeometry = new THREE.BoxGeometry(2.6, 2.6, 2.6);
const dotGeometry = new THREE.BoxGeometry(1.7, 1.7, 1.7);
const coneGeometry = new THREE.ConeGeometry(1.7, 3.4, 18);
const center = box.getCenter(new THREE.Vector3());
const topY = box.max.y + 1.4;
const x0 = box.min.x;
const x1 = box.max.x;
const z0 = box.min.z;
const z1 = box.max.z;
const xm = center.x;
const zm = center.z;
const cornerPoints = [
{ key: "far-left", kind: "scale" as const, point: new THREE.Vector3(x0, box.min.y + 1.3, z0) },
{ key: "far-right", kind: "scale" as const, point: new THREE.Vector3(x1, box.min.y + 1.3, z0) },
{ key: "near-left", kind: "scale" as const, point: new THREE.Vector3(x0, box.min.y + 1.3, z1) },
{ key: "near-right", kind: "scale" as const, point: new THREE.Vector3(x1, box.min.y + 1.3, z1) },
{ key: "far-left", kind: "scale" as const, point: new THREE.Vector3(x0, topY, z0) },
{ key: "far-right", kind: "scale" as const, point: new THREE.Vector3(x1, topY, z0) },
{ key: "near-left", kind: "scale" as const, point: new THREE.Vector3(x0, topY, z1) },
{ key: "near-right", kind: "scale" as const, point: new THREE.Vector3(x1, topY, z1) },
{ key: "top-height", kind: "height" as const, point: new THREE.Vector3(xm, box.max.y + 7, zm) },
];
cornerPoints.forEach(({ key, kind, point }) => {
const handle = new THREE.Mesh(handleGeometry, handleMaterial);
handle.position.copy(point);
handle.userData.shapeId = id;
handle.userData.transformHandle = kind;
handle.userData.transformHandleKey = key;
handle.userData.transformPlaneY = point.y;
group.add(handle);
const outline = new THREE.LineSegments(new THREE.EdgesGeometry(handleGeometry), new THREE.LineBasicMaterial({ color: "#2d3439", transparent: true, opacity: 0.86 }));
outline.position.copy(point);
outline.userData.shapeId = id;
outline.userData.transformHandle = handle.userData.transformHandle;
outline.userData.transformHandleKey = key;
outline.userData.transformPlaneY = point.y;
group.add(outline);
});
[
{ key: "far-mid", point: new THREE.Vector3(xm, topY, z0) },
{ key: "near-mid", point: new THREE.Vector3(xm, topY, z1) },
{ key: "left-mid", point: new THREE.Vector3(x0, topY, zm) },
{ key: "right-mid", point: new THREE.Vector3(x1, topY, zm) },
{ key: "far-mid", point: new THREE.Vector3(xm, box.min.y + 1.3, z0) },
{ key: "near-mid", point: new THREE.Vector3(xm, box.min.y + 1.3, z1) },
{ key: "left-mid", point: new THREE.Vector3(x0, box.min.y + 1.3, zm) },
{ key: "right-mid", point: new THREE.Vector3(x1, box.min.y + 1.3, zm) },
].forEach(({ key, point }) => {
const dot = new THREE.Mesh(dotGeometry, darkMaterial);
dot.position.copy(point);
dot.userData.shapeId = id;
dot.userData.transformHandle = "scale";
dot.userData.transformHandleKey = key;
dot.userData.transformPlaneY = point.y;
group.add(dot);
});
[
[new THREE.Vector3(xm, box.max.y + 7, zm), new THREE.Vector3(xm, box.min.y + 1.3, zm)],
].forEach(([from, to]) => {
const geometry = new THREE.BufferGeometry().setFromPoints([from, to]);
const line = new THREE.Line(geometry, dashMaterial);
line.computeLineDistances();
group.add(line);
});
[
{ key: "rotate-left", center: new THREE.Vector3(x0 - 5, topY + 5, z0 - 5), start: 0.15, end: 1.45, arrow: new THREE.Vector3(x0 - 2.8, topY + 5, z0 - 8.2), rotation: Math.PI * 0.35 },
{ key: "rotate-right", center: new THREE.Vector3(x1 + 5, topY + 5, z0 - 5), start: 1.7, end: 2.95, arrow: new THREE.Vector3(x1 + 8.2, topY + 5, z0 - 2.8), rotation: Math.PI * 0.85 },
{ key: "rotate-bottom", center: new THREE.Vector3(x1 + 5, topY + 5, z1 + 5), start: 3.3, end: 4.55, arrow: new THREE.Vector3(x1 + 2.8, topY + 5, z1 + 8.2), rotation: Math.PI * 1.35 },
].forEach((arc) => {
const line = createRotateArc(arc.center, 5.5, arc.start, arc.end, rotateMaterial);
line.userData.shapeId = id;
line.userData.transformHandle = "rotate";
line.userData.transformHandleKey = arc.key;
group.add(line);
const arrow = new THREE.Mesh(coneGeometry, darkMaterial);
arrow.position.copy(arc.arrow);
arrow.rotation.set(Math.PI / 2, 0, arc.rotation);
arrow.userData.shapeId = id;
arrow.userData.transformHandle = "rotate";
arrow.userData.transformHandleKey = arc.key;
group.add(arrow);
});
return group;
}
function createRotateArc(center: THREE.Vector3, radius: number, start: number, end: number, material: THREE.LineBasicMaterial) {
const points: THREE.Vector3[] = [];
for (let i = 0; i <= 18; i += 1) {
const angle = start + ((end - start) * i) / 18;
points.push(new THREE.Vector3(center.x + Math.cos(angle) * radius, center.y, center.z + Math.sin(angle) * radius));
}
return new THREE.Line(new THREE.BufferGeometry().setFromPoints(points), material);
}
function createShapeObject(shape: WorkplaneShape, showEdges = false, onTextureReady?: () => void) {
const group = new THREE.Group();
group.name = shape.name;
group.userData.shapeId = shape.id;
group.userData.showEdges = showEdges;
group.position.set(shape.x, (shape.elevation ?? 0) + shape.height / 2, shape.z);
group.rotation.set(
THREE.MathUtils.degToRad(shape.rotationX ?? 0),
THREE.MathUtils.degToRad(shape.rotation),
THREE.MathUtils.degToRad(shape.rotationZ ?? 0),
);
group.scale.set(mirrorSign(shape.mirrorX), mirrorSign(shape.mirrorY), mirrorSign(shape.mirrorZ));
if (shape.groupedShapes?.length && !shape.importedMesh) {
const content = new THREE.Group();
shape.groupedShapes
.filter((child) => !child.hidden)
.forEach((child) => {
const childShape = shape.hole ? { ...child, hole: true, color: "#b8c2cc" } : child;
const childObject = createShapeObject(childShape, showEdges, onTextureReady);
content.add(childObject);
});
const contentBox = new THREE.Box3().setFromObject(content);
const contentSize = contentBox.getSize(new THREE.Vector3());
content.scale.set(
shapeWidth(shape) / Math.max(0.001, contentSize.x),
shape.height / Math.max(0.001, contentSize.y),
shapeDepth(shape) / Math.max(0.001, contentSize.z),
);
content.position.y = -shape.height / 2;
group.add(content);
group.traverse((child) => {
child.userData.shapeId = shape.id;
});
return group;
}
const material = new THREE.MeshStandardMaterial({
color: shape.hole ? "#b7c0c9" : shape.color,
transparent: Boolean(shape.hole),
opacity: shape.hole ? (shape.importedMesh ? 0.34 : 0.52) : 1,
roughness: shape.hole ? 0.88 : 0.57,
metalness: 0.02,
side: shape.importedMesh?.sourceFormat === "json" || mirroredAxisCount(shape) % 2 === 1 ? THREE.DoubleSide : THREE.FrontSide,
});
const width = shapeWidth(shape);
const depth = shapeDepth(shape);
const size = Math.min(width, depth);
const height = shape.height;
switch (shape.kind) {
case "box":
addMesh(
group,
shape.radius && shape.radius > 0 ? new RoundedBoxGeometry(width, height, depth, shape.steps ?? 10, shape.radius) : new THREE.BoxGeometry(width, height, depth),
shape.imagePlate && !shape.hole ? createImagePlateMaterials(shape, material, onTextureReady) : material,
shape,
);
break;
case "cylinder":
addMesh(group, new THREE.CylinderGeometry(1, 1, height, shape.sides ?? 96, shape.segments ?? 1), material, shape, undefined, undefined, new THREE.Vector3(width / 2, 1, depth / 2));
break;
case "sphere":
addMesh(group, new THREE.SphereGeometry(1, Math.max(8, (shape.steps ?? 24) * 2), Math.max(6, shape.steps ?? 24)), material, shape, undefined, undefined, new THREE.Vector3(width / 2, height / 2, depth / 2));
break;
case "cone":
2026-06-13 15:56:06 +03:00
addMesh(
group,
new THREE.CylinderGeometry(shape.topRadius ?? 0, shape.baseRadius ?? width / 2, height, shape.sides ?? 96),
material,
shape,
undefined,
undefined,
new THREE.Vector3(1, 1, depth / Math.max(0.001, width)),
);
2026-06-06 23:10:29 +03:00
break;
case "pyramid":
addMesh(group, createPyramidGeometry(width, height, depth, shape.sides ?? 4), material, shape);
break;
case "roof":
addMesh(group, createRoofGeometry(width, height, depth), material, shape);
break;
case "roundRoof":
addMesh(group, createRoundRoofGeometry(width, height, depth, shape.sides ?? 64), material, shape);
break;
case "halfSphere":
addMesh(group, createHalfSphereGeometry(width, height, depth, shape.steps ?? 32), material, shape);
break;
case "torus":
addMesh(group, createTorusGeometry(width, height, depth), material, shape);
break;
case "ring":
addMesh(group, createHollowCylinderGeometry(width, height, depth, shape.bevel ?? 4, 144), material, shape);
break;
case "tube":
addMesh(group, createHollowCylinderGeometry(width, height, depth, shape.bevel ?? 4, 144), material, shape);
break;
case "wedge":
addMesh(group, createWedgeGeometry(width, height, depth), material, shape);
break;
case "polygon":
addMesh(group, new THREE.CylinderGeometry(1, 1, height, 6), material, shape, undefined, undefined, new THREE.Vector3(width / 2, 1, depth / 2));
break;
case "icosahedron":
addMesh(group, new THREE.IcosahedronGeometry(size / 2, 1), material, shape);
break;
case "text":
addTextShape(group, material, shape);
break;
case "mesh":
if (shape.importedMesh) {
addMesh(
group,
getImportedMeshGeometry(shape.importedMesh),
material,
shape,
undefined,
undefined,
new THREE.Vector3(
width / Math.max(0.001, shape.importedMesh.baseWidth),
height / Math.max(0.001, shape.importedMesh.baseHeight),
depth / Math.max(0.001, shape.importedMesh.baseDepth),
),
);
} else {
addMesh(group, new THREE.BoxGeometry(size, Math.max(3, height * 0.35), size * 0.72), material, shape);
}
break;
case "scribble":
addMesh(group, new THREE.TorusKnotGeometry(size * 0.22, size * 0.055, 120, 12), material, shape);
break;
case "sketch":
default:
addMesh(group, new THREE.BoxGeometry(size, Math.max(3, height * 0.35), size * 0.72), material, shape);
break;
}
group.traverse((child) => {
child.userData.shapeId = shape.id;
});
return group;
}
function createImagePlateMaterials(shape: WorkplaneShape, sideMaterial: THREE.MeshStandardMaterial, onTextureReady?: () => void) {
const sideMaterials = Array.from({ length: 5 }, (_, index) => (index === 0 ? sideMaterial : sideMaterial.clone()));
const topMaterial = new THREE.MeshStandardMaterial({
color: "#ffffff",
roughness: 0.64,
metalness: 0,
transparent: true,
alphaTest: 0.02,
side: THREE.FrontSide,
});
if (shape.imagePlate?.dataUrl) {
const texture = imageTextureLoader.load(shape.imagePlate.dataUrl, () => {
texture.needsUpdate = true;
topMaterial.needsUpdate = true;
onTextureReady?.();
});
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = 4;
texture.wrapS = THREE.ClampToEdgeWrapping;
texture.wrapT = THREE.ClampToEdgeWrapping;
topMaterial.map = texture;
}
return [
sideMaterials[0],
sideMaterials[1],
topMaterial,
sideMaterials[2],
sideMaterials[3],
sideMaterials[4],
];
}
function addMesh(
group: THREE.Group,
geometry: THREE.BufferGeometry,
material: THREE.Material | THREE.Material[],
shape: WorkplaneShape,
position?: THREE.Vector3,
rotation?: THREE.Euler,
scale?: THREE.Vector3,
) {
const prepared = geometry.userData.cached ? geometry : putGeometryOnBase(geometry);
const mesh = new THREE.Mesh(prepared, material);
mesh.castShadow = true;
mesh.receiveShadow = false;
if (position) {
mesh.position.copy(position);
}
mesh.position.y -= shape.height / 2;
if (rotation) {
mesh.rotation.copy(rotation);
}
if (scale) {
mesh.scale.copy(scale);
}
group.add(mesh);
const complexEdges =
shape.kind === "mesh" ||
Boolean(shape.importedMesh) ||
["cone", "pyramid", "roof", "roundRoof", "halfSphere", "torus", "tube", "ring", "wedge"].includes(shape.kind);
const importedTriangleCount = shape.importedMesh?.triangleCount ?? 0;
const skipHeavyImportedEdges = Boolean(shape.importedMesh) && importedTriangleCount > IMPORTED_SELECTED_EDGE_TRIANGLE_LIMIT;
if ((group.userData.showEdges || complexEdges) && !skipHeavyImportedEdges) {
const selectedOutline = Boolean(group.userData.showEdges);
const selectedRoundedBox = selectedOutline && shape.kind === "box" && Boolean(shape.radius && shape.radius > 0);
const edgeColor = selectedOutline ? "#00aeea" : shape.hole ? "#697989" : complexEdges ? "#141b21" : darkenHex(shape.color, 0.34);
const edgeOpacity = selectedRoundedBox ? 0 : selectedOutline ? 0.98 : shape.hole ? 0.44 : complexEdges ? 0.38 : shape.kind === "text" ? 0.86 : 0.2;
const edges = new THREE.LineSegments(getEdgesGeometry(shape, prepared, selectedOutline ? 1 : complexEdges ? 14 : 25), new THREE.LineBasicMaterial({ color: edgeColor, transparent: true, opacity: edgeOpacity }));
edges.userData.complexEdge = complexEdges;
edges.position.copy(mesh.position);
edges.rotation.copy(mesh.rotation);
edges.scale.copy(mesh.scale);
group.add(edges);
}
}
function getImportedMeshCache(mesh: NonNullable<WorkplaneShape["importedMesh"]>) {
const cached = importedGeometryCache.get(mesh);
if (cached) {
return cached;
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.Float32BufferAttribute(mesh.positions, 3));
if (mesh.normals && mesh.normals.length === mesh.positions.length) {
geometry.setAttribute("normal", new THREE.Float32BufferAttribute(mesh.normals, 3));
} else {
geometry.computeVertexNormals();
}
putGeometryOnBase(geometry);
geometry.userData.cached = true;
const next = { geometry, edges: new Map<number, THREE.EdgesGeometry>() };
importedGeometryCache.set(mesh, next);
return next;
}
function getImportedMeshGeometry(mesh: NonNullable<WorkplaneShape["importedMesh"]>) {
return getImportedMeshCache(mesh).geometry;
}
function getEdgesGeometry(shape: WorkplaneShape, geometry: THREE.BufferGeometry, threshold: number) {
if (!shape.importedMesh) {
return new THREE.EdgesGeometry(geometry, threshold);
}
const cache = getImportedMeshCache(shape.importedMesh);
const cached = cache.edges.get(threshold);
if (cached) {
return cached;
}
const edges = new THREE.EdgesGeometry(cache.geometry, threshold);
edges.userData.cached = true;
cache.edges.set(threshold, edges);
return edges;
}
function setComplexEdgeVisibility(object: THREE.Object3D, visible: boolean) {
object.traverse((child) => {
if (child.userData.complexEdge) {
child.visible = visible;
}
});
}
function addTextShape(group: THREE.Group, material: THREE.MeshStandardMaterial, shape: WorkplaneShape) {
const text = (shape.text ?? "TEXT").trim() || " ";
const bevel = clamp(shape.bevel ?? 0, 0, 8);
const fontName = shape.font ?? "Multilanguage";
const geometry = new TextGeometry(text, {
font: textFonts[fontName] ?? textFonts.Multilanguage,
size: 20,
depth: shape.height,
curveSegments: fontName === "Stencil" ? 1 : 8,
bevelEnabled: bevel > 0,
bevelThickness: bevel * 0.22,
bevelSize: bevel * 0.16,
bevelSegments: Math.max(1, shape.segments ?? 0),
});
geometry.computeBoundingBox();
const box = geometry.boundingBox;
if (box) {
const textWidth = Math.max(1, box.max.x - box.min.x);
const textDepth = Math.max(1, box.max.y - box.min.y);
const scale = Math.min(shapeWidth(shape) / textWidth, shapeDepth(shape) / textDepth);
geometry.scale(scale, scale, 1);
}
geometry.rotateX(-Math.PI / 2);
geometry.computeBoundingBox();
const rotatedBox = geometry.boundingBox;
if (rotatedBox) {
geometry.translate(
-(rotatedBox.min.x + rotatedBox.max.x) / 2,
-rotatedBox.min.y,
-(rotatedBox.min.z + rotatedBox.max.z) / 2,
);
}
addMesh(group, geometry, material, shape);
}
function putGeometryOnBase(geometry: THREE.BufferGeometry) {
geometry.computeVertexNormals();
geometry.computeBoundingBox();
const minY = geometry.boundingBox?.min.y ?? 0;
geometry.translate(0, -minY, 0);
return geometry;
}
function createRoofGeometry(width: number, height: number, depth: number) {
const w = width / 2;
const d = depth / 2;
const vertices = new Float32Array([
-w, 0, -d, w, 0, -d, 0, height, -d,
-w, 0, d, w, 0, d, 0, height, d,
]);
const indices = [
0, 2, 1,
3, 4, 5,
0, 1, 4, 0, 4, 3,
0, 3, 5, 0, 5, 2,
1, 2, 5, 1, 5, 4,
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.BufferAttribute(vertices, 3));
geometry.setIndex(indices);
return geometry.toNonIndexed();
}
function createWedgeGeometry(width: number, height: number, depth: number) {
const w = width / 2;
const d = depth / 2;
const vertices = new Float32Array([
-w, 0, -d, w, 0, -d, w, height, -d,
-w, 0, d, w, 0, d, w, height, d,
]);
const indices = [
0, 2, 1,
3, 4, 5,
0, 1, 4, 0, 4, 3,
1, 2, 5, 1, 5, 4,
0, 3, 5, 0, 5, 2,
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.BufferAttribute(vertices, 3));
geometry.setIndex(indices);
return geometry.toNonIndexed();
}
function createPyramidGeometry(width: number, height: number, depth: number, sides = 4) {
const count = Math.max(3, Math.round(sides));
if (count !== 4) {
const radius = Math.min(width, depth) / 2;
const geometry = new THREE.ConeGeometry(radius, height, count);
geometry.translate(0, height / 2, 0);
return geometry.toNonIndexed();
}
const w = width / 2;
const d = depth / 2;
const vertices = new Float32Array([
-w, 0, -d, w, 0, -d, w, 0, d, -w, 0, d,
0, height, 0,
]);
const indices = [
0, 1, 2, 0, 2, 3,
0, 4, 1,
1, 4, 2,
2, 4, 3,
3, 4, 0,
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.BufferAttribute(vertices, 3));
geometry.setIndex(indices);
return geometry.toNonIndexed();
}
function createTorusGeometry(width: number, height: number, depth: number) {
const tubeRadius = Math.max(0.1, height / 2);
const majorRadius = Math.max(0.2, Math.min(width, depth) / 2 - tubeRadius);
const geometry = new THREE.TorusGeometry(majorRadius, tubeRadius, 36, 144);
geometry.rotateX(Math.PI / 2);
const outerDiameter = (majorRadius + tubeRadius) * 2;
geometry.scale(width / outerDiameter, 1, depth / outerDiameter);
return geometry.toNonIndexed();
}
function createHollowCylinderGeometry(width: number, height: number, depth: number, thickness: number, segments = 96) {
const outerX = width / 2;
const outerZ = depth / 2;
const safeThickness = clamp(thickness, 0.1, Math.max(0.1, Math.min(outerX, outerZ) - 0.1));
const innerX = Math.max(0.1, outerX - safeThickness);
const innerZ = Math.max(0.1, outerZ - safeThickness);
const count = Math.max(12, Math.round(segments));
const positions: number[] = [];
const point = (rx: number, rz: number, y: number, index: number): [number, number, number] => {
const angle = (index / count) * Math.PI * 2;
return [Math.cos(angle) * rx, y, Math.sin(angle) * rz];
};
const addTri = (a: [number, number, number], b: [number, number, number], c: [number, number, number]) => positions.push(...a, ...b, ...c);
const addQuad = (a: [number, number, number], b: [number, number, number], c: [number, number, number], d: [number, number, number]) => {
addTri(a, b, c);
addTri(a, c, d);
};
for (let index = 0; index < count; index += 1) {
const next = index + 1;
const ob0 = point(outerX, outerZ, 0, index);
const ob1 = point(outerX, outerZ, 0, next);
const ot0 = point(outerX, outerZ, height, index);
const ot1 = point(outerX, outerZ, height, next);
const ib0 = point(innerX, innerZ, 0, index);
const ib1 = point(innerX, innerZ, 0, next);
const it0 = point(innerX, innerZ, height, index);
const it1 = point(innerX, innerZ, height, next);
addQuad(ob0, ot0, ot1, ob1);
addQuad(ib1, it1, it0, ib0);
addQuad(ot0, it0, it1, ot1);
addQuad(ob0, ob1, ib1, ib0);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3));
return geometry;
}
function createRoundRoofGeometry(width: number, height: number, depth: number, sides = 64) {
const radius = width / 2;
const segments = Math.max(4, Math.round(sides));
const shape = new THREE.Shape();
shape.moveTo(-radius, 0);
shape.absarc(0, 0, radius, Math.PI, 0, true);
shape.lineTo(-radius, 0);
shape.closePath();
const geometry = new THREE.ExtrudeGeometry(shape, { depth, bevelEnabled: false, steps: 1, curveSegments: segments });
geometry.translate(0, 0, -depth / 2);
geometry.scale(1, height / Math.max(0.001, radius), 1);
return geometry.toNonIndexed();
}
function createHalfSphereGeometry(width: number, height: number, depth: number, steps = 32) {
const lon = Math.max(8, Math.round(steps) * 2);
const lat = Math.max(4, Math.round(steps / 2));
const rx = width / 2;
const rz = depth / 2;
const positions: number[] = [];
const normals: number[] = [];
const point = (latIndex: number, lonIndex: number): [number, number, number] => {
const theta = (latIndex / lat) * (Math.PI / 2);
const phi = ((lonIndex % lon) / lon) * Math.PI * 2;
const ring = Math.sin(theta);
return [Math.cos(phi) * rx * ring, Math.cos(theta) * height, Math.sin(phi) * rz * ring];
};
const normal = ([x, y, z]: [number, number, number]): [number, number, number] => {
const vector = new THREE.Vector3(x / Math.max(0.001, rx * rx), y / Math.max(0.001, height * height), z / Math.max(0.001, rz * rz)).normalize();
return [vector.x, vector.y, vector.z];
};
const addTri = (a: [number, number, number], b: [number, number, number], c: [number, number, number]) => {
positions.push(...a, ...b, ...c);
normals.push(...normal(a), ...normal(b), ...normal(c));
};
const addCapTri = (a: [number, number, number], b: [number, number, number], c: [number, number, number]) => {
positions.push(...a, ...b, ...c);
normals.push(0, -1, 0, 0, -1, 0, 0, -1, 0);
};
const top: [number, number, number] = [0, height, 0];
for (let xStep = 0; xStep < lon; xStep += 1) {
addTri(top, point(1, xStep + 1), point(1, xStep));
}
for (let yStep = 1; yStep < lat; yStep += 1) {
for (let xStep = 0; xStep < lon; xStep += 1) {
const next = xStep + 1;
const a = point(yStep, xStep);
const b = point(yStep, next);
const c = point(yStep + 1, next);
const d = point(yStep + 1, xStep);
addTri(a, c, d);
addTri(a, b, c);
}
}
const capY = 0;
const bottomCenter: [number, number, number] = [0, capY, 0];
const capPoint = (lonIndex: number): [number, number, number] => {
const phi = ((lonIndex % lon) / lon) * Math.PI * 2;
return [Math.cos(phi) * rx, capY, Math.sin(phi) * rz];
};
for (let xStep = 0; xStep < lon; xStep += 1) {
addCapTri(bottomCenter, capPoint(xStep), capPoint(xStep + 1));
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3));
geometry.setAttribute("normal", new THREE.Float32BufferAttribute(normals, 3));
return geometry;
}
function disposeChildren(group: THREE.Group) {
while (group.children.length > 0) {
const child = group.children.pop();
if (child) {
disposeObject(child);
}
}
}
function disposeObject(object: THREE.Object3D) {
object.traverse((child) => {
const mesh = child as THREE.Mesh | THREE.LineSegments;
if ("geometry" in mesh && mesh.geometry) {
if (!mesh.geometry.userData.cached) {
mesh.geometry.dispose();
}
}
if ("material" in mesh && mesh.material) {
const materials = Array.isArray(mesh.material) ? mesh.material : [mesh.material];
materials.forEach((material) => {
const map = "map" in material ? (material.map as THREE.Texture | null) : null;
if (map) {
map.dispose();
}
material.dispose();
});
}
});
}
function darkenHex(hex: string, amount: number) {
const clean = hex.replace("#", "");
const value = Number.parseInt(clean.length === 3 ? clean.split("").map((char) => char + char).join("") : clean, 16);
const r = Math.max(0, Math.floor(((value >> 16) & 255) * (1 - amount)));
const g = Math.max(0, Math.floor(((value >> 8) & 255) * (1 - amount)));
const b = Math.max(0, Math.floor((value & 255) * (1 - amount)));
return `#${[r, g, b].map((channel) => channel.toString(16).padStart(2, "0")).join("")}`;
}