Skip to content

Environment and sky

Updated

Everything on this page goes on the scene root: it shapes the whole scene rather than one object. The Environment lights the scene from a panoramic HDR photo and gives shiny materials something to reflect. The Background, Sky, Day Sky and Night Sky decide what the camera sees behind everything. Fog fades distant objects toward a colour. Together they set the mood of a world, and scripts can change all of them live: a day that turns to dusk, fog that rolls in, a sky that switches to night.

  • Components are reached with entity.getComponent(Class) and changed with setProps. See Components.
  • A script reaches the scene root with scene.findEntity(scene.rootId), or with findEntity and the root’s name.
  • Skies are only what the camera sees. To light the scene you also need an Environment and, for the sun or moon, a Directional Light.
Component Script global What it does Lights the scene
Environment EnvironmentComponent Image-based lighting from an HDRI, optionally shown as the sky. Yes: ambient light and reflections.
Background BackgroundComponent A solid background colour. No
Fog FogComponent Distance fog. No
Sky SkyComponent A cheap procedural atmosphere with a sun disc. No
Day Sky DaySkyComponent A scattered daytime sky with a time of day and clouds. No
Night Sky NightSkyComponent Stars, the Milky Way, a moon and shooting stars. No

With several scenes loaded, the newest scene’s root decides the background, environment and fog that reach the screen. Skies follow the same rule: on an older scene’s root they hide and stop working, keeping what they computed for an instant return, until the scenes above it leave. This changes no props and no enabled flags.

Disabling a scene root withdraws its Background, Environment and Fog. Enabling it again restores them. While withdrawn, the Environment keeps its HDRI and prefiltered map, so it comes back at once; removing the component frees the map.

An HDRI is a 360° photograph that stores real light levels, not just colours, so bright sources such as the sun stay bright. The Environment lights the scene with one equirectangular HDRI (.hdr or .exr). It is prefiltered once into the scene’s environment map, which gives every Standard and Physical material ambient light and reflections that match its roughness. With background: true the same map is drawn as the sky, sharp or blurred, so no second copy is made.

Prop Type Default Description
src string none The HDRI, a URL or asset (.hdr or .exr). Empty lights nothing.
intensity number 1 Environment lighting strength, 0–100.
rotation number 0 Heading of the HDRI in degrees. Turns the lighting and the sky together, so reflections always match the sky.
background boolean true Show the HDRI as the sky.
backgroundBlur number 0 Sky blur, 0 (sharp) to 1 (fully diffuse).
backgroundIntensity number 1 Sky brightness, separate from the lighting, 0–100.
  • Loading. The scene waits for the HDRI before it is revealed, so it never shows unlit. A new src keeps the previous lighting until the new HDRI has loaded.
  • Memory. The prefiltered map follows the HDRI’s size: a 2k HDRI makes a 512 cube, a 4k one a 1024 cube with four times the memory. Use 1k or 2k HDRIs for phones and headsets.
  • Light probes. Inside a Light Probe volume, the baked probes replace the Environment’s diffuse light; its reflections stay everywhere.

A solid background colour. Every scene root starts with one.

Prop Type Default Description
color string '#ffffff' Background colour.

An Environment with background: true takes precedence over the colour.

Distance fog over the whole scene: objects fade toward the fog colour as they recede. Use it to soften the distance, to match a sky’s horizon, or to hide where a scene ends.

Prop Type Default Description
color string '#c8d2dc' The colour objects fade toward.
mode 'linear' | 'exponential' 'linear' linear ramps from near to far; exponential thickens along a density curve.
near number 1 Linear only: where the fog starts, in metres. At least 0 and less than far.
far number 100 Linear only: where the fog is complete, in metres. More than near, at most 100000.
density number 0.02 Exponential only: how thick the fog is, 0–10 per metre.

When you change near and far together, pass both in one setProps call, so the pair always stays in order.

The skies are drawn at infinity and are not fogged: pick a fog colour close to the sky’s horizon. Removing the Fog restores the scene’s previous fog.

SkyComponent is a cheap procedural atmosphere with a sun disc. The scattering is computed once into a cube map and redrawn only when its props change; each frame the sky costs one texture fetch per pixel. It also has a read-only sunDirection, the world-space direction toward its sun.

Prop Type Default Description
distance number 1000 Sky-box scale in metres, more than 0 and at most 100000.
elevation number 35 Sun elevation in degrees: 0 is the horizon, 90 the zenith.
azimuth number 135 Sun compass azimuth in degrees.
turbidity number 3 Haze, 0–1000. Higher is milkier with a brighter horizon.
rayleigh number 2 Blue scattering, 0–64.
mieCoefficient number 0.02 Scattering around the sun, 0–1.
mieDirectionalG number 0.9 Sun glow bias, 0 to 1. Higher is a tighter glow.
exposure number 1 Sky brightness multiplier, 0–100.

DaySkyComponent is a physically scattered daytime sky with a time of day and optional drifting clouds. Sky and clouds share one sun, so they always agree.

Prop Type Default Description
preset 'clearNoon' | 'clearMorning' | 'goldenHour' | 'sunset' | 'dawn' | 'hazyDay' | 'blueHour' 'clearNoon' The base look.
time number the preset’s Time of day, 0 (sunrise) to 1 (sunset). The sun rises at +X and sets at -X.
quality 'auto' | 'low' | 'high' 'auto' Atmosphere and cloud detail. auto picks low on phones and headsets, high elsewhere.
exposure number the preset’s Sky brightness multiplier, 0–100.
sunIntensity number the preset’s Atmosphere sunlight multiplier, 0–1000.
mieCoefficient number the preset’s Haze and sun glow, 0–1 on this atmosphere’s own scale.
mieDirectionalG number the preset’s Sun glow bias, 0 to 1. Higher is a tighter glow.
rayleigh number 1 Blue-scattering multiplier, 0–64.
horizonBlue string '#6699db' The blue the low daytime sky is pulled toward.
horizonBlend number 0.6 How far toward horizonBlue, 0 to 1.
clouds boolean true Draw the clouds. Off builds nothing and costs nothing.
cloudPreset 'partlyCloudy' | 'clearDay' | 'overcast' | 'dramatic' | 'sunset' | 'mackerel' 'partlyCloudy' The cloud look.
cloudOpacity number the cloud preset’s Cloud opacity, 0 to 1.
cloudWind number the cloud preset’s Signed drift speed, −1000 to 1000 m/s.
cloudAltitude number the cloud preset’s Cloud-base height in metres.
cloudDensity number 1 How many clouds, and how spread, 0–8. Turn clouds off to remove them all.
cloudSeed number 1337 Seed of the cloud layout.

Choosing a preset or cloudPreset fills in its group’s values; values you pass in the same setProps call win. exposure, rayleigh, mieCoefficient and mieDirectionalG mean the same as on the Sky, but on this atmosphere’s own scale, so values do not carry over one to one.

sunDirection is a read-only getter: the world-space direction toward the sun. Aim a Directional Light with it, as shown in Lights.

Cost. The atmosphere is recomputed into a small cube map only when preset, time, quality or an atmosphere prop changes; each frame draws one cube fetch and the sun. The clouds are one instanced draw whose drift runs on the GPU. Animating time every frame recomputes the sky every frame: fine on desktop, costly on mobile, so step it, for example once a second.

NightSkyComponent is a night sky: a gradient dome with the Milky Way and nebulae, a phased moon with its halo, twinkling stars and shooting stars. A full sky is three to five draw calls.

Prop Type Default Description
preset 'pristineMountain' | 'fullMoonNight' | 'suburbanSky' | 'meteorShower' | 'deepSpace' | 'twilight' 'pristineMountain' The base look.
quality 'auto' | 'low' | 'high' 'auto' Star count, Milky Way detail and meteor pool.
seed number 42 Star field seed: the same seed always gives the same sky.
starCount number the preset’s Base number of stars, a whole number, 0–100000.
starDensity number 1 Live multiplier on the visible stars, 0 to 2.
nebulaDrift number 0.0015 Signed drift speed of the nebulae, in radians per second. 0 holds them still.
moonPhase number the preset’s 0 is new, 0.5 full, 1 new again.
moonElevation number the preset’s Degrees above the horizon.
moonAzimuth number the preset’s Compass degrees.
exposure number the preset’s Star brightness multiplier, 0–100.
meteors boolean true Shooting stars. Off skips their update and draw.

moonDirection is a read-only getter: the world-space direction toward the moon, defined even when the preset shows no moon. Aim a moonlight with it the same way as the sun.

Changing starDensity only changes how many stars are drawn: nothing is rebuilt, so it is a good prop to animate.

  • Day Sky and Night Sky are drawn at infinity around each eye, so they stay correct in a headset with no per-frame CPU work. They ignore depth and are never hit by rays.
  • The skies tick in the editor too, so clouds drift and the atmosphere updates while you author.
GalleryEnvironment.jx
#pragma lifecycle(startup)
function startup() {
const root = scene.findEntity(scene.rootId);
const env = root.getComponent(EnvironmentComponent);
if (!env) {
log('No Environment on the scene root');
return;
}
// Turn the HDRI so its bright window faces the statues, and blur the backdrop.
env.setProps({ rotation: 90, backgroundBlur: 0.3, backgroundIntensity: 0.5, intensity: 0.8 });
}
  • scene.findEntity(scene.rootId) returns the scene root, where scene-wide components live.
  • rotation is in degrees and turns the lighting and the backdrop together, so reflections stay consistent with what you see.
  • A blurred, dimmer backdrop (backgroundBlur, backgroundIntensity) keeps attention on the objects without changing how they are lit.
  • Checking for undefined keeps the script safe in a scene without an Environment.

When the signal weather/fog arrives, the fog closes in over four seconds; weather/clear pushes it back.

RollingFog.jx
#pragma lifecycle(startup)
const CLEAR = { near: 40, far: 300 };
const FOGGY = { near: 2, far: 25 };
let fog;
let tween = null;
function moveFog(to) {
if (tween) tween.stop();
const from = { near: fog.props.near, far: fog.props.far };
tween = Tween.value(0, 1, (t) => {
// Both edges in one call, so near always stays below far.
fog.setProps({
near: from.near + (to.near - from.near) * t,
far: from.far + (to.far - from.far) * t,
});
}, { duration: 4, ease: Ease.InOutSine });
}
function startup() {
fog = scene.findEntity(scene.rootId).getComponent(FogComponent);
fog.setProps({ mode: 'linear', color: '#b8c4cc', ...CLEAR });
on('weather/fog', () => moveFog(FOGGY));
on('weather/clear', () => moveFog(CLEAR));
}
  • Tween.value(0, 1, …) produces a progress value from 0 to 1; the script mixes the two fog distances with it.
  • near and far are always set together. Setting near alone past the current far would make an invalid pair.
  • Fog is cheap per pixel, so changing it every frame during a transition is fine.
  • emit('weather/fog') from any script, or a Signal Emitter, starts the change.

The scene root has a Day Sky; a Directional Light named Sun sits directly under the root. One in-game day lasts four minutes.

DayCycle.jx
#pragma lifecycle(startup, update)
const DAY_SECONDS = 240; // sunrise to sunset
const STEP = 1; // update the sky once a second, not every frame
let sky;
let sun;
let sunLight;
let elapsed = 0;
let wait = 0;
function apply() {
sky.setProps({ time: (elapsed / DAY_SECONDS) % 1 });
sun.lookAt(sun.position.clone().add(sky.sunDirection));
sunLight.setProps({ intensity: 0.3 + Math.max(0, sky.sunDirection.y) * 2.2 });
}
function startup() {
sky = scene.findEntity(scene.rootId).getComponent(DaySkyComponent);
sun = findEntity('Sun');
sunLight = sun.getComponent(DirectionalLightComponent);
apply();
}
function update(dt) {
elapsed += dt;
wait -= dt;
if (wait > 0) return;
wait = STEP;
apply();
}
  • Changing time recomputes the atmosphere. Doing it once a second keeps the cost low on mobile while the sun still moves smoothly enough to look natural.
  • sky.sunDirection changes with time; the light is re-aimed right after, so shading and sky agree.
  • dt is game time: when the game is paused with Time.pause(), the day stops too.

The scene root carries both a Day Sky and a Night Sky; a Directional Light named Sun lights the scene. Press N to switch.

DayNightSwitch.jx
#pragma lifecycle(startup, update)
let day;
let night;
let sunLight;
let fog;
let isNight = false;
function show(nightTime) {
isNight = nightTime;
day.setEnabled(!nightTime);
night.setEnabled(nightTime);
const moon = { color: '#9fb4ff', intensity: 0.25 }; // cool moonlight
const sun = { color: '#fff4e0', intensity: 2.2 }; // warm sunlight
sunLight.setProps(nightTime ? moon : sun);
if (fog) fog.setProps({ color: nightTime ? '#0b1020' : '#c8d2dc' });
}
function startup() {
const root = scene.findEntity(scene.rootId);
day = root.getComponent(DaySkyComponent);
night = root.getComponent(NightSkyComponent);
fog = root.getComponent(FogComponent);
sunLight = findEntity('Sun').getComponent(DirectionalLightComponent);
show(false);
}
function update() {
if (Input.keyboard.isKeyReleased(Key.N)) show(!isNight);
}
  • setEnabled switches each sky. A disabled sky draws nothing and costs nothing.
  • The same Directional Light plays sun and moon: only its colour and intensity change, so no material is recompiled.
  • The fog colour follows the sky’s horizon, since skies themselves are never fogged.

5. A moonlit night with more stars over time

Section titled “5. A moonlit night with more stars over time”
Stargazing.jx
#pragma lifecycle(startup)
async function startup() {
const night = scene.findEntity(scene.rootId).getComponent(NightSkyComponent);
const moon = findEntity('Moonlight');
night.setProps({ preset: 'fullMoonNight', starDensity: 0.2 });
moon.lookAt(moon.position.clone().add(night.moonDirection));
// starDensity rebuilds nothing, so tweening it is cheap.
await Tween.value(0.2, 1.5, (value) => night.setProps({ starDensity: value }), { duration: 20, ease: Ease.InQuad });
log('All the stars are out');
}
  • Choosing a preset fills in the moon’s phase and position; moonDirection then points at it, and the moonlight is aimed the same way as a sun.
  • starDensity only changes how many stars are drawn, so animating it every frame is fine.
  • await on the tween waits for it to finish before logging.
  • Expecting a sky to light the scene. Skies are visual only. Add an Environment for ambient light and reflections, and a Directional Light for direct sun.
  • Putting these components on another entity. They belong on the scene root.
  • An Environment that hides the Day Sky. With background: true the HDRI is drawn as the sky. Set background: false when a procedural sky is the backdrop.
  • Animating Day Sky time every frame on mobile. Each change recomputes the atmosphere. Step it.
  • Setting fog near and far in separate calls. An intermediate pair can be out of order. Pass both in one setProps.
  • Using radians for Environment rotation. It is in degrees.
  • A fog colour that does not match the sky. Skies are not fogged, so distant objects fade into a different colour than the horizon.
  • Large HDRIs on phones. A 4k HDRI uses four times the memory of a 2k one. Use 1k or 2k.