DSHINTERACTIVE
Tutorials

Fire, Smoke and Blend Modes: 2D Effects Explained

An explosion in Iron Sector is a handful of small pictures laid over the battlefield every frame, and each one is only as good as the formula that lays it there. This lesson takes one 152 mm shell apart: the three blend modes, the clock of every layer, the curves that keep fire from flickering and smoke from stepping, and the render clock that cured a stutter, with the game's real numbers.

blend formulas: paint, light and shade
3
blend formulas: paint, light and shade
coverage in the middle of a flame cross-fade, instead of 75 %
94 %
coverage in the middle of a flame cross-fade, instead of 75 %
sprites for 100 blasts at once, in 0.2 ms
451
sprites for 100 blasts at once, in 0.2 ms
repeated frames in eleven at 60 Hz, before and after the render clock
6 → 0
repeated frames in eleven at 60 Hz, before and after the render clock

The arithmetic of blending

Every effect in a 2D game is a picture laid over another picture, and the only question is the formula that combines them. The new picture is the source, what is already on the screen is the destination, and the formula runs for every pixel and every colour channel. Three formulas matter here.

Normal blending is paint: the result is the source times its alpha, plus the destination times one minus alpha. Take an orange pixel (255, 140, 40) at half opacity over dark green grass (60, 90, 40): the result is (158, 115, 40), a muddy middle. Additive blending is light: the source times its alpha plus the destination, with nothing taken away. The same orange over the same green gives (188, 160, 60), brighter than either, and a second flash on top pushes it further until the channels clamp at 255. Multiply is shade: the source times the destination, channel by channel, divided by 255. White changes nothing, every other colour darkens: (60, 49, 6).

There is a fourth common mode, screen, the mirror of multiply, which brightens. Iron Sector never uses it. Smoke is paint, fire is light, night is shade: that is the whole lesson in three words, and the rest is detail.

normal:   out = src·a + dst·(1 − a)
add:      out = src·a + dst
multiply: out = src·dst / 255
src = (255, 140, 40), a = 0.5, dst = (60, 90, 40)
normal:   (127.5 + 30, 70 + 45, 20 + 20) = (158, 115, 40)
add:      (127.5 + 60, 70 + 90, 20 + 40) = (188, 160, 60)
multiply: (255·60, 140·90, 40·40) / 255 = (60, 49, 6)
One worked pixel: half-transparent orange over dark green, through the three formulas the game uses. Screen, the fourth, is not used anywhere in the game.
One worked pixel: half-transparent orange over dark green, through the three formulas the game uses. Screen, the fourth, is not used anywhere in the game.

Which layer uses which

The renderer puts each effect on the shelf its formula needs. Smoke, dust, fireballs and flames are normal-blended, and they live in the same depth-sorted layer as the tanks and the houses. Paint has to be sorted, because the order of two layers of paint changes the result: a smoke column behind a wall must be drawn before the wall.

Everything that is light goes on one shelf above all the objects, called fxAdd and drawn additively: muzzle flashes, the pressure ring of a blast, embers and sparks, the streak of a ricochet, the pops of a tank cooking off and, at night, the glow in the windows of a burning house. Light needs no sorting, because addition is commutative: the order of two flashes does not change their sum. The tracers get a thin additive glow of their own under their streaks, on a shelf called topGlow.

At the very top, above the fog, one full-screen sprite is multiplied over everything to make dawn and night. That one is the subject of the next lesson. Three formulas, three shelves.

a + b = b + a   (light: no sorting)
paint: a over b ≠ b over a   (smoke: sorted with the objects)
A knocked-out tank and the renderer's shelves, drawn bottom to top: the ground, the depth-sorted objects with their normal-blended smoke and flames, the two additive shelves, and the multiplied time of day on top.
A knocked-out tank and the renderer's shelves, drawn bottom to top: the ground, the depth-sorted objects with their normal-blended smoke and flames, the two additive shelves, and the multiplied time of day on top.

An explosion, layer by layer

Here is the explosion of a 152 mm shell with its clock. Every blast has a scale, and the scale picks its family: small up to 0.8, medium up to 1.1, heavy above. This shell has a scale of 1.25, so it is heavy. At zero comes the flash: three additive frames at 30 frames per second, one tenth of a second of light, sized (0.6 + 0.6 × scale) times a small random jitter between 0.9 and 1.15. Over it, the fireball: eight normal-blended frames at 22 frames per second, about a third of a second. For a medium or heavy shell, and never on water, a pressure ring expands for a quarter of a second at 9 % opacity, just enough to feel.

Under all of it lies the dust skirt: twelve frames at 10 frames per second, cross-faded so they do not step, and tinted with the colour of the ground under the shell. The skirt's frames themselves are neutral; the ground gives the colour: white on snow, pale yellow on sand, grey on a paved road, brown on soil.

Then the smoke column: three puffs living 2.5 seconds for a medium shell, five puffs living 5 seconds for a heavy one, two more, and paler, if the shell hit masonry. At 0.45 seconds a lingering dust cloud appears and hangs for 4.5 seconds times the ground's linger factor, which is 1.6 on sand and 2 inside a house, capped at 5.45 seconds: about a second longer.

Everything that floats is pushed by one wind vector: 9 units per second to the right and 3 up the screen. It is the same for every effect, so smoke, dust and embers all lean the same way, and that one shared number does more for believability than any of the frame counts.

families: small ≤ 0.8 < mid ≤ 1.1 < heavy;   152 mm: scale 1.25 → heavy
flash: 3 frames / 30 fps = 0.1 s;   size = (0.6 + 0.6 · 1.25) · jitter = 1.35 · (0.9 … 1.15) = 1.22 … 1.55
fireball: 8 / 22 = 0.36 s;   skirt: 12 / 10 = 1.2 s;   ring: 0.25 s at alpha 0.09
lingering dust = min(5.45, 4.5 · linger);   sand: min(5.45, 7.2) = 5.45 s
wind = (9, −3) units / s
The 152 mm burst slowed down, 0.68 s after the impact, with the clock of each layer. The flash, the ring and the fireball are already over; the skirt, the clods, the smoke column and the lingering dust are still running.
The 152 mm burst slowed down, 0.68 s after the impact, with the clock of each layer. The flash, the ring and the fireball are already over; the skirt, the clods, the smoke column and the lingering dust are still running.
The same burst 0.4 s after the impact on four grounds. The skirt's frames are neutral; the ground under the burst gives the tint.
The same burst 0.4 s after the impact on four grounds. The skirt's frames are neutral; the ground under the burst gives the tint.

Clods with physics

The clods are the one part of an explosion that obeys physics. A small blast throws 4 of them, a medium one 7 and a heavy one 11. Each clod gets an upward speed between 38 and 108 units per second and an outward speed between 14 and 60, both scaled by the shell's size, with r a random number taken from the effect's id, so the same blast always throws the same clods.

Gravity pulls at 160 units per second squared. A clod flies, lands, bounces once with 35 % of its vertical speed and 60 % of its horizontal speed, lands again and rests for half a second while it fades. The whole life is about 2.3 seconds. Each clod drags a small shadow across the ground under it, which is what tells the eye how high it is, and raises a puff of dust where it lands.

When a hundred shells land at once, a limiter scales the clod count down, aiming to keep all the blasts under 600 sprites. It counts about 23 sprites per blast, 9 for everything else and 14 for the clods, so it starts at about 26 blasts and leaves room for the rest of the explosion first. The explosion stays, the confetti goes.

v_up  = (38 + 70·r)(0.7 + 0.3·sc)
v_out = (14 + 46·r)(0.6 + 0.45·sc),   r ∈ [0, 1)
152 mm, sc = 1.25:  v_up = 40.9 … 116.1,  v_out = 16.3 … 69.8 units / s
g = 160 units / s²;   bounce: v_up × 0.35, v_out × 0.6;   rest 0.5 s
life = 2 · 108 · 1.35 / 160 + 0.5 = 2.32 s
share of clods k = (600 − 9n) / (14n),   from n · 23 > 600, n ≈ 26

The cross-fade curve

A flame loop at 20 frames per second steps visibly, so the game blends each frame into the next. Cross-fading two frames sounds trivial: mix them, the outgoing one at 1 − f and the incoming one at f. It is not, because of the normal blend formula. Two layers at half opacity do not cover like one layer at full opacity. The first covers half, the second covers half of what is left, and the total is 75 %. In the middle of every cross-fade the flame would lose a quarter of its body, twenty times a second: it would breathe with the frame rate.

The fix is a curve. The weight of the outgoing frame is (1 − f) raised to a power gamma, the weight of the incoming frame is f to the same power. With gamma below one, both weights are above a half in the middle. For flames gamma is 0.4: at the midpoint each frame is drawn at 76 %, and together they cover 94 %. Soft smoke and dust use 0.75, which gives 84 %. For an additive layer the plain mix is already exact, because adding does not dip; that is a gamma of 1.

One number, chosen per layer, and the flames stop breathing with the frame rate.

coverage of a over b = a + b·(1 − a)
linear, f = 0.5:  0.5 + 0.5 · 0.5 = 0.75
w_A = (1 − f)^γ,   w_B = f^γ
γ = 0.4:   0.5^0.4 = 0.76;    1 − 0.24² = 0.94
γ = 0.75:  0.5^0.75 = 0.59;   1 − 0.41² = 0.84
Two real frames of the flame loop at f = 0.5 with γ = 0.4: each at 0.76, together covering 94 %. On the right, the coverage of the pair across the whole fade: the linear mix sags to 0.75 in the middle, the curve with γ = 0.4 stays near the top.
Two real frames of the flame loop at f = 0.5 with γ = 0.4: each at 0.76, together covering 94 %. On the right, the coverage of the pair across the whole fade: the linear mix sags to 0.75 in the middle, the curve with γ = 0.4 stays near the top.

Smoke that never steps

Smoke takes the opposite approach from fire. The smoke that rises from a blast, a gun or a fire has no animation at all. There are six puff shapes, and a puff never changes its picture. It lives by moving with the wind, growing, turning slowly and fading.

Muzzle smoke after a shot is the same thing in three sizes: two puffs for a small gun, four for a medium one and six for a heavy one, living 1.6, 2.4 and 3.5 seconds. The reason is the frame rate. A flipbook steps from picture to picture, and the eye catches steps in a slow, soft shape far more easily than in a flash. A puff that only transforms is continuous at any frame rate, and it costs one sprite with no frames to store.

The one smoke that still has frames is the smoke screen, because it has to stand in one place and churn for up to a minute. It is three to five big sprites, each playing a slow loop of 16 frames at 8 frames per second, cross-faded with the soft gamma of 0.75 so it never steps. Its opacity rises over the first two seconds and thins over the last quarter of its life. A shell's screen lives 45 seconds, a barrage's 60.

muzzle smoke: 2 · 4 · 6 puffs, living 1.6 · 2.4 · 3.5 s
smoke screen loop: 16 frames / 8 fps = 2 s, γ = 0.75
opacity: up over 2 s, down over the last 25 %;   45 s screen: last 11.25 s

Fires that do not march in step

A fire is a loop of 24 frames at 20 frames per second, 1.2 seconds long; after it dies down, a smoulder loop of 16 frames at 12. The problem with loops is that a column of five burning tanks would burn in unison, and nothing says computer like five identical flames. So every fire is told apart by its id.

The id picks one of three variants and a horizontal flip from one of its bits, and its hash picks a speed between 88 and 112 %. Then the clock itself wanders: the frame position is time times frame rate times speed, plus an offset from the id, plus two slow sine waves of different periods. The waves add and subtract a frame or two, but their slopes are small enough that the clock never runs backwards, so the flame never plays in reverse. The height pulses by 6 % on a sine of its own, and two sprites are cross-faded with the gamma of 0.4 from the cross-fade chapter.

Embers rise additively: four for a tank, three for other vehicles, two for a truck or a gun, six for a house. Every two seconds there is a 55 % chance of a pop of seven sparks lasting 0.6 seconds. A wrecked vehicle burns for 25 to 60 seconds, chosen once from the seed, and then smoulders for the rest of the mission. A knocked-out gun burns too, with a smaller flame.

variant = id % 3,   flip = bit 1 of id,   speed = 0.88 + 0.24 · hash(id)
pos = t · 20 · speed + 3.7·id + 2.5·sin(0.9t + id) + 1.6·sin(0.37t + 2.1·id)
wave slopes ≤ 2.5·0.9 + 1.6·0.37 = 2.25 + 0.6 frames/s;   clock ≥ 12 · 0.88 ≈ 10.5 frames/s
tall = 1 + 0.06 · sin(1.3t + 1.7·id)
burn = 25 + 35 · R s  (25 … 60),  then smoulder
Eight fires drawn with the game's flame frames and its fire formula. Left: one loop, eight times in lock step. Right: variant, flip, speed, the wandering clock and the height pulse, all from each fire's id. — Told apart by id
Eight fires drawn with the game's flame frames and its fire formula. Left: one loop, eight times in lock step. Right: variant, flip, speed, the wandering clock and the height pulse, all from each fire's id. — All in step
All in stepTold apart by id
Drag to compare
Eight fires drawn with the game's flame frames and its fire formula. Left: one loop, eight times in lock step. Right: variant, flip, speed, the wandering clock and the height pulse, all from each fire's id.

Tracers

A tracer is three strokes: a hot core, a wider glow underneath it drawn additively at 28 %, and a flare at the head with a radius of 2.4. Machine-gun rounds are thin, a core of 1.3 pixels under a glow of 4.5. Gun rounds are thicker at 2 over 6.5, armour-piercing rounds 1.5 over 4.5, and high-explosive shells thickest at 2.2 over 7.5.

The streak trails the head by a fraction of the flight: 30 % for a machine-gun burst and a gun tracer, 24 % for a high-explosive shell, 14 % for a fast armour-piercing round, because a fast round draws a shorter line in the eye. And every round is a little brighter or dimmer than the last, by up to 3 %, from a hash of the moment it was fired. It is invisible when you look for it and alive when you do not.

The glow is the important part. Without it a tracer is a thin yellow line on a dark field. With it, it is a light.

core / glow, px:  MG 1.3 / 4.5 · gun 2 / 6.5 · AP 1.5 / 4.5 · HE 2.2 / 7.5
glow alpha 0.28;   head flare radius 2.4
streak = 0.3 (MG, gun) · 0.24 (HE) · 0.14 (AP) of the flight
alpha × (0.97 + 0.03 · sin(hash of the round's start))
A high-explosive round at quarter speed, enlarged: the hot core, the wider additive glow under it and the flare at the head.
A high-explosive round at quarter speed, enlarged: the hot core, the wider additive glow under it and the flare at the head.

Impacts by material

Where a round lands decides what you see. Every hit on the ground throws a small spurt of dust in the ground's own colour, and then the material adds its own: in water a splash and a ring instead, in snow a white puff and three chips, in sand just a puff, on a paved road four chips and, six times out of ten, a spark, on masonry a puff and four chips, in a wood four chips of bark.

On armour there are two outcomes, and the effects are how the player learns which one happened. A ricochet is a flash, four sparks and a streak that leaves the plate for 0.15 seconds. A penetration is a brighter flash, a dark puff and eight sparks. The simulation decides the outcome; the effect is its only report, and it has to be readable at a glance from a hundred metres away.

Where it landsWhat it throws
Any grounda spurt of dust in the ground's colour
Watera splash and a ring instead
Snowa white puff and 3 chips
Sanda puff
Paved road4 chips, a spark 60 % of the time
Masonrya puff and 4 chips
Woods4 chips of bark
Armour, ricocheta flash, 4 sparks and a 0.15 s streak
Armour, penetrationa brighter flash, a dark puff and 8 sparks
The impact recipes by material.
RicochetRicochet
PenetrationPenetration
The two outcomes on a T-34. The ricochet's flash with its streak leaving the plate; the penetration's brighter flash, just before its sparks.

The budget

Effects are the one thing in the game that can explode in number. A barrage can put a hundred shells on the ground in a second. So every layer has a cap per frame: 320 flipbooks, 120 flashes, 120 fireballs, 90 dust clouds, 100 sparks, 60 smoke columns, 240 smoke puffs. When a cap is hit, the effects nearest the centre of the view keep their sprites and the far ones are skipped, because the centre is where the player is looking. Anything more than 480 pixels outside the view is not drawn at all.

Sprites are never created or destroyed during a battle. They live in pooled slots that are numbered again from one every frame, and a slot that is not used is hidden, not destroyed. The quality setting scales the caps: Low halves them, halves the clods, keeps 60 % of the puffs and turns the cross-fade off; High raises the caps and the clods by half and the puffs by 40 %. In a test barrage of 100 shells in 1.2 seconds the renderer peaked at 1,321 sprites, with 6 layer sprites capped.

One confession. For a while, through a small mistake in a function call, the game never actually ran the budget code, and nobody noticed, because the caps are generous. Only a test ever called it the full way, so the test stayed green while the game skipped it. It was found while measuring the effects before a rewrite, which is a lesson about tests, not about effects.

Low: caps × 0.5, clods × 0.5, puffs × 0.6, no cross-fade
Normal: × 1;   High: caps × 1.5, clods × 1.5, puffs × 1.4
beginFxFrame(w)      // the budget never ran
beginFxFrame(w, c)   // the fix: c = the frame's view and render clock
The caps per frame, and the rule over a cap: the effects nearest the centre of the view keep their sprites. Beyond 480 map pixels outside the view, nothing is drawn.
The caps per frame, and the rule over a cap: the effects nearest the centre of the view keep their sprites. Beyond 480 map pixels outside the view, nothing is drawn.

The stutter and the render clock

Here is a bug worth a chapter. The simulation steps 30 times a second, and the screen draws 60 times or more. Tanks moved smoothly, because the renderer interpolates their positions between two steps. Fire did not, because an effect picked its frame from the simulation's time, and that time only changed 30 times a second. On a 60 Hz screen every second frame of a fire was identical to the one before it, and with the simulation in a worker thread it was worse. Measured on a burning wreck: in eleven consecutive frames, six were copies.

The fix is a second clock. The renderer knows how far past the last step it is, as a fraction of a step. Render time is the simulation's time plus that fraction times the step, and effects read render time, so they move on every screen frame. It is not the same number as the interpolation factor: that one is capped at 1 and sits at 1 while the game is paused.

The render clock is zero when the game is paused, when you seek, and when a screenshot or a video frame is taken, so those frames remain an exact function of the simulation. After the fix: eleven frames, zero copies.

before: frame = floor((t − t₀) · fps),   t in steps of 1/30 s
t_render = t + clock · dt,   dt = 1/30 s
60 Hz, ideally:  clock = 0, 0.5, 0, 0.5, …   →  t_render advances 1/60 s per screen frame
fire, 20 fps:  20/30 = 0.67 frames per step  →  20/60 = 0.33 per screen frame
running: α = min(1, ahead), clock = ahead;   paused: α = 1, clock = 0;   seek, screenshot: 0, 0
Twelve consecutive 60 Hz frames of the same burning tank. Before the fix, every frame outlined in red is a copy of the one before it: 6 of 11. With the render clock: 0 of 11.
Twelve consecutive 60 Hz frames of the same burning tank. Before the fix, every frame outlined in red is a copy of the one before it: 6 of 11. With the render clock: 0 of 11.

A short history of fake light

Effects have three ancestors. The oldest is the flipbook: an explosion is eight pictures played in order, and for most of the 1980s that was all there was. In 1983 William T. Reeves described particle systems for a film, clouds of tiny points with a birth, a velocity and a death, and games took the idea for sparks, smoke and fire. In the 1990s 3D hardware made additive blending cheap, and games discovered that a bright sprite added over a dark scene looks like light even though nothing is lit. Fake light, and it still works.

Iron Sector uses all three: flipbooks for the flash and the fireball, particles for the clods and embers, additive sprites for everything that glows. What is new is only the care: a different formula for each layer, and a clock that runs between the simulation's steps.

What it costs

Less than you would guess. A hundred simultaneous blasts of every age, with every layer, make 451 sprites, and the whole effects block takes 0.2 ms at the median and 0.3 ms at the 95th percentile, measured over 400 runs in a headless browser. That is why the game never needed a special particle container: ordinary sprites from a pool were fast enough. On the biggest battle, UK 07 with more than 600 men and vehicles, the layered effects added 0.15 to 0.2 ms to a frame, against a limit of 1 ms.

The pictures are small. The main effect sheet is 4095 × 213 pixels in the standard set, 0.77 MB on disk and 3.33 MiB in graphics memory; the layer pieces fit on a second sheet of 1023 × 200 pixels, 0.19 MB. The high-definition sheet holds four times the pixels, 13.74 MiB, or 18.3 MiB with its mipmaps. That is less than the high-definition sheet of a single T-34, 20.7 MiB.

The expensive part of effects was never the pixels. It was the care in the formulas, and that is paid once, by a person, not every frame by the machine. Paint, light and shade: three formulas, and every effect is one of them.

100 blasts: 451 sprites, 0.2 ms median, 0.3 ms p95
UK 07: +0.15 … 0.2 ms ≤ 1.0 ms
fx SD: 4095 × 213 × 4 B = 3.33 MiB
fx HD: 1019 × 3534 × 4 B = 13.74 MiB;   × 4/3 = 18.3 MiB
The same 152 mm burst at the same moment. Left: a single flipbook, the old way. Right: the game's layers, with the fireball, the clods, the dust skirt and the smoke column each on its own clock. — Layers
The same 152 mm burst at the same moment. Left: a single flipbook, the old way. Right: the game's layers, with the fireball, the clods, the dust skirt and the smoke column each on its own clock. — One sprite
One spriteLayers
Drag to compare
The same 152 mm burst at the same moment. Left: a single flipbook, the old way. Right: the game's layers, with the fireball, the clods, the dust skirt and the smoke column each on its own clock.
New lessons

Get new tutorials by email

One email when a new lesson comes out, with the video and the write-up.

We use your address only for these updates. No spam.

New tutorials on YouTube.

Devlogs, breakdowns and step-by-step lessons from the DSH Interactive team.

Subscribe on YouTube