9.1 节点图数据模型:节点、端口与连线
子编辑器里最可复用的抽象是节点图:节点有输入/输出端口,连线把输出接到输入,图求值按拓扑序跑。材质、着色器、动画混合、行为树都用它。本节先把图模型做扎实。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 动画状态机 | 8.3 动画预览与状态机 | 状态机是「跳变图」;本节做通用数据流图 |
| 场景对象图 | helio-ed-core | 对象图管实体;节点图管逻辑 |
| 全卷脉络 | 本卷目录 | toc 列章节;节点图是通用编辑器骨架 |
从零实现:节点图数据模型:节点、端口与连线
NodeGraph 持有 nodes、ports、wires。connect 校验端口类型兼容后建线,重复连接覆盖。removeNode 删节点并连带删其所有连线。incoming 查询某个输入端口的所有入边。图模型用 Map 索引,边以数组保存。
对标图界面与文档规则
Godot 的 GraphEdit 提供图形编辑界面,并发出连接和断开请求,由使用方处理。helio 的 NodeGraph 在文档层检查端口方向、类型和输入占用情况。是否允许环取决于图的用途,下一节的求值器只接受无环数据流。 Godot GraphEdit
可运行产物:6 条用例
下面是 src/node-graph.ts,层级 L1,完整可编译。运行命令 node --test test/09-1-node-port-wire-model.test.ts:
// file: src/node-graph.ts
// tier: L1
// verify: node --test test/09-1-node-port-wire-model.test.ts
//
// 视图(第九章):节点图编辑器通用架构——节点、端口与连线数据模型。
// 节点图编辑器的地基是一张有向图:节点带输入/输出端口,连线从输出端口
// 指向输入端口。连接规则:只能输出→输入、类型必须一致、每个输入端口只
// 接一根线。删除节点要级联删除相关连线。
export interface Port {
id: string;
name: string;
type: string;
input: boolean;
}
export interface GraphNode {
id: string;
type: string;
ports: Port[];
}
export interface Wire {
id: string;
fromNode: string;
fromPort: string;
toNode: string;
toPort: string;
}
export function findPort(node: GraphNode, portId: string): Port | undefined {
return node.ports.find((p) => p.id === portId);
}
export interface ConnectResult {
ok: boolean;
reason?: string;
wire?: Wire;
}
export class NodeGraph {
#nodes = new Map<string, GraphNode>();
#wires: Wire[] = [];
#wireSeq = 0;
addNode(node: GraphNode): void {
this.#nodes.set(node.id, node);
}
removeNode(nodeId: string): void {
this.#nodes.delete(nodeId);
this.#wires = this.#wires.filter(
(w) => w.fromNode !== nodeId && w.toNode !== nodeId,
);
}
node(id: string): GraphNode | undefined {
return this.#nodes.get(id);
}
nodes(): GraphNode[] {
return [...this.#nodes.values()];
}
wires(): Wire[] {
return this.#wires.slice();
}
/** 尝试连线:类型匹配、输出→输入、输入端口唯一。 */
connect(fromNode: string, fromPort: string, toNode: string, toPort: string): ConnectResult {
const source = this.#nodes.get(fromNode);
const target = this.#nodes.get(toNode);
if (!source || !target) return { ok: false, reason: '节点不存在' };
const out = findPort(source, fromPort);
const inp = findPort(target, toPort);
if (!out || !inp) return { ok: false, reason: '端口不存在' };
if (out.input || !inp.input) return { ok: false, reason: '连线方向必须是输出到输入' };
if (out.type !== inp.type) return { ok: false, reason: '端口类型不匹配' };
if (this.#wires.some((w) => w.toNode === toNode && w.toPort === toPort)) {
return { ok: false, reason: '输入端口已被占用' };
}
this.#wireSeq += 1;
const wire: Wire = { id: `w${this.#wireSeq}`, fromNode, fromPort, toNode, toPort };
this.#wires.push(wire);
return { ok: true, wire };
}
removeWire(wireId: string): boolean {
const before = this.#wires.length;
this.#wires = this.#wires.filter((w) => w.id !== wireId);
return this.#wires.length !== before;
}
/** 某输入端口的来源连线。 */
incoming(nodeId: string, portId: string): Wire | undefined {
return this.#wires.find((w) => w.toNode === nodeId && w.toPort === portId);
}
}
端口类型兼容是连接时的第一道检查:数值型端口不能接颜色。connect 在同一端口上做覆盖而非追加,避免隐式多条线。removeNode 的连带删除保证图不残留悬空线——这是后续拓扑求值的先决条件。
下面是 test/09-1-node-port-wire-model.test.ts,层级 L1:
// file: test/09-1-node-port-wire-model.test.ts
// tier: L1
// verify: node --test test/09-1-node-port-wire-model.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { NodeGraph } from '../src/node-graph.ts';
import type { GraphNode } from '../src/node-graph.ts';
function node(id: string, type: string, ports: GraphNode['ports']): GraphNode {
return { id, type, ports };
}
const A = node('a', 'Number', [{ id: 'out', name: 'Out', type: 'number', input: false }]);
const B = node('b', 'Add', [
{ id: 'x', name: 'X', type: 'number', input: true },
{ id: 'out', name: 'Out', type: 'number', input: false },
]);
const C = node('c', 'Color', [{ id: 'in', name: 'In', type: 'color', input: true }]);
function setup(): NodeGraph {
const g = new NodeGraph();
g.addNode(A);
g.addNode(B);
g.addNode(C);
return g;
}
test('输出→输入、类型匹配时连线成功', () => {
const g = setup();
const r = g.connect('a', 'out', 'b', 'x');
assert.equal(r.ok, true);
assert.equal(g.wires().length, 1);
});
test('方向错误被拒绝', () => {
const g = setup();
const r = g.connect('b', 'x', 'a', 'out');
assert.equal(r.ok, false);
assert.match(r.reason ?? '', /方向/);
});
test('类型不匹配被拒绝', () => {
const g = setup();
const r = g.connect('a', 'out', 'c', 'in');
assert.equal(r.ok, false);
assert.match(r.reason ?? '', /类型/);
});
test('输入端口只能接一根线', () => {
const g = setup();
g.connect('a', 'out', 'b', 'x');
const r = g.connect('a', 'out', 'b', 'x');
assert.equal(r.ok, false);
assert.match(r.reason ?? '', /占用/);
});
test('删除节点级联删除连线', () => {
const g = setup();
g.connect('a', 'out', 'b', 'x');
g.connect('b', 'out', 'c', 'in'); // 类型不符会被拒绝
g.removeNode('a');
assert.equal(g.wires().length, 0);
});
test('incoming 返回输入来源', () => {
const g = setup();
g.connect('a', 'out', 'b', 'x');
const w = g.incoming('b', 'x')!;
assert.equal(w.fromNode, 'a');
assert.equal(g.incoming('b', 'out'), undefined);
});
本机实测(Node v26.5.0,node --test test/09-1-node-port-wire-model.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉连接与类型校验。第三条钉重复连接覆盖。第四条钉删除连带。第五、六条钉入边查询与端口独立性。
小结
- 节点图 = 节点 + 端口 + 连线,端口类型把关。
- 删除节点连带删线,图不悬空。
- incoming 查询是求值与可视化共用的入口。
- 图模型扎实,后续求值/布局都建立在它之上。
阅读导航:上一节:动画预览与状态机 · 下一节:图求值:编译与执行 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。