Logo

Max Brun

Software Engineer
Game Engine & Game Systems Programmer

Building Fading Echo’s Grid System

A deep dive into C++ and systemic elemental interactions

Fading Echo Grid System banner

In Fading Echo, the world is alive with systemic elemental reactions: water trails absorbed by the sand, pools of lava cooled by water, and waste puddles that explode and spread out.
I designed and implemented a Grid System entirely in C++ within Unreal Engine: an optimized system composed of chunks and pooled cells that manages all elemental reactions. It is entirely data-driven, allowing designers to create unique systemic reactions in the game.

This post is a walkthrough of how that system works.

Water, toxic waste and lava reacting on the ground in Fading Echo
Note This post presents the Grid System as it shipped in Fading Echo. Some parts may be outdated by now.

Data structure and Baking

In Fading Echo, elements are not just a visual effect painted on the ground. They need to react with each other, and entities need to interact with them. So they have to exist on the gameplay side, not only on the rendering side.

To make that possible, I put a grid on top of the environment. Each cell knows which elements are on it. It can be a fluid added at runtime by an entity (water, lava, toxic waste…), or the type of ground described in the map (sand, rock…).

Debug view of the grid in Fading Echo
A debug view of the grid: each cell takes the color of its element with the highest quantity, and shows the quantity of each element added to it.

The world is not flat, but a 2D grid is. So each position of the grid doesn’t hold one cell, it holds a small list of cells, one per height. Under a bridge, for example, the same position holds a cell on the bridge, one on the water and one on the riverbed. When I need the cell under an entity, I take the one inside a small height range around it.

I kept the cell as small as possible: its coordinate, up to 4 elements with a one-byte quantity each, and two bit masks telling which reactions are currently running on it, 64 bits for the internal ones and 256 bits for the external ones. The elements are stored inline in a fixed-size array, so a cell never allocates memory on its own.

GridThe whole level, split in chunks
ChunkA square block of grid positions
PositionOne X, Y on the grid, with one cell per height
CellUp to 4 elements, each with a quantity

Baking

Building the grid while the game runs would be way too slow, so it’s baked in the editor, once per level. For each position of the grid, the baker casts a ray from the sky down to the ground. Every surface the ray hits becomes a cell at that height. If a hit is too close in height to the previous one, it is skipped. Otherwise, a few stacked meshes could generate dozens of cells at the same position for a single walkable surface.

Rays cast from the sky create one cell per surface hit, three under the bridge
One ray per position. Under the bridge, the ray hits the bridge, the water and the riverbed, so this position holds three cells.

The starting element of a cell comes from the physical material of the surface it was built on: a sand material gives a sand cell, a rock material gives a rock cell. This mapping lives in a data asset, so designers set it up themselves.

The result is saved in a data asset as two flat arrays, positions and cells. It’s a compact, contiguous memory layout that is serialized in bulk, which makes it fast to load. When a level loads, the baked data is converted into the runtime grid over several frames with a small time budget, instead of doing all the work in a single frame.

Chunks and Culling

A level contains a lot of cells, and most of them are far away from the player. So the grid is split in chunks, and only the chunks around the player are active: a 5×5 square centered on the chunk the player is standing in. All the other chunks are asleep. The culling is only recomputed when the player enters a new chunk: as long as they stay in the same one, the update stops right away.

Chunks waking up and going back to sleep while the player moves around.

The cells are pooled. Instead of allocating memory every time a chunk wakes up, the system keeps a pool of grid portions: blocks of memory preallocated at the exact size of a chunk. Each portion allocates its arrays once, and the cells of a position are stored contiguously in memory. When a chunk becomes active, it borrows a free portion and its baked cells are copied into that existing memory, without shrinking or reallocating the arrays. When it goes back to sleep, its reactions are flushed and the portion goes back to the pool. The goal was to stay cache-friendly: the baked data is two flat arrays and a cell stores its elements inline, so reading them doesn’t jump between scattered allocations.

Activating and flushing chunks also work with a time budget. When the player crosses a chunk border, the new chunks are filled over a few frames instead of all at once. And if the player turns back before a chunk was flushed, it is simply reactivated as it is.

5 by 5 active chunks around the player
1. ActiveThe 5×5 chunks around the player
Chunks being flushed and activated after the player moved
2. Crossing a borderRed chunks are flushed, blue ones are activated, green ones stay active
New 5 by 5 active chunks
3. A few frames laterThe new 5×5 square is active
Debug view showing chunk IDs, grid portion IDs and water quantities
Debug view: each chunk shows its ID, its coordinate and the grid portion it borrowed. The water cells show their quantity.

Portals

In Fading Echo, a portal shows a piece of another dimension, which is another level, right inside the current one. The grid has to follow what the player sees. When a portal opens, the cells of the active chunks inside the portal’s sphere are swapped with the baked cells of the other level. To find them, the system builds a lookup table of the other level’s baked positions, instead of allocating a full 2D grid for it. Inside the portal, the ground and its elements come from the other dimension.

Inside the portal, the grid shows the cells of the other level.

Elemental Reactions

Depending on the elements present in the same cell, reactions can be triggered. A reaction is a basic condition-effect pair: it checks the state of a cell and applies a bunch of effects to it. Here are some examples of reactions we have in the game:

  • Water on sand is absorbed until it disappears.
  • Water on lava creates cooled lava.
  • Cooled lava on lava is absorbed over time until it disappears.
  • Lava on toxic waste spreads across the waste puddles, removing the waste.

Below, the last one is simulated with the same rule as the propagate effect. Each burning cell sets its neighbors on fire after its own random delay, so the fire doesn’t move like a perfect wave and looks more natural.

Lava touching a toxic waste puddle
1.Lava touches the waste
Fire starting to spread on the waste
2.Burning cells ignite their neighbors
Fire spreading across the puddle
3.The fire runs across the puddle
The waste is consumed
4.The waste is gone
Elemental Propagate as a continuous cell effect: each update, the water spreads to the neighbor cells.

A reaction has a small life cycle on each cell it touches:

RegisterThe cell matches the start conditions, e.g. it contains water and sand
StartInstant effects are applied once
UpdateContinuous effects run over time, e.g. the water quantity goes down
UnregisterThe end conditions are met, closing effects are applied

Conditions can check which elements are in the cell, their quantity, how long the reaction has been running or whether its effects are finished, and they can be combined with And and Or. Effects can add or remove an element, change a quantity over time, spread or propagate to the neighbor cells, spawn a VFX or play a sound.

Cell Conditions
  • And
  • Or
  • Elements
  • Quantity
  • Duration
  • Effect Finished
Cell Effects
  • Elemental Add
  • Elemental Remove
  • Elemental Quantity
  • Elemental Propagate
  • Spread
  • VFX
  • Sound
The conditions and effects designers pick from.

A cell runs only one reaction at a time. Each reaction has a priority, and a reaction with a higher priority can take over one that is already running on a cell. When an internal reaction can’t take a cell yet, it keeps it in a pending list and tries again on the next updates.

Internal and external reactions

There are two kinds of reactions in the Grid System.

Internal reactions belong to the world. They aren’t linked to anything: they watch the cells and start as soon as a cell matches their conditions. All the examples above are internal reactions, and they are all referenced in a single data asset.

Internal Reactions Set Data Asset
  • Water absorbed by sand
  • Cooled lava absorbed by lava
  • Cooled lava created by water on lava
  • Water absorbed by rock
The internal reactions, referenced in a single data asset.

External reactions are linked to an entity, and their effects can also be applied to the entity itself. Here are some examples:

  • The player in their water form leaves water on the ground.
  • The player in their human form leeches the water on the ground, increasing their mana gauge.
  • Elemental enemies leave a puddle of their associated element on the ground when dying.
  • Entities start burning when walking on lava.

An entity registers to the Grid System with a Grid Monitor component, which references the external reactions of that entity.

Water Wave Ability
  • Entity Component
  • Grid Monitor
    External Reactions SetWater Wave Reactions
The water wave ability registers to the Grid System with its Grid Monitor component.

Instead of watching the whole world, an external reaction only looks at the cells inside a shape attached to the entity: a box (AABB or OBB) or a circle. This lets us adapt the reaction to each entity. For instance, to avoid being too punishing, the player needs a majority of lava cells below them to start burning, while other entities burn as soon as they have one lava cell below them.

A plant with a Grid Monitor component: a single lava cell under it is enough to tag it as Burning.
The water wave spell: an external reaction with an OBB attached to it. Water on lava creates cooled lava.

External reactions also come with a few extra settings: a gameplay condition that has to be true before the reaction even looks at the cells, game effects applied to the entity, once for the whole reaction or for each cell, and an infinite option. With it, cells stay in the reaction as long as they are inside the shape. That’s how the water form keeps a circle of water around it while it stands still.

External Reaction
External
Is Infinitebool
Gameplay ConditionCondition
Reaction Game Effectslist
Per Cell Game Effectslist
Reaction ShapeBox | Circle
Reaction
Priorityint
Register Conditions & Effectsconditions + instant effects
Unregister Conditions & Effectsconditions + instant effects
Cell Effectscontinuous effects
An external reaction: the external settings on top, the same settings as an internal reaction below.

External reactions usually have a higher priority than internal ones, so what an entity does wins over what the world is doing.

The data to rule them all

Nothing here is hard-coded. Grid settings, elements, reaction conditions and effects… everything is described in data assets. Designers can experiment, create and tweak reactions directly in the editor without any new code.

Debug views

To check all of this in game, the Grid System comes with a few debug commands that draw its data in the world:

Debug view of the active chunks
Debug Chunks: draws the active chunks, with their ID, coordinate and grid portion ID.
Debug view of the cells of the current chunk
Debug Grid In Chunk: draws the cells of the chunk the player is in.
Debug view of the shape of an external reaction
Debug Grid Shapes: draws the shapes of the external reactions.
Debug view of the cells in a circle around the player
Debug Grid In Shape: draws the cells in a shape around the player, here a circle. Useful to quickly test shapes for external reactions, using the command’s arguments.

The Runtime: Registering and Updating

Every frame, the Grid System does the same things in the same order: it updates the culling around the player, updates the external reactions, then the internal reactions, and finally sends the active cells to the renderer.

Both kinds of reactions at work. The player in their water form leaves water on the ground (external), then the water quantity of each cell goes down over time, and on the lava the water creates cooled lava (internal).

The simple version would be to test every reaction on every cell, every frame. That doesn’t scale, so the two kinds of reactions find their cells differently:

  • Internal reactions only look at the cells that just changed. When an element is added or removed, or when a quantity changes, the cell is put in a “recently updated” list. On the next frame, these reactions only test the cells of that list. On the simulation side, a cell that nothing touches costs nothing.
  • External reactions only test the cells inside the shape of their entity.
Only the recently updated cells of a water trail are tested
InternalOnly the cells that just changed, here the head of a water trail
Cells inside an AABB
External · AABBCells inside the box
Cells inside an OBB
External · OBBThe box follows the entity’s rotation
Cells inside a circle
External · CircleCells inside the radius

In orange, the cells tested this frame.

When a cell matches the start conditions, the reaction creates a small state for it: the time since it started, the current values of its continuous effects, and so on. These states live in a contiguous array, and a free list reuses the slots of the reactions that ended, so starting and stopping reactions on cells doesn’t allocate memory. And since each cell keeps a bit mask of the reactions running on it, knowing if a reaction is already running on a cell is a single bit check.

Then each running reaction updates its continuous effects and checks its end conditions. When they are true, the closing effects are applied and the state goes back to the pool.

The grid is shared between the game thread and the render thread. If the render thread is still reading it, the game thread doesn’t wait. It skips the Grid System update for this frame and adds the time to the next one.

Simulation (game thread)IdleStress test
Reactions and culling0.03 – 0.05 ms0.08 – 0.4 ms
Spatial queries (box, circle, OBB)1 – 6 µs per query1 – 6 µs per query

Measured in our worst-case scenario: a level with a lot of verticality, so a lot of cells stacked on the height. Idle is the player standing still, the stress test is an intense fight with fluids spreading everywhere.

Class diagram of the Fading Echo Grid System
The class diagram of the whole system (click to open it full size).

Rendering with Compute Shaders

Elements added by entities or created by reactions are written in render targets. They only cover the area around the player, so only a limited part of the grid is rendered at once. The shaders of the environment then read them to draw the elements on the ground.

Left: the game. Right: the render target around the player, updated live. Recorded while the system was still a work in progress, so you might spot a few glitches.

The render targets follow the player, but their position is snapped to the size of a texel. This way, the texels stay aligned with the same spots of the world, and the edges of the puddles don’t shimmer when the player moves.

The render targets are filled on the GPU with compute shaders, in a few passes:

Grid cells
1. CellsFor each position, the cell closest to the player’s height
Raw quantity
2. QuantityOne texel per cell, still very squarish
Jump Flood
3. Jump FloodEach empty texel takes the element of the closest cell
Gaussian blur
4. Gaussian blurThe quantity is blurred to get organic shapes
Final result
5. ResultThe ground shader draws the puddles

The grid is written in two render targets in RGBA16f: four channels of 16-bit floats, so 64 bits per texel. Here is what each channel holds:

Element texture
RFirst fluid
GSecond fluid
BCell height
ACell height
Quantity texture
RQuantity of the first fluid
GQuantity of the second fluid
BFree
AFree

So a single texel can hold two fluids at once, with their quantities and the height they are at.

The cells are uploaded to a GPU buffer that is only recreated when it gets too small, the rest of the time it is reused. Preparing this data on the render thread is currently the most expensive step of the system. Flagging the chunks as dirty, to only rebuild the ones whose cells changed, would cut down this hand-off between the CPU and the GPU.

Collect cellsRender thread
GPU bufferUpload
Cell passIDs + height
Jump Flood
Element texture
Cell passQuantities
BlurH + V
Quantity texture
From the active cells to the two render targets. The orange step runs on the CPU, the rest on the GPU.

The first pass runs one GPU thread per cell, and each thread draws its cell as a small square of texels. Seen like this, the puddles are just squares.

Each GPU thread draws its cell as a square of texels
Each GPU thread takes one cell from the buffer and draws it as a square of texels, at the cell’s position. Blue cells hold water, orange cells hold lava.

To avoid seeing the squarish aspect of the grid in the world, the quantity is blurred with a Gaussian blur, done in two passes, horizontal then vertical.

Quantity and Element textures after the cell pass
1. Cell passSquares in both textures
Element texture filled by the Jump Flood
2. Jump FloodThe Element texture is filled: every texel gets its closest cell
Quantity texture blurred
3. BlurThe Quantity is blurred. Inside the white outline, every texel already has an element
Final puddles without and with the Jump Flood
4. ResultWithout the Jump Flood, the blurred edges have no color and the puddles stay square

But the blurred quantity spreads a little outside of the original cells, onto texels that have no element in the Element texture. These soft edges would have some fluid but no color, and the puddles would stay square, as the top of step 4 shows.

That’s why the Jump Flood pass runs on the Element texture before the blur: it gives every texel the element of its closest cell, as in step 2. Once the quantity is blurred in step 3, every texel inside the white outline already has an element. To find that closest cell quickly, it works in a few passes. In each pass, every texel looks at 8 neighbors at a given distance and keeps the closest cell they lead to. Then the distance is halved for the next pass: far away first, then closer and closer.

Jump Flood start: one cell with an element
StartOne cell has an element, every other texel is empty
Jump Flood pass with a jump of 4
Jump 4The texels 4 away from the cell see it and store it
Jump Flood pass with a jump of 2
Jump 2Same at 2 texels: they pass it on to new texels, in bright
Jump Flood pass with a jump of 1
Jump 1Same with the direct neighbors: the whole texture now stores the cell
Jump Flood with two cells, water and lava
Two cellsWith several cells, each texel keeps the closest one

In the end, the two textures work together: the Quantity texture gives the shape of the puddle, and the Element texture tells which fluid it is, as in step 4. Both fluids are handled in the same passes, and the height of the closest cell is copied along with its element.

GPU passesIdleStress test
Jump Flood0.12 – 0.13 ms0.12 – 0.13 ms
Blur (2 passes)~0.05 ms~0.05 ms
Cell rasterization< 0.01 ms< 0.01 ms
Total0.22 – 0.23 ms0.23 – 0.24 ms

Same scenario as above. At 60 fps, the whole GPU pipeline takes about 1% of the frame, whatever happens on screen.

A render target is a 2D texture, and that gave us one big issue: a puddle on a bridge was also painted on the ground under the bridge, since both share the same X, Y. To fix it, each texel also stores the height of the cell it comes from. When two cells land on the same texel, the one closest to the player’s height wins.

Without height: the puddle on the bridge is also painted under it
BeforeThe puddle on the bridge is also painted under it
With height: the puddle is only drawn on the bridge
AfterWith the height stored per texel, it stays on the bridge
Several fluids on the ground in Fading Echo
The final result in game.

Creating a New Fluid

Adding a new reaction is only data. A new fluid just needs to be declared as a new element, with its color, and the ground shaders need to know how to draw it.

From there, the fluid is part of the system like any other element: it can be used in the baking, in conditions, in effects, and designers can start building new reactions around it.


The Grid System shipped with Fading Echo on PC, Xbox Series X|S, PS5 and Switch 2. If you want to see it in action, go grab the game and start pouring water on some lava!

This system was a team effort. Thanks to Rémi Ducher, my tech lead, to my colleague Valentin Comte, and to Thibaud Juge, the technical artist with whom I spent many hours figuring out the rendering challenges.