Sky and fog
Put a sky behind the scene, either a two-colour gradient or a cubemap image, fade distant surfaces into fog, and turn day into night.
component: skybox
component: fog
component: day_cycle
Vulkan
WebGPU
Desktop · Android · iOS · Web
A scene document and the scene DSL are two ways to write the same ECS components. Each pair of tabs
below builds equal Skybox and Fog components.
Add a gradient sky and fog
Put a skybox and a fog on one entity. The sky blends from horizonColor at the horizon to
zenithColor overhead.
- Open the Environment tab of the Inspector.
- Under Skybox & Horizon, turn Sky: On and choose Procedural. Set Horizon Color and Zenith Color.
- Under Atmosphere & Fog, set Fog Density.
Studio creates the skybox and fog components on first edit. You can also select an entity
and use Add component > Skybox or Fog; their Inspector sections have Enabled,
colour, Density and Type fields.
The gradient sky draws only when the app's render plan carries it:
contentFeatures = listOf(skyboxContentFeature(PackShaderSets.Skybox)). See
Render plans and shaders.
Use a cubemap sky
Set type to Cubemap and point cubemapPath at a strip image: six square faces side by side,
+X, −X, +Y, −Y, +Z, −Z from the left.
- Open the Environment tab of the Inspector.
- Under Skybox & Horizon, choose Cubemap.
- Set Cubemap Asset to the strip's path in the project, and Exposure.
Studio loads the image from the project's files and draws it in the viewport.
A cubemap sky needs SkyboxCubemapSystem to load the image and hand it to the renderer. It is not
installed by default. Add it to the scene with the coroutine scope and asset source your app loads
files with:
/** Loads the scene's cubemap sky from [assets] off the frame thread. */
fun SceneAppDsl.cubemapSky(scope: CoroutineScope, assets: AssetSource) {
frameSystem("cubemap-sky") {
SkyboxCubemapSystem(renderer as ContentFeatureHost, scope, assets)
}
}
The system reads the strip off the frame thread and attaches it on a later frame. Changing the path
or exposure loads the new image; leaving Cubemap mode, disabling the sky, or removing the component
takes it away.
Day and night
Put a day_cycle on the node of the scene's directional light. During play the sun crosses the sky
once every dayLengthSeconds, and each stop sets how the sky, the light, its ambient share and the
fog look at that time of day. Between stops they blend.
{
"version": 1,
"nodes": [
{
"name": "sun",
"components": [
{ "component": "light", "type": "Directional", "shadowDistance": 60.0 },
{
"component": "day_cycle",
"dayLengthSeconds": 600,
"time": 0.3,
"stops": [
{ "time": 0.0, "horizonColor": "#0B1020", "zenithColor": "#02040A", "lightIntensity": 0.0, "ambient": 0.08 },
{ "time": 0.25, "horizonColor": "#F2A65A", "zenithColor": "#3A4E7A", "lightColor": "#FFB070", "lightIntensity": 0.6, "ambient": 0.2 },
{ "time": 0.5, "horizonColor": "#B8CCE0", "zenithColor": "#3361AD", "lightColor": "#FFF4E0", "lightIntensity": 1.0, "ambient": 0.3 },
{ "time": 0.75, "horizonColor": "#F08A4B", "zenithColor": "#2E3A66", "lightColor": "#FF9A5C", "lightIntensity": 0.6, "ambient": 0.2 }
]
}
]
},
{
"name": "environment",
"components": [ { "component": "skybox", "type": "Procedural" } ]
}
]
}
time is a fraction of the day: 0 midnight, 0.25 sunrise, 0.5 noon, 0.75 sunset. The sun
rises at sunriseAzimuthDegrees and is highest at noon, at noonElevationDegrees. Below the horizon
the light still points at the sun, so the ground gets no direct light and the sky's moon stays
opposite the sun. The night stop's low lightIntensity, low ambient and dark sky make it night.
A field no stop sets keeps the value its own component authors, and blending wraps across midnight.
The day advances in play: runProject, or the systems sceneSystemsFor returns. Outside play the
scene shows its light, sky and fog as authored; DayCycleSystem().update(world, 0f) applies the
current time without advancing it. Saving writes the authored time back, never the time play
reached. See day_cycle for every field.
Sun and moon over any sky
A procedural sky draws its own sun and moon discs; a cubemap or flat-colour sky does not. Set
sunAndMoon to true on the skybox to draw them over any sky, where the light shines from and
opposite it. sunTexture and moonTexture replace a disc with an image on a square the size of the
disc, and transparent pixels leave the sky showing. moonPhase cuts the moon: 0 new, 0.25 first
quarter (lit on the right), 0.5 full, 0.75 last quarter (lit on the left). A texture or a phase
other than full draws the bodies without sunAndMoon, and a procedural sky then stops drawing its
own discs so they are not doubled. The moon always stays opposite the sun. With a day_cycle they
follow its sun path by time of day, not the light's direction, so a rotated light node can hold the
ground light still while the sun and moon cross the sky; without one they follow the light. Set
additiveBodies to add them to the sky instead of covering it, as a glowing sun texture wants. A stop's
sunVisibility and moonVisibility (0 hidden, 1 shown, blended between stops) give each body its hours
of the day, and let both be hidden for a while at dusk. Wire SkyboxCelestialSystem
beside SkyboxCubemapSystem, passing it the cubemap system so it draws after the sky.
A cubemap sky that follows the day
A stop can set skyTint, a colour the cubemap is multiplied by, and skyExposure, a multiplier on
the sky's own exposure. Both blend between stops like the other fields; a stop that sets neither
leaves the cubemap as baked. To change the picture itself, list more strips under the sky's
panoramas, each with the time it shows at, in time order. The day cycle blends the two either side
of its time, wrapping across midnight, so a sky can go from day to evening to night. Without a
day_cycle the first panorama shows. SkyboxCubemapSystem loads the two it needs and swaps to the
next pair as the day moves on.
Keep the sun's node unrotated
A rotated light node shines along its rotation and ignores the direction the day cycle writes.
Properties
skybox:
| Property | Type | Default | What it does |
|---|---|---|---|
enabled |
boolean | true |
Whether the sky draws. |
type |
Procedural · Cubemap · SolidColor |
Procedural |
Gradient, image, or one flat colour. |
horizonColor |
color | (0.72, 0.80, 0.88) |
Gradient colour at the horizon. For SolidColor, the whole sky. |
zenithColor |
color | (0.20, 0.38, 0.68) |
Gradient colour overhead. |
cubemapPath |
string | none | Cubemap only. The strip image, read through the app's asset source. |
exposure |
number | 1.0 |
Cubemap only. Multiplies the sampled colour. |
sunAndMoon |
boolean | false |
Draw the sun and moon over this sky, whatever its type. |
sunTexture |
string | none | Asset path of an image drawn as the sun. |
moonTexture |
string | none | Asset path of an image drawn as the moon. |
moonPhase |
number | 0.5 |
The moon's phase, 0 to 1: 0 new, 0.5 full. |
additiveBodies |
boolean | false |
Add the sun and moon to the sky instead of covering it. |
panoramas |
list of { time, path } |
[] |
Cubemap only. More strips by time of day, in time order; a day_cycle blends the two around its time. |
fog:
| Property | Type | Default | What it does |
|---|---|---|---|
enabled |
boolean | true |
Whether fog applies. |
density |
number | 0.001 |
How fast fog thickens with distance. 0 is no fog. |
color |
color | (0.55, 0.62, 0.70) |
The colour distant surfaces fade to. |
mode |
Exponential or Linear |
Exponential |
How fog grows with distance: by density, or evenly from start to end. |
start |
number | 0 |
Linear only. Metres from the camera where fog begins. At least 0. |
end |
number | 1000 |
Linear only. Metres from the camera where surfaces are wholly fog colour. Beyond start. |
A color is an object ({ "r": …, "g": …, "b": … }), an array, or a hex string such as "#C8D2DC".
In the scene DSL, skybox(Skybox(...)) and fog(Fog(...)) attach them to the current entity;
skyboxEntity() and fogEntity() create an entity for each.
How it works
The renderer reads the first Skybox and the first Fog in the world. With no Skybox, no sky is
drawn. With no Fog, there is no fog.
Fog is distance fog, computed per pixel in the lit and textured shaders. Exponential fog, the
default, shows 1 − e^(−density × d) of the fog colour on a surface d units from the camera, so
it thickens forever and never quite hides anything. Linear fog shows none nearer than start, then
(d − start) / (end − start) of it, and all of it from end on: use it when the far edge of the
world, or a draw distance, must vanish completely. Surfaces drawn by shaders that do not read fog,
such as your own, are not fogged.
Every Fog field may be changed while the scene runs; the next frame fogs with the new values, so a
system can pull end in as a storm arrives. A linear range the shaders cannot ramp across, a
negative start or an end at or before it, fails the document's validation, and Fog refuses it.
SolidColor uses horizonColor for the whole sky. The cubemap sky draws over the gradient sky when
the plan has both.
Tune density to the scene's scale
Density is per world unit. At 0.02, a surface 50 units away is about 63% fog; at the default
0.001 it is about 5%.
A missing plan feature leaves the sky blank
Without skyboxContentFeature(...) in the render plan, the gradient sky is never drawn, whatever
the skybox component says.
The cubemap image must be six squares wide
A strip whose width is not six times its height fails to load. The sky stays as it was.
Debugging
A cubemap that fails to load logs Sky '<path>' could not load: <reason> on the scene-sky logger
and keeps the previous sky. Any debug view, such as Shadow visibility, hides the sky so it does not
read as data.
See also
- Lights and shadows for the sun and ambient level.
- Render plans and shaders for
skyboxContentFeatureanddepthFogContentFeature.