Files
workinf_Blender_Wasm/web/protocol/geometry-node-commands.ts
mes123456 d0b6d2a164 feat(web): close remaining editor, layout and persistence gaps
- Area headers get a per-area editor switcher backed by a shared
  AreaEditorContext; Preferences can hide it.
- Animation camera area is a real navigable viewport aligned to the
  scene camera; Graph Editor adds channel selection, keyframe drag/
  insert/delete and interpolation.
- Geometry Nodes node locations round-trip through the native writer
  and reader, so saved graphs keep their layout.
- Texture Paint can make linked library images local via a new native
  makeImageLocal command before packing strokes.
- Compositor IMAGE and Render Layers nodes now persist to Main and
  execute against packed images / the last PBR render.
- Spreadsheet row cap raised to 250k; Outliner gains Ctrl+A and arrow/
  Home/End navigation.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-13 07:47:10 +08:00

80 lines
6.0 KiB
TypeScript

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 = <T,>(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")];
return [];
};
function uniqueId(base: string, ids: ReadonlySet<string>): 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<WebEngineEditCommand, { type: "setGeometryNodeGraph" }> {
return { type: "setGeometryNodeGraph", meshId, graph };
},
} as const);