DSHINTERACTIVE
Tutorials

Fog, Night and Weather: Atmosphere in a 2D Game

Night, the fog of war, a morning mist and falling snow are the same battlefield with a few screen-wide layers on top, and none of them touches a sprite. This lesson builds those layers one at a time, from a grid of bytes to a multiplication, with the game's real numbers.

cells in the vision grid of Kursk, one byte each
172,032
cells in the vision grid of Kursk, one byte each
time constant of the fog's smoothing
250 ms
time constant of the fog's smoothing
the light level of a night mission
0.18
the light level of a night mission
processor time of all these layers on most frames
< 0.1 ms
processor time of all these layers on most frames

The fog is a grid of bytes

Before the fog is a picture, it is a grid. The simulation keeps a vision grid of square cells, each 4 units of the world across. Observers stamp what they see into it, and every 6 simulation steps the new grid takes over: at 30 steps a second, five times a second. On the Kursk map, 1,792 × 1,536 units, the grid is 448 × 384, a little over 172 thousand cells.

Each cell is one byte. 255 means nobody of yours can see it. Any other value is the distance at which it is seen, halved and rounded up, and the rules use that for spotting. The renderer does not care about the distance. It asks every cell exactly one question: are you 255?

That question is the whole input to the fog of war. The next three chapters turn a grid of yes and no into a soft, slow, honest shadow.

⌈1792 / 4⌉ × ⌈1536 / 4⌉ = 448 × 384 = 172032
byte = 255  |  ⌈d / 2⌉
30 / 6 = 5 Hz
The vision grid of Kursk, one pixel per cell: dark is seen up close, white is 255. On the right, a patch of real cells from frame 900 of the battle, with 255 wherever nobody of the player's can see.
The vision grid of Kursk, one pixel per cell: dark is seen up close, white is 255. On the right, a patch of real cells from frame 900 of the battle, with 255 wherever nobody of the player's can see.

One texture, one pixel per cell

The fog is one texture with one pixel per cell. Its colour is black and its alpha is the fog. On Kursk that is 448 × 384 pixels of 4 bytes, 688 kilobytes, less than a single sprite sheet.

A sprite stretches it over the whole map. A cell is 4 units, and a unit is 3.75 pixels across the screen, so each cell becomes 15 pixels wide, and half that, 7.5 pixels, tall, because this camera sees the ground as 2 : 1 diamonds.

The texture is sampled bilinear, so each cell blends into its neighbours instead of showing as a hard block. Pixel art is sampled to keep its pixels crisp: the ground of the standard set, for example, is sampled nearest. The fog and the mist are soft on purpose, because they are not pictures of things but weather, and weather is soft.

448 × 384 × 4 B = 688128 B ≈ 688 KB
4 u × 3.75 px/u = 15 px,   15 px / 2 = 7.5 px
The fog texture's alpha over 23 × 26 cells, stretched 2 : 1 as on the ground. Nearest sampling shows a hard block per cell; bilinear sampling blends each cell into its neighbours.
The fog texture's alpha over 23 × 26 cells, stretched 2 : 1 as on the ground. Nearest sampling shows a hard block per cell; bilinear sampling blends each cell into its neighbours.

The blur

Bilinear sampling alone still leaves a staircase, one step per cell, and the fog would look like a map of tiles. So before the texture is built, the mask (1 where a cell is 255, 0 elsewhere) is blurred. Each row is convolved with the weights 1, 4, 6, 4, 1, and then each column.

These are binomial weights, the fifth row of Pascal's triangle, and they are a cheap stand-in for a Gaussian. One pass sums to 16, so two passes make a 5 × 5 kernel whose weights sum to 256. The blurred value v of a cell runs from 0 to 256, and 256 is chosen so the division becomes a shift by eight bits.

The alpha of a cell is 89 times v, plus 128, shifted right by 8. The 89 is 35 % of 255, the darkness of the fog; the 128 is half of 256, so the shift rounds instead of truncating. Take a cell with v = 160: 89 × 160 = 14,240, plus 128 is 14,368, and shifted by eight that is 56. The cell is 56 out of 255 dark, part of a soft edge. The whole edge now ramps over about four cells, and the staircase is gone.

1 + 4 + 6 + 4 + 1 = 16,   16 × 16 = 256
alpha = (DIM · v + 128) >> 8,   DIM = round(0.35 · 255) = 89
v = 160:  89 · 160 = 14240;  + 128 = 14368;  >> 8 = 56
The alpha of each cell after both passes of the 1-4-6-4-1 blur. Full fog is 89 of 255; one cell with v = 160 works out to 56, a step of the soft edge.
The alpha of each cell after both passes of the 1-4-6-4-1 blur. Full fog is 89 of 255; one cell with v = 160 works out to 56, a step of the soft edge.

Smoothing in time

Space is handled, and time is next. The grid changes five times a second, in steps, and a fog that jumped five times a second would flicker at every edge. So the texture does not follow the grid. It chases it.

Each frame, every cell's alpha moves toward its target by a fraction k, where k = 1 − e^(−dt / 0.25). That is a first-order filter, an exponential smoothing, with a time constant of 250 milliseconds: after a quarter of a second the fog is 63 % of the way there, after a second 98 %. At 30 frames a second dt is 1/30 s and k is 0.125. The frame time is clamped at 0.1 s, so a long hitch does not make the fog leap. The blurred targets themselves are recomputed at most five times a second, and only when the grid has changed.

Only cells still on their way are touched. While a tank drives across Kursk that is about 1,600 of the 172,032, under one per cent. And there is a switch called snap. When you seek in a replay, load a save or take a screenshot, the fog is set to its target at once, so a frame is still an exact function of the simulation, with no history of its own.

alpha += (target − alpha) · k,   k = 1 − e^(−dt / 0.25)
dt = 1/30 s  →  k = 1 − e^(−0.133) ≈ 0.125
t = 0.25 s: 1 − e⁻¹ = 63 %,   t = 1 s: 1 − e⁻⁴ = 98 %
dt ≤ 0.1 s  →  k ≤ 0.33
The grid's target climbs in steps every 0.2 s; the alpha chases it smoothly with k = 1 − e^(−dt / 0.25) and arrives about a second later.
The grid's target climbs in steps every 0.2 s; the alpha chases it smoothly with k = 1 − e^(−dt / 0.25) and arrives about a second later.

Dirty tiles

Uploading 688 kilobytes to the graphics card every frame would be wasteful, and on the biggest map, Goodwood, 1,200 × 850 cells, it would be 4.08 megabytes. So the texture is divided into tiles of 32 × 32 cells, 4 kilobytes each, and every frame only the tiles that changed, and only those near the view, are uploaded. A run of neighbouring dirty tiles goes up as one rectangle.

The upload uses texSubImage2D, and one small trick. By telling the driver the row length of the whole texture (UNPACK_ROW_LENGTH = 448 on Kursk) and where the rectangle starts, the renderer can hand it a rectangle out of the middle of the buffer without first copying it into a separate array. That needs WebGL 2. On an older context the whole texture goes up when anything changes, which still works, just slower.

Measured while a tank drives across Kursk: about 13 tiles a frame, some 53 kilobytes instead of 688.

(448 / 32) × (384 / 32) = 14 × 12 = 168
32 × 32 × 4 B = 4096 B
13 × 4096 B = 53248 B ≈ 53 KB   (688 KB)
1200 × 850 × 4 B = 4.08 MB
One frame of a tank driving across Kursk. The gold rectangles are the tiles of 32 × 32 cells sent to the graphics card in this frame: 13 tiles, 53 KB instead of 688.
One frame of a tank driving across Kursk. The gold rectangles are the tiles of 32 × 32 cells sent to the graphics card in this frame: 13 tiles, 53 KB instead of 688.

What the fog hides, and what it does not

Many strategy games have three states of fog: never seen, seen before, and seen now. This one draws two. Where you see, the overlay is clear. Everywhere else it is 35 % black.

The third state exists, but it is not a tint. It is what is allowed to be drawn under the overlay. The ground is always drawn, with every crater and track on it. Baked bodies and wrecks are always drawn, because they are part of the ground now. Enemy units are drawn only while you see them, and an enemy wreck once you have seen it. Houses and trees are shown even in the dark, unless a mission asks to hide the unexplored, and none does today.

The overlay sits above the mist and below the light grade, so at night the fog is tinted blue along with everything else. The computer opponent reads the same grid, and nothing else.

The same frame with the fog overlay off and on. Under the fog the craters and a wreck seen earlier are still drawn, while a Panzer IV nobody sees is not drawn at all.
The same frame with the fog overlay off and on. Under the fog the craters and a wreck seen earlier are still drawn, while a Panzer IV nobody sees is not drawn at all.

Ghosts

When an enemy slips out of sight, it leaves a ghost. The ghost is the unit's own resting picture, its hull and its turret, drawn at the last position and heading you saw, at 35 % opacity and tinted grey (#9a9a9a). It cannot be selected and does not move.

Guns, machine guns, mortars, tanks, assault guns, carriers, trucks and cars leave ghosts. A pillbox does not, because it is a building and you remember buildings anyway. A ghost disappears when you see its place again and the unit is not there. A ghost of a vehicle that nobody confirms again is also dropped after 120 seconds, since the tank has surely moved; a ghost of a gun stays, since guns do not. The minimap shows ghosts as hollow marks, and the computer opponent keeps a memory of ghosts of its own.

On a map with hills the fog is not a flat sprite but a mesh lifted over the ground, of up to 65,536 vertices, so the shadow follows the slopes. Lesson 5 is about those hills.

Left, the fog off: the Panzer has driven on. Right, what the player sees: its ghost, the resting frame at the last place and heading, at alpha 0.35 and tinted #9a9a9a.
Left, the fog off: the Panzer has driven on. Right, what the player sees: its ghost, the resting frame at the last place and heading, at alpha 0.35 and tinted #9a9a9a.

Time of day

Night and dawn start with one number, the light level, between 0 and 1. A mission declares its time of day, and the time of day is a curve over the battle clock. Day is a flat 1. Dawn starts at 0.35, reaches 0.6 after ten minutes and full daylight after 25. Night is a flat 0.18.

Beside the light curve runs a second one on the same clock, for the rules: how far a unit can see. At dawn it is 55 % of the daytime range, climbing to 70 % and then to full range with the light; at night it is 35 %. A unit that has just fired is seen from much further at night: from 80 % of the distance at which a shot gives itself away by day.

The light curve goes only to the renderer, which uses it to colour the screen. Two curves of one clock, so the picture darkens exactly as far as your units' eyes do.

DAY:    light 1
DAWN:   light 0.35 (0 s) → 0.6 (600 s) → 1 (1500 s);   vision 0.55 → 0.7 → 1
NIGHT:  light 0.18;   vision 0.35;   flash 0.8
The light curves of day, dawn and night, with the dashed vision curves for the rules. Below, one frame of the Moscow mission graded at light 0.35, 0.6 and 1.
The light curves of day, dawn and night, with the dashed vision curves for the rules. Below, one frame of the Moscow mission graded at light 0.35, 0.6 and 1.

The multiply grade

The grade is the third blend mode of the previous lesson, multiply, and it is almost embarrassingly simple. One sprite covers the whole screen. It is white, it is tinted, and it is multiplied over everything below it. Multiplying by white changes nothing; multiplying by a colour darkens each channel by that colour's share of 255.

At dawn the tint is a cold blue grey, #4a5888, lightened toward white by the light level k. At k = 0.35 that gives #8992b2, a blue a little darker than half. A thin veil, a second sprite of #5a6478 at 20 % × (1 − k), 13 % here, adds a slight haze. At night the tint is a deeper blue, #5668a6, with its own veil of #24304e at 8 %. Between a level of 0.18 and 0.35 the two palettes blend, and overcast weather multiplies in a grey of its own, #dfe2e6.

Take a pixel of grass, (118, 128, 70). Multiply by the night tint channel by channel and divide by 255. Red: 118 × 86 / 255 = 40. Green: 52. Blue: 46. Dark, cold, and the green has gone. That is all night is: a multiplication.

out = pixel · tint / 255
DAWN:  tint = c + (255 − c) · k,   c = #4a5888 = (74, 88, 136)
k = 0.35:  tint = (137, 146, 178) = #8992b2,   veil = 0.2 · (1 − 0.35) = 0.13
NIGHT:  tint = #5668a6 = (86, 104, 166),   veil = #24304e · 0.08
(118, 128, 70) · (86, 104, 166) / 255 = (40, 52, 46)
The night mission at Pegasus with the grade off and on: one white sprite tinted #5668a6, multiplied over the whole frame, with a veil of #24304e at 0.08. — Night grade
The night mission at Pegasus with the grade off and on: one white sprite tinted #5668a6, multiplied over the whole frame, with a veil of #24304e at 0.08. — Grade off
Grade offNight grade
Drag to compare
The night mission at Pegasus with the grade off and on: one white sprite tinted #5668a6, multiplied over the whole frame, with a veil of #24304e at 0.08.

Light on the ground

A multiplied night is dark, and that is what makes the second half possible: light. Every flash and every fire draws an additive spot on the ground. The spot is one small texture, 64 × 64 pixels, a radial gradient from full at the centre to 45 % at a third of the radius to nothing at the edge. It is drawn as an ellipse, twice the radius wide and one and a half times tall, because the ground is foreshortened.

Its strength follows the light level. The night part grows from nothing at a level of 0.35 to 85 % at full night, 0.18; by day a small floor remains, 0.22 for flashes and 0.3 for fires, so a fire still warms the grass a little. A shot lights a radius of 10 units for 0.12 s. A blast lights 22 units for 0.6 s, with a hot white core of 7 for the first tenth. A burning tank lights 26 units, with a second spot twice as wide at a quarter strength, and it flickers on two sine waves multiplied together, so it never repeats. A burning house lights 34, and up to 12 of its windows glow on their own sine.

The colours are warm: a yellow for flashes (#ffd890), an orange for fire (#ff8a30). Tracers draw thin additive streaks along their path. The spots are drawn above the grade, over everything inside them, so a tank beside a fire is brightened too, but flatly, like a coloured wash. To light a tank by its shape, the sprites need to know their shape, and that is the last lesson of the course, on normal maps.

night = clamp((0.35 − light) / (0.35 − 0.18)) · 0.85
alpha = a · max(night, day),   day = 0.22 (flash) | 0.3 (fire)
spot: 2r × 1.5r,   1 → 0.45 (0.35 r) → 0
flicker = 0.8 + 0.2 · sin(11 t) · sin(4.3 t)
windows = 0.55 + 0.3 · sin(7 t)
A village at night with the glows hidden and shown: a burning house, a burning tank and a shell's blast, each an additive spot of the same 64-pixel texture on the graded ground. — Glows on
A village at night with the glows hidden and shown: a burning house, a burning tank and a shell's blast, each an additive spot of the same 64-pixel texture on the graded ground. — Glows hidden
Glows hiddenGlows on
Drag to compare
A village at night with the glows hidden and shown: a burning house, a burning tank and a shell's blast, each an additive spot of the same 64-pixel texture on the graded ground.
LightRadius, unitsLasts
Shot10 · sc0.12 s
Blast22 · sc, + a core of 7 · sc0.6 s, the core 0.1 s
Impact6 · sc0.25 s
Flame pool181.5 s
Burning tank26, + a wide spot × 2 at 0.25while it burns
Burning house34while it burns
The ground lights. sc is the scale of the effect that made the light.

Mist

Three missions begin in mist: the Dnieper crossing, the Seelow Heights and Arracourt. The mist is the softest thing the renderer draws: one texture of pale grey, (214, 220, 218), with a texel every 8 units or more and at most 60 thousand texels for a whole map. On the Dnieper that is 292 × 206 texels, one every 11.7 units.

A texel's alpha is a base haze of 45 % plus 55 % of a smoothed noise, so the valley has denser patches and clearer ones, and the whole layer is at most half opaque. The noise is value noise: a random value at each point of a lattice, blended smoothly between them. Three octaves at falling scales, 320 × 200 units, 120 × 90 and 45 × 40, are summed with weights 0.6, 0.3 and 0.1. The cells are wider than they are tall, so the mist is stretched east to west into drifting sheets, and it is drawn on the ground, squashed on screen like the ground. On a map with hills it is thinner on the heights.

The texture is built once. Each frame only its overall density changes: the scenario holds the mist for six, eight or twelve minutes, then thins it to nothing over four or five. And it is honest, like the fog: the same density that whitens the screen shortens how far every unit can see, down to half at full density.

texel = max(8, √(W · H / 60000)) u   →   292 × 206 · 11.7 u
n = 0.6 · o₁ + 0.3 · o₂ + 0.1 · o₃   (320 × 200, 120 × 90, 45 × 40 u)
alpha = 0.45 + 0.55 · smoothstep(0.3, 0.8, n)
layer = 0.5 · density,   vision × (1 − 0.5 · density)
The mist's noise n over the Dnieper map and its three octaves, 320 × 200, 120 × 90 and 45 × 40 units, weighted 0.6, 0.3 and 0.1.
The mist's noise n over the Dnieper map and its three octaves, 320 × 200, 120 × 90 and 45 × 40 units, weighted 0.6, 0.3 and 0.1.

Snow and seasons

Snow is the only falling weather in the game. There is no rain. On a winter map the snow's density is one minus the mission's visibility, and from a density of 0.4 up 600 flakes fall across the screen. About a third of them, 35 %, are near: 2 × 2 pixels, falling at 46 to 64 pixels a second. The rest are single pixels, slower and fainter, at 22 to 40.

The flakes have no state. Each one's position is a hash of its index and the current time, so any frame of snow can be drawn on its own, which is what a deterministic replay needs. They are drawn in screen space above the world, so the grade does not darken them. The Moscow mission has showers: for 75 seconds, about every ten minutes, the visibility drops from 0.9 to 0.7, and the snow thickens from 150 flakes to 450.

Seasons themselves are simpler than they look. A season swaps the sprite sheets, so a tank on a winter map uses its winter sheet, and the ground was baked white or sand coloured before the mission ever loaded.

density = 1 − visibility,   flakes = 600 · min(1, density / 0.4)
visibility 0.9 → 150,   0.7 → 450
near 35 %: 2 × 2 px, 46 + 18 h px/s;   far: 1 × 1 px, 22 + 18 h px/s
x, y = hash(index) + speed · t

A short history of the shroud

The fog of war is an idea from Clausewitz in the 1830s, and a game feature since the 1970s. In 1977 Empire left the unexplored parts of its map blank. In 1992 Dune II drew a black shroud that peeled back where your units went and never closed again. By 1998 StarCraft had two layers: the black shroud of the unexplored and, over explored ground, a lighter fog where enemies could hide. That three-state fog became the standard.

This game draws two states and lets the ground carry the third, because on a battlefield that remembers, the explored ground has craters in it, and a grey tint would hide them. Night came later than fog. Early games darkened their palette. Then a full-screen multiply became cheap, and the day could turn into night without touching a single sprite, which is exactly how it is done here.

What it costs

These are the cheapest layers in the game, and that is the point of their design. The fog is one texture, 688 kilobytes on Kursk and 4.08 megabytes on the biggest map, resampled at most five times a second and uploaded in tiles. The grade is two full-screen quads with a multiply. Every light is one sprite of the same 64-pixel texture. The mist is one sprite. The snow is 600 rectangles.

Measured with the renderer's own section timer, all of them together cost the processor less than a tenth of a millisecond on most frames. The frames that re-sample the fog, five times a second, stay under a millisecond on Kursk (0.7 ms at the 95th percentile) and reach 2 ms on the Dnieper. The expensive atmosphere, light that falls on the tanks and knows their shape, costs more and needs more data, and it has a lesson of its own.

A grid of bytes, a blur, a multiply and a handful of glowing ellipses: that is fog, night and weather.

MissionMedian frame, msRe-sampling frame (p95), ms
Kursk0.00.7
Moscow (snow)0.00.7
Pegasus (night)0.01.1
Dnieper (mist)0.02.0
Processor time of the fog, mist and snow layers per frame, measured in the browser over 240 frames with a timer of 0.1 ms resolution. The grade and the glows took 0.1 ms at most.
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