MassBattleAssemblySlotBridge.h
Source/MassBattleEditor/Public/UnitAssembly/MassBattleAssemblySlotBridge.h
概述Overview
以程序方式改造 Niagara 系统的安装器:把烘焙出来的 NS 从「模板形态」变成「槽位驱动形态」——确保恰好 S 个槽位 renderer、把选槽逻辑做成系统自包含的内联模块、并把多部件/多版本结构(一个部件一个 emitter、一个版本一个单网格 renderer、每个 (部件,版本,section) 一个骨骼材质参数)一次建好。它是纯代码路径:不依赖任何控制台命令、打开的面板或选中的 actor,烘焙流程可以在后台直接调用。
An installer that reshapes a Niagara system programmatically: it turns a baked NS from "template shape" into "slot-driven shape" — ensuring exactly S slot renderers, making the slot-selection logic a self-contained inline module inside the system, and building the multi-part/multi-version structure (one emitter per part, one single-mesh renderer per version, one bone material parameter per (part, version, section)) in one pass. It is a pure code path: no console command, open panel or selected actor is involved, so a bake can call it headlessly.
核心不变量:一个 renderer 只装一个网格。Niagara 的材质覆盖是一张按 section 下标索引、套到该 renderer 所持有的每一个网格上的表——同一个 renderer 里的多个网格永远会拿到同一份骨骼材质。一个网格一个 renderer 之后,第 i 个 section 只可能指那唯一的网格,参数 <基名>_P<部件>_V<版本>_S<下标> 才有意义。这条约束解释了整个结构的其余部分。安装器会在结束时自行编译并保存系统,因此烘焙流程拿到的是可用的成品。
The core invariant: one mesh per renderer. Niagara's material override is a single table indexed by section and applied to every mesh the renderer holds — several meshes in one renderer can never receive distinct bone materials. With one mesh per renderer, section i can only mean that single mesh, which is what makes the parameter <base>_P<part>_V<version>_S<index> meaningful. That constraint explains the rest of the structure. The installer compiles and saves the system itself at the end, so the bake always receives a ready-to-use asset.
结构体Structs
这三个结构体是「调用方告诉安装器要建成什么样」的输入描述:逐部件 → 逐版本的网格与材质,外加每个网格相对粒子的位姿。
These three structs are the input description of "what the caller wants built": per part → per version mesh and materials, plus each mesh's pose relative to the particle.
一个 (部件, 版本) 要画的那一个网格,以及它相对粒子的位姿。
The one mesh a (part, version) draws, plus its pose relative to the particle.
成员变量Members
| 名称Name | 类型Type | 默认值 | 说明Description |
|---|---|---|---|
Mesh | UStaticMesh* | nullptr | 该版本唯一要画的网格。为空时对应 renderer 仍会建出来,只是网格列表留空(什么都不画)——这样版本下标不会前移。The single mesh that version draws. When null the corresponding renderer is still created with an empty mesh list (it draws nothing), so version indices never shift. |
SectionMaterials | TArray<UMaterialInterface*> | 空empty | 与 Mesh 的材质槽下标一一对应,同时也对齐 renderer 的覆盖材质表下标。每一条都会绑一个该 (部件,版本,section) 独占的用户参数。One entry per material-slot index of Mesh, index-aligned with the renderer's override-material table. Each entry is bound to a user parameter that only that (part, version, section) can use. |
LocalOffset | FTransform | FTransform::Identity | 本网格相对粒子(共享渲染根)的位姿,写进网格条目的缩放/旋转/枢轴偏移。所有 actor 相对的偏移都走这里(body 部件的部件×版本位姿即标准用法);必须跟随骨骼的位姿(socket 挂接的挂件)绝不能走这里——条目作用在渲染根空间、不认识骨骼,绑定时看着对、骨一转就漂,那类位姿要烘进几何。This mesh's pose relative to the particle (the shared render root), written into the mesh entry's scale/rotation/pivot offset. Every actor-relative offset belongs here (a body part's part × version pose is the canonical case). A pose that must follow a bone (a socket-mounted attachment) must never come here — the entry is applied in render-root space and knows nothing about bones, so it looks right at bind pose and drifts as the bone rotates; those poses are baked into geometry. |
一个部件的描述:展示名 + 逐版本网格。数组下标即版本号,构建器据此派生可见性标签。
Description of one part: a label plus the per-version meshes. The array index is the version number, from which the builder derives the visibility tag.
成员变量Members
| 名称Name | 类型Type | 默认值 | 说明Description |
|---|---|---|---|
Label | FString | 空empty | 展示名,只用于日志与 emitter 命名。Cosmetic label used in logs and emitter naming. |
Versions | TArray<FPartVersionMesh> | 空empty | 下标即版本号(0 起),对应 renderer 的可见性标签为「版本号 + 1」;0 留给「隐藏」,没有 renderer 会取它。数组不能为空。The index is the version number (from 0), and the renderer's visibility tag is "version + 1"; 0 is reserved for "hidden" and no renderer carries it. The array must not be empty. |
结构构建的结果摘要,便于烘焙把结构规模写进日志。
Summary of the structure build, so the bake can log its size.
成员变量Members
| 名称Name | 类型Type | 默认值 | 说明Description |
|---|---|---|---|
bOk | bool | false | 结构是否建成。为 true 时系统已编译并保存。Whether the structure was built. When true the system has been compiled and saved. |
NumParts | int32 | 0 | 建成的部件 emitter 数。Number of part emitters built. |
NumRenderers | int32 | 0 | 建成的版本 renderer 总数(各部件版本数之和)。Total version renderers built (sum of the parts' version counts). |
NumBoneParams | int32 | 0 | 声明并归组的骨骼材质参数总数(所有 (部件,版本,section) 之和)。Bone material parameters declared and grouped (all (part, version, section) triples). |
公开方法Public Methods
确保系统中恰好有 S 个「槽位」网格 renderer(按 Particles.MeshIdx_N 绑定识别)。缺的槽位以配置源复制补齐——有槽 0 就用槽 0,否则用第一个非槽位基准 renderer——完整继承其动态材质参数绑定与材质覆盖模式,再把新槽重新绑到自己的 VisTag_N / MeshIdx_N;随后拆除旧的基准 renderer(它们会以旧绑定叠画),并把编号 ≥S 的槽位裁掉。它不编译,编译由后续步骤或调用方负责。
Ensures the system has exactly S "slot" mesh renderers (recognized by their Particles.MeshIdx_N binding). Missing slots are filled by duplicating a config source — slot 0 when present, otherwise the first non-slot base renderer — inheriting its dynamic material-parameter bindings and material-override mode in full, then rebinding the new slot to its own VisTag_N / MeshIdx_N. Legacy base renderers are then removed (they would double-draw on their old bindings) and slots numbered ≥ S are pruned. It does not compile; a later step or the caller does.
Input
| 参数Param | 类型Type | 说明Description |
|---|---|---|
NS | UNiagaraSystem* | 目标系统;为空时直接返回。目标 emitter 取「拥有网格 renderer 的那个」,多个时优先名为 AgentRenderer_VAT 的。Target system; a null pointer returns immediately. The target emitter is the one owning mesh renderers, preferring the one named AgentRenderer_VAT when several qualify. |
S | int32 | 需要的槽位数,钳制到 1–16。Required slot count, clamped to 1–16. |
Log | const TFunction<void(const FString&)>& | 日志回调(烘焙把它接到 creator 日志与进度窗)。Log callback (the bake routes it into the creator log and progress dialog). |
Output
无返回值。副作用:增删该 emitter 上的 renderer 对象并把被删对象送去瞬态包以立即腾出名字。系统里没有任何带网格 renderer 的 emitter,或找不到可复制的配置源时,只在日志里报错。
No return value. Side effects: adds/removes renderer objects on that emitter and moves removed ones to the transient package so their names free up immediately. If no emitter owns a mesh renderer, or no config source exists to duplicate, it only logs an error.
安装 S 槽选槽桥接,替换语义:先清掉模板原有的共享模块调用与上一轮遗留的桥接调用(含其悬空的绑定管线),再把共享模块复制成一个内联 scratchpad 登记进系统——由系统自己拥有,双击即可打开模块图。随后把该 scratchpad 的接线裁到恰好 S 个槽,接到粒子生成与粒子更新两条链上(生成链保证新粒子在首帧渲染前就有有效的标签/网格下标),把 S 个参数归进同一用户参数分组,最后编译、轮询、保存。安装后的系统自包含:运行时不再引用任何共享模块资产,因此共享模块的改动不会波及已烘焙的单位。
Installs the S-slot selection bridge with replace semantics: the template's existing shared-module calls and any bridge calls left by a previous pass are removed first (including their orphaned binding plumbing), then the shared module is duplicated into an inline scratchpad registered on the system — owned by the system, double-click opens its graph. The scratchpad's wiring is pruned to exactly S slots, attached to both the particle spawn and particle update chains (spawn guarantees fresh particles have valid tags/mesh indices before the first render), its S parameters are filed under one user-parameter group, and the whole thing is compiled, polled and saved. The installed system is self-contained: it references no shared module asset at runtime, so edits to that module cannot affect already-baked units.
Input
| 参数Param | 类型Type | 说明Description |
|---|---|---|
NS | UNiagaraSystem* | 目标系统;为空返回 false。Target system; null returns false. |
S | int32 | 槽位数,钳制到 1–16。Slot count, clamped to 1–16. |
Log | const TFunction<void(const FString&)>& | 日志回调。Log callback. |
Output
返回安装后两条阶段链是否都确实由引用该 scratchpad 的调用节点供针——只挂上一条即判失败。副作用:改写 emitter 图、增删重新登记 scratchpad、编译并保存包。失败时图可能已被部分改动,重新安装一次即可(替换语义保证可重复执行)。
Returns whether both stage chains are actually fed by a call node referencing that scratchpad after installation — one stage only counts as failure. Side effects: rewrites the emitter graph, (re)registers the scratchpad, compiles and saves the package. On failure the graph may already be partly modified; running the install again is safe (replace semantics make it repeatable).
把系统重建成多部件/多版本结构,流程固定:① 先按部件数安装桥接,使之后复制出的每个 emitter 都带完整 P 槽桥接;② 清掉上一轮遗留的 Part* emitter(腾出名字);③ 找到拥有网格 renderer 的基准 emitter;④ 把基准 emitter 收缩成唯一一份原始渲染源(槽 0),其余槽位 renderer 送走——这一步对每个部件都一样,所以只做一次;⑤ 一次性复制出全部部件 emitter(Part0… ),此时任何部件专属编辑都还没发生,每个部件从同一状态起步;⑥ 逐 emitter 整形:恰好「版本数」个 renderer,每个只装一个网格、可见性 = 版本号 + 1、读本部件自己的 VisTag_p、逐 section 绑独占参数;⑦ 校验两个部件没有共用同一个 renderer 对象,共用即报致命错误;⑧ 声明并归组全部骨骼参数;⑨ 编译、轮询、保存。
Rebuilds the system into the multi-part/multi-version structure in a fixed order: ① install the bridge with the part count first, so every later emitter copy carries the full P-slot bridge; ② evict Part* emitters left by a previous pass (freeing their names); ③ locate the base emitter that owns mesh renderers; ④ collapse it to a single pristine render source (slot 0) and drop the other slot renderers — identical for every part, so it happens once; ⑤ duplicate all part emitters up front (Part0…), before any part-specific edit, so every part starts from the same state; ⑥ shape each emitter: exactly "version count" renderers, each holding one mesh, visibility = version + 1, reading that part's own VisTag_p, with one exclusive parameter per section; ⑦ prove no renderer object is shared between two parts (a shared one is a fatal error); ⑧ declare and group every bone parameter; ⑨ compile, poll, save.
Input
| 参数Param | 类型Type | 说明Description |
|---|---|---|
NS | UNiagaraSystem* | 目标系统;为空即失败。Target system; null fails. |
Parts | const TArray<FPartVersionStructure>& | 逐部件的版本/网格描述。部件数须在 1–16;每个部件至少一个版本,否则直接失败。Per-part version/mesh description. Part count must be 1–16, and every part needs at least one version or the call fails outright. |
BoneParamBase | FName | 骨骼材质参数的基础名;最终名字形如 <基名>_P<部件>_V<版本>_S<下标>。Base name of the bone material parameters; final names look like <base>_P<part>_V<version>_S<index>. |
Log | const TFunction<void(const FString&)>& | 日志回调。Log callback. |
Output
返回 FPartStructureResult(是否成功、部件数、renderer 数、骨骼参数数)。副作用:删除并复制 emitter、改写 renderer 的网格/可见性/材质覆盖、声明用户参数,最后编译并保存系统包。桥接安装失败、基准 emitter 丢失、renderer 被共用等情形都会中止并返回未成功的结果。
Returns FPartStructureResult (success, parts, renderers, bone params). Side effects: removes and duplicates emitters, rewrites renderers' meshes/visibility/material overrides, declares user parameters, then compiles and saves the system package. A failed bridge install, a missing base emitter, or shared renderers all abort with an unsuccessful result.
声明给定的材质用户参数(缺失才建)并把它们归进同一个用户参数分组,使它们在这个 NS 的参数面板里成组出现而不是散落。声明必须落在系统的暴露参数仓上——系统没声明的材质变量会静默吞掉运行时的推送。已在该分组里的参数不会重复添加,因此可反复调用。
Declares the given material user parameters (creating the missing ones) and files them under one user-parameter category so they appear as a group in this NS's parameter panel instead of scattered. The declaration must land on the system's exposed parameter store — a material variable the system does not declare silently swallows runtime pushes. Parameters already filed are not added twice, so the call is repeatable.
Input
| 参数Param | 类型Type | 说明Description |
|---|---|---|
NS | UNiagaraSystem* | 目标系统;为空或参数列表为空时返回 0。Target system; null or an empty parameter list returns 0. |
ParamNames | const TArray<FName>& | 要声明并归组的参数名(材质类型)。Parameter names to declare and group (material type). |
CategoryName | const FString& | 分组名;不存在时新建该分组,已存在则复用。Category name; created when absent, reused when present. |
Log | const TFunction<void(const FString&)>& | 日志回调。Log callback. |
Output
返回最终落组的参数数量(含此前已在该组里的)。系统没有编辑器数据时返回 0 并记一行日志——此时参数不会被声明,运行时推送同样会被吞掉,需要确认系统是以可编辑资产打开的。
Returns how many parameters ended up filed (including ones already in the category). With no editor data on the system it returns 0 and logs a line — the parameters are then not declared and runtime pushes are swallowed, so make sure the system is opened as an editable asset.
管线角色Pipeline Role
全部入口都由烘焙流程调用,没有运行时消费者。单网格 quickpath 在 FMassBattleAgentCreatorCore::CreateRenderer 里用 EnsureSlotRenderers(NS, 1) + Install(NS, 1) + EnsureMaterialParamsGrouped 把复制出的 NS 改成 1 槽结构——quickpath 就是多部件结构的 K=1、单版本退化情形,两者参数命名一致,运行时不必关心资产由哪条烘焙路线产出。多部件路线则由 MassBattleAssemblyCompositeBake 调 BuildPartVersionStructure,一次建好部件 emitter、版本 renderer 与骨骼参数。运行时读取的是渲染器 CDO 上的配方表与逐 (部件,版本,section) 参数,以及桥接逐粒子写入的 VisTag_p / MeshIdx_p。
Every entry point is called by the bake; there is no runtime consumer. The single-mesh quickpath uses EnsureSlotRenderers(NS, 1) + Install(NS, 1) + EnsureMaterialParamsGrouped inside FMassBattleAgentCreatorCore::CreateRenderer to turn the duplicated NS into a 1-slot structure — the quickpath is the K=1, single-version degenerate case of the multi-part structure, and both use the same parameter naming, so runtime code never needs to know which bake produced an asset. The multi-part route calls BuildPartVersionStructure from MassBattleAssemblyCompositeBake, building all part emitters, version renderers and bone parameters in one pass. Runtime reads the recipe table on the renderer CDO, the per-(part, version, section) parameters, and the VisTag_p / MeshIdx_p the bridge writes per particle.
补充说明Notes
① PivotOffset = R⁻¹·T 的原因:引擎先施加网格条目的旋转、再加平移(平移按条目旋转后再乘一次),所以原始平移必须先用条目旋转的逆转一次,否则会被转两遍而偏出去;枢轴偏移空间取 Mesh,因为只有网格空间随粒子/agent 朝向走。② 位姿分工:actor 相对的位姿放进网格条目;socket 相对的位姿烘进几何。放错不会报错,只会在骨骼转动时看到漂移。③ 版本下标稳定:烘焙失败的版本保留空 renderer,不删除——删掉会让其后的版本下标前移,静默改写配方指向。④ 可重复执行:所有入口都按「替换」而非「追加」工作,重烘同一个 NS 不会累积重复的 renderer、参数或 scratchpad。⑤ 上限:槽位与部件数都封顶 16,超出时结构构建直接失败而不是静默丢弃。
① Why PivotOffset = R⁻¹·T: the engine applies the mesh entry's rotation first and the translation afterwards (multiplying it by that rotation once more), so the raw translation must be pre-rotated by the inverse entry rotation or it gets rotated twice and drifts off. The pivot offset space is Mesh because only mesh space follows the particle/agent facing. ② Pose split: actor-relative poses go into the mesh entry; socket-relative poses are baked into geometry. Getting this backwards raises no error — you see the drift only when the bone rotates. ③ Stable version indices: a version whose bake failed keeps an empty renderer instead of being deleted, since deleting it would shift every later version's index and silently repoint the recipe. ④ Repeatable: every entry point works by replacement rather than accumulation, so re-baking the same NS never piles up duplicate renderers, parameters or scratchpads. ⑤ Ceiling: slots and parts both cap at 16; exceeding it fails the structure build instead of silently dropping the excess.