A way that joins twice a sway
The eye looks down on the stone from far off with no perspective: things do not get smaller with distance. In such a picture a step towards the eye or away from it does not show at all. So two pieces of stone that are metres apart in depth can sit edge to edge on the screen, and a man can be drawn stepping from one to the other.
Here the eye turns 15 degrees to each side and back. A piece standing five or nine metres behind another slides sideways against it as the eye turns, and the two meet at one end of the sway only. The pieces are placed so that every gap ahead of him is shut at an end of the sway, the far ones at one end and the near ones at the other, in turn. His walk is timed to arrive at each gap as it shuts, and the sway is held still for the moment he steps.
It cannot be a closed ring of stone. Seen without perspective a closed circuit always closes on the screen, so the gaps along it add up to nothing, and a walker who only ever goes right cannot have every gap open ahead of him. So the stone is a frieze: the same 32 metres over and over along the picture, streaming to the left as he walks to the right. One lap of his walk is one period of it, and the lap’s last frame is its first.
What is drawn how
Nothing here is path traced. The stone, the water, the man, his pursuer and the birds are ordinary triangles with one shadow map. The clouds are a second picture, made by sending a ray through the air for every pixel and adding up what it meets, then laid over the first where the stone is further away than the cloud.
The clouds come from six files of real cloud shapes, each a block of densities a few centimetres fine. Twenty are placed about the stone, and each is there twice over, half a lap apart: it forms upwind of its place, is whole as it passes it, and thins away downwind. Their light is not painted in. Every frame, on a grid of half-metre cells, the sun’s way down through the clouds is followed, and then the light is passed from cell to cell in sixteen directions, so that a cloud is bright on the side the sun reaches, glows a little through its thin parts, and takes colour from the stone beside it. The stone is in that grid too: it shades the clouds and throws light back into them.
At the start of the work on speed, a frame of 2000 by 2000 pixels took 15.4 milliseconds, a little inside the 16.7 that sixty frames a second allow. This is where it went:
| Part of a frame | ms |
|---|---|
| The march through the clouds | about 9 |
| of which reading the clouds at their own fineness | about 6 |
| of which the short walk towards the sun at each sample | 1.3 |
| Working out the light in the clouds | 2.7 |
| Smoothing the stone’s edges (four samples a pixel) | 1.1 |
| The shadow map | 0 to 2 |
| The man, his pursuer, the birds, the flags | about 0 |
The stone is nearly free. More architecture would cost nothing that could be measured; the frame is the clouds.
One ray through the clouds
A ray starts at the eye’s side of the block of air and goes on until it meets stone or has gone dark. Three things keep it from doing more than it must.
- Strides through empty air. A coarse grid holds, for every half-metre cell, how many cells away the nearest cloud is. Where that is far, the ray crosses that many cells in one stride. The grid is made again as the clouds drift.
- A list in every cell. Each cell also holds which clouds reach it, up to eight. A sample asks only those, each in the cloud’s own block, at its own fineness, and joins what they answer.
- A cheap look first. Before asking the fine blocks, the sample reads a coarse, blurred copy of all the clouds together. Only where that says there is cloud near does it ask.
At each sample that finds cloud, the ray takes three steps towards the sun through the cloud as drawn, so that the folds of a cloud shade each other at a finer grain than the half-metre light has. Then it reads the worked-out light and adds it to the pixel, dimmed by what the ray has already passed.
The clouds’ picture is smaller than the screen’s: at most 1.6 million rays, however big the window. Each is put back among the full picture’s pixels by its neighbours at the same depth, and a pixel with no such neighbour, at the edge of a tower, marches a ray of its own.
Measuring before cutting
Two things had to be found before any of it could be made faster: a way to tell how long a frame takes, and a way to tell whether the picture had changed.
Time
Frames a second say little. The browser draws in step with the screen, so a frame of 9 milliseconds and one of 15 both show as sixty. The graphics card’s own timers, as the browser reports them on this machine, read 30 milliseconds for a frame that took 8.5. What worked is to hand the card a burst of frames at once and wait for it to say it has finished them all. Even so, the same page reads a millisecond apart from one run to the next, since the card is shared with everything else on the machine. Anything worth less than half a millisecond can only be seen by switching it on and off inside one page, many times over, and taking the middle of the differences.
The picture
Every change was held against a reference: the same lap, marched at half the step. The two are compared in motion, after 120 frames of the walk, because a fault that hides in a still picture often shows as a shimmer in a moving one. The measure is the usual one in decibels, where more is closer; two runs of the same settings come out between 62 and 71, since the clouds’ grain differs from frame to frame, and the page before any of this stood at 51.
Counting
And before guessing where a ray’s time goes, it was counted. The clouds’ picture can be made to hold, in place of colour, how many times each ray did each thing. On average a ray takes 7 or 8 strides through empty air, 23 to 30 samples beside or inside a cloud, and asks the fine blocks 8 to 12 times, of which 6 to 8 find cloud. That count settled one plan at once: a tighter map of where the clouds are, baked ahead of time, could save only the two to four wasted asks. It was never built.
What worked
Asking the fine blocks less
The cheap look first was too eager: it sent a sample to the fine blocks wherever the coarse copy was above a very low mark, and most of those asks fell in the soft margin about a cloud and came back empty. Raising the mark two and a half times left the picture where it was (77 to 84 decibels against the old one, no pixel off by more than a sixtieth of the range). A sample now also leaves at once where the cell’s list is empty, and the walk towards the sun is taken again only where it can change the pixel by more than a fiftieth. Together: 15.4 milliseconds to 13.5.
A fault that looked like grain
The plainest way to go faster is a longer step, and doubling it saved almost five milliseconds. It also changed the picture a great deal, and at first that looked like the price of coarse sampling: grainy rims. It was not. Each sample’s share of the pixel was being worked out over the step that came after it, and how long that step is depends on what the sample finds. Every thin rim was drawn fatter by about one step, so a longer step drew fatter clouds. Counting each sample over the stride that led to it took most of the difference away, and made every later saving possible.
Gathering the clouds over frames
What grain is left comes from every ray starting at a slightly different depth, so that the steps do not show as rings. If that start is moved each frame, and the frames are added up, the grain averages out and the step can be longer still. So the clouds’ picture is now mixed, a fifth new to four fifths old, with the one before it.
- The old picture is read where that bit of cloud was a frame ago: the eye’s turn and the wind’s drift are undone first.
- It is thrown away where the stone’s depth there does not match (something has come in front), and held within what the new frame’s neighbours show, so that nothing leaves a trail.
- How see-through a pixel is gets mixed as a thickness, not as a share of light let through. Mixing the share itself makes every rim thinner than it is.
- The starts come from blue noise, a pattern with no clumps in it, made as the page opens and shifted every frame by the golden ratio.
One trap: the renderer does not draw a picture that nothing reads. The small picture of depths that the old clouds are checked against was only ever copied from, so it stayed empty, every old pixel was thrown away, and the numbers still looked believable. It showed when the picture was no smoother than without.
Half the rays a frame
With the frames being added up anyway, a ray need not be sent every frame. Half are sent, in a checkerboard that swaps each frame; the other half keep what they had. A ray whose old value was thrown away, with nothing of its own this frame, takes the mean of the neighbours that were sent, at its own depth.
The packing matters. The first try kept the full-size picture and told every other pixel to stop at once; that saved half a millisecond. Most likely the card runs neighbouring pixels together, and those that stop at once still wait for the ones beside them. Packed side by side, every pixel has work, and the saving is 1.3 milliseconds.
| The page, 2000 × 2000, dusk, in motion | Against the reference | Frame |
|---|---|---|
| Before gathering: step 8 cm, every ray | 51.4 dB | 12.6 ms |
| Gathered over frames, blue noise, step 12 cm | 51.1 dB | 11.6 ms |
| And half the rays a frame (as the page is) | 50.9 dB | 10.5 ms |
| The same at a step of 16 cm (in the panel, for a slow card) | 47.8 dB | 7.6 ms |
Each table here was timed in one sitting, its rows against each other. Between sittings the same page reads a millisecond or two apart, so compare within a table. Timed again as this was written, all in one page: 16.6 ms with the eager look, 14.7 before gathering, 9.7 as the page is.
What did not
As much was thrown away as kept. Each of these was built, measured and taken out again, or measured and not built.
| Tried | What happened |
|---|---|
| A longer step as the ray gets darker | 49 dB. |
| The fine blocks only while the ray is still mostly clear | 53 dB. |
| The mark for the cheap look raised further | 60 dB, just under what two runs of the same settings give. |
| A double step only inside thick cloud | Same picture, no time saved. The time is in thin cloud and the air beside it. |
| The light worked out in turns, one direction a frame | Saves 0.45 ms; 46 dB and one pixel in a hundred visibly off. Every fourth frame: 42 dB. |
| Skipping every other pixel inside a full-size picture | 0.5 ms where packing them gives 1.3. |
| The shadow map drawn once and slid along with the stone | 0 to 1.2 ms, 0.8 in the middle. Under the millisecond set as worth the trouble. |
| A baked, tighter map of where the clouds are | Not built: the count showed at most a millisecond in it. |
The light is the one worth a word. It looked like the easy saving, since light changes slowly. But the stone slides through the grid all the time, and the light in each cell is always a little behind where the stone now is. Doing less of it each frame does not just delay the answer; it leaves it further behind, and that shows.
Holding sixty
On a slower card all of this is still too much, and on a faster screen the page would draw 120 frames a second to no purpose. So the page watches itself.
It cannot ask how much time is left over. The card’s timers are wrong, and the time from handing over a frame to hearing it is done never falls below about ten milliseconds whatever the work. What can be seen is whether the card keeps up: when it does, its “done” calls come one a screen beat; when it does not, they come as far apart as a frame’s work takes. If the middle gap over half a second is an eighth over the beat, the page does less: first fewer rays for the clouds, down to a third, then fewer pixels for everything. If it has been in time for two seconds it tries a fifth more, watches that for two seconds, and goes straight back if it fails.
The first version went up and down for ever at a load just on the edge. Now a level that failed is remembered and not tried again for ten seconds, then twenty, up to two minutes; a change of window size forgets it.
On a screen faster than sixty the page draws every second beat (or third, or fourth), found from how close together the browser’s own frame times come. That part has only been tried on a sixty-beat screen.
The wait at the start
The page used to sit under its cover for 18 seconds. Three things are worked out before the first picture, off the main thread: where clouds may not be (his way, and everything between it and the eye, at each end of the sway), the stone’s shadow in the air, and a map of how the wind goes round the stone, so that clouds part at a tower instead of passing through it. Then the light is given time to settle.
| At the start | Was | Is | How |
|---|---|---|---|
| Where clouds may not be | 8.0 s | 0.6 s | Every cell asked every stretch of his way: 348,000 cells, 92 stretches, 5 turns of the eye. Now the stretches are sorted into squares of the flat picture, and a cell asks its own square. |
| The clouds laid out in one block, and a first go at their light | about 1.5 s | 0 | Left from when the clouds stood still. The moving page lays them out on the card every frame and never read it. |
| The wind round the stone | 4.6 s | 2.8 s | Its smoothing looked at every cell 240 times; now only at cells next to one that changed. And it runs beside the rest, on a thread of its own. |
| Letting the light settle | 2.0 s | 0.8 s | Measured: the picture is within its own grain 40 frames after the wind map lands. It used to wait 120. |
| Until the picture is settled | 18.3 s | 4.3 s | The cover starts to go half a second before that, since its going takes that long. |
None of it changed what is worked out: the three volumes come out the same to the last byte as before. The lesson was the old one. The log said “clouds: 9927 ms”, the clouds were the obvious thing to cut, and cutting them saved one second of ten. Timing the parts found the other eight in a loop nobody would have suspected.
The cover itself is the page’s idea in small. The word is cut into bands that hang at different depths on a stage that sways; seen from one end of the sway they make the word, and from anywhere else they are scattered. It is one style-sheet animation, so it keeps moving while the page is busy building its shaders.
Made with
- Drawn in the browser with three.js on WebGPU, in its shading language TSL, through ssam. The light in the clouds and the laying out of the clouds are compute shaders.
- The cloud shapes are from JangaFX’s free CloudPack (CC0), six of them, packed to 4.6 MB.
- The man is the author’s own figure; his walk is a Mixamo clip, refitted to stairs foot by foot.
- Two lights, at the top of the page: dusk, a low sun behind the stone with lamps lit in the doors, and day, a high sun in front and everything blue. Only colours and the sun’s place differ; the clouds’ light follows by itself.
Add ?light=day to the address to open by day, ?govern=0 to let it draw all of the picture whatever the pace, and ?gui for a frame meter and the panel: it has the march’s settings, each with a switch to turn it off.