Journal
Voxels13 min read

Water, Weather, Fire and a Fox

My first fox walked into a 60 cm test pit and couldn't climb back out - here's everything else that started moving in the world.

By James

The voxel glade around the pond, with a fox trotting along the far bank and a flock of birds in the sky above the autumn trees.

Until now, nothing in the world moved except grass and leaves in the wind. Since then: water, weather, fire, falling chunks and the first animals.

Moving things are where a voxel engine's data problem comes back, because now millions of voxels can change on their own, every frame, without anyone touching them.

Water that goes somewhere

The pond used to be a solid block of material that happened to look like water. Dig a trench from its edge and nothing happened. The pond just stood there with a trench next to it.

Now water is a simulation. The world is cut into 5 cm cells, each holding some amount of water, and thirty times a second every cell works out where its water wants to go. My first version did the obvious thing: each cell compared itself with its neighbours and passed a share of the difference across. It works, in the sense that water does eventually end up level. Eventually was the problem. Water took nearly three minutes to run six metres down a channel.

The fix is a well-known trick called virtual pipes. Imagine a little pipe between every pair of neighbouring cells. The pipe carries a flow, and the flow remembers last tick's value. The difference in water height pushes on the flow rather than setting it outright, so water picks up momentum, sloshes and runs downhill like water. It's the standard approach in erosion simulation.

// Virtual pipe update for one cell and its +X neighbour.
// h = water surface height (terrain + water depth), flow is kept between ticks.
float dh = (terrain[a] + depth[a]) - (terrain[b] + depth[b]);
flowAB = Math.Max(0, flowAB * friction + dt * gravity * dh);

// Never send more water than the cell actually holds, across all its pipes.
float totalOut = flowAB + flowAC + flowAD + flowAE;
if (totalOut * dt > depth[a])
{
    float scale = depth[a] / (totalOut * dt);
    flowAB *= scale; flowAC *= scale; flowAD *= scale; flowAE *= scale;
}

// Then every cell adds what flows in and subtracts what flows out.

With the pipes, the same six metres take about six seconds.

A straight trench cut through the flower meadow from the pond's edge, with a shallow sheet of water running down it toward the camera.
A trench cut from the pond, with water running down it.

Two details made it practical. Water mass is stored as whole numbers, not floating point, so it can never quietly appear or vanish through rounding - over thousands of ticks, a pond that slowly leaks or slowly grows is very noticeable. And still water goes to sleep. A calm pond costs nothing to simulate. A pond draining down a trench costs a few milliseconds a tick, on a background thread, so the frame never waits for it.

Then it had to look like water. Deep water absorbs more light than shallow water, and it absorbs red fastest, which is why a pond goes from clear at the edge to teal in the middle. That's a one-line formula, the Beer-Lambert law: give red a higher absorption than blue and the colour arrives by itself.

The pond seen from its sandy rim, clear and pale at the edges and deep teal in the middle, with ripple lines across the surface.
Clear at the rim, teal in the middle.

A day, and its weather

The glade now runs through a full day: sunrise, noon, a long golden evening, and night with a moon and stars. The moon is less than half a percent as bright as the sun, so the exposure adapts the way your eyes do and lifts the night by up to twelve times.

Weather moves through five moods - clear, fair, overcast, rain and storm - each lasting from half a minute to five minutes and easing into the next over 15 to 30 seconds. Clouds are a soft deck high above the world, and their shadows drift across the ground. Overcast closes the deck into a grey ceiling, widens the sun and dims it, which softens every shadow at once.

The same view in a storm, the sky a flat grey and the colours muted by haze.
The pond under a fair sky with scattered clouds and bright sunlit flowers.
FairStorm
The same view, fair and stormy.

After rain, surfaces stay wet for a while. Wet stone and turf turn darker and pick up a sheen of the sky. Anything under a tree's crown stays dry, because the engine keeps a map of what's sheltered from above. That map comes back several times below.

The Court's paving after rain in low evening sun, the wet stone darker and glossy, with the colonnade and arcade casting long shadows.
The Court's paving, still wet after rain, in the evening light.
The stairs up to the bridge at night, lit by two fire bowls and two street lamps, with the temple faint in the distance under a starry sky.
Night on the bridge, lit by the fire bowls and street lamps.

Rain

Rain needed something the engine didn't have: lots of small, fast, short-lived things. So it got a particle system - tens of thousands of particles living on the graphics card, updated each frame by a compute shader and spawned from seeded hashes, so a recorded run plays back identically every time. That matters a lot when the way I test is comparing frames byte for byte.

Rain itself is about 24,000 thin streaks falling around the camera, slanted by the wind. Where a drop meets the ground it splashes, and where it meets the pond it leaves an expanding ring on the surface.

The pond in rain, its surface dotted with small white splashes and rings, with streaks of rain falling through the frame.
Rain on the pond: streaks, splashes and rings.

The rain feeds the water simulation too. Using the same sheltered-from-above map, rain that falls into a hollow becomes real water and collects there. Dig a decent-sized hole in heavy rain and after a minute it holds about 5 cm of water. When the weather clears, puddles slowly dry.

The Court in heavy rain, the paving grey and streaked, with the arcade and colonnade fading into the misty background.
The Court in a downpour.

Fire

Fire is a second simulation on the same pattern as the water. Every voxel of something that can burn (grass, flowers, leaves, wood, planks) can hold heat and fuel. Thirty times a second, burning voxels heat their neighbours. A neighbour catches once it gets hot enough, burns through its fuel, and leaves something behind. The interesting part is how heat spreads:

// One burning voxel heating one neighbour, per tick.
float heat = burnRate;
if (neighbourIsAbove) heat *= upwardBias;          // flames climb
heat *= 1f + windBias * Dot(windDir, toNeighbour); // and lean downwind
heat *= flammability[neighbourMaterial];           // dry grass, yes; stone, no
heat *= 1f - wetness[neighbour];                   // rain and puddles fight back

neighbourHeat += heat;
if (neighbourHeat > ignitionPoint && TouchesAir(neighbour))
    Ignite(neighbour);

Each of those rules is common sense on its own. Together they give a grass fire that creeps downwind at about 16 cm a second and a tree that goes up from its lowest branches. Grass and leaves burn away completely, wood and planks turn to charcoal, and turf turns to scorched earth. Rain and water put it out.

The flames are voxels too: three tiers of animated flame plus glowing embers, and flames are real lights. A forest fire could easily add thousands of lights, far too many to trace, so the engine keeps the most important ones and leaves the rest as glow.

A small campfire burning in the meadow beside the pond at dusk, throwing orange light on the flowers, with sparks drifting up.
A campfire at dusk, lighting the flowers around it.

Embers and sparks ride the particle system up into the air. Smoke is a coarse 3D grid around the fire that drifts with the wind and slowly thins out. It's drawn as part of the haze, so the sun lights it and it catches the light in sunbeams.

A tree burning in the meadow, its lower crown alight with sparks and embers drifting up, and a dark scorched patch spreading through the flowers below it.
A tree catching from the grass below it, with the scorched patch spreading downwind.
A pale plume of smoke rising from a smouldering patch beside a tree, lit from behind by the low sun.
Smoke catching the low sun.

Better digging

The builder's tools got three upgrades. A noisy brush roughens the edge of every stroke with a seeded pattern, because a perfect sphere looks machined and a hole dug into a hillside should look dug. It stays exactly repeatable, so undo still works. A smooth brush turns each voxel solid if more than half of the small cube of voxels around it is solid, so lone spikes disappear and small pits fill in.

Three stone blocks side by side: one with a perfectly round carved hole, one with a rough noisy hole, and one with the rough hole smoothed.
Left to right: a plain carve, a noisy carve, and the noisy carve after one pass of smoothing.

The third is floating chunks. If you carve away the base of something, it should fall. After every carve the engine runs a flood fill outward from the cut, the same idea the voxel importer uses to find the inside of a model. If the fill reaches the ground, everything is still supported, and so is anything big enough to reach well beyond the cut, which is why a tree with its trunk cut through still stands. If it runs out of voxels to visit first, whatever it found is floating, so it bursts into debris and falls. One undo puts it all back. A flood fill can get expensive in a world of millions of voxels, so this one gives up after a few tens of thousands of voxels and assumes anything that big is held up by something.

Four frames: a pale block standing in the meadow, then its base carved out and the block breaking up into falling chips.
A block with its support carved out, falling over four frames.

A cliff

Until now the terrain was a gently rolling height map, one height for each point on the ground. Height maps are cheap and simple, and they can't do cliffs, overhangs or caves, because a height map has exactly one surface per column.

The usual way around that, used famously by Minecraft's newer terrain, is to think in density instead: a voxel is solid wherever a density function is positive. Start from the height map, add a 3D noise term that pushes the surface in and out, and keep that term confined to one region and capped at a known size, so everything else in the engine that relies on "the ground is about here" still works. I used it to put a crag at the edge of the clearing: a cliff about nine metres high with ledges, a bench, and a cap rock overhanging a three-metre undercut. The rest of the glade stays exactly as it was.

Rock layers come from height: five bands of stone run through the whole crag, so they show in the cliff face and carry on inside anything you dig into it. Steep faces are bare rock, and grass and flowers only grow where there's somewhere to stand.

A voxel cliff rising out of the flower meadow, banded in grey, buff, red and white layers of rock, with flowers on its ledges and trees on either side.
The crag at noon, layered rock with flowers on its ledges.
A rectangular hole dug into the pale rock of the cliff face, showing a dark grey band of rock continuing inside.
A hole dug into the cliff face, with the rock layers continuing inside.

Trees, improved

The old trees were a trunk, a few branches and some big blobs of leaves. Fine from a distance, not up close.

The new ones grow by space colonisation. Scatter a cloud of attraction points through the shape you want the crown to fill, then grow the tree towards them one small step at a time: each point pulls on its nearest branch tip, and a point is used up once a branch reaches it. Branches compete for space, the interior thins out, and the tree comes out looking much more grown than assembled. A broadleaf tree takes a few hundred segments. Branch thickness follows the pipe model - a branch's cross-section is roughly the sum of its children's - so the trunk is thick, the limbs taper and the finest twigs end as single voxels.

Leaves come as many small clusters at the tips of the twigs, only in the outer shell of the crown, with gaps between them so light gets through. Each crown mixes three shades. My first clusters were too big and came out looking like solid bubbles. On another early version, clusters wrapped around the trunk, which made the tree look like it was wearing a scarf.

There are three kinds of tree so far: broadleaf trees in autumn colours, a pale-trunked birch, and a conifer grown by a different rule, with tiers of drooping branches that shorten towards the top.

A broadleaf tree with a lime green crown standing in the meadow, its branching limbs visible through gaps in the leaves, with the crag in the distance.
A broadleaf tree grown by space colonisation, its limbs visible through the leaves.
A conifer with tiers of dark green drooping branches.
A tall birch with a pale trunk and a narrow golden crown.
BirchConifer
The birch and the conifer.

The gaps also let the sun through. A crown seen against a low sun now glows at its edges instead of reading as a solid blot.

A tree silhouetted against a hazy low sun, light glowing through the gaps in its crown and its trunk and limbs clearly visible.
Backlit, with light coming through the crown.

Twigs sway in the wind with their leaves, while the trunk and big limbs stay put. Fire treats leaves as leaves and twigs as wood, so a burning crown loses its leaves and leaves a charred skeleton.

A broadleaf tree whose crown has caught fire on one side, flames and smoke among the leaves and charred twigs showing through.
A crown on fire, the leaves burning away and the twigs charring.

Trees can also come from a 3D model. This one is a free model from Poly Haven, voxelised by the importer from the last entry into about 400,000 voxels and planted in the glade. It sways and burns like the grown trees.

A large spreading tree with a deep red crown standing alone in the meadow, with conifers and a golden birch behind it.
An imported tree model, turned into voxels and planted in the glade.

The new crowns are hollow and porous, so they hold far fewer voxels, and a small spatial grid inside each tree makes them much quicker to generate. The Court now starts in about 15 seconds instead of 24, and uses about 120 MB less on the graphics card.

One thing I didn't expect: a porous crown is full of loose leaves, and the floating-chunk check correctly decided that every leaf not touching a twig was floating. Carving through a crown dropped about thirty loose clumps of leaves in one frame and hitched for 30 milliseconds. Fragments made only of leaves and twigs, under 64 voxels, now stay where they are.

And a fox

Last, the first animals. Everything else in the world is voxels sitting on a fixed grid, but a fox that moves in 5 cm jumps looks jerky. So creatures are built differently: each body part (head, body, each segment of each leg, tail) is a small rigid block of voxels that can move and turn freely, attached to a skeleton. The ray tracing hardware already supports moving objects like this, which helps a lot. Moving things also confuse the denoiser, which normally assumes the world holds still, so it now gets told where each part was in the previous frame.

The fox is about 18 voxels long. It walks with its legs in a fixed rhythm, each foot planted on the ground while the body moves over it. Each foot is placed with two-bone inverse kinematics, which gets the knee angle straight from the leg lengths and the distance to the target. On top of that the body bobs twice per stride, and the head and tail follow on springs, so they lag slightly behind the body.

A small black-and-tan voxel fox trotting across the Court's white paving, with the colonnade and a statue behind it.
The fox crossing the Court.

The fox also gave me my favourite bug of the lot. It could step down much further than it could step up, so it happily walked into a 60 cm deep test pit and then couldn't get out. The fix was to make the step down the same size as the step up, so a fox can always retrace a step it's just taken.

Birds use boids, which is three rules per bird: don't crowd your neighbours, fly the same way they do, and stay near the middle of the group. Nobody leads, and a flock appears anyway. My birds flap and glide, flap harder when they climb, and land on the grass or the tops of trees. Fish use the same rules, kept inside the pond's water, and swim by sending a wave down their bodies from head to tail. Butterflies flit from one patch of flowers to the next.

A loose flock of small red-winged birds flying high over the glade, with a red tree and the treeline below.
A flock of birds over the glade.
White butterflies flitting low over the flower meadow near the pond.
A small school of pale fish swimming in the clear teal water of the pond.
FishButterflies
Fish schooling in the pond, and butterflies over the meadow.

Animals also notice what's going on around them. They run from you if you get too close, and from fire and digging. When it rains the foxes look for cover under a tree, the birds land and the butterflies leave. Birds roost at night.

Smoke rising from a patch of burning meadow beside the pond, while the fox runs along the bank and butterflies and birds scatter away from it.
A grass fire beside the pond, with the fox, birds and butterflies moving away from it.

There are 38 animals in the glade at the moment: two foxes, twelve birds, twelve fish and twelve butterflies. Updating all of them takes about a tenth of a millisecond.

Data, not code

All of those reactions are settings in a data file rather than code written for each animal, and that goes well beyond animals. Materials, colour palettes, props, rock layers, tree species and animals now all live in plain data files instead of being written into the engine. The engine provides the systems: water, fire, growth rules, skeletons, ways of walking and flying, things to react to. The data decides what a particular world contains. A wolf would be a fox with bigger parts and different numbers. A new tree is a new entry in a file. That's the line I want between the engine and whatever gets built on top of it.

The bill

Frame times for the standard test views with everything switched on, animals included, measured as paired runs:

ViewFrame time
Centre of the glade10.1 ms
Forest edge12.7 ms
Corner of the glade11.8 ms
Walking at golden hour13.4 ms
Polished marble floor12.1 ms
Looking at the sun in the pond12.1 ms
Dusk with a lamp13.2 ms
Through a glass wall16.3 ms
Down the Court7.4 ms

The budget is 16.6 ms, a steady 60 frames a second, and every view is under it. The glass wall still has the least room to spare.

That finishes the first big stretch of work: everything I planned for this first milestone is in. Next is tuning - mostly trees and terrain - and a first video.

Further reading: Mei, Decaudin and Hu (2007) on virtual pipes for erosion; Runions, Lane and Prusinkiewicz (2007) on space colonisation; Craig Reynolds (1987) on boids.