ush-develop
AesWorld USH Material V2 框架维护与开发指南。Use when: 给 USH 框架添加新参数类型、 修改解析器/表达式节点/材质构建逻辑、调试 USH 相关编译错误、 理解 USH 内部架构。 Triggers: EarthUshMaterialBuildUtils, EarthUshFileParser, UMaterialExpressionCustom, BuildM
SKILL.md.claude/skills/ush-develop/SKILL.md
GitHubUSH Material V2 框架维护手册
读者: AI agent / 人类开发者 目标: 维护和扩展 USH Material V2 框架——添加新参数类型、修改解析逻辑、调试编译错误 框架入口: Source/EarthModeler/Public/Material/
快速导航
| 要做什么 | 读哪一节 |
|---|---|
| 写一个新的 .ush 材质 | §1 文件骨架 + §14 完整起手示例(或用 ush-create skill) |
| 了解支持哪些参数类型 | §2 参数类型 |
| 参数行语法 | §3 参数行语法 |
| 定元数据(材质域/混合模式) | §4 元数据注解 |
| FMaterialAttributes 有哪些字段 | §5 FMaterialAttributes 字段 |
| HLSL 里怎么采纹理 | §6 HLSL 采样 API |
| 参数顺序规则 | §7 参数排序铁律 |
| 虚拟路径映射 | §8 虚拟路径 |
| 选模板 | §9 分场景参考模板 |
| AI 产出后自检 | §10 自检清单 |
| 编译报错 | §11 常见错误诊断 |
| 参数→节点映射 | §12 参数名→MaterialParameter 映射 |
| 给框架加新类型 | §13 迭代模板 |
| 完整代码示例 | §14 完整起手示例 |
| 人工编辑保护 | §0.2 V2 维护边界 |
§0 开发者标准操作流程
收到 USH 框架维护任务后:
- 理解变更范围:确定涉及哪些模块(解析器 / 表达式节点 / 材质构建 / 导入工厂)
- 阅读相关章节:按快速导航表定位
- 找到源码:参考附录源码索引定位目标文件
- 修改代码:遵循 §13 迭代模板的同步修改清单
- 创建/修改测试模板:在
Templates/中添加或修改模板验证功能 - 编译验证:UHT/UBT 编译通过
- 导入测试:用测试模板导入验证完整流程
- 更新本手册:同步更新 §2/§4/§6 等受影响章节
§0.1 系统架构概述
USH Material V2 通过 .ush 文件 + 注释元数据自动生成普通 UE UMaterial。
(编写/修改)
开发者 ─────────────────▶ .ush 文件
│
(内容监听) │
Import/Reimport │
▼
UEarthUshMaterialFactory
(EarthUshMaterialFactory.cpp)
│
▼
EarthUshMaterialBuildUtils::BuildMaterialFromUsh
(EarthUshMaterialBuildUtils.cpp)
│
┌──────────────────┼──────────────────┐
▼ ▼ ▼
FEarthUshFileParser WritePortableUshFiles BuildUshMaterialExpressions
(解析元数据/参数) (写入便携 .ush) (构建 UMaterial 节点图)
│
▼
UMaterialExpressionCustom
(UE 原生 Custom Code 节点)
│
▼
Shader Map → GPU
关键事实: .ush 只负责产出 FMaterialAttributes;所有材质域、混合模式、参数声明通过注释元数据表达。框架自动构建等价的普通 UMaterial 节点图。
§0.2 V2 维护边界
- 导入结果必须是普通
UMaterial,命名规则为M_<AssetName>。 - Custom HLSL 必须使用 UE 原生
UMaterialExpressionCustom,不要恢复 USH 专属材质表达式节点。 - 右键菜单只保留 USH 重建/重新绑定能力;材质实例创建使用 UE 原生 Create Material Instance。
AssetImportData保存源.ush磁盘路径,UPackage::UMetaData保存 USH 绑定信息和ExpectedMaterialGraphHash。- Reimport/Rebuild/Rebind 在检测到人工编辑时必须弹确认;文件监听自动同步检测到人工编辑时只跳过并提示,不弹 modal。
ExpectedMaterialGraphHash缺失时按可能人工修改处理,避免旧资产首次重导入静默覆盖。
1. 文件骨架(必须遵循)
// <简短描述>
//
// @material_domain: <Surface|Decal|PostProcess|LightFunction|UI>
// @blend_mode: <Opaque|Masked|Translucent|Additive|Modulate>
// @shading_model: <DefaultLit|Unlit|Subsurface|ClearCoat|SubsurfaceProfile|TwoSidedFoliage>
// @two_sided: <true|false>
// @entry: <函数名> ← 必填
//
// @param <Type> <Name> [= <Default>] [[Min, Max]] [[group=GroupName]] [[desc=Description]] [[sort=N]] [[cpd=N]] [[channels=R,G,B[,A]]]
// @param ...
#pragma once
#include "/Plugin/AesWorld/EarthMaterialUtils.ush"
FMaterialAttributes <函数名>(
<按 @param 声明顺序的函数参数>,
<Texture2D 后必须紧跟 SamplerState XxxSampler>)
{
FMaterialAttributes Result = EarthMakeDefaultMaterialAttributes();
// ... 赋值 Result.BaseColor / Metallic / Roughness / ...
return Result;
}
硬约束:
@entry必填,函数名必须与@entry完全一致- 函数返回类型必须是
FMaterialAttributes - 必须
#include "/Plugin/AesWorld/EarthMaterialUtils.ush"(提供默认值) - 必须用
EarthMakeDefaultMaterialAttributes()初始化结果(零初始化会导致 Opacity=0 等错误) #pragma once避免重复 include- 所有注解行必须是 HLSL 注释
//+@key: value格式
2. 参数类型(11 种)
| 注解类型 | HLSL 类型 | 函数参数形式 | 默认值格式 | 备注 |
|---|---|---|---|---|
float |
float |
float Name |
0.5 |
标量 |
float2 |
float2 |
float2 Name |
(1.0, 1.0) |
|
float3 |
float3 |
float3 Name |
(1.0, 0.5, 0.2) |
颜色/方向 |
float4 |
float4 |
float4 Name |
(1, 1, 1, 1) |
RGBA/IOR等 |
bool |
float |
float Name |
true / false |
函数签名写 float;HLSL 中用 if (Name > 0.5) 判断 |
Texture2D |
Texture2D |
Texture2D Name, SamplerState NameSampler |
"" |
必须紧跟同名 Sampler |
TextureCube |
TextureCube |
TextureCube Name, SamplerState NameSampler |
"" |
同上 |
Texture2DArray |
Texture2DArray |
Texture2DArray Name, SamplerState NameSampler |
"" |
同上;采样 UV=float3(u,v,idx) |
VolumeTexture / Texture3D |
Texture3D |
Texture3D Name, SamplerState NameSampler |
"" |
注解写 VolumeTexture,函数签名写 Texture3D |
TextureCubeArray |
TextureCubeArray |
TextureCubeArray Name, SamplerState NameSampler |
"" |
需 #if FEATURE_LEVEL >= FEATURE_LEVEL_SM5 保护 |
TexCoord |
float2 |
float2 Name |
0 |
自动连接到 TextureCoordinate 节点(索引=默认值) |
实现参考:
- 类型枚举:
EEarthUshParamType - 字符串映射:
FEarthUshFileParser::StringToParamType() - 节点创建与默认纹理:
EarthUshMaterialBuildUtils::BuildMaterialFromUsh()
3. 参数行语法
// @param <Type> <Name> [= <Default>] [[Min, Max]] [[group=GroupName]] [[desc=Description]] [[sort=N]] [[cpd=N]] [[channels=R,G,B[,A]]]
解析规则 (FEarthUshFileParser::ParseParamLine()):
| 组成 | 是否可选 | 示例 | 规则 |
|---|---|---|---|
<Type> |
必填 | float, Texture2D |
见 §2 列表,大小写敏感 |
<Name> |
必填 | Roughness |
合法 HLSL 标识符 [a-zA-Z_][a-zA-Z0-9_]* |
= <Default> |
可选 | = 0.5, = (1,0,0), = true |
纹理/TexCoord 可省略 |
[Min, Max] |
可选 | [0.0, 1.0] |
仅标量在 MIC 编辑器显示为滑块 |
[group=X] |
可选 | [group=PBR Textures] |
MIC 分组;名字可含空格 |
[desc=X] |
可选 | [desc=控制金属度缩放] |
参数描述,显示为材质编辑器注释气泡 / MI 中的 tooltip |
[sort=N] |
可选 | [sort=5] |
参数排序优先级(整数);未指定时按声明顺序自动分配 0, 1, 2, ... |
[cpd=N] |
可选 | [cpd=0] |
Custom Primitive Data 槽位索引 (0–31);对 Scalar、Bool 和 Vector 类型有效 |
[channels=R,G,B[,A]] |
可选 | [channels=X,Y,Z] |
Vector 通道自定义名称,2、3 或 4 个逗号分隔;仅对 Vector2/3/4 有效 |
合法示例:
// @param float Roughness = 0.5 [0.0, 1.0] [group=PBR]
// @param float3 BaseColorTint = (1, 1, 1) [group=Color]
// @param Texture2D AlbedoMap [group=Textures]
// @param Texture2D MaskMap = "" [group=Masks]
// @param TexCoord UV0 = 0
// @param bool bUseEmissive = false [group=Toggle]
// @param float4 CustomData = (1, 2, 3, 4)
// @param float MetallicScale = 1.0 [0.0, 2.0] [group=PBR] [desc=控制金属度的全局缩放系数]
// @param float WindStrength = 1.0 [0.0, 5.0] [group=Dynamics] [desc=风力强度] [cpd=0]
// @param float3 EnvDirection = (0,0,1) [group=Env] [channels=X,Y,Z]
// @param float4 SubsurfaceParams = (1,0.2,0.1,1) [group=SSS] [desc=次表面散射参数] [channels=散射R,散射G,散射B,强度] [cpd=4]
// @param float AO = 1.0 [0.0, 1.0] [sort=99]
// @param VolumeTexture NoiseVol [group=3D Textures]
// @param TextureCubeArray SkyCubes [group=Environment]
应避免(解析器不主动拦截,但将导致运行时错误或歧义行为):
// @param float = 0.5 ← 缺参数名
// @param MyType X = 0 ← 未知类型(降级为 Scalar 并输出 Warning)
// @param float My Value = 0 ← 名字含空格
// @param float XxxSampler ← 后缀"Sampler"保留(框架自动生成)
// @param Texture2D BaseColor ← 与 FMaterialAttributes 字段同名(歧义)
// @param float A = 0 ... @param float A = 1 ← 重复参数名(MIC 中只显示一个)
4. 元数据注解
实现: FEarthUshFileParser::ParseMetadataLine()
| 注解 | 合法值(大小写不敏感) | 默认 |
|---|---|---|
@material_domain |
Surface, Decal, PostProcess, LightFunction, UI |
Surface |
@blend_mode |
Opaque, Masked, Translucent, Additive, Modulate |
Opaque |
@shading_model |
DefaultLit, Unlit, Subsurface, ClearCoat, SubsurfaceProfile, TwoSidedFoliage |
DefaultLit |
@two_sided |
true, false |
false |
@entry |
合法 HLSL 函数名 | 必填 |
选取指南:
Masked必须赋值Result.OpacityMask(<0.5像素丢弃)Translucent/Additive/Modulate必须赋值Result.OpacityUnlit只需Result.EmissiveColor(其他 PBR 字段被忽略)Subsurface/SubsurfaceProfile必须赋Result.SubsurfaceColorClearCoat必须赋Result.ClearCoat+Result.ClearCoatRoughnessTwoSidedFoliage自动双面,无需再设@two_sided
4.1 扩展材质属性注解
除上述 5 个核心注解外,USH 还支持直接设置 UMaterial 的任意可编辑属性。注解键使用 UMaterial C++ 属性名(大小写不敏感),通过 UE 反射系统自动应用。
语法: // @<UMaterial属性名>: <值>
实现: UshApplyExtendedMaterialProperties()
示例:
// @OpacityMaskClipValue: 0.5
// @bFullyRough: true
// @bTwoSided: true
// @bUsedWithSkeletalMesh: true
// @bUsedWithNanite: true
// @TranslucencyLightingMode: TLM_Surface
// @bScreenSpaceReflections: true
// @bCastRayTracedShadows: false
// @RefractionDepthBias: 0.1
类型处理规则:
| 类型 | 值格式 | 示例 |
|---|---|---|
| bool | true/false/yes/no/1/0(大小写不敏感) |
// @bFullyRough: true |
| float | 数值 | // @OpacityMaskClipValue: 0.333 |
| int32 | 整数 | // @BlendablePriority: 10 |
| enum | UE 枚举值名称 | // @TranslucencyLightingMode: TLM_VolumetricNonDirectional |
| FLinearColor | (R,G,B,A) |
// @TranslucentMultipleScatteringExtinction: (1,1,1,1) |
被阻止的属性(由核心逻辑管理,不允许通过扩展注解覆盖):
MaterialDomain→ 用@material_domainBlendMode→ 用@blend_modeShadingModel→ 用@shading_modelTwoSided→ 用@two_sidedbUseMaterialAttributes→ USH 架构强制为true
设计要点:
- 未声明的属性保持 UMaterial 默认值,无需列出所有属性
- 无效属性名产生 Warning,不会崩溃
- 引擎新增的 UMaterial 属性自动可用,无需更新 USH 系统代码
5. FMaterialAttributes 字段
实现: Shaders/EarthMaterialUtils.ush
EarthMakeDefaultMaterialAttributes() 初始化后的值:
| 字段 | 类型 | 默认值 | 用途 |
|---|---|---|---|
BaseColor |
float3 |
(0, 0, 0) |
漫反射 |
Metallic |
float |
0.0 |
金属度 [0,1] |
Specular |
float |
0.5 |
镜面强度 [0,1],默认 0.5 不是 0 |
Roughness |
float |
0.5 |
粗糙度 [0,1] |
Anisotropy |
float |
0.0 |
各向异性 |
EmissiveColor |
float3 |
(0, 0, 0) |
自发光 |
Opacity |
float |
1.0 |
默认 1 |
OpacityMask |
float |
1.0 |
默认 1(Masked 必须赋值) |
Normal |
float3 |
(0, 0, 1) |
切线空间,默认 Z+ |
Tangent |
float3 |
(1, 0, 0) |
切线空间 |
WorldPositionOffset |
float3 |
(0, 0, 0) |
顶点位移 |
Displacement |
float |
0.5 |
|
SubsurfaceColor |
float3 |
(1, 1, 1) |
|
ClearCoat |
float |
1.0 |
|
ClearCoatRoughness |
float |
0.1 |
|
AmbientOcclusion |
float |
1.0 |
|
Refraction |
float3 |
(1, 0, 0) |
(IOR, 0, 0) |
PixelDepthOffset |
float |
0.0 |
关键: 绝不要 FMaterialAttributes Result = (FMaterialAttributes)0;(会把 Opacity/OpacityMask/AmbientOcclusion 全置 0,法线置零向量,导致渲染全黑或全透明)。
6. HLSL 采样 API
全部由引擎提供,直接在 .ush 入口函数内调用:
// Texture2D:
float4 Tex2D = Texture2DSample(AlbedoMap, AlbedoMapSampler, UV);
// TextureCube(Direction 建议 normalize):
float4 Cube = TextureCubeSample(EnvMap, EnvMapSampler, normalize(Dir));
// Texture2DArray(UVW.z = layer 整数):
float4 Layer = Texture2DArraySample(Layers, LayersSampler, float3(UV, floor(Index)));
// Texture3D(三维坐标):
float4 Vol = Texture3DSample(NoiseVol, NoiseVolSampler, float3(UV, Slice));
// TextureCubeArray(必须 SM5+ 保护!):
#if FEATURE_LEVEL >= FEATURE_LEVEL_SM5
float4 SkyColor = TextureCubeArraySample(SkyCubes, SkyCubesSampler, float4(Dir, floor(Idx)));
#else
float4 SkyColor = float4(0, 0, 0, 1); // ES3.1/SM4 回退
#endif
SamplerState 命名铁律: 若参数名是 AlbedoMap,对应 Sampler 必须命名为 AlbedoMapSampler(ParamName + "Sampler")。这是 UE5 UMaterialExpressionCustom 的自动生成约定,框架依赖此规则。
7. 参数排序铁律
包装代码生成器 (EarthUshMaterialBuildUtils::GenerateWrapperCode()) 从 HLSL 函数签名解析 FunctionParamOrder 作为权威顺序。
强烈建议: @param 注解顺序 = 函数签名参数顺序。让两者完全对齐可避免一切歧义。
纹理参数的签名扩展规则:
// 注解:
// @param Texture2D AlbedoMap
// 函数签名必须:
Texture2D AlbedoMap,
SamplerState AlbedoMapSampler, // ← 紧跟,命名 = Name + "Sampler"
Bool 特殊:
// @param bool bEnabled = true
// 函数签名必须写 float(不是 bool):
float bEnabled,
// HLSL 内用:if (bEnabled > 0.5) { ... }
8. 虚拟路径
解析器: FEarthModelerModule::ResolveVirtualPathToDisk()
| 虚拟路径 | 磁盘解析 |
|---|---|
/Plugin/AesWorld/<rest> |
<PluginDir>/Shaders/<rest> |
/Project/<rest> |
<ProjectDir>/Shaders/<rest> |
本框架 .ush 模板放在 /Plugin/AesWorld/Materials/Templates/<Name>.ush(即 Plugins/AesWorld/Shaders/Materials/Templates/<Name>.ush)。
9. 分场景参考模板
所有模板位于 Templates/:
WARNING: Templates/ 目录是只读参考。不要直接修改模板文件。需要新模板时,复制一个模板到新文件再修改。
| 场景 | 模板文件 | 涉及类型 |
|---|---|---|
| 最简 PBR | EarthDefaultSurfaceMaterial.ush | Scalar, Vector3 |
| 标准 PBR | EarthBasicSurface.ush | Scalar, Vector3 |
| 纹理 PBR | EarthTexturedPBR.ush | Texture2D, TexCoord |
| 多 UV 采样 | EarthMultiUVTextured.ush | Texture2D, TexCoord×N |
| 遮罩纹理 | EarthMaskedTextured.ush | Texture2D + blend_mode: Masked |
| 遮罩 Alpha | EarthMaskedAlpha.ush | Scalar OpacityMask |
| 透明玻璃 | EarthTranslucentGlass.ush | blend_mode: Translucent + Refraction |
| 自发光 | EarthUnlitMaterial.ush | shading_model: Unlit |
| 自发光光晕 | EarthEmissiveGlow.ush | Unlit + Translucent |
| 双面植被 | EarthFoliageTwoSided.ush | TwoSidedFoliage + Subsurface |
| 次表面皮肤 | EarthSubsurfaceSkin.ush | shading_model: Subsurface |
| 清漆汽车 | EarthClearCoatCar.ush | shading_model: ClearCoat |
| 立方环境 | EarthCubeEnvMap.ush | TextureCube |
| 立方数组天空盒 | EarthCubeArraySkybox.ush | TextureCubeArray + SM5 保护 |
| 地形纹理数组 | EarthTex2DArrayTerrain.ush | Texture2DArray |
| 体积雾 | EarthVolumeFog.ush | VolumeTexture + Translucent |
| 世界位移 | EarthWorldOffset.ush | WorldPositionOffset |
| 全 PBR | EarthFullPBR.ush | 所有 PBR 字段 |
| 向量测试 | EarthVectorParamsTest.ush | Vector2/3/4 |
| 全类型集大成 | EarthAllParamsShowcase.ush | 11 种全覆盖 |
AI 工作法: 根据需求挑选最接近的模板作为起点,按需添加/删除参数。
10. 自检清单(AI 产出后必须逐项确认)
- 首行
@material_domain、@blend_mode、@shading_model、@entry齐全 -
@entry函数名 = HLSL 函数名(大小写一致) - 函数返回
FMaterialAttributes - 首行
FMaterialAttributes Result = EarthMakeDefaultMaterialAttributes(); - 含
#pragma once+#include "/Plugin/AesWorld/EarthMaterialUtils.ush" - 每个
@param的Type在 §2 列表中 - 每个 Texture* 参数在函数签名中紧跟同名
Sampler(Name + "Sampler") - bool 参数在函数签名中写成
float -
VolumeTexture注解在函数签名中写成Texture3D -
TextureCubeArraySample调用被#if FEATURE_LEVEL >= FEATURE_LEVEL_SM5包裹 - 未赋值
.BaseColor / .Opacity / .OpacityMask / .Normal(依赖默认值时) -
@blend_mode: Masked时赋值了Result.OpacityMask -
@blend_mode: Translucent/Additive/Modulate时赋值了Result.Opacity -
@shading_model: ClearCoat时赋值了Result.ClearCoat+Result.ClearCoatRoughness -
@shading_model: Subsurface*时赋值了Result.SubsurfaceColor - 参数名没有后缀
Sampler - 参数名没有与 FMaterialAttributes 字段同名(BaseColor/Metallic/...)
-
@param注解顺序 = 函数签名参数顺序(排除自动生成的 SamplerState) -
[cpd=N]仅用于 Scalar、Bool 和 Vector 类型,槽位索引在 0–31 范围内;Vector3 占 3 个连续槽位、Vector4 占 4 个,起始索引 + 分量数 ≤ 32 -
[channels=]仅用于 Vector2/3/4,通道名数量与分量数匹配(2、3 或 4 个) - float4 参数的 MIC 修改能影响第四分量(通过 AppendVector 中转实现)
-
[desc=]内容简洁有意义,不得包含[或]字符(会截断 bracket 解析) -
[sort=N]的 N 为非负整数;负数等同于未指定(被自动分配覆盖) - 参数名不得重复(同名参数在 MIC 中只显示一个)
11. 常见错误诊断
编译错误
| 错误 | 原因 | 修复 |
|---|---|---|
redefinition of '<EntryFunc>' |
.ush 被双重 include |
加 #pragma once |
use of undeclared identifier 'Result.XXX' |
FMaterialAttributes 字段名拼错 | 对照 §5 字段表 |
call to undeclared function 'TextureCubeArraySample' |
SM4/ES3.1 不支持 | 加 #if FEATURE_LEVEL >= FEATURE_LEVEL_SM5 |
cannot convert from 'Texture2D' to 'Texture3D' |
类型占位纹理错误 | 检查 @param 类型与函数签名类型是否匹配 |
parameter 'XxxSampler' redefinition |
手动声明了 SamplerState 参数 | 删除,由 Texture 参数自动生成 |
parameter count mismatch |
签名参数数 ≠ 注解+Sampler 扩展数 | 逐一对比 §7 顺序规则 |
Unknown param type 'XXX', defaulting to Scalar |
参数类型拼写错误(大小写敏感) | 对照 §2 类型表修正(如 texture2d → Texture2D) |
运行时问题
| 现象 | 原因 | 修复 |
|---|---|---|
| 渲染全黑 | 未初始化就赋值 Result.Normal 等 |
用 EarthMakeDefaultMaterialAttributes() |
| 全透明 | 零初始化导致 Opacity=0 | 同上 |
| Masked 模式模型整片消失 | OpacityMask 默认 0 或全部 <0.5 |
检查 OpacityMask 计算 |
| MIC 修改参数无效 | 参数名与引擎 Material 其他节点冲突 | 改名,避开 FMaterialAttributes 字段名 |
| 花屏/噪点 | 直接连 MaterialAttributes 到输出 |
框架已自动插 Break+Make 中转,不要手动连 |
| CPD 运行时不生效 | [cpd=N] 用在了 Texture/TexCoord 上,或 N > 31 |
确认 CPD 仅用于 Scalar、Bool 和 Vector,索引在 0–31 范围内 |
| channels 名称未显示 | [channels=] 用在了非 Vector 类型上 |
仅对 Vector2/3/4 有效,从其他类型上移除 channels 标签 |
| MIC 修改 float4 的 Alpha 无效 | AppendVector 中转节点创建失败,回退为直连 float3 | 检查日志 Failed to create AppendVector,清理材质后重新构建 |
[desc=] 内容被截断 |
描述中含 ] 字符导致 bracket 解析提前关闭 |
desc 内容避免使用 [ 和 ] |
[sort=N] 指定了但排序不生效 |
N 为负数,被自动分配逻辑覆盖 | sort 值必须 ≥ 0 |
热重载未触发
- 确认
.ush位于被监听的 Shaders 目录下(Plugins/AesWorld/Shaders/或<ProjectDir>/Shaders/) - 导入时日志
LogEarthUshAsset应显示路径解析成功 - 手动触发: 资产上右键 → USH Material → Rebuild from USH
12. 参数名 → 生成的 MaterialParameter 映射
EarthUshMaterialBuildUtils::BuildMaterialFromUsh() 创建并连接的表达式节点:
| .ush 参数类型 | 生成的 UMaterialExpression 类 | 在 MIC 中的表现 |
|---|---|---|
| Scalar | UMaterialExpressionScalarParameter |
Scalar 参数(支持 Desc / SortPriority / CPD) |
| Bool | UMaterialExpressionScalarParameter |
Scalar 参数(显示为 0/1,SliderMin=0 / SliderMax=1 硬编码不可自定义,支持 Desc / SortPriority / CPD) |
| Vector2/3 | UMaterialExpressionVectorParameter |
Vector 参数(支持 Desc / SortPriority / CPD / ChannelNames),直连 USH Shader |
| Vector4 | UMaterialExpressionVectorParameter → UMaterialExpressionAppendVector |
Vector 参数,通过 AppendVector 将 RGB + A 合成 float4 后传给 USH Shader,确保第四分量可在 MIC 中独立修改 |
| TexCoord | UMaterialExpressionTextureCoordinate |
非参数,硬连接 |
| Texture* | UMaterialExpressionTextureObjectParameter |
Texture 参数(支持 Desc / SortPriority) |
通用参数属性映射
| 注解标签 | 对应 UMaterialExpression 属性 | 效果 |
|---|---|---|
[group=X] |
Group |
MIC 参数分组 |
[Min, Max] |
SliderMin / SliderMax |
仅 Scalar,MIC 滑块范围 |
[desc=X] |
Desc |
材质编辑器注释气泡 + MI tooltip |
[sort=N] |
SortPriority |
参数在属性面板中的排序顺序;未指定时按声明顺序自动分配 0, 1, 2, ... |
[cpd=N] |
bUseCustomPrimitiveData + PrimitiveDataIndex |
参数绑定到 Custom Primitive Data 槽位,允许运行时通过 SetCustomPrimitiveDataFloat/Vector 动态驱动 |
[channels=R,G,B[,A]] |
ChannelNames (FParameterChannelNames) |
Vector 通道自定义名称,在材质编辑器中显示为通道标签 |
float4 AppendVector 中转机制
UE5 的 UMaterialExpressionVectorParameter 默认输出为 float3(RGB),第四分量(A)单独输出。为了让 float4 参数的四个分量都能正确传递到 USH Shader,框架自动插入 AppendVector 节点:
VectorParameter(RGB) ──▶ AppendVector.A ──▶ USH Shader Input
VectorParameter(A) ──▶ AppendVector.B ─┘
这确保在 MIC 中修改 Alpha 通道的值能实际生效,而不是被截断为 float3。
完整节点图拓扑
生成的材质节点图并非直接连接 USH Shader 到 Material Output,而是插入了 Break+Make MaterialAttributes 中转层以规避 Substrate 数据初始化导致的渲染伪影:
ScalarParam ──┐
VectorParam ──┤ Material
TextureParam ─┼──▶ UMaterialExpressionCustom ──▶ BreakMaterialAttributes ──▶ MakeMaterialAttributes ──▶ Output
TexCoord ─────┘ (FMaterialAttributes) (逐字段拆解) (逐字段重组)
Texture 子类型默认纹理
框架根据纹理子类型自动加载引擎默认纹理,确保 Texture->GetMaterialType() 推断正确的 MCT 类型:
| 子类型 | 默认纹理 |
|---|---|
Texture2D |
null(引擎默认 MCT_Texture2D) |
TextureCube |
/Engine/.../DefaultTextureCube |
VolumeTexture |
/Engine/.../DefaultVolumeTexture |
Texture2DArray |
/Engine/.../WeightMapArrayPlaceholderTexture |
TextureCubeArray |
Transient 占位对象 |
MIC 派生: 生成的 Material 使用标准 Parameter 节点,直接使用 UE 原生 Create Material Instance 即可覆盖所有参数。
实现参考: BuildMaterialFromUsh()
13. 迭代模板
添加新支持类型时需同步修改:
EEarthUshParamType枚举加成员FEarthUshFileParser::StringToParamType()加字符串映射EarthUshMaterialBuildUtils::BuildMaterialFromUsh()相关表达式创建逻辑加分支EarthUshMaterialBuildUtils::GenerateWrapperCode()加 Sampler 追加(如果是新纹理类型)EarthUshMaterialBuildUtils::ConfigureCustomExpression()确认 Custom input/output 配置满足新类型- 本手册 §2 / §6 更新
添加新元数据注解时:
FEarthUshMaterialMetadata加字段FEarthUshFileParser::ParseMetadataLine()加分支UshApplyExtendedMaterialProperties()或专用材质属性应用逻辑写入 UMaterial- 本手册 §4 更新
14. 完整起手示例
最简 PBR:
// My PBR material
//
// @material_domain: Surface
// @blend_mode: Opaque
// @shading_model: DefaultLit
// @entry: MyPbrMain
//
// @param float3 BaseColor = (0.8, 0.8, 0.8) [group=PBR]
// @param float Metallic = 0.0 [0.0, 1.0] [group=PBR]
// @param float Roughness = 0.5 [0.0, 1.0] [group=PBR]
#pragma once
#include "/Plugin/AesWorld/EarthMaterialUtils.ush"
FMaterialAttributes MyPbrMain(
float3 BaseColor,
float Metallic,
float Roughness)
{
FMaterialAttributes Result = EarthMakeDefaultMaterialAttributes();
Result.BaseColor = BaseColor;
Result.Metallic = Metallic;
Result.Roughness = Roughness;
return Result;
}
带纹理采样:
// Textured PBR
//
// @material_domain: Surface
// @blend_mode: Opaque
// @shading_model: DefaultLit
// @entry: TexturedPbrMain
//
// @param TexCoord UV = 0 [group=UV]
// @param Texture2D AlbedoMap [group=Textures]
// @param float Roughness = 0.5 [0.0, 1.0] [group=PBR]
#pragma once
#include "/Plugin/AesWorld/EarthMaterialUtils.ush"
FMaterialAttributes TexturedPbrMain(
float2 UV,
Texture2D AlbedoMap, SamplerState AlbedoMapSampler,
float Roughness)
{
FMaterialAttributes Result = EarthMakeDefaultMaterialAttributes();
Result.BaseColor = Texture2DSample(AlbedoMap, AlbedoMapSampler, UV).rgb;
Result.Roughness = Roughness;
return Result;
}
附录 · 关键源码索引
| 关注点 | 源文件 | 符号 |
|---|---|---|
| 参数类型枚举 | EarthUshMaterialTypes.h | EEarthUshParamType, FEarthUshMaterialParam, FEarthUshMaterialMetadata |
| 文件解析 | EarthUshFileParser.cpp | ParseContent, ParseParamLine, ParseMetadataLine, StringToParamType |
| 材质构建 | EarthUshMaterialBuildUtils.cpp | BuildMaterialFromUsh, GenerateWrapperCode, ConfigureCustomExpression, WritePortableUshFiles, SetUshMetadata |
| 人工编辑保护 | EarthUshMaterialBuildUtils.cpp | ComputeUshMaterialGraphHash, GetExpectedMaterialGraphHash, SetExpectedMaterialGraphHash, HasUshMaterialManualEdits |
| 右键菜单 | EarthUshMaterialActions.cpp | CreateMenuExtender, AddUshMenuEntries |
| 导入工厂 | EarthUshMaterialFactory.cpp | FactoryCreateFile, Reimport, InferVirtualPathFromDisk |
| 虚拟路径 | EarthUshMaterialBuildUtils.cpp | ResolveVirtualPathToDisk, InferVirtualPathFromDisk |
| HLSL 工具 | EarthMaterialUtils.ush | EarthMakeDefaultMaterialAttributes |