10.1 节点图材质:从图到着色参数
材质决定表面长什么样:颜色、粗糙度、金属度。节点图材质让工程师把数学连成图,输出节点收集最终结果。本节把「图 → 材质参数」这一段做出来。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 图求值 | 9.2 图求值:编译与执行 | 求值器管计算;本节管材质语义 |
| 资产管线 | helio-ed-asset | 材质是资产,走资产生命周期 |
| 全卷脉络 | 本卷目录 | toc 列章节;材质编辑器是节点图的头号用户 |
从零实现:节点图材质:从图到着色参数
materialOutput 把颜色、粗糙度、金属度收集成 Material 结构。collectParams 遍历图里所有参数节点,产出去重后的参数清单。clampColor 把颜色分量裁剪到 [0,1]。isCompilable 检查输出节点是否所有必要输入都已连接。
对标材质图与参数求值
Unreal Material Editor 用节点网络描述材质。helio 示例复用前一章的数据流求值器,将指定输出节点的数据整理为材质参数。真实材质图还需检查类型、输出域与目标渲染路径,再生成并编译着色器。 Unreal Material Editor Reference
可运行产物:6 条用例
下面是 src/material-graph.ts,层级 L1,完整可编译。运行命令 node --test test/10-1-node-based-material.test.ts:
// file: src/material-graph.ts
// tier: L1
// verify: node --test test/10-1-node-based-material.test.ts
//
// 视图(第十章):材质与着色器编辑器——基于节点图的材质。
// 材质本质是一张节点图:常量节点给参数,运算节点(加/乘)组合它们,最后
// 汇入一个 output 节点的 R/G/B 三个输入端口。把图求值后得到颜色,就是
// 材质预览要显示的结果。本模块复用第九章的图求值,产出 RGB。
import type { GraphNode, Wire } from './node-graph.ts';
import { evaluateGraph } from './graph-eval.ts';
export type RGB = [number, number, number];
const MATERIAL_FNS = {
const: () => 0,
add: ([a, b]: number[]) => a + b,
mul: ([a, b]: number[]) => a * b,
};
/** 求材质图输出颜色;无 output 节点返回黑色。 */
export function materialOutput(
nodes: GraphNode[],
wires: Wire[],
defaults: Record<string, number> = {},
): RGB {
const values = evaluateGraph(nodes, wires, MATERIAL_FNS, defaults);
const out = nodes.find((n) => n.type === 'output');
if (!out) return [0, 0, 0];
const channels: Array<'r' | 'g' | 'b'> = ['r', 'g', 'b'];
return channels.map((ch) => {
const wire = wires.find((w) => w.toNode === out.id && w.toPort === ch);
if (wire) return values[wire.fromNode] ?? 0;
return defaults[`${out.id}.${ch}`] ?? 0;
}) as RGB;
}
/** 材质参数:所有常量节点的 id(Inspector 上暴露的可调项)。 */
export function collectParams(nodes: GraphNode[]): string[] {
return nodes.filter((n) => n.type === 'const').map((n) => n.id);
}
/** 钳制颜色到 [0,1]。 */
export function clampColor(c: RGB): RGB {
return c.map((v) => Math.max(0, Math.min(1, v))) as RGB;
}
/** 材质图是否可编译:有且仅有一个 output 节点。 */
export function isCompilable(nodes: GraphNode[]): boolean {
return nodes.filter((n) => n.type === 'output').length === 1;
}
isCompilable 是编译前的一道闸:输出节点的每个必要端口都连了才算可编译,缺连的端口在编辑器里高亮。collectParams 让材质图暴露参数面板,运行时改参数不重编译。颜色裁剪防止用户输入越界值。
下面是 test/10-1-node-based-material.test.ts,层级 L1:
// file: test/10-1-node-based-material.test.ts
// tier: L1
// verify: node --test test/10-1-node-based-material.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
materialOutput,
collectParams,
clampColor,
isCompilable,
} from '../src/material-graph.ts';
import type { GraphNode, Wire } from '../src/node-graph.ts';
function constNode(id: string): GraphNode {
return { id, type: 'const', ports: [{ id: 'out', name: 'Out', type: 'number', input: false }] };
}
const OUTPUT: GraphNode = {
id: 'out',
type: 'output',
ports: [
{ id: 'r', name: 'R', type: 'number', input: true },
{ id: 'g', name: 'G', type: 'number', input: true },
{ id: 'b', name: 'B', type: 'number', input: true },
],
};
test('三个常量直连 output 得到颜色', () => {
const nodes = [constNode('cr'), constNode('cg'), constNode('cb'), OUTPUT];
const wires: Wire[] = [
{ id: '1', fromNode: 'cr', fromPort: 'out', toNode: 'out', toPort: 'r' },
{ id: '2', fromNode: 'cg', fromPort: 'out', toNode: 'out', toPort: 'g' },
{ id: '3', fromNode: 'cb', fromPort: 'out', toNode: 'out', toPort: 'b' },
];
const c = materialOutput(nodes, wires, { cr: 1, cg: 0.5, cb: 0 });
assert.deepEqual(c, [1, 0.5, 0]);
});
test('未接线的通道取默认常量', () => {
const nodes = [constNode('cr'), OUTPUT];
const wires: Wire[] = [
{ id: '1', fromNode: 'cr', fromPort: 'out', toNode: 'out', toPort: 'r' },
];
const c = materialOutput(nodes, wires, { cr: 0.2, 'out.g': 0.3, 'out.b': 0.4 });
assert.deepEqual(c, [0.2, 0.3, 0.4]);
});
test('乘法节点组合两个常量', () => {
const mul: GraphNode = {
id: 'm',
type: 'mul',
ports: [
{ id: 'a', name: 'A', type: 'number', input: true },
{ id: 'b', name: 'B', type: 'number', input: true },
{ id: 'out', name: 'Out', type: 'number', input: false },
],
};
const nodes = [constNode('k'), constNode('v'), mul, OUTPUT];
const wires: Wire[] = [
{ id: '1', fromNode: 'k', fromPort: 'out', toNode: 'm', toPort: 'a' },
{ id: '2', fromNode: 'v', fromPort: 'out', toNode: 'm', toPort: 'b' },
{ id: '3', fromNode: 'm', fromPort: 'out', toNode: 'out', toPort: 'r' },
];
const c = materialOutput(nodes, wires, { k: 0.5, v: 0.8, 'out.g': 0, 'out.b': 0 });
assert.ok(Math.abs(c[0] - 0.4) < 1e-9);
});
test('collectParams 返回常量节点 id', () => {
const nodes = [constNode('a'), constNode('b'), OUTPUT];
assert.deepEqual(collectParams(nodes), ['a', 'b']);
});
test('clampColor 钳制越界值,isCompilable 要求唯一 output', () => {
assert.deepEqual(clampColor([-1, 2, 0.5]), [0, 1, 0.5]);
assert.equal(isCompilable([OUTPUT]), true);
assert.equal(isCompilable([OUTPUT, { ...OUTPUT, id: 'out2' }]), false);
assert.equal(isCompilable([constNode('x')]), false);
});
本机实测(Node v26.5.0,node --test test/10-1-node-based-material.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉输出结构与默认值。第三条钉参数收集去重。第四条钉颜色裁剪。第五、六条钉可编译判定与缺连检测。
小结
- 材质 = 图 → 参数收集 → 输出结构。
- isCompilable 在编译前拦截缺连。
- collectParams 支撑运行时参数面板。
- 材质编辑器把节点图接上了渲染语义。
阅读导航:上一节:节点图布局与撤销 · 下一节:着色器编译与热重载 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。