编译器行为
诊断与排错
DreamShaderLang 常见错误、定位来源和处理方法。
DreamShaderLang 的错误大致分为四层:本地编辑诊断、解析错误、生成错误、Unreal 材质编译错误。
| 来源 | 说明 |
|---|
| VSCode 本地诊断 | 编辑时发现语法、引用和简单语义问题。 |
| Unreal Parser | 保存或重编时解析 .dsm / .dsh。 |
| Material Generator | 创建节点、连接输出、应用设置时产生。 |
| Unreal Material Compiler | Unreal 自身材质编译报错。 |
当 .dsm import 了 .dsh,DreamShader 会尽量把错误映射回真实源文件行列:
DShader/Shared/Common.dsh:12:5
这对共享 helper 很重要。否则所有错误都会落到展开后的合并文本里,难以定位。
| 错误 | 原因 | 处理 |
|---|
Shader(Name="...") is required | Shader 缺少 Name。 | 补上资产路径。 |
Only one top-level Shader block is currently supported | 一个文件里写了多个 Shader。 | 拆成多个 .dsm。 |
Shader must provide a Graph block | Shader 没有 Graph。 | 添加 Graph = { ... }。 |
Unknown shader section | Shader 中写了不支持的 section。 | 使用 Properties / Settings / Outputs / Graph。 |
Shader graph sections now use Graph | 还在 Shader 里使用 Code。 | 改为 Graph。 |
Namespace may only contain Function blocks | namespace 里放了其他顶层块。 | 只保留 Function。 |
| 错误 | 原因 | 处理 |
|---|
Unknown Graph function | 函数未定义或未 import。 | 检查 import 和 namespace。 |
Graph variable is declared more than once | 重复声明局部变量。 | 改名或复用已有变量。 |
Arithmetic operators cannot be applied to Texture2D values | 对纹理对象做算术。 | 先采样,或只对采样结果运算。 |
String literals can only be used in named UE builtin arguments | 字符串作为普通值使用。 | 只在 UE.* 命名参数里使用字符串。 |
Graph if condition ... must evaluate to a scalar value | 条件不是标量。 | 使用 .r、.x 或 ComponentMask。 |
Graph if statement cannot select Texture2D value | 分支选择纹理对象。 | 分支选择采样后的数值。 |
| 错误 | 原因 | 正确写法 |
|---|
must be called with explicit out variables | 把 Function 当返回值函数。 | ApplyTint(a, b, result); |
currently uses positional arguments only | 使用了命名参数。 | Fn(a, b, outValue); |
out argument must be a plain variable name | out 位置传了表达式。 | 先声明变量再传入。 |
expects N arguments | 输入和输出目标数量不对。 | 参数数量必须等于 in 加 out。 |
| 错误 | 原因 | 处理 |
|---|
Unsupported UE builtin call | 调用了未注册的 UE.*,又没写 OutputType。 | 使用 UE.Expression(..., OutputType="...")。 |
Generic UE.* calls require named arguments | 泛型调用用了位置参数。 | 改成命名参数。 |
Class must be a literal value | Class 不是字符串或字面量。 | 写 Class="Sine"。 |
OutputType is not supported | 输出类型不在支持范围。 | 使用 float1/2/3/4 或纹理类型。 |
OutputIndex is out of range | 输出索引不存在。 | 检查 MaterialExpression 输出。 |
| 错误 | 原因 | 处理 |
|---|
Unsupported material output | Base.* 名称不支持。 | 查 输出绑定。 |
bound material property expects a different type | 输出变量类型与目标不匹配。 | 修改声明类型或绑定目标。 |
Expression output target must use .Pin[index] | 辅助输出节点格式错误。 | 使用 Expression(Class="...").Pin[0]。 |
- 先看 VSCode 红线,修掉明显语法和 import 问题。
- 执行
DreamShaderLang: Recompile Current Source。
- 看 Unreal Output Log 或 VSCode 桥接诊断。
- 如果是材质编译器错误,定位到生成的 Custom 节点代码。
- 对复杂逻辑,先缩小到最小
.dsm,再逐步恢复 import 和 helper。