Mass Battle Frameworkv1.20.0

MassEntity 与 Niagara:
同一个想法的两种写法 MassEntity and Niagara: One Idea, Two Spellings

RENDERING

把两套系统逐项对上去,会发现它们是同一件事 · 2026-11-22Put the two systems side by side and they turn out to be the same thing · 2026-11-22

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

先说结论:它们是同一类东西 The Claim, Up Front

用久了会看出一件事:MassEntity 和 Niagara 表面上差得很远,底下是同一个东西。Niagara 就是一个 ECS,只不过它的"实体"叫粒子,处理器用节点图写,而且同一套图既能编到 CPU 上跑,也能编到 GPU 上跑。

Spend enough time in both and one thing becomes hard to miss: MassEntity and Niagara look nothing alike and are the same thing underneath. Niagara is an ECS. Its entities are called particles, its processors are written as node graphs, and the same graph compiles to run on the CPU and on the GPU.

第二件事才让我真心服气:一份图,两个后端,行为一致。这说明抽象真的立住了。节点图是中间表示,往下一层才是 HLSL,或者 CPU 上的解释执行。做到这个靠的是设计,堆工程量堆不出来。

The second part is what earns real respect: one graph, two execution backends, the same behaviour. The graph is an intermediate representation, and HLSL and the CPU interpreter are targets below it. Holding that together takes design; no amount of engineering hours substitutes for it.

逐项对照 Term by Term

把两边的名词摆在一起,对应关系几乎是机械的:

Set the two vocabularies side by side and the mapping is almost mechanical:

NiagaraNiagaraMassEntityMassEntity为什么是一回事Why they are the same
System / EmitterSystem / Emitter 整条管线 / 其中一条处理器链The whole pipeline / one chain inside it 一个 System 里挂着多个 emitter,各自成链,可以并行也可以串行。System 对得上整条管线,Emitter 只对得上一条链:两者都是"一批同类数据,按固定顺序过几趟"A System holds several emitters, each its own chain, running in parallel or in sequence. System matches the whole pipeline; an emitter matches one chain. Both are "one batch of homogeneous data put through a fixed sequence of passes"
ModuleModule ProcessorProcessor 都把整批数据走一遍,只做一件事;模块按顺序串,处理器也按顺序串Both walk the whole batch and do one job; modules chain in order, processors chain in order
粒子属性(Position、Velocity、Age…)Particle attributes (Position, Velocity, Age…) 片段里的字段Fields inside fragments 都是一个实体身上的一个值。区别只是 Mass 把字段按类型切成若干片段、按原型聚块;Niagara 把一颗粒子摊成一个整体结构Both are a value on one entity. The difference is packaging: Mass slices fields into typed fragments and groups them into archetypes; Niagara lays one particle out as a single struct
Simulation StageSimulation Stage GPU 上的处理器(外加一个同步点)A processor on the GPU, plus a sync point 见下一节,这条对得最准See the next section; this is the closest match of all
粒子 IDParticle id 实体句柄Entity handle 连二进制布局都一样,见后面那节Even the binary layout agrees; see below

Simulation Stage 就是 GPU 上的 Processor A Simulation Stage Is a Processor on the GPU

这是两套系统里对得最准的一处。Simulation Stage 干的事就一件:把这一帧的粒子整体过一遍,跑一段脚本,结束。它还能设迭代次数(NumIterations),所以同一个 stage 在一次 dispatch 里能跑好几遍。

This is the tightest correspondence between the two. A Simulation Stage does exactly one thing: walk every particle once, run a script, finish. It also takes an iteration count (NumIterations), so one stage can run several times inside a single dispatch.

翻成 Mass 的话:一个 processor 把这一帧的实体整体过一遍,结束。"结束"在两边是一个意思:一个同步点。GPU 侧,一次 dispatch 之内不能同步,dispatch 结束才对齐;CPU 侧,一趟并行波之内不能同步,波结束才对齐。所以"每跑一趟 simulation 就有一个同步点"和"每个 processor 后面都是一个同步点"说的是同一件事。

In Mass terms: a processor walks every entity once, then finishes. "Finishes" means the same thing on both sides: a sync point. On the GPU you cannot synchronise inside a dispatch; alignment happens when it ends. On the CPU you cannot synchronise inside a parallel wave; alignment happens when the wave ends. So "one sync point per simulation pass" and "every processor ends at a sync point" say the same thing.

连句柄都是同一个设计 Even the Handle Is the Same Design

这一条我起初当成巧合,直到把两个结构体并排放在一起:

I took this for a coincidence until I put the two structs side by side:

NiagaraNiagaraMassEntityMassEntity
FNiagaraID { int32 Index; int32 AcquireTag; }FNiagaraID { int32 Index; int32 AcquireTag; } FMassEntityHandle { int32 Index; int32 SerialNumber; }FMassEntityHandle { int32 Index; int32 SerialNumber; }

两边解的是同一个问题:槽位会被复用,"第几个"不够用,还得知道"第几代"。粒子死了,它的 Index 归下一个粒子;实体销毁了,Index 同样被收走。第二个整数记的就是"这一代是什么时候发的"。连注释里的说法都差不多。

Both solve the same problem: slots get reused, so "which slot" is not enough on its own; you also need "which generation". A particle dies and its Index goes to the next one. An entity is destroyed and its Index is reused the same way. The second integer records when that generation was handed out. Even the comments read alike.

由此有个很实用的推论:把实体句柄原样丢进 Niagara 当一个 ID,是能对上的。我们的渲染路径接得那么顺,一部分原因就在这里:两边对"一个个体"的表示本来就是同构的。

That gives a practical corollary: you can hand an entity handle straight to Niagara as an id and it lines up. Our render path connects as cleanly as it does partly for this reason: both systems already represent "one individual" the same way.

不一样的地方,也要说清 Where They Genuinely Differ

一、过滤。Mass 有 tag:查询在遍历之前就把一整块实体挡在外面,处理器压根不进去。Niagara 没有这一层,模块只能把所有粒子走一遍,再在脚本里判断。两边用途不同,粒子不需要"只处理某一类"的批。(不过引擎给的是能力:我们工程上主要用 flag 分状态,每趟走该类的全部,这一点在下面「类比到哪里为止」里展开。)

One: filtering. Mass has tags, so a query can keep a whole chunk out before anything is walked and the processor never enters it. Niagara has no such layer: a module walks every particle and decides inside the script. The jobs differ; a particle batch rarely needs "only this category". (That is the engine's capability, though. In practice we carry state in flags and walk everything of the kind; see "Where the Analogy Stops" below.)

二、数据的切法。Mass 把字段切成有类型的片段,按原型分块,没带的列不占块空间。Niagara 一颗粒子就是一个结构,属性全塞在里面。前者省内存,查询也方便按需挑;后者简单,整包搬到 GPU 很容易。

Two: how the data is cut. Mass slices fields into typed fragments and groups them by archetype, so a column nobody carries costs no chunk space. Niagara lays a particle out as one struct with all its attributes. The first saves memory and makes selective queries easy. The second is simple and easy to ship to the GPU in one block.

三、确定性。Mass 这套是照"两端逐位一致"设计的。Niagara 的 GPU 模拟面向画面,不面向权威状态。所以我们只让它在渲染那一段分担计算,模拟结果不让它碰。

Three: determinism. Mass is designed for bit-identical agreement between peers. Niagara's GPU simulation aims at the picture, not at authoritative state. That is why it only takes part in the rendering stage with us, and never decides the simulation's result.

一份图、两个后端,为什么这件事了不起 One Graph, Two Backends: Why That Is Hard

把节点图同时编译到 CPU 和 GPU,两套代码生成器是容易的部分。难处在抽象边界画在哪:图里能用什么、不能用什么,才既够表达、又能在两端落地。任何一个"只在 GPU 上成立"的功能都会立刻在抽象上撕开一个洞。最后被设计出来的,是一份两端都能落地的语义子集。

Two code generators are the easy part of compiling one graph to both CPU and GPU. The hard part is where the abstraction boundary sits: what the graph may and may not express, so that it is sufficient and still implementable on either side. Any feature that only holds on the GPU tears a hole in that abstraction. What actually had to be designed is a semantic subset that lands on both.

再收紧一点:"同一套图两个后端"不等于"任意图随便换后端"。GPU 有它跑不了的东西(simulation stage 基本只存在于 GPU 侧),同一份图里也有要在两端分别处理的差异。那份语义子集本身就是一次取舍,代价照付。

Sharpen it further: "one graph, two backends" does not mean "any graph, either backend". The GPU has things it cannot run, and simulation stages exist essentially on the GPU side only. One graph also carries differences that have to be handled per backend. The semantic subset is a set of decisions, and it was paid for.

从使用者的角度看,收益很直接:同一份逻辑,换了执行位置不用重写。调参、调试、验证,都只对着一个东西做。

For the user the payoff is direct: the same logic does not have to be rewritten when the execution target changes. Tuning, debugging and verification all happen against one artifact.

渲染那一段,两边过的是同一道坎 The Last Mile Is the Same Hurdle

Niagara 自带整条链:GPU 上算完,紧接着用自己的渲染器合批画出来。MassEntity 没有自己的渲染器,官方做法是 Mass 负责算,交给 ISM(实例化静态网格)或粒子系统去画。

Niagara carries the whole chain: compute on the GPU, then draw batched through its own renderers. MassEntity ships no renderer; the official pattern is that Mass computes and ISM (instanced static meshes) or a particle system draws.

但两边踩的是同一条线:这类系统都躲不开"合批渲染"这道坎。几万个个体,每个都要有自己的身份,又要在几次 drawcall 里画完,唯一的路子是一次提交、一份实例数据。差别只在这段代码由谁写。

They cross the same line: every system of this class has to get through batched rendering. Tens of thousands of individuals, each with its own identity, all drawn in a handful of draw calls. The only route is one submission over one instance buffer. The difference is who writes that code.

我们的做法:中间多一块 GPU 加速 Our Shape: An Extra GPU Block in the Middle

MassBattle 走混合路线:Mass 跑模拟逻辑,把结果交给 Niagara;Niagara 用自己的 GPU compute 分担一部分计算,最后合批渲染。中间多出来的这一块,把一部分逐实体的插值、摆放这类纯计算搬到 GPU 上,CPU 那边的担子轻了。

MassBattle takes the hybrid route: Mass runs the simulation logic and hands the result to Niagara, Niagara takes a share of the computation in its own GPU compute passes, and then draws batched. That extra block in the middle moves a share of the per-entity interpolation and placement, pure computation, onto the GPU. The CPU side gets lighter.

这条线跟前几篇是同一条:权威跑在 CPU 上,跑在固定位置,跑得可复现。GPU 那边只接已经定稿的状态,做它擅长的活:大量相同的小计算,一次画完。中间这块加速搬的是模拟之后的那一段,主要是逐显示帧的插值和速度预测,把定稿的状态补到 60 / 120 帧。它不参与权威计算,能被允许存在也是因为这个:它产出的东西没有任何一份会被同步。

The same line the earlier notes draw: authority runs on the CPU, at fixed points, reproducibly. The GPU side only receives state that is already settled, and does what it is good at: many identical small computations, drawn in one go. The middle block moves the part after the simulation, mostly per-display-frame interpolation and velocity prediction, filling settled state out to 60 or 120 frames. It never joins authoritative computation. That is also why it is allowed to exist: nothing it produces is state anyone synchronises.

类比到哪里为止 Where the Analogy Stops

两边对得太齐,容易得出一个过头的结论:Mass 就是个"带 AI 的 Niagara"。这个结论不成立。粒子系统里只有一种粒子,一个 emitter 内部完全同质:所有粒子带同一套属性,模块遍历的时候不用问"这个有没有那个字段"。

Lining the two up that neatly makes it easy to overshoot: Mass is "Niagara with AI". That conclusion does not hold. A particle system has one kind of particle, and inside an emitter everything is homogeneous. Every particle carries the same attributes, so a module never asks "does this one have that field".

Mass 多的那一块是异质性:实体的片段组合可以不同,不同组合落成不同原型,住进不同的块。查询能按 tag 把整块挡在外面,处理器只进它要的那几块。粒子系统没有这一层,也不需要:粒子从来不用"只处理某一类"。

Mass adds heterogeneity. Entities can carry different fragment sets; different sets become different archetypes in different chunks. A query can hold whole chunks out by tag, and a processor enters only the ones it wants. A particle system has none of that, and needs none of it: particles never need "only this category".

再往下一层,是引擎能力和工程选择的差别。MassEntity 支持按 tag 过滤,教科书上的 ECS 就是这么用的:把"处于某个状态"的实体筛成一个子集,处理器只跑这一小撮。MassBattle 工程上没走这条路。状态基本都用 flag 区分,查询按"种类"划:一趟走全部 agent,一趟走全部 projectile,一趟走全部 host。于是每趟遍历的都是该类实体的全部,差不多就是"一个 emitter 只管自己那类粒子的所有粒子"。

One layer down sits a gap between what the engine can do and what we chose to do. MassEntity supports tag filtering, and that is how ECS is usually taught: filter entities in a given state into a subset and run a processor over just those. MassBattle does not work that way. State is almost always held in flags, and queries are cut by kind: one pass over all agents, one over all projectiles, one over all hosts. Each pass therefore walks all entities of its kind, which is about as close as it gets to "one emitter handles every particle of its own type".

所以这个类比在实践层面比在引擎能力层面更贴:我们借了 ECS 的存储布局和并行,刻意放弃了"按状态分桶",换来一趟遍历覆盖全部、遍数固定、帧时间平稳。那条取舍在另外两篇里写过:标记该用 tag 还是 flag,处理器该切多细。

That is why the analogy fits the practice better than the engine. We take ECS's storage layout and parallelism, and deliberately decline to bucket by state. In exchange a pass covers everything, the number of passes is fixed, and frame time stays flat. Two other notes spell that trade out: which marker to use, and how finely to cut a processor.

所以准确的说法是:Mass = 粒子系统那套设计模式 + 原型 + 查询。前一半是这个类比成立的原因,后一半是它比粒子系统多出来的部分,只不过我们用得克制:查询只按种类开,状态交给 flag。少了后半句,读者会以为 Mass 只是粒子系统换了个名字;说得太满,又会以为我们天天按状态筛子集,其实没有。

The accurate summary: Mass = the particle-system pattern + archetypes + queries. The first half is why the analogy holds. The second is what Mass has beyond a particle system, and we use it sparingly: queries are cut by kind, state is carried in flags. Drop the second half and a reader will take Mass for a particle system under another name. Overstate it and they will think we filter subsets by state all day. We do not.

今天还值得从它身上学什么 What Is Still Worth Learning Today

说实话,今天从零做同样的东西,我未必会选节点图:有了 coding agent,直接写代码、甚至直接写 HLSL,比连几百个节点快得多。可视化编程当初解决的是"让美术也能改效果"。这个问题今天还在,只是不再只有一条路。

Honestly, starting the same thing from scratch today, I might not pick node graphs. With a coding agent at hand, writing the code, or the HLSL directly, is faster than wiring several hundred nodes. Visual scripting was there to "let artists change the effect too". That problem has not gone away. It just is not the only road any more.

真正值得学的东西与界面无关,有两件。一是"一份描述、多个执行后端"的抽象:它的价值随硬件种类变多而上升,不会因为工具风尚变了就贬值。二是粒子系统这套设计模式本身:把状态摊平,按批处理,每趟只做一件事,趟与趟之间同步。MassEntity 把这套模式从粒子推广到了"任何成千上万的同类个体",包括会跑、会打、会死的单位。

What is worth learning has nothing to do with the UI, and there are two pieces. First, "one description, several execution backends": its value grows as hardware variety grows, and does not fade when tool fashions change. Second, the particle-system pattern itself: flatten the state, process in batches, one job per pass, synchronise between passes. MassEntity generalises that pattern from particles to "any tens of thousands of homogeneous individuals", including units that run, fight and die.

一句话 In One Line

Niagara 是用节点图写的 ECS,MassEntity 是用代码写的 ECS。一个面向画面,一个面向权威状态,底下的设计模式是同一个。看懂了粒子系统,这类模拟系统的大半也就懂了,连句柄都是同样的两个整数。

Niagara is an ECS written as a node graph; MassEntity is an ECS written as code. One aims at the picture, the other at authoritative state; underneath is the same pattern. Understand a particle system and you understand most of this family, down to a handle that is the same two integers.

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

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