Mass Battle Frameworkv1.20.0

合批渲染:一千个东西,一个组件 Batched Rendering: A Thousand Things, One Component

RENDERING

同一个模式画单位、血条、跳字和特效 · 2026-11-22One pattern draws units, health bars, damage numbers and effects · 2026-11-22

系列这些文章写给人和他的 agent 一起看。这套文档怎么读 →SeriesThese posts are for you and your agent. How to read these docs →

同一套画法 One Drawing Path

这个框架里没有"一个东西一个组件"的画法。单位、血条、伤害数字、子弹、爆炸、掉落物,最后都走同一条路:一个 Niagara 组件管一批槽位,槽位就是数组下标;每帧 CPU 把这一帧的值写进几列数组,用 SetNiagaraArrayVector、SetNiagaraArrayQuat、SetNiagaraArrayInt32 这些接口推给 Niagara;粒子按自己的槽位号把属性读出来。第 i 颗粒子的属性,就是第 i 个东西这一帧的样子。

Nothing in this framework draws one component per object. Units, health bars, damage numbers, bullets, explosions, drops — they all end up on the same road: one Niagara component covers a batch of slots, and a slot is an array index. Each frame the CPU writes this frame's values into column arrays and pushes them over with SetNiagaraArrayVector, SetNiagaraArrayQuat, SetNiagaraArrayInt32; each particle reads its attributes by its slot number. Particle i's attributes are thing i's look this frame.

两个渲染器是同一个模式的两份填法。Agent 渲染器管单位那套又宽又杂的数据,一批默认 5000 个槽位;特效渲染器管拖尾、爆炸这种一瞬间的东西,一批默认 1000 个。往数组里写、往 Niagara 推的机器是同一台,差别只在每帧搬多少列。

The two renderers are the same pattern with two payloads. The agent renderer carries a unit's wide, messy data, 5000 slots a batch by default; the FX renderer carries momentary things like trails and explosions, 1000 a batch. The machine that fills arrays and pushes them into Niagara is the same one — only the number of columns changes.

Agent 这一路 The Agent Path

Agent 每帧推的列多一些:位置、朝向、缩放、Mesh 下标、样式、动画轨道、DynamicParams,还有血条那一列。血条不单独画,它就是批次里的一列:不透明度、当前比例、目标比例三个数塞进一个 Vector3(HealthBar_Opacity_CurrentRatio_TargetRatio_Array)。槽位空着,血条也跟着没有。

The agent batch pushes more columns: location, orientation, scale, mesh index, style, animation tracks, dynamic params — and the health bar. The bar is not drawn on its own; it is one column in the batch: opacity, current ratio and target ratio packed into one Vector3 (HealthBar_Opacity_CurrentRatio_TargetRatio_Array). When the slot is empty, the bar is gone with it.

伤害数字走数据通道。跳字是"一瞬间、在世界坐标上出现一次"的东西,形状跟爆炸一样:先攒进批次自己的文本数组(Text_Location_Array 与 Text_Value_Style_Scale_Offset_Array),再整批写进渲染器上的 NDC_TextPop 通道,NS 侧读出来生成。通道资产不对,日志会给一条带 SubType 和丢弃数量的警告。

Damage numbers go through a data channel. A pop is momentary and appears once at a world position — the same shape as an explosion: pops gather into the batch's own text arrays (Text_Location_Array and Text_Value_Style_Scale_Offset_Array), then the batch is written into the renderer's NDC_TextPop channel, and the NS reads it and spawns. If the channel asset is wrong, the log warns with the SubType and the number discarded.

网格本身有三种画法,共用同一批数组:骨骼(每批一对 RenderTarget 加一个计算着色器,蒙皮在 GPU 上做)、顶点动画(VAT)、图集(Sprite)。每个槽位按自己的 LOD 档位挑画法,所以同一批里近处是骨骼、远处是 sprite 很正常;再近,整档可以换成真的 Actor 来画。

The mesh itself has three drawing paths over the same arrays: bones (one render-target pair and one compute shader per batch, skinning on the GPU), vertex animation (VAT), and atlas sprites. Each slot picks its path by its own LOD band, so one batch holding bones up close and sprites far away is normal — and closer still, a band can switch wholesale to a real Actor.

特效这一路:子弹为什么没有渲染器 The FX Path: Why Bullets Have No Renderer

子弹和掉落物用不上单位那一整套。一颗炮弹在屏幕上就是一团烟火,一个掉落物就是一件发光的东西。给它们各写一个渲染器,等于把单位那套数组通道抄两遍,抄完九成是空的。所以它们没有渲染器,它们靠特效实体被看见。

Bullets and drops need none of the unit's full payload. A shell on screen is a puff of smoke; a drop is one glowing thing. Writing each of them its own renderer would mean copying the unit's array plumbing twice and leaving ninety percent of it empty. So they genuinely have none. They are seen through an FX entity.

它走普通生成路径。生成一支军队、生成一个障碍、生成一个特效实体,用的是同一个入口。投射物和掉落物的 DataAsset 里都有一组特效配置(FFxConfig,Fragments/FxHostConfig.h:46),出生时照着这张表来。

It goes through the ordinary spawn path. Spawning an army, an obstacle, an FX entity — same entry points. Both the projectile and the loot DataAsset carry a list of effect configs (FFxConfig, Fragments/FxHostConfig.h:46) and spawn from that list at birth.

常见的一次配置有两行:一行附着(bAttached = true)当弹头火焰,一行不附着(bAttached = false)当拖尾或爆点。附着那行跟着父级的变换走,父级消失就自毁;不附着那行自己在世界里待着,靠 LifeSpan 收尾。同一行里还有 Delay、BindToAnimIndex、SpawnProbability:一个特效什么时候出现、活多久、出不出,都写在这一行里。

A typical setup is two rows: one attached (bAttached = true) for the warhead flame, one unattached (bAttached = false) for the trail or the impact. The attached row follows its parent's transform and dies with it. The unattached row sits in the world on its own and ends on LifeSpan. The same row also holds Delay, BindToAnimIndex and SpawnProbability: when it appears, how long it lives, whether it appears at all.

两条路:挂资产,或者合批 Two Paths: Point at an Asset, or Batch

配置里有两个字段决定走哪条路,别把它们混着用:

Two fields decide which path you are on. Do not mix them:

走法Path填什么What you fill in结果Result
直接挂Point at an asset NiagaraAsset_UnBatched 或 CascadeAsset_UnBatchedNiagaraAsset_UnBatched or CascadeAsset_UnBatched 每个特效实体一个独立组件。省事:一个资产填进去就完事One component per FX entity. Easy: paste one asset and you are done
合批Batch 资产留空,只填 SubType_Index 与 StyleType_IndexLeave the assets empty; fill only SubType_Index and StyleType_Index 特效实体只写进数组;一个批次一个组件,一千个爆炸共用它The entity only writes into arrays; one component per batch, shared by a thousand explosions

直接挂这条路撑不住数量。每个组件都有 Tick、有包围盒、有渲染代理、有自己的一次绘制调用;一百发炮弹同时在飞,就是一百个组件。合批把这一百个收成一次数组推送,代价是你得先配好一个 Niagara 系统。这篇剩下的部分都在讲这件事。

Pointing at an asset does not survive count. Every component ticks, has bounds, has a render proxy, and costs its own draw. A hundred shells in flight is a hundred components. Batching turns those hundred into one array push. The price is configuring a Niagara system first, and the rest of this post is about that.

SubType:这个特效归谁 SubType: Which Renderer Owns It

SubType_Index 回答的是"这个特效归谁"。每个 AMassBattleFxRenderer 身上挂一个 FSubType(Rendering/MassBattleFxRenderer.h:70),处理器按这个数字把实体分派过去:FxRenderProcessor 只收同时带 FSubType、FStyleType 和实体标志的实体(MassBattleFxRenderProcessor.cpp:76),然后去 MB.FxRenderers 里按索引找渲染器(:381)。

SubType_Index answers "which FX renderer owns this". Each AMassBattleFxRenderer carries one FSubType (Rendering/MassBattleFxRenderer.h:70), and the processor dispatches on that number: FxRenderProcessor only takes entities that carry FSubType, FStyleType and the entity flag (MassBattleFxRenderProcessor.cpp:76), then looks the renderer up in MB.FxRenderers by index (:381).

这里有一条规矩跟单位渲染器不一样:渲染器必须真实存在于场景里。单位那一路是全自动的:烘一个单位时,插件复制模板 NS、把网格和绑定填好,生成一个 NS_{资产名};运行时发现这个 SubType 还缺渲染器,就按配置好的类自己生成一个,连 SubType 一起写好。特效这一路没有这些:渲染器得你自己拖进关卡,或者用 SpawnActorOfClass 生成,不用了也得自己收;NS 也得自己搭。找不到对应 SubType 的渲染器,实体就只是躺在 chunk 里,屏幕上什么都没有。说"特效不显示"的人,多半卡在这一条,比 Niagara 配错常见得多。

One rule differs from the unit renderer: the renderer has to exist in the level. The unit path is fully automated. Baking a unit duplicates the template NS, fills in the meshes and bindings, and produces an NS_{asset name}; at run time, when a SubType still has no renderer, one is spawned from the configured class with the SubType written in. The FX path has none of that: you drag the renderer into the level yourself, or spawn it with SpawnActorOfClass, and you remove it yourself; the NS you build yourself too. With no renderer for that SubType, the entity simply sits in its chunk and nothing shows. When someone says "my effect does not show", this is usually where it died, far more often than a mistuned Niagara.

这块以后会补上。MCP 这边后面要做的就是这个:让 MCP 能帮你改造非合批的特效素材,把 NS 里的模块、绑定、参数尽量自动配好,就像烘焙替单位做的那样。在那之前,特效的 NS 是这篇里唯一必须手写的一环。

This gap is next on the list. MCP is meant to take it on: converting unbatched effect assets for you, wiring the modules, bindings and parameters inside the NS as far as it can go, the way baking does it for units. Until then, the FX NS is the one part of this post you have to author by hand.

渲染器上还有三个字段值得看一眼:NiagaraSystemAsset(这个 SubType 用哪个 NS)、NDC_BurstFx(爆发走哪个数据通道)、RenderBatchSize(默认 1000,一批塞多少个槽)。附着和不附着是两条通道,各自有自己的数组,所以同一个 SubType 下既有弹头火焰又有拖尾很正常,只要它们都在这个渲染器的批次里。

Three fields on the renderer are worth a look: NiagaraSystemAsset (which NS this SubType uses), NDC_BurstFx (which data channel the bursts go through), and RenderBatchSize (1000 by default: how many slots one batch holds). Attached and unattached are separate channels with their own arrays, so a warhead flame and a trail under one SubType is perfectly normal, as long as both land in that renderer's batches.

StyleType:传给 NS 的一个数字 StyleType: One Number Handed to Niagara

StyleType_Index 是交给 NS 的一个 int32,插件不解释它。处理器把它写进批次里的样式数组(StyleArray_Attached 与 StyleArray_Burst,Fragments/RenderBatchData.h:623),NS 拿每个粒子的这个值当自己的开关。参数名在批次建立时解析,优先新名 StyleTypeArray*,兼容旧名 StyleArray*;NS 里没有这个参数,样式数组就不推(bUseStyleArray,同文件 :640)。

StyleType_Index is an int32 handed to the NS; the plugin does not interpret it. The processor writes it into the batch's style array (StyleArray_Attached and StyleArray_Burst, Fragments/RenderBatchData.h:623), and Niagara uses each particle's value as its own switch. The parameter name is resolved when the batch is built — StyleTypeArray* preferred, legacy StyleArray* accepted — and if the NS does not expose it, the array is never pushed (bUseStyleArray, :640 in the same header).

用法有两种,挑一种就行:

There are two ways to use it; pick one:

一是 Mesh Index。一个 Mesh 渲染器里塞多个网格,StyleType 直接当网格索引喂进去。想换模型就换一个数字,没有比这更省事的。

First, Mesh Index. Put several meshes in one mesh renderer and feed StyleType in as the mesh index. Changing the model is changing a number; nothing is simpler.

二是 Visibility Tag。一个发射器里放多个渲染器,每个渲染器勾不同的 Visibility Tag;把 StyleType 写到粒子的 Visibility Tag 上,0 表示这个粒子哪个渲染器都不画。好处是能混用类型:同一个发射器里既有网格又有 sprite,网格跟 tag 1、sprite 跟 tag 2,改一下 StyleType 就换了一种表现。

Second, Visibility Tag. Put several renderers in one emitter, each ticking a different visibility tag, then write StyleType onto the particle's Visibility Tag — 0 means no renderer draws it. The upside is mixing kinds: one emitter can hold a mesh under tag 1 and a sprite under tag 2, and moving StyleType switches which you get.

两条其实是同一个想法,只是尺度不同:SubType 在场景里选一个渲染器,StyleType 在 NS 里选一个渲染器。先按"归谁管"分一次,再按"画成什么样"分一次。后面看到的配置,都是这句话的展开。

Both are the same idea at different scales: SubType picks a renderer in the scene, StyleType picks a renderer inside the NS. Partition once by owner, once by look. Every setting below is that sentence spelled out.

合批这条线怎么跑 How the Batched Path Runs

流程跟单位渲染器一个形状,只是数据少。每帧 FxRenderProcessor 先并行:各线程把要生成、要更新的特效实体收进线程本地的登记队列,顺手记下 SubType 与 StyleType(MassBattleFxRenderProcessor.cpp:156-166)。然后串行:按 SubType 找渲染器、按批次号找批次、分配槽位、把这一帧的值写进数组。

Same shape as the unit renderer, with less data. Each frame FxRenderProcessor goes parallel first: threads collect the FX entities that need spawning or updating into thread-local registration queues, noting SubType and StyleType as they go (MassBattleFxRenderProcessor.cpp:156-166). Then serial: resolve the renderer by SubType, the batch by id, allocate a slot, write this frame's values into the arrays.

最后一段推给 Niagara。源码注释写得很直白(:614):NS 数组并行推,NDC 爆发串行写。原因还是那条边界:SetNiagaraArray* 改的是数据,可以并行;NDC 的写入要按渲染器跨批次聚合,聚合本身是串行段。

The last stage pushes to Niagara. The comment there is blunt (:614): NS arrays go out in parallel, NDC bursts are written serially. The boundary again: SetNiagaraArray* changes data, so it parallelises; an NDC write aggregates bursts per renderer across batches, and aggregation is serial.

一个批次就是一个 UNiagaraComponent(FFxRenderBatchData::SpawnedNiagaraSystem)。RenderBatchSize 塞满了就开下一批,空下来的批次过了冷却时间被回收。所以"一千个爆炸"在场景里通常只有一两个组件,槽位就是一个数组下标。

A batch is one UNiagaraComponent (FFxRenderBatchData::SpawnedNiagaraSystem). When RenderBatchSize fills up, the next batch opens; a batch that empties is reclaimed after its cooldown. So "a thousand explosions" is usually one or two components in the scene, and a slot is just an array index.

非附着的爆发走 NDC Unattached Bursts Go Through NDC

附着的特效每帧都在,位置跟着父级,用数组推最自然。非附着的爆发不一样:它是一瞬间的事,位置在世界里,寿命几百毫秒。这类走 Niagara Data Channel:渲染器把这一帧所有爆发写进通道,NS 侧读出来,在正确的位置生成。Content/Core/FxRenderer/NS_Modules/NDC_BurstFx.uasset 是插件带的默认通道资产。

Attached effects exist every frame and follow their parent, so arrays are the natural fit. Unattached bursts are not like that: a momentary event at a world position with a few hundred milliseconds of life. Those go through a Niagara Data Channel — the renderer writes this frame's bursts into the channel, and the NS reads them and spawns at the right place. Content/Core/FxRenderer/NS_Modules/NDC_BurstFx.uasset is the default channel asset shipped with the plugin.

有两处要填,大多数人只填了一处:渲染器上的 NDC_BurstFx,和 NS 里读通道的模块参数。两处都留空也能跑,会用默认通道;只填一处就会静默地什么都看不到,所以下面那条警告值得记住。

It has to be set in two places, and most people set one: NDC_BurstFx on the renderer and the channel parameter on the NS module that reads it. Leaving both empty works, the default channel is used; set only one and nothing shows, with no error. That is why the warning below is worth knowing.

字段填错、或者资产没 cook,处理器不会装没事:这一批爆发全部丢掉,日志里打一条警告,带上 SubType 和丢掉的数量(MassBattleFxRenderProcessor.cpp:763-776)。看到这行日志,先别去翻 NS,去看渲染器上那个字段。

When the field is wrong or the asset is not cooked, the processor does not pretend: it drops that renderer's bursts and logs a warning naming the SubType and the count (MassBattleFxRenderProcessor.cpp:763-776). If you see that line, stop digging in the NS and look at the renderer's field first.

通道是全局的:所有指向同一个 NDC 的渲染器都往里面写,靠数据里的 SubType 与 StyleType 区分,不是自己的会被滤掉。所以用默认通道完全可行。想要最后那点性能,就复制一份专属的:写入和遍历都只碰自己这一类,NS 侧也省掉一次过滤比较。

The channel is global: every renderer pointed at the same NDC writes into it, and the payload's SubType and StyleType separate them; anything that is not yours gets filtered out. Using the default channel is therefore fine. For the last bit of performance, copy a dedicated one: writes and iteration then only touch your own kind, and the NS side skips the filter compare.

StyleType 决定"哪个 emitter 触发",也是这个通道给的:官方读通道的组件自带比较,模块里挂一个 equal 就行,爆发记录里的 StyleType 等于本 emitter 配的值就生成,不等就不生成。一个 NS 里于是可以排好几个爆发 emitter,各认一个 StyleType,外面看到的就是"一个系统,好几套爆炸"。实现不止这一种,但它复用了官方组件,最省事。

StyleType deciding which emitter fires also comes out of the channel: the stock reader module can compare, so you add one equal node — spawn only when the burst's recorded StyleType matches the value on this emitter, otherwise not. That lets one NS hold several burst emitters, each answering a different StyleType, which from the outside looks like "one system, several explosions". It is not the only way to do it, but it reuses the stock component and costs the least work.

把商城特效改造成合批 Converting a Marketplace Effect

这是被问得最多的一件事,也是我说"找个特效师"说得最多的一件事:Niagara 里可调的参数太多,插件只负责把值传进来,剩下的排版是美术活。流程本身还是能写下来的,插件的模块都在 Content/Core/FxRenderer/NS_Modules/:

This is the question I get most, and the one where I most often say "find an FX artist": Niagara has too many knobs, and the plugin's job is only to hand values in; the rest is art. The procedure itself can be written down, though. The modules live in Content/Core/FxRenderer/NS_Modules/:

模块Module用在哪Where it goes
NM_SetAttachedFxParamsFromArray附着特效:从数组取这一帧的位置、朝向、缩放Attached effects: read this frame's location, rotation and scale from the arrays
NM_UpdateAttachedFxParams_GPU / _CPU附着的插值:GPU 版是 Custom HLSL,CPU 版是逐帧 VInterpToAttached interpolation: the GPU one is Custom HLSL, the CPU one is per-frame VInterpTo
NM_SetBurstFxParams不附着、但仍走批次数组的爆发Unattached bursts that still ride the batch arrays
NM_BurstFxInitFromNDC / ReadFromNDC / SpawnFromNDC不附着的爆发走 NDC 的三件套The three modules for unattached bursts over NDC
NM_SampleParticlesFromOtherEmitter / _Interp从一个发射器取粒子喂给另一个(拖尾跟弹头这类)Feed one emitter's particles into another (a trail following a warhead)

步骤:复制一份模板资产(我会直接复制插件里那套再调),把素材自带的 Spawn 模块关掉,改用上面这些模块触发;位置更新也换成插件的模块。漏掉任何一步,都会看到粒子全生成在原点,或者粒子多出一整套。附着的插值模式在 FFxConfig::InterpMode(FxHostConfig.h:115):GPU 走 Custom HLSL,只对 GPU 发射器有效;CPU 是逐帧 VInterpTo,任何发射器都能用,代价在 CPU 上。

The steps: copy a template asset (I copy the plugin's and adjust), turn the asset's own Spawn module off and trigger from the modules above; swap position updates over to the plugin's module too. Miss either step and you get particles at the origin, or a whole second set of them. The interpolation mode on the attached path is FFxConfig::InterpMode (FxHostConfig.h:115): GPU uses Custom HLSL and only works with GPU emitters; CPU is per-frame VInterpTo, works with any emitter, and costs CPU time.

还有一件常被忽略的:模板有附着和不附着两套,别把附着那套套在爆发上。它每帧读一个父级变换,而爆发没有父级。

One more thing people skip: attached and unattached are two different templates. Do not put the attached one on a burst; it reads a parent transform every frame, and a burst has no parent.

六个坑 Six Ways It Goes Wrong

一、渲染器没摆进场景,或者 SubType 对不上。屏幕上什么都没有,日志也不报。先数一下场景里放了几个 AMassBattleFxRenderer,再看它们身上的 SubType。

One: the renderer is not in the level, or the SubType does not match. Nothing on screen, nothing in the log. Count the AMassBattleFxRenderer actors in the level, then read their SubTypes.

二、NS 没有暴露样式数组参数。所有爆炸长得一模一样。先查名字:StyleTypeArray* 优先,StyleArray* 兼容。名字对不上,样式数组根本不推(bUseStyleArray)。

Two: the NS does not expose the style array. Every explosion looks identical. Check the name: StyleTypeArray* first, StyleArray* accepted. Get it wrong and the array is never pushed at all (bUseStyleArray).

三、NDC 只填了一处。附着的都正常,就是没有爆点。往回翻日志,找 BurstFx: NDC_BurstFx asset invalid。

Three: NDC set in only one place. Attached effects look fine; only the impact is missing. Search the log for BurstFx: NDC_BurstFx asset invalid.

四、素材自带的 Spawn 模块没关。粒子数量翻倍,位置还互相打架。合批模块和素材原来的模块同时在生成,两边都以为自己是唯一的来源。

Four: the asset's own Spawn module is still on. Twice the particles, and they fight over position. The batched module and the original are both spawning, each thinking it is the only source.

五、给同一种爆炸开了两个 SubType。只是想换个外观的话,用 StyleType。每多一个 SubType 就多一个渲染器、多一组批次组件,而外观的差别本该在 NS 内部解决。

Five: two SubTypes for one kind of explosion. If all you want is a different look, use StyleType. Every extra SubType costs another renderer and another set of batch components, and a difference in look belongs inside the NS.

六、视锥剔除把整批剔掉。渲染器的 bDynamicFixedBounds 默认开着,每帧按实际槽位推紧包围盒,让组件级剔除跟着真实内容走;但它只在 NS 本来就用固定包围盒时才覆盖,动态包围盒的资产不受影响。特效在镜头边缘整批消失,先看这个开关和 NS 的包围盒设置。

Six: frustum culling removes the whole batch. The renderer's bDynamicFixedBounds is on by default and pushes a tight box from the live slots every tick, so component-level culling tracks what is actually there — but it only overrides when the NS already uses fixed bounds, and leaves dynamic-bounds assets alone. If a whole batch vanishes near the screen edge, check that switch and the NS bounds setting.

一句话 In One Line

合批把"一个东西一个组件"换成"一批槽位、每帧一次数组推送"。单位、血条、跳字、特效都是同一台机器:数组里写值,Niagara 按槽位画。特效这一路多两步配置:SubType 在场景里选渲染器,StyleType 在 NS 里选渲染器;附着的走数组,非附着的爆发走 NDC。省下来的是组件、Tick 与绘制调用,付出的是 Niagara 的功课:插件的模块能替你做掉一半,剩下一半只能自己看。

Batching trades "one component per thing" for "a batch of slots and one array push a frame". Units, health bars, damage numbers and effects all ride the same machine: values into arrays, Niagara draws by slot. The FX path adds two configuration steps on top: SubType picks a renderer in the scene, StyleType picks one inside the NS; attached effects ride the arrays, unattached bursts ride the NDC. What you save is components, ticks and draw calls. What you pay is a little Niagara homework: the plugin's modules cover half of it, the other half you have to look at yourself.

Leroy Works · Mass Battle Framework 作者
联系:Discord · QQ 群 916358710 · Fab · 全部联系方式

Leroy Works — author of Mass Battle Framework
Contact: Discord · QQ group 916358710 · Fab · all channels