feat: initial group nodes /w some bugs
This commit is contained in:
@@ -183,7 +183,7 @@
|
||||
activeNodeId = node.id;
|
||||
}}
|
||||
>
|
||||
{node.id.split('/').at(-1)}
|
||||
{node.meta?.title ?? node.id.split('/').at(-1)}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
@@ -3,11 +3,14 @@ import { RemoteNodeRegistry } from '$lib/node-registry/index';
|
||||
import type {
|
||||
Edge,
|
||||
Graph,
|
||||
GroupSocket,
|
||||
NodeDefinition,
|
||||
NodeGroupDefinition,
|
||||
NodeId,
|
||||
NodeInput,
|
||||
NodeInstance,
|
||||
NodeRegistry,
|
||||
SerializedNode,
|
||||
Socket
|
||||
} from '@nodarium/types';
|
||||
import { fastHashString } from '@nodarium/utils';
|
||||
@@ -56,6 +59,14 @@ function areEdgesEqual(firstEdge: Edge, secondEdge: Edge) {
|
||||
return true;
|
||||
}
|
||||
|
||||
function isVirtualType(type: string): boolean {
|
||||
return type.startsWith('__virtual/');
|
||||
}
|
||||
|
||||
function isGroupInstanceType(type: string): boolean {
|
||||
return type === '__virtual/group/instance';
|
||||
}
|
||||
|
||||
export class GraphManager extends EventEmitter<{
|
||||
save: Graph;
|
||||
result: unknown;
|
||||
@@ -79,6 +90,12 @@ export class GraphManager extends EventEmitter<{
|
||||
|
||||
currentUndoGroup: number | null = null;
|
||||
|
||||
// Group-related state
|
||||
groups: Map<string, NodeGroupDefinition> = new Map();
|
||||
groupNodeDefinitions: Map<string, NodeDefinition> = new Map();
|
||||
currentGroupContext: string | null = null;
|
||||
graphStack: { rootGraph: Graph; groupId: string; cameraPosition: [number, number, number] }[] = $state([]);
|
||||
|
||||
inputSockets = $derived.by(() => {
|
||||
const s = new SvelteSet<string>();
|
||||
for (const edge of this.edges) {
|
||||
@@ -88,37 +105,523 @@ export class GraphManager extends EventEmitter<{
|
||||
});
|
||||
|
||||
history: HistoryManager = new HistoryManager();
|
||||
private serializeFullGraph(): Graph {
|
||||
if (this.graphStack.length === 0) return this.serialize();
|
||||
// Merge the current internal state upward through every stack level.
|
||||
// $state.snapshot strips Svelte reactive proxies so the result can cross
|
||||
// the postMessage boundary to the worker.
|
||||
let merged: Graph = this.serialize();
|
||||
for (let i = this.graphStack.length - 1; i >= 0; i--) {
|
||||
const { rootGraph, groupId } = $state.snapshot(this.graphStack[i]);
|
||||
merged = {
|
||||
...rootGraph,
|
||||
groups: {
|
||||
...rootGraph.groups,
|
||||
[groupId]: {
|
||||
...rootGraph.groups?.[groupId]!,
|
||||
graph: { nodes: merged.nodes, edges: merged.edges }
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
|
||||
execute = throttle(() => {
|
||||
if (this.loaded === false) return;
|
||||
this.emit('result', this.serialize());
|
||||
this.emit('result', this.serializeFullGraph());
|
||||
}, 10);
|
||||
|
||||
constructor(public registry: NodeRegistry) {
|
||||
super();
|
||||
}
|
||||
|
||||
// --- Group helpers ---
|
||||
|
||||
private buildGroupNodeDefinition(group: NodeGroupDefinition): NodeDefinition {
|
||||
return {
|
||||
id: `__virtual/group/${group.id}` as NodeId,
|
||||
meta: { title: group.name },
|
||||
inputs: Object.fromEntries(
|
||||
group.inputs.map(s => [s.name, { type: s.type, external: true } as NodeInput])
|
||||
),
|
||||
outputs: group.outputs.map(s => s.type),
|
||||
execute(input: Int32Array): Int32Array { return input; }
|
||||
};
|
||||
}
|
||||
|
||||
buildGroupInputNodeDef(group: NodeGroupDefinition): NodeDefinition {
|
||||
return {
|
||||
id: '__virtual/group/input' as NodeId,
|
||||
inputs: {},
|
||||
outputs: group.inputs.map(s => s.type),
|
||||
execute(input: Int32Array): Int32Array { return input; }
|
||||
};
|
||||
}
|
||||
|
||||
buildGroupOutputNodeDef(group: NodeGroupDefinition): NodeDefinition {
|
||||
return {
|
||||
id: '__virtual/group/output' as NodeId,
|
||||
inputs: Object.fromEntries(
|
||||
group.outputs.map(s => [s.name, { type: s.type }])
|
||||
) as Record<string, NodeInput>,
|
||||
outputs: [],
|
||||
execute(input: Int32Array): Int32Array { return input; }
|
||||
};
|
||||
}
|
||||
|
||||
private getNodeTypeWithContext(type: string, props?: Record<string, unknown>): NodeDefinition | undefined {
|
||||
if (type === '__virtual/group/input' && this.currentGroupContext) {
|
||||
const group = this.groups.get(this.currentGroupContext);
|
||||
if (group) return this.buildGroupInputNodeDef(group);
|
||||
}
|
||||
if (type === '__virtual/group/output' && this.currentGroupContext) {
|
||||
const group = this.groups.get(this.currentGroupContext);
|
||||
if (group) return this.buildGroupOutputNodeDef(group);
|
||||
}
|
||||
if (type === '__virtual/group/instance') {
|
||||
const groupId = props?.groupId as string | undefined;
|
||||
if (groupId) return this.groupNodeDefinitions.get(`__virtual/group/${groupId}`);
|
||||
return undefined;
|
||||
}
|
||||
return this.groupNodeDefinitions.get(type) || this.registry.getNode(type);
|
||||
}
|
||||
|
||||
// --- Group creation ---
|
||||
|
||||
createGroup(nodeIds: number[]): NodeInstance | undefined {
|
||||
if (nodeIds.length === 0) return;
|
||||
|
||||
const selectedNodes = nodeIds
|
||||
.map(id => this.getNode(id))
|
||||
.filter(Boolean) as NodeInstance[];
|
||||
if (selectedNodes.length === 0) return;
|
||||
|
||||
const selectedSet = new Set(nodeIds);
|
||||
|
||||
// Snapshot boundary edges
|
||||
const incomingEdges = this.edges.filter(e =>
|
||||
!selectedSet.has(e[0].id) && selectedSet.has(e[2].id)
|
||||
);
|
||||
const outgoingEdges = this.edges.filter(e =>
|
||||
selectedSet.has(e[0].id) && !selectedSet.has(e[2].id)
|
||||
);
|
||||
|
||||
const inputs: GroupSocket[] = incomingEdges.map((e, i) => ({
|
||||
name: `input_${i}`,
|
||||
type: e[0].state.type?.outputs?.[e[1]] || '*'
|
||||
}));
|
||||
|
||||
const outputs: GroupSocket[] = outgoingEdges.map((e, i) => ({
|
||||
name: `output_${i}`,
|
||||
type: e[0].state.type?.outputs?.[e[1]] || '*'
|
||||
}));
|
||||
|
||||
const groupId = `grp_${Date.now().toString(36)}`;
|
||||
|
||||
const xs = selectedNodes.map(n => n.position[0]);
|
||||
const ys = selectedNodes.map(n => n.position[1]);
|
||||
const minX = Math.min(...xs);
|
||||
const maxX = Math.max(...xs);
|
||||
const avgY = ys.reduce((a, b) => a + b, 0) / ys.length;
|
||||
const centroidX = xs.reduce((a, b) => a + b, 0) / xs.length;
|
||||
|
||||
// Find unique IDs for virtual nodes in the internal graph
|
||||
const existingIds = new Set(selectedNodes.map(n => n.id));
|
||||
let internalInputId = 1;
|
||||
while (existingIds.has(internalInputId)) internalInputId++;
|
||||
existingIds.add(internalInputId);
|
||||
let internalOutputId = internalInputId + 1;
|
||||
while (existingIds.has(internalOutputId)) internalOutputId++;
|
||||
|
||||
const internalNodes: SerializedNode[] = [
|
||||
{
|
||||
id: internalInputId,
|
||||
type: '__virtual/group/input' as NodeId,
|
||||
position: [minX - 25, avgY]
|
||||
},
|
||||
...selectedNodes.map(n => {
|
||||
// Use $state.snapshot to get plain values (no reactive proxies)
|
||||
const props = n.props ? $state.snapshot(n.props) : undefined;
|
||||
const meta = n.meta ? $state.snapshot(n.meta) : undefined;
|
||||
return {
|
||||
id: n.id,
|
||||
type: n.type,
|
||||
position: [n.position[0], n.position[1]] as [number, number],
|
||||
...(props !== undefined ? { props } : {}),
|
||||
...(meta ? { meta } : {})
|
||||
};
|
||||
}),
|
||||
{
|
||||
id: internalOutputId,
|
||||
type: '__virtual/group/output' as NodeId,
|
||||
position: [maxX + 25, avgY]
|
||||
}
|
||||
];
|
||||
|
||||
const internalEdges: Graph['edges'] = [
|
||||
...this.getEdgesBetweenNodes(selectedNodes),
|
||||
...incomingEdges.map((e, i) =>
|
||||
[internalInputId, i, e[2].id, e[3]] as [number, number, number, string]
|
||||
),
|
||||
...outgoingEdges.map((e, i) =>
|
||||
[e[0].id, e[1], internalOutputId, `output_${i}`] as [number, number, number, string]
|
||||
)
|
||||
];
|
||||
|
||||
const group: NodeGroupDefinition = {
|
||||
id: groupId,
|
||||
name: 'Group',
|
||||
inputs,
|
||||
outputs,
|
||||
graph: { nodes: internalNodes, edges: internalEdges }
|
||||
};
|
||||
|
||||
this.groups.set(groupId, group);
|
||||
if (!this.graph.groups) this.graph.groups = {};
|
||||
this.graph.groups[groupId] = group;
|
||||
|
||||
const groupNodeDef = this.buildGroupNodeDefinition(group);
|
||||
this.groupNodeDefinitions.set(groupNodeDef.id, groupNodeDef);
|
||||
|
||||
this.startUndoGroup();
|
||||
|
||||
// Remove selected nodes and all their edges
|
||||
for (const node of selectedNodes) {
|
||||
const connectedEdges = this.edges.filter(
|
||||
e => e[0].id === node.id || e[2].id === node.id
|
||||
);
|
||||
for (const e of connectedEdges) {
|
||||
this.removeEdge(e, { applyDeletion: false });
|
||||
}
|
||||
this.nodes.delete(node.id);
|
||||
}
|
||||
|
||||
// Place group instance node (plain object like _init — don't wrap in $state()
|
||||
// to avoid Svelte 5 deeply-proxying the NodeDefinition execute function)
|
||||
const groupNodeId = this.createNodeId();
|
||||
const groupNode = {
|
||||
id: groupNodeId,
|
||||
type: '__virtual/group/instance' as NodeId,
|
||||
position: [centroidX, avgY] as [number, number],
|
||||
props: { groupId },
|
||||
state: { type: groupNodeDef }
|
||||
} as NodeInstance;
|
||||
this.nodes.set(groupNodeId, groupNode);
|
||||
|
||||
// Reconnect boundary edges
|
||||
for (let i = 0; i < incomingEdges.length; i++) {
|
||||
const e = incomingEdges[i];
|
||||
this.createEdge(e[0], e[1], groupNode, inputs[i].name, { applyUpdate: false });
|
||||
}
|
||||
for (let i = 0; i < outgoingEdges.length; i++) {
|
||||
const e = outgoingEdges[i];
|
||||
this.createEdge(groupNode, i, e[2], e[3], { applyUpdate: false });
|
||||
}
|
||||
|
||||
this.saveUndoGroup();
|
||||
this.execute();
|
||||
|
||||
return groupNode;
|
||||
}
|
||||
|
||||
// --- Ungrouping ---
|
||||
|
||||
ungroup(nodeId: number) {
|
||||
const groupNode = this.getNode(nodeId);
|
||||
if (!groupNode || !isGroupInstanceType(groupNode.type)) return;
|
||||
|
||||
const groupId = groupNode.props?.groupId as string | undefined;
|
||||
if (!groupId) return;
|
||||
const group = this.groups.get(groupId);
|
||||
if (!group) return;
|
||||
|
||||
const incomingEdges = this.getEdgesToNode(groupNode);
|
||||
const outgoingEdges = this.getEdgesFromNode(groupNode);
|
||||
|
||||
const inputVirtualId = group.graph.nodes.find(
|
||||
n => n.type === '__virtual/group/input'
|
||||
)?.id;
|
||||
const outputVirtualId = group.graph.nodes.find(
|
||||
n => n.type === '__virtual/group/output'
|
||||
)?.id;
|
||||
|
||||
this.startUndoGroup();
|
||||
|
||||
// Remove the group instance node (and its edges)
|
||||
this.removeNode(groupNode, { restoreEdges: false });
|
||||
|
||||
// Re-insert internal nodes
|
||||
const idMap = new Map<number, number>();
|
||||
const realInternalNodes = group.graph.nodes.filter(
|
||||
n => n.type !== '__virtual/group/input' && n.type !== '__virtual/group/output'
|
||||
);
|
||||
|
||||
for (const n of realInternalNodes) {
|
||||
const newId = this.createNodeId();
|
||||
idMap.set(n.id, newId);
|
||||
const nodeType = this.getNodeTypeWithContext(n.type, n.props as Record<string, unknown>);
|
||||
const newNode: NodeInstance = $state({
|
||||
id: newId,
|
||||
type: n.type,
|
||||
position: [...n.position] as [number, number],
|
||||
...(n.props ? { props: { ...n.props } } : {}),
|
||||
state: nodeType ? { type: nodeType } : {}
|
||||
});
|
||||
this.nodes.set(newId, newNode);
|
||||
}
|
||||
|
||||
// Re-wire edges
|
||||
for (const e of group.graph.edges) {
|
||||
const fromIsInput = e[0] === inputVirtualId;
|
||||
const toIsOutput = e[2] === outputVirtualId;
|
||||
|
||||
if (fromIsInput) {
|
||||
const inputIdx = e[1];
|
||||
const parentEdge = incomingEdges.find(
|
||||
pe => pe[3] === group.inputs[inputIdx]?.name
|
||||
);
|
||||
if (parentEdge) {
|
||||
const toNode = this.getNode(idMap.get(e[2])!);
|
||||
if (toNode) {
|
||||
this.createEdge(parentEdge[0], parentEdge[1], toNode, e[3], {
|
||||
applyUpdate: false
|
||||
});
|
||||
}
|
||||
}
|
||||
} else if (toIsOutput) {
|
||||
const outputSocketName = e[3];
|
||||
const outputIdx = group.outputs.findIndex(s => s.name === outputSocketName);
|
||||
const parentEdge = outgoingEdges.find(pe => pe[1] === outputIdx);
|
||||
if (parentEdge) {
|
||||
const fromNode = this.getNode(idMap.get(e[0])!);
|
||||
const toNode = this.getNode(parentEdge[2].id);
|
||||
if (fromNode && toNode) {
|
||||
this.createEdge(fromNode, e[1], toNode, parentEdge[3], {
|
||||
applyUpdate: false
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const fromNode = this.getNode(idMap.get(e[0])!);
|
||||
const toNode = this.getNode(idMap.get(e[2])!);
|
||||
if (fromNode && toNode) {
|
||||
this.createEdge(fromNode, e[1], toNode, e[3], { applyUpdate: false });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove group definition if no more instances
|
||||
const hasOtherInstances = Array.from(this.nodes.values()).some(
|
||||
n => n.type === '__virtual/group/instance' && (n.props?.groupId as string) === groupId
|
||||
);
|
||||
if (!hasOtherInstances) {
|
||||
this.groups.delete(groupId);
|
||||
this.groupNodeDefinitions.delete(`__virtual/group/${groupId}`);
|
||||
if (this.graph.groups) {
|
||||
delete this.graph.groups[groupId];
|
||||
}
|
||||
}
|
||||
|
||||
this.saveUndoGroup();
|
||||
this.execute();
|
||||
}
|
||||
|
||||
// --- Group socket management (called from inside a group) ---
|
||||
|
||||
addGroupSocket(kind: 'input' | 'output', socketType: string) {
|
||||
if (!this.currentGroupContext) return;
|
||||
const group = this.groups.get(this.currentGroupContext);
|
||||
if (!group) return;
|
||||
|
||||
const arr = kind === 'input' ? group.inputs : group.outputs;
|
||||
const name = `${kind}_${arr.length}`;
|
||||
arr.push({ name, type: socketType });
|
||||
|
||||
this._refreshGroupContext(group);
|
||||
this.save();
|
||||
}
|
||||
|
||||
removeGroupSocket(kind: 'input' | 'output', index: number) {
|
||||
if (!this.currentGroupContext) return;
|
||||
const group = this.groups.get(this.currentGroupContext);
|
||||
if (!group) return;
|
||||
|
||||
const arr = kind === 'input' ? group.inputs : group.outputs;
|
||||
arr.splice(index, 1);
|
||||
|
||||
this._refreshGroupContext(group);
|
||||
this.save();
|
||||
}
|
||||
|
||||
private _refreshGroupContext(group: NodeGroupDefinition) {
|
||||
const groupId = group.id;
|
||||
|
||||
// Keep graph.groups in sync
|
||||
if (this.graph.groups?.[groupId]) {
|
||||
this.graph.groups[groupId] = group;
|
||||
}
|
||||
|
||||
// Rebuild the group node definition (used in parent graph)
|
||||
const groupNodeDef = this.buildGroupNodeDefinition(group);
|
||||
this.groupNodeDefinitions.set(groupNodeDef.id, groupNodeDef);
|
||||
|
||||
// Update virtual input/output nodes in the current internal graph,
|
||||
// and any group instance nodes that reference this group
|
||||
const inputDef = this.buildGroupInputNodeDef(group);
|
||||
const outputDef = this.buildGroupOutputNodeDef(group);
|
||||
for (const node of this.nodes.values()) {
|
||||
if (node.type === '__virtual/group/input') node.state.type = inputDef;
|
||||
if (node.type === '__virtual/group/output') node.state.type = outputDef;
|
||||
if (node.type === '__virtual/group/instance' && (node.props?.groupId as string) === groupId) {
|
||||
node.state.type = groupNodeDef;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Group navigation ---
|
||||
|
||||
enterGroup(nodeId: number, cameraPosition: [number, number, number] = [0, 0, 4]): boolean {
|
||||
const groupNode = this.getNode(nodeId);
|
||||
if (!groupNode || !isGroupInstanceType(groupNode.type)) return false;
|
||||
|
||||
const groupId = groupNode.props?.groupId as string | undefined;
|
||||
if (!groupId) return false;
|
||||
const group = this.groups.get(groupId);
|
||||
if (!group) return false;
|
||||
|
||||
const currentSerialized = this.serialize();
|
||||
this.graphStack.push({ rootGraph: currentSerialized, groupId, cameraPosition });
|
||||
|
||||
this.currentGroupContext = groupId;
|
||||
|
||||
const internalGraph: Graph = {
|
||||
id: this.graph.id,
|
||||
nodes: group.graph.nodes,
|
||||
edges: group.graph.edges,
|
||||
groups: this.graph.groups
|
||||
};
|
||||
|
||||
this.graph = internalGraph;
|
||||
this._init(internalGraph);
|
||||
this.history.reset();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
exitGroup(): [number, number, number] | false {
|
||||
if (this.graphStack.length === 0) return false;
|
||||
|
||||
const { rootGraph, groupId, cameraPosition } = this.graphStack[this.graphStack.length - 1];
|
||||
this.graphStack.pop();
|
||||
|
||||
// Serialize current internal graph state
|
||||
const internalState = this.serialize();
|
||||
|
||||
// Update the group definition in the root graph
|
||||
const updatedRootGraph: Graph = {
|
||||
...rootGraph,
|
||||
groups: {
|
||||
...rootGraph.groups,
|
||||
[groupId]: {
|
||||
...rootGraph.groups?.[groupId]!,
|
||||
graph: {
|
||||
nodes: internalState.nodes,
|
||||
edges: internalState.edges
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.currentGroupContext = this.graphStack.length > 0
|
||||
? this.graphStack[this.graphStack.length - 1].groupId
|
||||
: null;
|
||||
|
||||
this.graph = updatedRootGraph;
|
||||
this._init(updatedRootGraph);
|
||||
this.history.reset();
|
||||
this.save();
|
||||
|
||||
return cameraPosition;
|
||||
}
|
||||
|
||||
get isInsideGroup(): boolean {
|
||||
return this.graphStack.length > 0;
|
||||
}
|
||||
|
||||
get breadcrumbs(): { name: string; groupId: string | null }[] {
|
||||
const crumbs: { name: string; groupId: string | null }[] = [
|
||||
{ name: 'Root', groupId: null }
|
||||
];
|
||||
for (const entry of this.graphStack) {
|
||||
const group = this.groups.get(entry.groupId);
|
||||
crumbs.push({ name: group?.name ?? entry.groupId, groupId: entry.groupId });
|
||||
}
|
||||
return crumbs;
|
||||
}
|
||||
|
||||
// --- Serialization ---
|
||||
|
||||
private serializeGroups(): Graph['groups'] | undefined {
|
||||
const src = this.graph.groups;
|
||||
if (!src || Object.keys(src).length === 0) return undefined;
|
||||
const result: NonNullable<Graph['groups']> = {};
|
||||
for (const [id, group] of Object.entries(src)) {
|
||||
result[id] = {
|
||||
id: group.id,
|
||||
name: group.name,
|
||||
inputs: group.inputs.map(s => ({ name: s.name, type: s.type })),
|
||||
outputs: group.outputs.map(s => ({ name: s.name, type: s.type })),
|
||||
graph: {
|
||||
nodes: group.graph.nodes.map(n => ({
|
||||
id: n.id,
|
||||
type: n.type,
|
||||
position: [n.position[0], n.position[1]] as [number, number],
|
||||
...(n.props !== undefined ? {
|
||||
props: Object.fromEntries(
|
||||
Object.entries(n.props).map(([k, v]) => [
|
||||
k,
|
||||
Array.isArray(v) ? [...v] : v
|
||||
])
|
||||
)
|
||||
} : {}),
|
||||
...(n.meta ? { meta: { title: n.meta.title, lastModified: n.meta.lastModified } } : {})
|
||||
})),
|
||||
edges: group.graph.edges.map(
|
||||
e => [e[0], e[1], e[2], e[3]] as [number, number, number, string]
|
||||
)
|
||||
}
|
||||
};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
serialize(): Graph {
|
||||
const nodes = Array.from(this.nodes.values()).map((node) => ({
|
||||
id: node.id,
|
||||
position: [...node.position],
|
||||
position: [...node.position] as [number, number],
|
||||
type: node.type,
|
||||
props: node.props
|
||||
})) as NodeInstance[];
|
||||
props: node.props ? $state.snapshot(node.props) : undefined
|
||||
}));
|
||||
const edges = this.edges.map((edge) => [
|
||||
edge[0].id,
|
||||
edge[1],
|
||||
edge[2].id,
|
||||
edge[3]
|
||||
]) as Graph['edges'];
|
||||
|
||||
const groups = this.serializeGroups();
|
||||
|
||||
const serialized = {
|
||||
id: this.graph.id,
|
||||
settings: $state.snapshot(this.settings),
|
||||
meta: $state.snapshot(this.graph.meta),
|
||||
nodes,
|
||||
edges
|
||||
edges,
|
||||
...(groups ? { groups } : {})
|
||||
};
|
||||
logger.log('serializing graph', serialized);
|
||||
return clone($state.snapshot(serialized));
|
||||
return clone(serialized) as Graph;
|
||||
}
|
||||
|
||||
private lastSettingsHash = 0;
|
||||
@@ -133,7 +636,12 @@ export class GraphManager extends EventEmitter<{
|
||||
}
|
||||
|
||||
getNodeDefinitions() {
|
||||
return this.registry.getAllNodes();
|
||||
const all = this.registry.getAllNodes();
|
||||
// Only show the Group node in AddMenu when there's at least one group to assign
|
||||
if (this.groups.size === 0) {
|
||||
return all.filter(n => n.id !== '__virtual/group/instance');
|
||||
}
|
||||
return all;
|
||||
}
|
||||
|
||||
getLinkedNodes(node: NodeInstance) {
|
||||
@@ -209,19 +717,14 @@ export class GraphManager extends EventEmitter<{
|
||||
const draggedInputs = Object.values(draggedNode.state.type.inputs ?? {});
|
||||
const draggedOutputs = draggedNode.state.type.outputs ?? [];
|
||||
|
||||
// Optimization: Pre-calculate parents to avoid cycles
|
||||
const parentIds = new SvelteSet(this.getParentsOfNode(draggedNode).map(n => n.id));
|
||||
|
||||
return this.edges.filter((edge) => {
|
||||
const [fromNode, fromSocketIdx, toNode, toSocketKey] = edge;
|
||||
|
||||
// 1. Prevent cycles: If the target node is already a parent, we can't drop here
|
||||
if (parentIds.has(toNode.id)) return false;
|
||||
|
||||
// 2. Prevent self-dropping: Don't drop on edges already connected to this node
|
||||
if (fromNode.id === nodeId || toNode.id === nodeId) return false;
|
||||
|
||||
// 3. Check if edge.source can plug into ANY draggedNode.input
|
||||
const edgeOutputSocketType = fromNode.state?.type?.outputs?.[fromSocketIdx];
|
||||
const canPlugIntoDragged = draggedInputs.some(input => {
|
||||
const acceptedTypes = [input.type, ...(input.accepts || [])];
|
||||
@@ -230,7 +733,6 @@ export class GraphManager extends EventEmitter<{
|
||||
|
||||
if (!canPlugIntoDragged) return false;
|
||||
|
||||
// 4. Check if ANY draggedNode.output can plug into edge.target
|
||||
const targetInput = toNode.state?.type?.inputs?.[toSocketKey];
|
||||
const targetAcceptedTypes = [targetInput?.type, ...(targetInput?.accepts || [])];
|
||||
|
||||
@@ -267,15 +769,35 @@ export class GraphManager extends EventEmitter<{
|
||||
}
|
||||
|
||||
private _init(graph: Graph) {
|
||||
const nodes = new SvelteMap(
|
||||
graph.nodes.map((node) => {
|
||||
const nodeType = this.registry.getNode(node.type);
|
||||
const n = node as NodeInstance;
|
||||
if (nodeType) {
|
||||
n.state = {
|
||||
type: nodeType
|
||||
};
|
||||
// Rebuild group definitions from the graph
|
||||
this.groups.clear();
|
||||
this.groupNodeDefinitions.clear();
|
||||
if (graph.groups) {
|
||||
for (const [groupId, group] of Object.entries(graph.groups)) {
|
||||
this.groups.set(groupId, group);
|
||||
const def = this.buildGroupNodeDefinition(group);
|
||||
this.groupNodeDefinitions.set(def.id, def);
|
||||
}
|
||||
}
|
||||
|
||||
const nodes = new SvelteMap(
|
||||
graph.nodes.map((serialized) => {
|
||||
// Migration: old __virtual/group/{groupId} format → __virtual/group/instance with props.groupId
|
||||
let node = serialized;
|
||||
if (node.type.startsWith('__virtual/group/')
|
||||
&& node.type !== '__virtual/group/input'
|
||||
&& node.type !== '__virtual/group/output'
|
||||
&& node.type !== '__virtual/group/instance') {
|
||||
const oldGroupId = node.type.split('/')[2];
|
||||
node = { ...node, type: '__virtual/group/instance' as NodeId, props: { ...node.props, groupId: oldGroupId } };
|
||||
}
|
||||
|
||||
// IMPORTANT: copy the node so we don't mutate the original SerializedNode
|
||||
// (which may be stored in a group definition). Mutating it would add
|
||||
// state.type (with an execute fn) making it non-cloneable.
|
||||
const nodeType = this.getNodeTypeWithContext(node.type, node.props as Record<string, unknown>);
|
||||
const n = { ...node } as NodeInstance;
|
||||
n.state = nodeType ? { type: nodeType } : {};
|
||||
return [node.id, n];
|
||||
})
|
||||
);
|
||||
@@ -311,7 +833,10 @@ export class GraphManager extends EventEmitter<{
|
||||
|
||||
logger.info('loading graph', { nodes: graph.nodes, edges: graph.edges, id: graph.id });
|
||||
|
||||
const nodeIds = Array.from(new SvelteSet([...graph.nodes.map((n) => n.type)]));
|
||||
// Filter out virtual group types — they are resolved locally, not fetched remotely
|
||||
const nodeIds = Array.from(new SvelteSet([
|
||||
...graph.nodes.map((n) => n.type).filter(t => !isVirtualType(t))
|
||||
]));
|
||||
await this.registry.load(nodeIds);
|
||||
|
||||
// Fetch all nodes from all collections of the loaded nodes
|
||||
@@ -332,13 +857,13 @@ export class GraphManager extends EventEmitter<{
|
||||
logger.info('loaded node types', this.registry.getAllNodes());
|
||||
|
||||
for (const node of this.graph.nodes) {
|
||||
if (isVirtualType(node.type)) continue;
|
||||
const nodeType = this.registry.getNode(node.type);
|
||||
if (!nodeType) {
|
||||
logger.error(`Node type not found: ${node.type}`);
|
||||
this.status = 'error';
|
||||
return;
|
||||
}
|
||||
// Turn into runtime node
|
||||
const n = node as NodeInstance;
|
||||
n.state = {};
|
||||
n.state.type = nodeType;
|
||||
@@ -347,7 +872,6 @@ export class GraphManager extends EventEmitter<{
|
||||
// load settings
|
||||
const settingTypes: Record<
|
||||
string,
|
||||
// Optional metadata to map settings to specific nodes
|
||||
NodeInput & { __node_type: string; __node_input: string }
|
||||
> = {};
|
||||
const settingValues = graph.settings || {};
|
||||
@@ -376,6 +900,10 @@ export class GraphManager extends EventEmitter<{
|
||||
this.settings = settingValues;
|
||||
this.emit('settings', { types: settingTypes, values: settingValues });
|
||||
|
||||
// Reset navigation
|
||||
this.graphStack = [];
|
||||
this.currentGroupContext = null;
|
||||
|
||||
this.history.reset();
|
||||
this._init(this.graph);
|
||||
|
||||
@@ -442,9 +970,7 @@ export class GraphManager extends EventEmitter<{
|
||||
}
|
||||
|
||||
getNodesBetween(from: NodeInstance, to: NodeInstance): NodeInstance[] | undefined {
|
||||
// < - - - - from
|
||||
const toParents = this.getParentsOfNode(to);
|
||||
// < - - - - from - - - - to
|
||||
const fromParents = this.getParentsOfNode(from);
|
||||
if (toParents.includes(from)) {
|
||||
const fromChildren = this.getChildren(from);
|
||||
@@ -453,7 +979,6 @@ export class GraphManager extends EventEmitter<{
|
||||
const toChildren = this.getChildren(to);
|
||||
return fromParents.filter((n) => toChildren.includes(n));
|
||||
} else {
|
||||
// these two nodes are not connected
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -507,7 +1032,6 @@ export class GraphManager extends EventEmitter<{
|
||||
}
|
||||
|
||||
createGraph(nodes: NodeInstance[], edges: [number, number, number, string][]) {
|
||||
// map old ids to new ids
|
||||
const idMap = new SvelteMap<number, number>();
|
||||
|
||||
let startId = this.createNodeId();
|
||||
@@ -558,6 +1082,26 @@ export class GraphManager extends EventEmitter<{
|
||||
position: NodeInstance['position'];
|
||||
props: NodeInstance['props'];
|
||||
}) {
|
||||
if (type === '__virtual/group/instance') {
|
||||
const firstEntry = this.groups.entries().next();
|
||||
if (firstEntry.done) {
|
||||
logger.error('No groups available to create a group node');
|
||||
return;
|
||||
}
|
||||
const [groupId] = firstEntry.value;
|
||||
const groupNodeDef = this.groupNodeDefinitions.get(`__virtual/group/${groupId}`);
|
||||
const node = {
|
||||
id: this.createNodeId(),
|
||||
type: '__virtual/group/instance' as NodeId,
|
||||
position,
|
||||
props: { groupId, ...props },
|
||||
state: { type: groupNodeDef }
|
||||
} as NodeInstance;
|
||||
this.nodes.set(node.id, node);
|
||||
this.save();
|
||||
return node;
|
||||
}
|
||||
|
||||
const nodeType = this.registry.getNode(type);
|
||||
if (!nodeType) {
|
||||
logger.error(`Node type not found: ${type}`);
|
||||
@@ -588,7 +1132,6 @@ export class GraphManager extends EventEmitter<{
|
||||
): Edge | undefined {
|
||||
const existingEdges = this.getEdgesToNode(to);
|
||||
|
||||
// check if this exact edge already exists
|
||||
const existingEdge = existingEdges.find(
|
||||
(e) => e[0].id === from.id && e[1] === fromSocket && e[3] === toSocket
|
||||
);
|
||||
@@ -597,7 +1140,6 @@ export class GraphManager extends EventEmitter<{
|
||||
return;
|
||||
}
|
||||
|
||||
// check if socket types match
|
||||
const fromSocketType = from.state?.type?.outputs?.[fromSocket];
|
||||
const toSocketType = [to.state?.type?.inputs?.[toSocket]?.type];
|
||||
if (to.state?.type?.inputs?.[toSocket]?.accepts) {
|
||||
@@ -665,12 +1207,13 @@ export class GraphManager extends EventEmitter<{
|
||||
const state = this.serialize();
|
||||
this.history.save(state);
|
||||
|
||||
// This is some stupid race condition where the graph-manager emits a save event
|
||||
// when the graph is not fully loaded
|
||||
if (this.nodes.size === 0 && this.edges.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Don't emit save event while navigating inside a group
|
||||
if (this.graphStack.length > 0) return;
|
||||
|
||||
this.emit('save', state);
|
||||
logger.log('saving graphs', state);
|
||||
}
|
||||
@@ -729,9 +1272,7 @@ export class GraphManager extends EventEmitter<{
|
||||
|
||||
const sockets: [NodeInstance, string | number][] = [];
|
||||
|
||||
// if index is a string, we are an input looking for outputs
|
||||
if (typeof index === 'string') {
|
||||
// filter out self and child nodes
|
||||
const children = new SvelteSet(this.getChildren(node).map((n) => n.id));
|
||||
const nodes = this.getAllNodes().filter(
|
||||
(n) => n.id !== node.id && !children.has(n.id)
|
||||
@@ -750,9 +1291,6 @@ export class GraphManager extends EventEmitter<{
|
||||
}
|
||||
}
|
||||
} else if (typeof index === 'number') {
|
||||
// if index is a number, we are an output looking for inputs
|
||||
|
||||
// filter out self and parent nodes
|
||||
const parents = new SvelteSet(this.getParentsOfNode(node).map((n) => n.id));
|
||||
const nodes = this.getAllNodes().filter(
|
||||
(n) => n.id !== node.id && !parents.has(n.id)
|
||||
|
||||
@@ -99,6 +99,9 @@ export class GraphState {
|
||||
edges: [number, number, number, string][];
|
||||
} = null;
|
||||
|
||||
// Saved camera position per group so re-entering restores where you left off
|
||||
groupCameras = new Map<string, [number, number, number]>();
|
||||
|
||||
cameraBounds = $derived([
|
||||
this.cameraPosition[0] - this.width / this.cameraPosition[2] / 2,
|
||||
this.cameraPosition[0] + this.width / this.cameraPosition[2] / 2,
|
||||
|
||||
@@ -100,6 +100,9 @@
|
||||
if (typeof index === 'string') {
|
||||
return node.state.type?.inputs?.[index].type || 'unknown';
|
||||
}
|
||||
if (node.type === '__virtual/group/instance') {
|
||||
index += 1;
|
||||
}
|
||||
return node.state.type?.outputs?.[index] || 'unknown';
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -87,6 +87,95 @@
|
||||
manager.load(graph);
|
||||
}
|
||||
});
|
||||
|
||||
function navigateToBreadcrumb(index: number) {
|
||||
const crumbs = manager.breadcrumbs;
|
||||
const depth = crumbs.length - 1 - index;
|
||||
let restoredCamera: [number, number, number] | false = false;
|
||||
for (let i = 0; i < depth; i++) {
|
||||
const groupId = manager.currentGroupContext;
|
||||
if (groupId) {
|
||||
state.groupCameras.set(groupId, [...state.cameraPosition] as [number, number, number]);
|
||||
}
|
||||
restoredCamera = manager.exitGroup();
|
||||
}
|
||||
state.activeNodeId = -1;
|
||||
state.clearSelection();
|
||||
if (restoredCamera !== false) {
|
||||
state.cameraPosition[0] = restoredCamera[0];
|
||||
state.cameraPosition[1] = restoredCamera[1];
|
||||
state.cameraPosition[2] = restoredCamera[2];
|
||||
} else {
|
||||
state.centerNode();
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
{#if manager.isInsideGroup}
|
||||
<div class="breadcrumb-bar">
|
||||
{#each manager.breadcrumbs as crumb, i}
|
||||
{#if i > 0}
|
||||
<span class="sep">›</span>
|
||||
{/if}
|
||||
<button
|
||||
class="crumb"
|
||||
class:active={i === manager.breadcrumbs.length - 1}
|
||||
onclick={() => navigateToBreadcrumb(i)}
|
||||
>
|
||||
{crumb.name}
|
||||
</button>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<GraphEl {keymap} {safePadding} />
|
||||
|
||||
<style>
|
||||
.breadcrumb-bar {
|
||||
position: absolute;
|
||||
top: 8px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
z-index: 100;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
background: rgba(10, 15, 28, 0.85);
|
||||
border: 1px solid rgba(255, 255, 255, 0.12);
|
||||
border-radius: 6px;
|
||||
padding: 4px 10px;
|
||||
font-size: 12px;
|
||||
pointer-events: all;
|
||||
backdrop-filter: blur(8px);
|
||||
}
|
||||
|
||||
.sep {
|
||||
opacity: 0.4;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.crumb {
|
||||
background: none;
|
||||
border: none;
|
||||
color: rgba(255, 255, 255, 0.6);
|
||||
cursor: pointer;
|
||||
padding: 2px 4px;
|
||||
border-radius: 4px;
|
||||
font-size: 12px;
|
||||
transition: color 0.15s, background 0.15s;
|
||||
}
|
||||
|
||||
.crumb:hover {
|
||||
color: white;
|
||||
background: rgba(255, 255, 255, 0.08);
|
||||
}
|
||||
|
||||
.crumb.active {
|
||||
color: white;
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.crumb.active:hover {
|
||||
background: none;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -3,6 +3,9 @@ import type { NodeDefinition, NodeInstance } from '@nodarium/types';
|
||||
export function getParameterHeight(node: NodeDefinition, inputKey: string) {
|
||||
const input = node.inputs?.[inputKey];
|
||||
if (!input) {
|
||||
if (inputKey.startsWith('__virtual')) {
|
||||
return 50;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -53,7 +56,9 @@ export function getSocketPosition(
|
||||
|
||||
const nodeHeightCache: Record<string, number> = {};
|
||||
export function getNodeHeight(node: NodeDefinition) {
|
||||
if (node.id in nodeHeightCache) {
|
||||
// Don't cache virtual nodes — their inputs can change dynamically
|
||||
const isVirtual = (node.id as string).startsWith('__virtual/');
|
||||
if (!isVirtual && node.id in nodeHeightCache) {
|
||||
return nodeHeightCache[node.id];
|
||||
}
|
||||
if (!node?.inputs) {
|
||||
@@ -66,6 +71,8 @@ export function getNodeHeight(node: NodeDefinition) {
|
||||
height += h;
|
||||
}
|
||||
|
||||
if (!isVirtual) {
|
||||
nodeHeightCache[node.id] = height;
|
||||
}
|
||||
return height;
|
||||
}
|
||||
|
||||
@@ -45,8 +45,26 @@ export function setupKeymaps(keymap: Keymap, graph: GraphManager, graphState: Gr
|
||||
|
||||
keymap.addShortcut({
|
||||
key: 'Escape',
|
||||
description: 'Deselect nodes',
|
||||
description: 'Deselect nodes / Exit group',
|
||||
callback: () => {
|
||||
if (graph.isInsideGroup) {
|
||||
const groupId = graph.currentGroupContext;
|
||||
if (groupId) {
|
||||
graphState.groupCameras.set(
|
||||
groupId,
|
||||
[...graphState.cameraPosition] as [number, number, number]
|
||||
);
|
||||
}
|
||||
const savedCamera = graph.exitGroup();
|
||||
if (savedCamera !== false) {
|
||||
graphState.activeNodeId = -1;
|
||||
graphState.clearSelection();
|
||||
graphState.cameraPosition[0] = savedCamera[0];
|
||||
graphState.cameraPosition[1] = savedCamera[1];
|
||||
graphState.cameraPosition[2] = savedCamera[2];
|
||||
return;
|
||||
}
|
||||
}
|
||||
graphState.activeNodeId = -1;
|
||||
graphState.clearSelection();
|
||||
graphState.edgeEndPosition = null;
|
||||
@@ -159,4 +177,80 @@ export function setupKeymaps(keymap: Keymap, graph: GraphManager, graphState: Gr
|
||||
if (!edge) graph.smartConnect(nodes[1], nodes[0]);
|
||||
}
|
||||
});
|
||||
|
||||
keymap.addShortcut({
|
||||
key: 'g',
|
||||
ctrl: true,
|
||||
preventDefault: true,
|
||||
description: 'Group selected nodes',
|
||||
callback: () => {
|
||||
if (!graphState.isBodyFocused()) return;
|
||||
const nodeIds = Array.from(
|
||||
new Set([
|
||||
...(graphState.selectedNodes.size > 0 ? graphState.selectedNodes.values() : []),
|
||||
...(graphState.activeNodeId !== -1 ? [graphState.activeNodeId] : [])
|
||||
])
|
||||
);
|
||||
if (nodeIds.length === 0) return;
|
||||
const groupNode = graph.createGroup(nodeIds);
|
||||
if (groupNode) {
|
||||
graphState.selectedNodes.clear();
|
||||
graphState.activeNodeId = groupNode.id;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
keymap.addShortcut({
|
||||
key: 'g',
|
||||
alt: true,
|
||||
shift: true,
|
||||
preventDefault: true,
|
||||
description: 'Ungroup selected node',
|
||||
callback: () => {
|
||||
if (!graphState.isBodyFocused()) return;
|
||||
const nodeId = graphState.activeNodeId !== -1
|
||||
? graphState.activeNodeId
|
||||
: graphState.selectedNodes.size === 1
|
||||
? [...graphState.selectedNodes.values()][0]
|
||||
: -1;
|
||||
if (nodeId === -1) return;
|
||||
graph.ungroup(nodeId);
|
||||
graphState.activeNodeId = -1;
|
||||
graphState.clearSelection();
|
||||
}
|
||||
});
|
||||
|
||||
keymap.addShortcut({
|
||||
key: 'Tab',
|
||||
preventDefault: true,
|
||||
description: 'Enter focused group node',
|
||||
callback: () => {
|
||||
if (!graphState.isBodyFocused()) return;
|
||||
const entered = graph.enterGroup(
|
||||
graphState.activeNodeId,
|
||||
[...graphState.cameraPosition] as [number, number, number]
|
||||
);
|
||||
if (entered) {
|
||||
graphState.activeNodeId = -1;
|
||||
graphState.clearSelection();
|
||||
// Restore group-specific camera if we've been here before, else snap to center
|
||||
const groupId = graph.currentGroupContext;
|
||||
const saved = groupId ? graphState.groupCameras.get(groupId) : undefined;
|
||||
if (saved) {
|
||||
graphState.cameraPosition[0] = saved[0];
|
||||
graphState.cameraPosition[1] = saved[1];
|
||||
graphState.cameraPosition[2] = saved[2];
|
||||
} else {
|
||||
const nodes = [...graph.nodes.values()];
|
||||
if (nodes.length) {
|
||||
const avgX = nodes.reduce((s, n) => s + n.position[0], 0) / nodes.length;
|
||||
const avgY = nodes.reduce((s, n) => s + n.position[1], 0) / nodes.length;
|
||||
graphState.cameraPosition[0] = avgX;
|
||||
graphState.cameraPosition[1] = avgY;
|
||||
graphState.cameraPosition[2] = 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
<script lang="ts">
|
||||
import type { NodeInstance } from '@nodarium/types';
|
||||
import { getGraphState } from '../graph-state.svelte';
|
||||
import type { NodeDefinition, NodeInstance } from '@nodarium/types';
|
||||
import { getGraphManager, getGraphState } from '../graph-state.svelte';
|
||||
import NodeHeader from './NodeHeader.svelte';
|
||||
import NodeParameter from './NodeParameter.svelte';
|
||||
|
||||
let ref: HTMLDivElement;
|
||||
|
||||
const graphState = getGraphState();
|
||||
const manager = getGraphManager();
|
||||
|
||||
type Props = {
|
||||
node: NodeInstance;
|
||||
@@ -30,10 +31,38 @@
|
||||
const zOffset = Math.random() - 0.5;
|
||||
const zLimit = 2 - zOffset;
|
||||
|
||||
const parameters = Object.entries(node.state?.type?.inputs || {}).filter(
|
||||
function buildParameters(node: NodeInstance, inputs: NodeDefinition['inputs']) {
|
||||
let parameters = Object.entries(inputs || {}).filter(
|
||||
(p) => p[1].type !== 'seed' && !('setting' in p[1]) && p[1]?.hidden !== true
|
||||
);
|
||||
|
||||
if (node.type === '__virtual/group/instance') {
|
||||
parameters = [['__virtual/groupId', {
|
||||
type: 'select',
|
||||
value: node.props?.groupId as string,
|
||||
options: [...manager?.groups?.keys()]
|
||||
}], ...parameters];
|
||||
}
|
||||
|
||||
return parameters;
|
||||
}
|
||||
|
||||
const parameters = $derived(buildParameters(node, node?.state?.type?.inputs || {}));
|
||||
|
||||
const currentGroupId = $derived((node.props?.groupId as string) ?? '');
|
||||
|
||||
function onGroupSelect(event: Event) {
|
||||
const select = event.target as HTMLSelectElement;
|
||||
const newGroupId = select.value;
|
||||
if (!manager || newGroupId === currentGroupId) return;
|
||||
const newGroupDef = manager.groupNodeDefinitions.get(`__virtual/group/${newGroupId}`);
|
||||
if (!newGroupDef) return;
|
||||
node.props = { ...(node.props ?? {}), groupId: newGroupId };
|
||||
node.state = { type: newGroupDef };
|
||||
manager.execute();
|
||||
manager.save();
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
if ('state' in node && !node.state.ref) {
|
||||
node.state.ref = ref;
|
||||
@@ -55,6 +84,22 @@
|
||||
>
|
||||
<NodeHeader {node} />
|
||||
|
||||
{#if false && node.type === '__virtual/group/instance'}
|
||||
<div class="group-param">
|
||||
<select
|
||||
value={currentGroupId}
|
||||
onchange={onGroupSelect}
|
||||
onmousedown={(e) => e.stopPropagation()}
|
||||
onclick={(e) => e.stopPropagation()}
|
||||
onkeydown={(e) => e.stopPropagation()}
|
||||
>
|
||||
{#each manager?.groups?.entries() ?? [] as [gid, gdef]}
|
||||
<option value={gid}>{gdef.name}</option>
|
||||
{/each}
|
||||
</select>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#each parameters as [key, value], i (key)}
|
||||
<NodeParameter
|
||||
bind:node
|
||||
@@ -66,6 +111,24 @@
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.group-param {
|
||||
padding: 5px 8px;
|
||||
border-bottom: solid 1px var(--color-layer-2);
|
||||
background: var(--color-layer-1);
|
||||
}
|
||||
|
||||
.group-param select {
|
||||
width: 100%;
|
||||
background: var(--color-layer-2);
|
||||
color: var(--color-text);
|
||||
border: 1px solid rgba(255, 255, 255, 0.12);
|
||||
border-radius: 4px;
|
||||
padding: 4px 6px;
|
||||
font-size: 0.8em;
|
||||
cursor: pointer;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.node {
|
||||
box-sizing: border-box;
|
||||
user-select: none !important;
|
||||
|
||||
@@ -70,7 +70,7 @@
|
||||
{#if appSettings.value.debug.advancedMode}
|
||||
<span class="bg-white text-black! mr-2 px-1 rounded-sm opacity-30">{node.id}</span>
|
||||
{/if}
|
||||
{node.type.split('/').pop()}
|
||||
{node.state?.type?.meta?.title ?? node.type.split('/').pop()}
|
||||
</div>
|
||||
<div
|
||||
class="target"
|
||||
|
||||
@@ -45,7 +45,7 @@
|
||||
|
||||
$effect(() => {
|
||||
const a = $state.snapshot(value);
|
||||
const b = $state.snapshot(node?.props?.[id]);
|
||||
const b = $state.snapshot(node?.props?.[id]) as number | number[] | undefined;
|
||||
const isDiff = Array.isArray(a) ? diffArray(a, b) : a !== b;
|
||||
if (value !== undefined && isDiff) {
|
||||
node.props = { ...node.props, [id]: a };
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
});
|
||||
}
|
||||
|
||||
const leftBump = $derived(nodeType.inputs?.[id].internal !== true);
|
||||
const leftBump = $derived(!id.startsWith('__virtual') && nodeType.inputs?.[id].internal !== true);
|
||||
const cornerBottom = $derived(isLast ? 5 : 0);
|
||||
const aspectRatio = 0.5;
|
||||
|
||||
@@ -83,7 +83,7 @@
|
||||
>
|
||||
{#key id && graphId}
|
||||
<div class="content" class:disabled={graph?.inputSockets?.has(socketId)}>
|
||||
{#if inputType?.label !== ''}
|
||||
{#if inputType?.label !== '' && !id.startsWith('__virtual')}
|
||||
<label for={elementId} title={input.description}>{input.label || id}</label>
|
||||
{/if}
|
||||
{#if inputType?.external !== true}
|
||||
|
||||
25
app/src/lib/node-registry/groupNodes.ts
Normal file
25
app/src/lib/node-registry/groupNodes.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import type { NodeDefinition } from '@nodarium/types';
|
||||
|
||||
export const groupInputNode: NodeDefinition = {
|
||||
id: '__virtual/group/input',
|
||||
inputs: {},
|
||||
outputs: [],
|
||||
execute(_data: Int32Array): Int32Array { return _data; }
|
||||
} as unknown as NodeDefinition;
|
||||
|
||||
export const groupOutputNode: NodeDefinition = {
|
||||
id: '__virtual/group/output',
|
||||
inputs: {},
|
||||
outputs: [],
|
||||
execute(_data: Int32Array): Int32Array { return _data; }
|
||||
} as unknown as NodeDefinition;
|
||||
|
||||
// Stub registered in the registry so it appears in AddMenu.
|
||||
// Actual inputs/outputs are resolved from props.groupId at runtime.
|
||||
export const groupNode: NodeDefinition = {
|
||||
id: '__virtual/group/instance',
|
||||
meta: { title: 'Group' },
|
||||
inputs: {},
|
||||
outputs: [],
|
||||
execute(_data: Int32Array): Int32Array { return _data; }
|
||||
} as unknown as NodeDefinition;
|
||||
@@ -6,6 +6,142 @@ import type {
|
||||
RuntimeExecutor,
|
||||
SyncCache
|
||||
} from '@nodarium/types';
|
||||
|
||||
function isGroupInstanceType(type: string): boolean {
|
||||
return type === '__virtual/group/instance';
|
||||
}
|
||||
|
||||
export function expandGroups(graph: Graph): Graph {
|
||||
if (!graph.groups || Object.keys(graph.groups).length === 0) {
|
||||
return graph;
|
||||
}
|
||||
|
||||
let nodes = [...graph.nodes];
|
||||
let edges = [...graph.edges];
|
||||
const groups = graph.groups;
|
||||
|
||||
let changed = true;
|
||||
while (changed) {
|
||||
changed = false;
|
||||
|
||||
for (let i = 0; i < nodes.length; i++) {
|
||||
const node = nodes[i];
|
||||
if (!isGroupInstanceType(node.type)) continue;
|
||||
|
||||
const groupId = (node.props as Record<string, unknown> | undefined)?.groupId as string | undefined;
|
||||
if (!groupId) continue;
|
||||
const group = groups[groupId];
|
||||
if (!group) continue;
|
||||
|
||||
changed = true;
|
||||
|
||||
// Recursively expand nested groups inside this group's internal graph
|
||||
const expandedInternal = expandGroups({
|
||||
id: 0,
|
||||
nodes: group.graph.nodes,
|
||||
edges: group.graph.edges,
|
||||
groups
|
||||
});
|
||||
|
||||
const ID_PREFIX = node.id * 1000000;
|
||||
const idMap = new Map<number, number>();
|
||||
|
||||
const inputVirtualNode = expandedInternal.nodes.find(
|
||||
n => n.type === '__virtual/group/input'
|
||||
);
|
||||
const outputVirtualNode = expandedInternal.nodes.find(
|
||||
n => n.type === '__virtual/group/output'
|
||||
);
|
||||
|
||||
const realInternalNodes = expandedInternal.nodes.filter(
|
||||
n => n.type !== '__virtual/group/input' && n.type !== '__virtual/group/output'
|
||||
);
|
||||
|
||||
for (const n of realInternalNodes) {
|
||||
idMap.set(n.id, ID_PREFIX + n.id);
|
||||
}
|
||||
|
||||
const parentIncomingEdges = edges.filter(e => e[2] === node.id);
|
||||
const parentOutgoingEdges = edges.filter(e => e[0] === node.id);
|
||||
|
||||
// Edges from/to virtual nodes in the expanded internal graph
|
||||
const edgesFromInput = expandedInternal.edges.filter(
|
||||
e => e[0] === inputVirtualNode?.id
|
||||
);
|
||||
const edgesToOutput = expandedInternal.edges.filter(
|
||||
e => e[2] === outputVirtualNode?.id
|
||||
);
|
||||
|
||||
const newEdges: Graph['edges'] = [];
|
||||
|
||||
// Short-circuit: parent source → internal target (via group input)
|
||||
for (const parentEdge of parentIncomingEdges) {
|
||||
const socketName = parentEdge[3];
|
||||
const socketIdx = group.inputs.findIndex(s => s.name === socketName);
|
||||
if (socketIdx === -1) continue;
|
||||
|
||||
for (const internalEdge of edgesFromInput.filter(e => e[1] === socketIdx)) {
|
||||
const remappedId = idMap.get(internalEdge[2]);
|
||||
if (remappedId !== undefined) {
|
||||
newEdges.push([parentEdge[0], parentEdge[1], remappedId, internalEdge[3]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Short-circuit: internal source → parent target (via group output)
|
||||
for (const parentEdge of parentOutgoingEdges) {
|
||||
const outputIdx = parentEdge[1];
|
||||
const outputSocketName = group.outputs[outputIdx]?.name;
|
||||
if (!outputSocketName) continue;
|
||||
|
||||
for (const internalEdge of edgesToOutput.filter(e => e[3] === outputSocketName)) {
|
||||
const remappedId = idMap.get(internalEdge[0]);
|
||||
if (remappedId !== undefined) {
|
||||
newEdges.push([remappedId, internalEdge[1], parentEdge[2], parentEdge[3]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remap internal-to-internal edges
|
||||
const internalEdges = expandedInternal.edges.filter(
|
||||
e => e[0] !== inputVirtualNode?.id
|
||||
&& e[0] !== outputVirtualNode?.id
|
||||
&& e[2] !== inputVirtualNode?.id
|
||||
&& e[2] !== outputVirtualNode?.id
|
||||
);
|
||||
|
||||
for (const e of internalEdges) {
|
||||
const fromId = idMap.get(e[0]);
|
||||
const toId = idMap.get(e[2]);
|
||||
if (fromId !== undefined && toId !== undefined) {
|
||||
newEdges.push([fromId, e[1], toId, e[3]]);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the group node
|
||||
nodes.splice(i, 1);
|
||||
|
||||
// Add remapped internal nodes
|
||||
for (const n of realInternalNodes) {
|
||||
nodes.push({ ...n, id: idMap.get(n.id)! });
|
||||
}
|
||||
|
||||
// Remove group node's edges and add short-circuit edges
|
||||
const groupEdgeKeys = new Set([
|
||||
...parentIncomingEdges.map(e => `${e[0]}-${e[1]}-${e[2]}-${e[3]}`),
|
||||
...parentOutgoingEdges.map(e => `${e[0]}-${e[1]}-${e[2]}-${e[3]}`)
|
||||
]);
|
||||
edges = edges.filter(
|
||||
e => !groupEdgeKeys.has(`${e[0]}-${e[1]}-${e[2]}-${e[3]}`)
|
||||
);
|
||||
edges.push(...newEdges);
|
||||
|
||||
break; // Restart loop with updated nodes array
|
||||
}
|
||||
}
|
||||
|
||||
return { ...graph, nodes, edges };
|
||||
}
|
||||
import {
|
||||
concatEncodedArrays,
|
||||
createLogger,
|
||||
@@ -75,7 +211,11 @@ export class MemoryRuntimeExecutor implements RuntimeExecutor {
|
||||
throw new Error('Node registry is not ready');
|
||||
}
|
||||
|
||||
await this.registry.load(graph.nodes.map((node) => node.type));
|
||||
// Only load non-virtual types (virtual nodes are resolved locally)
|
||||
const nonVirtualTypes = graph.nodes
|
||||
.map(node => node.type)
|
||||
.filter(t => !t.startsWith('__virtual/'));
|
||||
await this.registry.load(nonVirtualTypes as any);
|
||||
|
||||
const typeMap = new Map<string, NodeDefinition>();
|
||||
for (const node of graph.nodes) {
|
||||
@@ -163,6 +303,9 @@ export class MemoryRuntimeExecutor implements RuntimeExecutor {
|
||||
let a = performance.now();
|
||||
this.debugData = {};
|
||||
|
||||
// Expand group nodes into a flat graph before execution
|
||||
graph = expandGroups(graph);
|
||||
|
||||
// Then we add some metadata to the graph
|
||||
const [outputNode, nodes] = await this.addMetaData(graph);
|
||||
let b = performance.now();
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
import { debugNode } from '$lib/node-registry/debugNode';
|
||||
import { groupInputNode, groupNode, groupOutputNode } from '$lib/node-registry/groupNodes';
|
||||
import { IndexDBCache, RemoteNodeRegistry } from '$lib/node-registry/index';
|
||||
import type { Graph } from '@nodarium/types';
|
||||
import { createPerformanceStore } from '@nodarium/utils';
|
||||
import { MemoryRuntimeExecutor } from './runtime-executor';
|
||||
import { expandGroups, MemoryRuntimeExecutor } from './runtime-executor';
|
||||
import { MemoryRuntimeCache } from './runtime-executor-cache';
|
||||
|
||||
const indexDbCache = new IndexDBCache('node-registry');
|
||||
const nodeRegistry = new RemoteNodeRegistry('', indexDbCache, [debugNode]);
|
||||
const nodeRegistry = new RemoteNodeRegistry('', indexDbCache, [
|
||||
debugNode,
|
||||
groupInputNode,
|
||||
groupOutputNode,
|
||||
groupNode
|
||||
]);
|
||||
|
||||
const cache = new MemoryRuntimeCache();
|
||||
const executor = new MemoryRuntimeExecutor(nodeRegistry, cache);
|
||||
@@ -34,7 +40,13 @@ export async function executeGraph(
|
||||
graph: Graph,
|
||||
settings: Record<string, unknown>
|
||||
): Promise<Int32Array> {
|
||||
await nodeRegistry.load(graph.nodes.map((n) => n.type));
|
||||
// Expand groups before loading types so we only load real (non-virtual) node types
|
||||
const expandedGraph = expandGroups(graph);
|
||||
await nodeRegistry.load(
|
||||
expandedGraph.nodes
|
||||
.map(n => n.type)
|
||||
.filter(t => !t.startsWith('__virtual/')) as any
|
||||
);
|
||||
performanceStore.startRun();
|
||||
const res = await executor.execute(graph, settings);
|
||||
performanceStore.stopRun();
|
||||
|
||||
@@ -96,6 +96,4 @@
|
||||
bind:value={store}
|
||||
type={nodeDefinition}
|
||||
/>
|
||||
{:else}
|
||||
<p class="mx-4 mt-4">Node has no settings</p>
|
||||
{/if}
|
||||
|
||||
@@ -5,22 +5,27 @@
|
||||
|
||||
type Props = {
|
||||
manager: GraphManager;
|
||||
node: NodeInstance | undefined;
|
||||
node: NodeInstance;
|
||||
};
|
||||
|
||||
let { manager, node = $bindable() }: Props = $props();
|
||||
|
||||
const inputs = $derived(node?.state?.type?.inputs || {});
|
||||
|
||||
const hasSettings = $derived(
|
||||
Object.values(inputs).find(entry => {
|
||||
return entry.hidden === true;
|
||||
}) !== undefined
|
||||
);
|
||||
|
||||
$inspect({ inputs, hasSettings });
|
||||
</script>
|
||||
|
||||
<div class='{node?"border-l-2 pl-3.5!":""} bg-layer-2 flex items-center h-[70px] border-b-1 border-l-selected border-b-outline pl-4'>
|
||||
{#key node.id}
|
||||
{#if node && hasSettings}
|
||||
<div class="border-l-2 pl-3.5! bg-layer-2 flex items-center h-[70px] border-b-1 border-l-selected border-b-outline pl-4">
|
||||
<h3>Node Settings</h3>
|
||||
</div>
|
||||
|
||||
{#if node}
|
||||
{#key node.id}
|
||||
{#if node}
|
||||
</div>
|
||||
<ActiveNodeSelected {manager} bind:node />
|
||||
{/if}
|
||||
{/key}
|
||||
{:else}
|
||||
<p class="mx-4 mt-4">No node selected</p>
|
||||
{/if}
|
||||
{/key}
|
||||
|
||||
148
app/src/lib/sidebar/panels/GroupContextPanel.svelte
Normal file
148
app/src/lib/sidebar/panels/GroupContextPanel.svelte
Normal file
@@ -0,0 +1,148 @@
|
||||
<script lang="ts">
|
||||
import type { GraphManager } from '$lib/graph-interface/graph-manager.svelte';
|
||||
import { InputSelect } from '@nodarium/ui';
|
||||
|
||||
type Props = { manager: GraphManager; groupId: string };
|
||||
const { manager, groupId }: Props = $props();
|
||||
|
||||
$inspect({ groupId });
|
||||
|
||||
const group = $derived(manager.groups.get(groupId));
|
||||
|
||||
const COMMON_TYPES = ['plant', 'float', 'int', 'vec3', 'bool'];
|
||||
let selectedTypeIdx = $state(0);
|
||||
let customType = $state('');
|
||||
|
||||
function rename(e: Event) {
|
||||
if (!group) return;
|
||||
const name = (e.target as HTMLInputElement).value.trim();
|
||||
if (!name) return;
|
||||
group.name = name;
|
||||
if (manager.graph.groups?.[groupId]) manager.graph.groups[groupId].name = name;
|
||||
const def = manager.groupNodeDefinitions.get(`__virtual/group/${groupId}`);
|
||||
if (def?.meta) def.meta.title = name;
|
||||
manager.save();
|
||||
}
|
||||
|
||||
function addSocket() {
|
||||
const type = customType.trim() || COMMON_TYPES[selectedTypeIdx];
|
||||
if (!type) return;
|
||||
manager.addGroupSocket('input', type);
|
||||
customType = '';
|
||||
}
|
||||
|
||||
function removeSocket(index: number) {
|
||||
manager.removeGroupSocket('input', index);
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="bg-layer-2 flex items-center h-[70px] border-b-1 border-outline pl-4">
|
||||
<h3>Group Settings</h3>
|
||||
</div>
|
||||
|
||||
<div class="px-4 py-3 flex flex-col gap-4">
|
||||
<div class="flex flex-col gap-1.5">
|
||||
<span class="section-label">Group name</span>
|
||||
<input
|
||||
type="text"
|
||||
value={group?.name ?? ''}
|
||||
onchange={rename}
|
||||
onkeydown={(e) => e.stopPropagation()}
|
||||
placeholder="Group name"
|
||||
class="bg-layer-2 text-text rounded-[5px] px-2 py-1.5 text-sm w-full box-border outline outline-1 outline-outline"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="flex flex-col gap-1.5">
|
||||
<span class="section-label">Inputs</span>
|
||||
|
||||
{#if (group?.inputs?.length ?? 0) === 0}
|
||||
<p class="text-sm opacity-40 italic m-0">No inputs yet</p>
|
||||
{:else}
|
||||
<ul class="socket-list">
|
||||
{#each group?.inputs ?? [] as socket, i}
|
||||
<li class="socket-item">
|
||||
<span class="flex-1 opacity-80 text-sm">{socket.name}</span>
|
||||
<span class="text-xs opacity-45 italic">{socket.type}</span>
|
||||
<button class="remove-btn" onclick={() => removeSocket(i)} title="Remove">×</button>
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
{/if}
|
||||
|
||||
<div class="flex gap-1.5 items-center">
|
||||
<InputSelect options={COMMON_TYPES} bind:value={selectedTypeIdx} />
|
||||
<input
|
||||
type="text"
|
||||
placeholder="custom type…"
|
||||
bind:value={customType}
|
||||
onkeydown={(e) => {
|
||||
e.stopPropagation();
|
||||
if (e.key === 'Enter') addSocket();
|
||||
}}
|
||||
class="bg-layer-2 text-text rounded-[5px] px-2 py-1 text-sm flex-1 min-w-0 outline outline-1 outline-outline"
|
||||
/>
|
||||
<button class="add-btn" onclick={addSocket}>+ Add</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.section-label {
|
||||
font-size: 0.72em;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.06em;
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
.socket-list {
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 3px;
|
||||
}
|
||||
|
||||
.socket-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
background: var(--color-layer-2);
|
||||
border-radius: 5px;
|
||||
padding: 4px 8px;
|
||||
outline: 1px solid var(--color-outline);
|
||||
}
|
||||
|
||||
.remove-btn {
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--color-text);
|
||||
cursor: pointer;
|
||||
opacity: 0.4;
|
||||
padding: 0 2px;
|
||||
font-size: 1.1em;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.remove-btn:hover {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.add-btn {
|
||||
background: var(--color-layer-2);
|
||||
color: var(--color-text);
|
||||
border: none;
|
||||
outline: 1px solid var(--color-outline);
|
||||
border-radius: 5px;
|
||||
padding: 0.4em 0.7em;
|
||||
font-size: 0.8em;
|
||||
cursor: pointer;
|
||||
white-space: nowrap;
|
||||
font-family: var(--font-family);
|
||||
}
|
||||
|
||||
.add-btn:hover {
|
||||
outline-color: var(--color-selected);
|
||||
}
|
||||
</style>
|
||||
@@ -5,6 +5,7 @@
|
||||
import { debounceAsyncFunction } from '$lib/helpers';
|
||||
import { createKeyMap } from '$lib/helpers/createKeyMap';
|
||||
import { debugNode } from '$lib/node-registry/debugNode.js';
|
||||
import { groupInputNode, groupNode, groupOutputNode } from '$lib/node-registry/groupNodes.js';
|
||||
import { IndexDBCache, RemoteNodeRegistry } from '$lib/node-registry/index';
|
||||
import NodeStore from '$lib/node-store/NodeStore.svelte';
|
||||
import PerformanceViewer from '$lib/performance/PerformanceViewer.svelte';
|
||||
@@ -21,6 +22,7 @@
|
||||
import Changelog from '$lib/sidebar/panels/Changelog.svelte';
|
||||
import ExportSettings from '$lib/sidebar/panels/ExportSettings.svelte';
|
||||
import GraphSource from '$lib/sidebar/panels/GraphSource.svelte';
|
||||
import GroupContextPanel from '$lib/sidebar/panels/GroupContextPanel.svelte';
|
||||
import Keymap from '$lib/sidebar/panels/Keymap.svelte';
|
||||
import { panelState } from '$lib/sidebar/PanelState.svelte';
|
||||
import Sidebar from '$lib/sidebar/Sidebar.svelte';
|
||||
@@ -37,7 +39,12 @@
|
||||
|
||||
const registryCache = new IndexDBCache('node-registry');
|
||||
|
||||
const nodeRegistry = new RemoteNodeRegistry('', registryCache, [debugNode]);
|
||||
const nodeRegistry = new RemoteNodeRegistry('', registryCache, [
|
||||
debugNode,
|
||||
groupInputNode,
|
||||
groupOutputNode,
|
||||
groupNode
|
||||
]);
|
||||
const workerRuntime = new WorkerRuntimeExecutor();
|
||||
const runtimeCache = new MemoryRuntimeCache();
|
||||
const memoryRuntime = new MemoryRuntimeExecutor(nodeRegistry, runtimeCache);
|
||||
@@ -341,7 +348,20 @@
|
||||
type={graphSettingTypes}
|
||||
bind:value={graphSettings}
|
||||
/>
|
||||
{#if activeNode?.id}
|
||||
<ActiveNodeSettings {manager} bind:node={activeNode} />
|
||||
{/if}
|
||||
{#if manager?.isInsideGroup}
|
||||
<GroupContextPanel
|
||||
{manager}
|
||||
groupId={manager.currentGroupContext!}
|
||||
/>
|
||||
{:else if activeNode?.type === '__virtual/group/instance'}
|
||||
<GroupContextPanel
|
||||
{manager}
|
||||
groupId={activeNode?.props?.groupId as string}
|
||||
/>
|
||||
{/if}
|
||||
</Panel>
|
||||
<Panel
|
||||
id="changelog"
|
||||
|
||||
312
docs/LLM.md
Normal file
312
docs/LLM.md
Normal file
@@ -0,0 +1,312 @@
|
||||
# Nodarium - LLM Documentation
|
||||
|
||||
## Overview
|
||||
|
||||
Nodarium is a **WebAssembly-based visual programming language** for creating procedural 3D plants. The app features a node-based interface where users connect WASM modules to generate plant models in real-time. Currently used to develop https://nodes.max-richter.dev, a procedural modelling tool for 3D plants.
|
||||
|
||||
## Architecture
|
||||
|
||||
### Core Components
|
||||
|
||||
#### 1. Node System (`app/static/nodes/`)
|
||||
|
||||
WASM-based nodes that perform computations. All nodes must implement the NodeDefinition interface.
|
||||
|
||||
- **Node Storage**: `app/static/nodes/max/plantarium/`
|
||||
- `box.wasm` - Box geometry node
|
||||
- `branch.wasm` - Branch generation
|
||||
- `float.wasm` - Float value node
|
||||
- `gravity.wasm` - Gravity/physics node
|
||||
- `instance.wasm` - Instance rendering
|
||||
- `leaf.wasm` - Leaf geometry
|
||||
- `math.wasm` - Math operations
|
||||
- `noise.wasm` - Noise generation
|
||||
- `output.wasm` - Output node
|
||||
- `random.wasm` - Random value generation
|
||||
- `rotate.wasm` - Rotation node
|
||||
- `shape.wasm` - Shape geometry
|
||||
- `stem.wasm` - Stem generation
|
||||
- `triangle.wasm` - Triangle geometry
|
||||
- `vec3.wasm` - Vector3 node
|
||||
|
||||
- **Node Registry**: `app/src/lib/node-registry.ts`
|
||||
- Loads and manages WASM nodes
|
||||
- `getNodeWasm()` - Creates WASM wrapper from bytes
|
||||
- `getNode()` - Retrieves node definition
|
||||
|
||||
- **Debug Node**: `app/src/lib/node-registry/debugNode.js`
|
||||
- Special debug node with wildcard inputs
|
||||
- Variable-height nodes and parameters
|
||||
- Quick-connect shortcut
|
||||
|
||||
#### 2. Graph Interface
|
||||
|
||||
Visual node editor built with Svelte 5.
|
||||
|
||||
- **Main Wrapper**: `app/src/lib/graph-interface/graph/Wrapper.svelte`
|
||||
- Entry point for graph interface
|
||||
- Manages GraphManager and GraphState
|
||||
|
||||
- **GraphManager**: `app/src/lib/graph-interface/graph-manager.svelte.ts`
|
||||
- Core entity managing the node graph
|
||||
- Handles node connections and execution flow
|
||||
|
||||
- **GraphState**: `app/src/lib/graph-interface/graph-state.svelte.ts`
|
||||
- Tracks UI state (selection, snapping, help, active nodes)
|
||||
|
||||
- **Graph Components**:
|
||||
- `app/src/lib/graph-interface/graph/` - Graph rendering
|
||||
- `app/src/lib/graph-interface/node/` - Node rendering
|
||||
- `app/src/lib/graph-interface/edges/` - Edge rendering
|
||||
- `app/src/lib/graph-interface/components/` - UI components (AddMenu, Socket, etc.)
|
||||
- `app/src/lib/graph-interface/debug/` - Debug overlays
|
||||
- `app/src/lib/graph-interface/background/` - Grid/dots backgrounds
|
||||
|
||||
- **Helpers**:
|
||||
- `app/src/lib/helpers/` - Utility functions
|
||||
- `app/src/lib/helpers/createKeyMap.ts` - Keyboard shortcuts
|
||||
|
||||
#### 3. Runtime Execution
|
||||
|
||||
Performs graph execution via WASM nodes.
|
||||
|
||||
- **Runtime Executors** (`app/src/lib/runtime/`):
|
||||
- **MemoryRuntime**: Direct WASM execution in main thread
|
||||
- **WorkerRuntime**: WebWorker-based execution for performance
|
||||
- Both implement the RuntimeExecutor interface
|
||||
|
||||
- **Runtime Cache**: `app/src/lib/runtime/cache.ts`
|
||||
- Memory-based caching for graph execution
|
||||
|
||||
- **Execution Flow**:
|
||||
1. Graph serialized from graph interface
|
||||
2. Runtime executes nodes in topological order
|
||||
3. Results passed through connected edges
|
||||
4. Final mesh output rendered
|
||||
|
||||
#### 4. 3D Viewer (`app/src/lib/result-viewer/`)
|
||||
|
||||
Three.js-based rendering for 3D output.
|
||||
|
||||
- **Viewer**: `app/src/lib/result-viewer/Viewer.svelte`
|
||||
- Renders generated 3D meshes
|
||||
- Uses @threlte/core (Svelte-Three.js wrapper)
|
||||
|
||||
#### 5. Application Structure (`app/src/routes/`)
|
||||
|
||||
SvelteKit application routing.
|
||||
|
||||
- **Main Page**: `app/src/routes/+page.svelte`
|
||||
- Combines GraphInterface + 3D Viewer
|
||||
- Manages runtime selection (memory vs worker)
|
||||
- Handles settings and performance tracking
|
||||
|
||||
- **Layout**: `app/src/routes/+layout.svelte`
|
||||
- Application shell
|
||||
|
||||
- **Server**: `app/src/routes/+layout.server.ts`
|
||||
- Loads git metadata and changelog
|
||||
|
||||
#### 6. Settings System (`app/src/lib/settings/`)
|
||||
|
||||
Application and graph settings.
|
||||
|
||||
- **App Settings**: `app/src/lib/settings/app-settings.svelte.ts`
|
||||
- Debug mode, themes, node interface options
|
||||
|
||||
- **NestedSettings**: `app/src/lib/settings/NestedSettings.svelte`
|
||||
- Recursive settings UI component
|
||||
|
||||
#### 7. Sidebar Panels (`app/src/lib/sidebar/`)
|
||||
|
||||
- `app/src/lib/sidebar/Sidebar.svelte` - Main sidebar
|
||||
- `app/src/lib/sidebar/panels/` - Individual panels:
|
||||
- `ActiveNodeSettings.svelte` - Selected node properties
|
||||
- `BenchmarkPanel.svelte` - Performance benchmarking
|
||||
- `Changelog.svelte` - Version history
|
||||
- `ExportSettings.svelte` - Export options
|
||||
- `GraphSource.svelte` - Graph JSON view
|
||||
- `Keymap.svelte` - Keyboard shortcuts
|
||||
|
||||
#### 8. Project Management (`app/src/lib/project-manager/`)
|
||||
|
||||
- `app/src/lib/project-manager/project-manager.svelte` - Project save/load
|
||||
- Uses IndexedDB for persistence
|
||||
|
||||
#### 9. Node Store (`app/src/lib/node-store/`)
|
||||
|
||||
- `app/src/lib/node-store/NodeStore.svelte`
|
||||
- Remote node registry management
|
||||
- IndexDBCache for offline storage
|
||||
|
||||
#### 10. Graph Templates (`app/src/lib/graph-templates/`)
|
||||
|
||||
Pre-built graph templates for testing:
|
||||
|
||||
- Grid, Tree, LottaFaces, LottaNodes, LottaNodesAndFaces
|
||||
|
||||
## Key Types (`app/src/lib/types.ts`)
|
||||
|
||||
```typescript
|
||||
interface NodeDefinition {
|
||||
id: string;
|
||||
name: string;
|
||||
inputs: Socket[];
|
||||
outputs: Socket[];
|
||||
parameters: Parameter[];
|
||||
execute: (inputs: any[], parameters: any[]) => any[];
|
||||
}
|
||||
|
||||
interface Socket {
|
||||
id: string;
|
||||
name: string;
|
||||
type: string; // datatype (e.g., "number", "vec3", "*")
|
||||
defaultValue?: any;
|
||||
optional?: boolean;
|
||||
}
|
||||
|
||||
interface Parameter {
|
||||
id: string;
|
||||
name: string;
|
||||
type: string;
|
||||
defaultValue: any;
|
||||
min?: number;
|
||||
max?: number;
|
||||
options?: string[];
|
||||
}
|
||||
|
||||
interface Graph {
|
||||
nodes: NodeInstance[];
|
||||
edges: Edge[];
|
||||
}
|
||||
|
||||
interface NodeInstance {
|
||||
id: number;
|
||||
nodeId: string;
|
||||
position: { x: number; y: number };
|
||||
parameters: Record<string, any>;
|
||||
}
|
||||
|
||||
interface Edge {
|
||||
id: number;
|
||||
fromNode: number;
|
||||
fromSocket: string;
|
||||
toNode: number;
|
||||
toSocket: string;
|
||||
}
|
||||
```
|
||||
|
||||
## Development Workflow
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- Node.js
|
||||
- pnpm
|
||||
- Rust
|
||||
- wasm-pack
|
||||
|
||||
### Build Commands
|
||||
|
||||
```bash
|
||||
# Install dependencies
|
||||
pnpm i
|
||||
|
||||
# Build WASM nodes
|
||||
pnpm build:nodes
|
||||
|
||||
# Start development server
|
||||
cd app && pnpm dev
|
||||
|
||||
# Run tests
|
||||
cd app && pnpm test
|
||||
|
||||
# Lint and typecheck
|
||||
cd app && pnpm lint
|
||||
cd app && pnpm check
|
||||
|
||||
# Format code
|
||||
cd app && pnpm format
|
||||
```
|
||||
|
||||
### Creating New Nodes
|
||||
|
||||
See `docs/DEVELOPING_NODES.md` for detailed instructions on creating custom WASM nodes.
|
||||
|
||||
## Features
|
||||
|
||||
### Current Features
|
||||
|
||||
- Visual node-based programming with real-time 3D preview
|
||||
- WebAssembly nodes for high-performance computation
|
||||
- Debug node with wildcard inputs and runtime integration
|
||||
- Color-coded node sockets and edges (indicating data types)
|
||||
- Variable-height nodes and parameters
|
||||
- Edge dragging with valid socket highlighting
|
||||
- InputNumber snapping to predefined values (Alt+click)
|
||||
- Project save/load with IndexedDB
|
||||
- Performance monitoring and benchmarking
|
||||
- Changelog viewer
|
||||
- Advanced mode settings
|
||||
|
||||
### UI Components
|
||||
|
||||
- **InputNumber**: Numeric input with arrow controls
|
||||
- **InputColor**: Color picker
|
||||
- **InputShape**: Shape selector with preview
|
||||
- **InputSelect**: Dropdown with options
|
||||
|
||||
## File Structure
|
||||
|
||||
```
|
||||
nodarium/
|
||||
├── app/
|
||||
│ ├── src/
|
||||
│ │ ├── lib/
|
||||
│ │ │ ├── config.ts
|
||||
│ │ │ ├── graph-interface/ # Node editor
|
||||
│ │ │ ├── graph-manager.svelte.ts
|
||||
│ │ │ ├── graph-state.svelte.ts
|
||||
│ │ │ ├── graph-templates/ # Test templates
|
||||
│ │ │ ├── grid/
|
||||
│ │ │ ├── helpers/
|
||||
│ │ │ ├── node-registry.ts
|
||||
│ │ │ ├── node-registry/ # Node loading
|
||||
│ │ │ ├── node-store/
|
||||
│ │ │ ├── performance/
|
||||
│ │ │ ├── project-manager/
|
||||
│ │ │ ├── result-viewer/ # 3D viewer
|
||||
│ │ │ ├── runtime/ # Execution
|
||||
│ │ │ ├── settings/ # App settings
|
||||
│ │ │ ├── sidebar/
|
||||
│ │ │ └── types.ts
|
||||
│ │ └── routes/
|
||||
│ │ ├── +page.svelte
|
||||
│ │ └── +layout.svelte
|
||||
│ ├── static/
|
||||
│ │ └── nodes/
|
||||
│ │ └── max/
|
||||
│ │ └── plantarium/ # WASM nodes
|
||||
│ └── package.json
|
||||
├── docs/
|
||||
│ ├── ARCHITECTURE.md
|
||||
│ ├── DEVELOPING_NODES.md
|
||||
│ ├── NODE_DEFINITION.md
|
||||
│ └── PLANTARIUM.md
|
||||
├── nodes/ # WASM node source (Rust)
|
||||
└── package.json
|
||||
```
|
||||
|
||||
## Release Process
|
||||
|
||||
1. Create annotated tag:
|
||||
```bash
|
||||
git tag -a v1.0.0 -m "Release notes"
|
||||
git push origin v1.0.0
|
||||
```
|
||||
|
||||
2. CI workflow:
|
||||
- Runs lint, format check, type check
|
||||
- Builds project
|
||||
- Updates package.json versions
|
||||
- Generates CHANGELOG.md
|
||||
- Creates Gitea release
|
||||
@@ -4,7 +4,9 @@ export type {
|
||||
Box,
|
||||
Edge,
|
||||
Graph,
|
||||
GroupSocket,
|
||||
NodeDefinition,
|
||||
NodeGroupDefinition,
|
||||
NodeId,
|
||||
NodeInstance,
|
||||
SerializedNode,
|
||||
|
||||
@@ -51,7 +51,7 @@ export const NodeSchema = z.object({
|
||||
id: z.number(),
|
||||
type: NodeIdSchema,
|
||||
props: z
|
||||
.record(z.string(), z.union([z.number(), z.array(z.number())]))
|
||||
.record(z.string(), z.union([z.number(), z.array(z.number()), z.string()]))
|
||||
.optional(),
|
||||
meta: z
|
||||
.object({
|
||||
@@ -76,6 +76,33 @@ export type Socket = {
|
||||
|
||||
export type Edge = [NodeInstance, number, NodeInstance, string];
|
||||
|
||||
export type GroupSocket = {
|
||||
name: string;
|
||||
type: string;
|
||||
};
|
||||
|
||||
export type NodeGroupDefinition = {
|
||||
id: string;
|
||||
name: string;
|
||||
inputs: GroupSocket[];
|
||||
outputs: GroupSocket[];
|
||||
graph: {
|
||||
nodes: SerializedNode[];
|
||||
edges: [number, number, number, string][];
|
||||
};
|
||||
};
|
||||
|
||||
const NodeGroupDefinitionSchema: z.ZodType<NodeGroupDefinition> = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
inputs: z.array(z.object({ name: z.string(), type: z.string() })),
|
||||
outputs: z.array(z.object({ name: z.string(), type: z.string() })),
|
||||
graph: z.object({
|
||||
nodes: z.array(NodeSchema),
|
||||
edges: z.array(z.tuple([z.number(), z.number(), z.number(), z.string()]))
|
||||
})
|
||||
});
|
||||
|
||||
export const GraphSchema = z.object({
|
||||
id: z.number(),
|
||||
meta: z
|
||||
@@ -86,7 +113,8 @@ export const GraphSchema = z.object({
|
||||
.optional(),
|
||||
settings: z.record(z.string(), z.any()).optional(),
|
||||
nodes: z.array(NodeSchema),
|
||||
edges: z.array(z.tuple([z.number(), z.number(), z.number(), z.string()]))
|
||||
edges: z.array(z.tuple([z.number(), z.number(), z.number(), z.string()])),
|
||||
groups: z.record(z.string(), NodeGroupDefinitionSchema).optional()
|
||||
});
|
||||
|
||||
export type Graph = z.infer<typeof GraphSchema>;
|
||||
|
||||
Reference in New Issue
Block a user