import { GEOMETRY_NODE_GRAPH_SCHEMA, type GeometryNodeDataType, type GeometryNodeGraphIR, type GeometryNodeIR, type GeometryNodeLinkIR, type GeometryNodeSocketIR } from "./geometry-nodes"; import type { WebEngineEditCommand } from "./web-engine"; const clone = (value: T): T => JSON.parse(JSON.stringify(value)) as T; const socket = (id: string, name: string, direction: "INPUT" | "OUTPUT", dataType: GeometryNodeDataType, defaultValue?: boolean | number | string | number[]): GeometryNodeSocketIR => ({ id, name, direction, dataType, ...(defaultValue === undefined ? {} : { defaultValue }) }); const nodeSockets = (type: string): GeometryNodeSocketIR[] => { if (type === "NodeGroupInput") return [socket("Geometry", "Geometry", "OUTPUT", "GEOMETRY")]; if (type === "NodeGroupOutput") return [socket("Geometry", "Geometry", "INPUT", "GEOMETRY")]; if (type === "GeometryNodeTransform") return [socket("Geometry", "Geometry", "INPUT", "GEOMETRY"), socket("Translation", "Translation", "INPUT", "VECTOR", [0, 0, 0]), socket("Rotation", "Rotation", "INPUT", "VECTOR", [0, 0, 0]), socket("Scale", "Scale", "INPUT", "VECTOR", [1, 1, 1]), socket("Geometry", "Geometry", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodeSetPosition") return [socket("Geometry", "Geometry", "INPUT", "GEOMETRY"), socket("Offset", "Offset", "INPUT", "VECTOR", [0, 0, 0]), socket("Position", "Position", "INPUT", "VECTOR", [0, 0, 0]), socket("Geometry", "Geometry", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodeJoinGeometry") return [socket("Geometry", "Geometry", "INPUT", "GEOMETRY"), socket("Geometry", "Geometry", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodeRealizeInstances") return [socket("Geometry", "Geometry", "INPUT", "GEOMETRY"), socket("Geometry", "Geometry", "OUTPUT", "GEOMETRY")]; if (type === "FunctionNodeInputInt") return [socket("Value", "Value", "OUTPUT", "INT", 0)]; if (type === "FunctionNodeInputVector") return [socket("Vector", "Vector", "OUTPUT", "VECTOR", [0, 0, 0])]; if (type === "ShaderNodeValue") return [socket("Value", "Value", "OUTPUT", "FLOAT", 0)]; if (type === "ShaderNodeMath") return [socket("Value", "Value", "INPUT", "FLOAT", 0), socket("Value_001", "Value", "INPUT", "FLOAT", 0), socket("Value", "Value", "OUTPUT", "FLOAT")]; if (type === "GeometryNodeSubdivisionSurface") return [socket("Geometry", "Geometry", "INPUT", "GEOMETRY"), socket("Level", "Level", "INPUT", "INT"), socket("Render Level", "Render Level", "INPUT", "INT"), socket("Geometry", "Geometry", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodeMeshToPoints") return [socket("Mesh", "Mesh", "INPUT", "GEOMETRY"), socket("Selection", "Selection", "INPUT", "BOOLEAN"), socket("Position", "Position", "INPUT", "VECTOR"), socket("Radius", "Radius", "INPUT", "FLOAT"), socket("Points", "Points", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodePoints") return [socket("Count", "Count", "INPUT", "INT"), socket("Position", "Position", "INPUT", "VECTOR"), socket("Radius", "Radius", "INPUT", "FLOAT"), socket("Points", "Points", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodeInstanceOnPoints") return [socket("Points", "Points", "INPUT", "GEOMETRY"), socket("Selection", "Selection", "INPUT", "BOOLEAN"), socket("Instance", "Instance", "INPUT", "GEOMETRY"), socket("Pick Instance", "Pick Instance", "INPUT", "BOOLEAN"), socket("Instance Index", "Instance Index", "INPUT", "INT"), socket("Rotation", "Rotation", "INPUT", "VECTOR"), socket("Scale", "Scale", "INPUT", "VECTOR"), socket("Instances", "Instances", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodeMeshCube") return [socket("Size", "Size", "INPUT", "VECTOR"), socket("Vertices X", "Vertices X", "INPUT", "INT"), socket("Vertices Y", "Vertices Y", "INPUT", "INT"), socket("Vertices Z", "Vertices Z", "INPUT", "INT"), socket("Mesh", "Mesh", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodeMeshGrid") return [socket("Size X", "Size X", "INPUT", "FLOAT"), socket("Size Y", "Size Y", "INPUT", "FLOAT"), socket("Vertices X", "Vertices X", "INPUT", "INT"), socket("Vertices Y", "Vertices Y", "INPUT", "INT"), socket("Mesh", "Mesh", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodeMeshIcoSphere") return [socket("Radius", "Radius", "INPUT", "FLOAT"), socket("Subdivisions", "Subdivisions", "INPUT", "INT"), socket("Mesh", "Mesh", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodeMeshUVSphere") return [socket("Segments", "Segments", "INPUT", "INT"), socket("Rings", "Rings", "INPUT", "INT"), socket("Radius", "Radius", "INPUT", "FLOAT"), socket("Mesh", "Mesh", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodeMeshCylinder") return [socket("Vertices", "Vertices", "INPUT", "INT"), socket("Side Segments", "Side Segments", "INPUT", "INT"), socket("Radius", "Radius", "INPUT", "FLOAT"), socket("Depth", "Depth", "INPUT", "FLOAT"), socket("Mesh", "Mesh", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodeDeleteGeometry") return [socket("Geometry", "Geometry", "INPUT", "GEOMETRY"), socket("Selection", "Selection", "INPUT", "BOOLEAN"), socket("Geometry", "Geometry", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodeSetShadeSmooth") return [socket("Geometry", "Geometry", "INPUT", "GEOMETRY"), socket("Selection", "Selection", "INPUT", "BOOLEAN"), socket("Shade Smooth", "Shade Smooth", "INPUT", "BOOLEAN"), socket("Geometry", "Geometry", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodeInputPosition") return [socket("Position", "Position", "OUTPUT", "VECTOR")]; if (type === "GeometryNodeInputIndex") return [socket("Index", "Index", "OUTPUT", "INT")]; if (type === "GeometryNodeInputNormal") return [socket("Normal", "Normal", "OUTPUT", "VECTOR")]; if (type === "GeometryNodeDistributePointsOnFaces") return [socket("Mesh", "Mesh", "INPUT", "GEOMETRY"), socket("Selection", "Selection", "INPUT", "BOOLEAN"), socket("Distance Min", "Distance Min", "INPUT", "FLOAT"), socket("Density Max", "Density Max", "INPUT", "FLOAT"), socket("Density Factor", "Density Factor", "INPUT", "FLOAT"), socket("Seed", "Seed", "INPUT", "INT"), socket("Points", "Points", "OUTPUT", "GEOMETRY"), socket("Normal", "Normal", "OUTPUT", "VECTOR"), socket("Rotation", "Rotation", "OUTPUT", "VECTOR")]; if (type === "GeometryNodeTranslateInstances") return [socket("Instances", "Instances", "INPUT", "GEOMETRY"), socket("Selection", "Selection", "INPUT", "BOOLEAN"), socket("Translation", "Translation", "INPUT", "VECTOR"), socket("Local Space", "Local Space", "INPUT", "BOOLEAN"), socket("Instances", "Instances", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodeRotateInstances") return [socket("Instances", "Instances", "INPUT", "GEOMETRY"), socket("Selection", "Selection", "INPUT", "BOOLEAN"), socket("Rotation", "Rotation", "INPUT", "VECTOR"), socket("Local Space", "Local Space", "INPUT", "BOOLEAN"), socket("Instances", "Instances", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodeScaleInstances") return [socket("Instances", "Instances", "INPUT", "GEOMETRY"), socket("Selection", "Selection", "INPUT", "BOOLEAN"), socket("Scale", "Scale", "INPUT", "VECTOR"), socket("Local Space", "Local Space", "INPUT", "BOOLEAN"), socket("Instances", "Instances", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodeCurvePrimitiveCircle") return [socket("Resolution", "Resolution", "INPUT", "INT"), socket("Radius", "Radius", "INPUT", "FLOAT"), socket("Curve", "Curve", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodeCurveToMesh") return [socket("Curve", "Curve", "INPUT", "GEOMETRY"), socket("Profile Curve", "Profile Curve", "INPUT", "GEOMETRY"), socket("Fill Caps", "Fill Caps", "INPUT", "BOOLEAN"), socket("Mesh", "Mesh", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodeMeshToCurve") return [socket("Mesh", "Mesh", "INPUT", "GEOMETRY"), socket("Selection", "Selection", "INPUT", "BOOLEAN"), socket("Curve", "Curve", "OUTPUT", "GEOMETRY")]; if (type === "GeometryNodeExtrudeMesh") return [socket("Mesh", "Mesh", "INPUT", "GEOMETRY"), socket("Selection", "Selection", "INPUT", "BOOLEAN"), socket("Offset", "Offset", "INPUT", "VECTOR"), socket("Offset Scale", "Offset Scale", "INPUT", "FLOAT"), socket("Individual", "Individual", "INPUT", "BOOLEAN"), socket("Mesh", "Mesh", "OUTPUT", "GEOMETRY"), socket("Top", "Top", "OUTPUT", "BOOLEAN"), socket("Side", "Side", "OUTPUT", "BOOLEAN")]; if (type === "GeometryNodeTriangulate") return [socket("Mesh", "Mesh", "INPUT", "GEOMETRY"), socket("Selection", "Selection", "INPUT", "BOOLEAN"), socket("Minimum Vertices", "Minimum Vertices", "INPUT", "INT"), socket("Mesh", "Mesh", "OUTPUT", "GEOMETRY")]; return []; }; function uniqueId(base: string, ids: ReadonlySet): string { let id = base; let suffix = 2; while (ids.has(id)) id = `${base}-${suffix++}`; return id; } /** Workbench constructors for validated Geometry Node graph transactions. */ export const geometryNodeCommands = Object.freeze({ createGraph(meshId: string, name = "Web Geometry") : GeometryNodeGraphIR { const input = { id: "group-input", type: "NodeGroupInput", name: "Group Input", sockets: [socket("GeometryInput", "Geometry", "OUTPUT", "GEOMETRY")], location: { x: 20, y: 80 } }; const output = { id: "group-output", type: "NodeGroupOutput", name: "Group Output", sockets: [socket("GeometryOutput", "Geometry", "INPUT", "GEOMETRY")], location: { x: 620, y: 80 } }; return { schemaVersion: GEOMETRY_NODE_GRAPH_SCHEMA, id: `geometry-graph:${meshId}`, name, interfaceInputs: [socket("GeometryInput", "Geometry", "INPUT", "GEOMETRY")], interfaceOutputs: [socket("GeometryOutput", "Geometry", "OUTPUT", "GEOMETRY")], nodes: [input, output], links: [{ fromNodeId: input.id, fromSocketId: "GeometryInput", toNodeId: output.id, toSocketId: "GeometryOutput" }] }; }, addNode(graph: GeometryNodeGraphIR, type: string, name = type, location?: { x: number; y: number }): GeometryNodeGraphIR { const next = clone(graph); const ids = new Set(next.nodes.map((node) => node.id)); const id = uniqueId(`node-${type.replace(/[^a-z0-9]+/gi, "-").toLowerCase()}`, ids); const sockets = nodeSockets(type).map((item, index, all) => all.some((other, otherIndex) => otherIndex !== index && other.id === item.id) ? { ...item, id: `${item.direction.toLowerCase()}:${item.id}` } : item); const node: GeometryNodeIR = { id, type, name: name.trim() || type, sockets, location }; next.nodes.push(node); return next; }, moveNode(graph: GeometryNodeGraphIR, nodeId: string, location: { x: number; y: number }): GeometryNodeGraphIR { const next = clone(graph); const node = next.nodes.find((candidate) => candidate.id === nodeId); if (node) node.location = { x: location.x, y: location.y }; return next; }, removeNode(graph: GeometryNodeGraphIR, nodeId: string): GeometryNodeGraphIR { const next = clone(graph); next.nodes = next.nodes.filter((node) => node.id !== nodeId); next.links = next.links.filter((link) => link.fromNodeId !== nodeId && link.toNodeId !== nodeId); return next; }, connect(graph: GeometryNodeGraphIR, link: GeometryNodeLinkIR): GeometryNodeGraphIR { const next = clone(graph); next.links = next.links.filter((item) => !(item.toNodeId === link.toNodeId && item.toSocketId === link.toSocketId)); if (!next.links.some((item) => item.fromNodeId === link.fromNodeId && item.fromSocketId === link.fromSocketId && item.toNodeId === link.toNodeId && item.toSocketId === link.toSocketId)) next.links.push(link); return next; }, disconnect(graph: GeometryNodeGraphIR, link: GeometryNodeLinkIR): GeometryNodeGraphIR { const next = clone(graph); next.links = next.links.filter((item) => !(item.fromNodeId === link.fromNodeId && item.fromSocketId === link.fromSocketId && item.toNodeId === link.toNodeId && item.toSocketId === link.toSocketId)); return next; }, setSocketValue(graph: GeometryNodeGraphIR, nodeId: string, socketId: string, value: GeometryNodeSocketIR["defaultValue"]): GeometryNodeGraphIR { const next = clone(graph); const node = next.nodes.find((candidate) => candidate.id === nodeId); const target = node?.sockets.find((candidate) => candidate.id === socketId && candidate.direction === "INPUT"); if (target) target.defaultValue = value; return next; }, setGraph(meshId: string, graph: GeometryNodeGraphIR): Extract { return { type: "setGeometryNodeGraph", meshId, graph }; }, } as const);