PulsarPulsar
← Back to blog

rust

The Sky Is a System, Not a Texture: Atmosphere, Clouds and Night in Helio

How Helio draws a sky from physics: the atmosphere's lookup tables, aerial perspective, volumetric clouds with a planet-sized weather map, the layered night sky with stars, shooting stars and a panorama, how a light's pitch becomes a time of day, how the sea reads all of it, and the GPU planning that took the pass from 1.8 ms to 0.23.

··19 min read

It is easy to treat a sky as wallpaper: pick a nice picture, wrap it around the level and move on. That works until the sun has to move, a mountain has to fade into the distance or the sea has to reflect the clouds overhead. In Helio, the sky is something the engine computes, and the same air that colours the horizon also lights the world underneath it. This post is about how that works, from a clear noon to a night full of stars.

We stand on other people's work to do it. Our air model is Sébastien Hillaire's, from "A Scalable and Production Ready Sky and Atmosphere Rendering Technique" (2020). The density of our clouds follows Andrew Schneider's Nubis talks, and their lighting uses Wrenninge's multiple scattering octaves. Our thanks to them for sharing it. The galaxy and nebula in our night shots come from the Deep-Space Skybox by Toolbench, released under CC0 1.0, and we are grateful for that too.

Introduction: A Sky With Nothing In It

The frame above has no mesh in it. There is a sea, there is a horizon, and above that every pixel is computed. The sun is a directional light with a pitch of -14 degrees and a colour of 1.0, 0.62, 0.32. No panorama, no painted gradient. A set of tables the size of a few thumbnails turns that light into the colour of the air, the clouds are marched from noise, and the sea underneath reflects what the sky pass wrote.

The scene is the voxel project's ocean level, with one water volume and one sky object. Sun, clouds and stars are the same component all the way through. The four frames below share a camera. The first three differ only in the sun light's pitch and colour, and the fourth turns the light off and the panorama on.

This post goes through the machinery in the order the data flows. The air comes first: a handful of lookup tables that answer how much light survives a path through the atmosphere and how much is scattered toward the eye. Then the composite that paints those tables behind and in front of geometry, the clouds with their weather, the night layers, and the way a light becomes a time of day. The back half covers the sea that reads the sky, and a profile that found 1.6 ms of waste.

1. What the Sky Owes Everyone Else

In an engine where a sky is a cubemap, the sky answers one question: what colour is the background. Here a dozen readers ask it a dozen different things. Deferred lighting needs the sun's colour after it crossed the air to reach a pixel, and the ambient light a surface gets from the sky above it. Global illumination probes whose rays hit nothing take the sky's radiance in that direction. Volumetric fog wants the medium itself, the scattering and extinction coefficients. Terrain and water read the medium too.

So the sky plugin publishes a frame structure and a few tables, and every one of those readers takes what it needs without knowing a sky exists. The names, from helio-frame-api:

  • sky_frame, a buffer of 1872 bytes: the medium (Rayleigh, Mie, ozone), the planet, the suns, the eye's irradiance.
  • sky_view, a 256 by 108 texture of the sky's radiance seen from the eye.
  • sky_table, the sky's irradiance anywhere in the air, as low-order spherical harmonics. 16 altitudes by 40 sun cosines.
  • sky_radiance, a texture array, 32 by 64 texels per entry and 640 entries, for rays that start anywhere in the air.

The sky's own business stays in a second block that only its passes read: the clouds, the weather, the stars, the panorama. The split matters for the rest of the post. What other kinds see is the air and the light. What only the sky sees is how it dresses itself.

The authored side is one component, AtmosphereComponent, with 57 properties in the editor. It compiles to a 304-byte row, the first enabled row in the level is the one in use, and the scene's first directional light is the sun. There is no clock anywhere in this path. Hold on to that, because it is the answer to the question about time of day.

2. The Air

Light crossing the atmosphere loses energy to scattering and absorption. If the extinction coefficient at a point is σt\sigma_t, the fraction that survives a path from aa to bb is

T(a,b)=exp⁡ ⁣(−∫abσt(s) ds)T(a,b)=\exp\!\left(-\int_a^b \sigma_t(s)\,ds\right)

and the coefficient is a sum over three species, each with its own density profile with height hh in kilometres. Helio uses Hillaire's Earth values:

σt(h)=βR e−h/8+(βM+κM) e−h/1.2+κO max⁡ ⁣(0,  1−∣h−25∣15)\sigma_t(h)=\beta_R\,e^{-h/8}+(\beta_M+\kappa_M)\,e^{-h/1.2}+\kappa_O\,\max\!\left(0,\;1-\frac{|h-25|}{15}\right)

Rayleigh scattering βR\beta_R is (0.005802, 0.013558, 0.0331) per kilometre for red, green and blue. Blue scatters 5.7 times as strongly as red, which is the whole reason the sky is blue and the sun goes orange when its light crosses far more air near the horizon than at noon. Mie scattering from aerosols is grey, 0.003996, with an absorption of 0.0044 and a scale height of only 1.2 km. Ozone is a tent around 25 km, 30 km wide, and it only absorbs. Its coefficients (0.00065, 0.001881, 0.000085) eat mostly green, some red and almost no blue, and the code comment credits it with the blue at the zenith at dusk.

The phase functions are the textbook pair. Rayleigh is 316π(1+cos⁡2θ)\frac{3}{16\pi}(1+\cos^2\theta). Mie uses Cornette-Shanks with g=0.8g=0.8, a strong forward lobe that puts the haze glow around the sun.

Evaluating TT with a ray march for every pixel would be absurd, so the first table stores it. The transmittance table is 256 by 64 texels, indexed by height and the cosine of the angle to the zenith. Everything else integrates through it.

Light does not stop after one scattering event, and a sky without the later events is too dark and too blue. Hillaire's answer is a second table, 32 by 32, indexed by sun angle and height, holding the transfer function of all the isotropic bounces after the first: the light that reaches a point after any number of them, for a unit sun. The shader calls it psi_ms.

The right frame is the left one with clouds_enabled off. That gradient is the sky-view table drawn through the composite and nothing else. The table is 256 by 108 and covers the full circle of azimuths about the sun, with its rows concentrated at the horizon where the colour changes fastest. It is recomputed every frame, since it depends on where the camera is and where the sun is.

2.1 The planet

The air sits on a planet. The default is Earth: a radius of 6360 km, an atmosphere top at 6460 km, a ground albedo of 0.3, and a sun with an angular radius of 0.004675 radians, which is 0.268 degrees. All of it is a field in the component. The placement says whether the ground is the world origin (a flat level) or the planet's centre sits somewhere else, and a project can put the whole sky around a voxel planet instead. The scene in these pictures uses the flat placement with Earth's numbers.

3. Aerial Perspective and the Composite

A distant mountain turns blue because the air between you and it scatters light into your eye and removes some of the mountain's own. This is aerial perspective, and Hillaire stores it as a froxel volume: 32 by 32 by 32, with the first two axes spanning the screen and the third spanning distance. Each column integrates the in-scattered radiance from every directional light and the mean transmittance to each slice, writing one RGBA texel per slice. The slice distance is quadratic, d=k2⋅ranged=k^2\cdot\text{range} in normalised slice units, so the first few slices are close together and the far ones stretch. Four integration steps per slice per light is all it takes.

The composite pass runs after opaque lighting and picks one of two things per pixel. Where there is no geometry it writes the sky: the sky-view table, the sun's disk, and the night layers when it is dark. Where there is geometry it leaves the lit colour in place, attenuates it by the air in front with the volume's transmittance (the alpha of the blend) and adds the scattered light. The blend is dst * alpha + src, one draw for both cases.

The clouds composite after that and lie in front of whatever is behind them, sky or geometry farther than their front, with the air already applied. From orbit the table approach falls apart, since the sky-view table would spend its resolution on empty space and the froxel volume would not reach the planet. The composite marches each pixel's own ray through the shell instead. A level on a planet needs it. The ocean scene does not.

4. Clouds

The clouds are a layer between two altitudes. In this scene it runs from 1.5 km to 4 km, with a coverage of 0.45 and a form value of 0.85, where 0 is flat stratus and 1 is towering cumulus. A cirrus setting of 0.5 at 9 km sits above it. Density comes from noise in four textures across five scales, and the weather decides which of them matters where.

TextureSizeRole
Planet weathercube, 6 faces of 512 by 512Circulation by latitude, cyclones, fronts, highs, regimes from stratocumulus decks to cumulus fields, cirrus
Local weather64 cubedThe groups the cumulus gather in
Shape noise128 cubedThe billows
Detail noise32 cubedWisps that erode the edges

The planet map is why the cover reads as weather from orbit and nothing repeats. The scales nest: the planet's circulation and synoptic systems (1500 km in this scene), mesoscale cells at 80 km, groups of cumulus at 24 km, billows at 6 km, wisps at 0.8 km, with the wind carrying all of it. Every scale's noise keeps the spread its mips would hide, so the share of the sky covered stays put at any distance. A cloud at the horizon is as present as one overhead.

4.1 Lighting a cloud

A cloud is a medium with a very high albedo, and a ray marched through it needs two things the air does not: a shadow toward the sun, and light that was scattered more than once. For each primary step the shader marches toward the sun in six steps, two of them in a coarse set, and gets an optical depth τ\tau. The scattering uses four octaves in Wrenninge's scheme, each octave weaker, less extinguished and less forward than the last:

L=∑k=03ak pk(cos⁡θ) e−τbk,ak=0.6k,bk=0.3k,ck=0.5kL=\sum_{k=0}^{3} a_k\,p_k(\cos\theta)\,e^{-\tau b_k},\qquad a_k=0.6^k,\quad b_k=0.3^k,\quad c_k=0.5^k

with pk=mix(HG(cos⁡θ, 0.8 ck), HG(cos⁡θ, −0.3 ck), 0.3)p_k=\mathrm{mix}\big(\mathrm{HG}(\cos\theta,\,0.8\,c_k),\,\mathrm{HG}(\cos\theta,\,-0.3\,c_k),\,0.3\big). A strong forward lobe gives the silver lining. A weak backward one darkens the far side of a cloud against the sun. The comment above the function records what the octaves buy: thick clouds keep most of a diffuse reflector's light, so their tops come out brighter than the ground seen from above, at the price of thin clouds scattering about twice their single scattering. The sun reaches a cloud through the atmosphere's transmittance table and the sky's light comes in from the sky table, so a cloud at dusk takes its colour from the light that reaches it and no code says what colour a sunset cloud is.

4.2 Marching them cheaply

The primary march takes up to 96 steps, 40 per layer thickness, and leaps over empty cells. It still costs too much at full resolution, so it runs at half resolution and a quarter of those pixels each frame, in a 2 by 2 pattern, and the rest comes from reprojected history. The history weight is 0.85. The cloud pass publishes a colour with an opacity and a depth of the cloud's front, and the composite takes whichever of the four half-resolution texels around a pixel reached at least as far as the pixel's geometry.

That is why the cloud edge in the looking-up frame above is soft. It is a half-resolution march with history.

4.3 The shadow clouds cast

Clouds that do not shadow anything look pasted on. A shadow map along the sun's direction is built each frame after the atmosphere resolves, with two cascades of 256 by 256 texels. The near one covers 32 km across (the code's half extent is 16 km) and marches the billows. The far one is at least four times wider and takes the share of the sun the weather's cover lets through, because its texels are too coarse for billows. The texels sit on lattices fixed to the planet, so the shadows hold still while the camera moves. The result is published as the frame's sun transmittance, and passes that light something under the clouds, like the volumetric fog's shafts and the GI probes' hits, opt into it with one line and multiply the sun by what it returns.

The cloud pass also writes the sun's visibility at each pixel, the terrain's directional visibility times the clouds' shadow, which deferred lighting multiplies into the sun. The tables in all of this, drawn to scale from the constants:

The engine has no debug view of these tables.

5. Night

Below the horizon the sun contributes nothing and the air has almost no light to scatter. The night sky is three layers composited over the sky-view result.

The first is stars. Each one is a point with a core and a faint halo, never drawn thinner than about a pixel with its light spread to match, because a sub-pixel source sampled once per pixel would blink as the camera moved. The palette runs from blue-white to orange, brightness is a distribution, and a fraction of them twinkle. The count is clamped to 20000. This scene has 18000 at a brightness of 2, with 16 percent twinkling at a speed of 1.3.

The second is shooting stars. A meteor is a head that crosses a short great-circle path of 5 to 25 degrees in 0.35 to 1.1 seconds at speed 1, starting 8 to 80 degrees up and heading mostly down. Its brightness follows a light curve that peaks late and burns out fast, most are as bright as a bright star, and a few, from a power law, are far brighter. The train behind the head glows as the head did when it passed and decays, and it outlives the head for a moment. Arrivals are a Poisson process at the rate per minute you set. The shader cuts time into slots with a handful of candidates each, every candidate present by chance, so several meteors can share the sky at irregular times and each pixel only tests a bounded few.

The third layer is a panorama. An equirectangular HDR is drawn behind everything, additively, multiplied by the air's transmittance and by the darkness factor below. The Deep-Space Skybox ships as two layers, galaxy and nebula, and as one file combining them. Helio decodes it on a worker thread, caps it at 4096 texels wide, builds mips and uploads one Rgba16Float texture. While it loads, a 1 by 1 black texture is bound, so nothing flashes. A .hdr or .exr is scene-linear and an 8 or 16 bit file is treated as sRGB and linearised on load. The editor installs the files for you from the Tools menu, under Starter Content: it downloads a pinned archive on a worker thread, checks its SHA-256 and writes the three files, 15.1, 15.9 and 7.8 MB, into the project's assets/sky/deep-space/. No binary carries them.

The next gallery turns each layer on alone at the same camera. The panorama's intensity is 10 and its opacity 1.

The nebula frame is nearly empty because this camera looks at a thin part of that layer. The panorama does not rotate with the sun, and it has no yaw tied to the time, so you orient it by hand with yaw, pitch and roll.

5.1 What makes it night

There is no flag for night. Three layers fade by a number computed from the directional lights. For each light the shader takes its luminance, weighted by how high above the horizon it stands:

D=∑iYi  smoothstep(−0.05, 0.15, d^i,y),darkness=1−smoothstep(0.05, 0.6, D)D=\sum_i Y_i\;\mathrm{smoothstep}(-0.05,\,0.15,\,\hat{d}_{i,y}),\qquad \text{darkness}=1-\mathrm{smoothstep}(0.05,\,0.6,\,D)

The sun at its default of about 2 is daylight whether or not you are looking at it. A moon light at 0.025 is not, and it being up does not end the night, since the test is brightness with no horizon check. The stars, shooting stars and panorama fade together between a daylight sum of 0.05 and 0.6. The panorama is a night backdrop and contributes appearance only. Scene lighting, moonlight included, comes from the level's light components.

6. Time of Day Is a Light

The four frames at the top use the same level, the same camera and one directional light, rotated and recoloured. The noon frame uses pitch -65 and colour (1, 0.97, 0.9), golden hour pitch -14 and (1, 0.62, 0.32) at intensity 2, dusk pitch -2 at intensity 1.2 and (1, 0.42, 0.22), night an intensity of zero.

The light's colour is authored. The air computes a physically plausible orange from a low sun, and the light's colour multiplies it, so the golden frame is warmer than the air alone would make it.

An engine with a time of day would have one object that owns the sun's path, a latitude and a date. Helio does not have it. It has the sun as a light, and a sky that does the right thing wherever you put it, which turns out to be a cleaner contract. A script that wants a day cycle can rotate one light. A level that wants a permanent sunset can park one. The sky has no state of its own to keep in sync.

7. The Sea Reads the Sky

The water plugin lists sky_frame and sky_view among its inputs, with a comment that says what for: the sky the sea reflects and scatters. Its shader function sky_color takes a reflected ray and looks it up in the sky-view table, the same table the sky itself is drawn from. A ray that points below the horizon is mirrored upward, as in Ultra-Water, so a tilted facet never reflects black ground. The sun's radiance at the sea is reddened by the air it crosses at a low sun. Without an atmosphere in the level the function falls back to a hand-made gradient from a horizon colour to a zenith colour, which is the sky the water had before the sky was a system.

The reflections in the hero frame show this. The clouds sit in the water below the horizon at the same tint as the clouds above it. In these frames the sea goes from blue-green at noon to olive at golden hour with no change to the water component. The planar reflection of opaque geometry is a separate path that falls back to the sky when there is nothing to mirror. The water post covers the rest of it: Water Is a Volume.

8. 1.6 Milliseconds for Nothing

The sky was one of the more expensive passes in a profile of a packaged game, windowed at 1280 by 720, vsync off, two runs of every build. Before the fix, Atmosphere took 1.80 to 1.84 ms of a GPU frame of 5.0 to 5.3 ms.

A dozen small compute dispatches make up the pass, and the profiler had been reporting them as one line. Per-dispatch GPU scopes went in first. While the profiler records, each dispatch becomes its own compute pass inside a named scope, and unprofiled they share one pass and cost nothing extra. Two lines stood out.

Atmosphere: resolve, a single-thread kernel, took 0.56 ms. It walked the whole atmosphere row table, one memory latency per row, and the table holds a row for every slot the component has ever used. The sky was paying for the history of the level.

Clouds: planet weather took 0.74 ms. The planet weather map was being rebuilt on about two thirds of frames, with the 0.19 ms multi-scattering table beside it. The rebuild was gated on a CPU-side key: rebuild when the rows changed. That key came from the row join's content_generation, and the join reruns whenever any transform moves. The game's character moves. Every step the character took told the sky its atmosphere had changed.

The fix moved the question to the GPU. resolve became a workgroup of 256 threads that finds the first enabled row together, hashes the words of that row, and compares the hash with last frame's. It writes indirect dispatch arguments: zero groups for the transmittance, multi-scattering and sky-table kernels and for the weather bake unless the row actually changed, and the weather bake reads a flag that says whether to take its whole map or this frame's slice. Nothing is read back. The CPU key, the params_only flag and the whole-bake flag are deleted, and so is a diagnostic environment variable that existed only to watch the old behaviour.

After the change Atmosphere costs 0.23 ms and the frame 3.29 and 3.35 ms in two runs. What is left of the pass: the cloud shadow map at 0.077 ms, aerial perspective at 0.046, irradiance at 0.031, and everything else under 0.02.

The fix covers the sky only. For every other consumer of a RowJoin, the joined buffer's coarse content generation still moves on every transform.

9. The Editor Panel

One object in the level holds the sky, and its component shows up in the Properties panel grouped by category: Atmosphere, Planet, Scattering, Clouds, Weather, Night Sky, Sky Texture, Shooting Stars. The first image shows the air and the cloud layer, the second the night categories with the panorama and its file picker.