Skip to content

Scene Root

Updated

Solid background color of the scene.

Type
ent-scene-root
Script class
SceneRootEntity
Family
Scene

The Scene Root is the entity at the top of a scene. Every other entity of the scene sits under it, directly or through its parents. Each scene has exactly one root.

The root is an entity like the others, but it also carries the components that apply to the whole scene: the background, the sky, the fog, the environment lighting and the post-processing effects. Unlike every other entity, it is not an element: it has no Layer, Lock, Enable or Transform component of its own.

A new root starts with a Background (comp-background), a solid colour behind everything, whose color defaults to white. You can add an Environment, a Sky, a Day Sky, a Night Sky, Fog and a Light Probe Solver.

When several scenes are loaded, the newest one is the ruling scene: its root’s background, environment lighting, fog and post-processing apply to the whole view. Disabling a scene root withdraws its Background, Environment and Fog; enabling it again restores them.

  1. Select the root at the top of the scene’s hierarchy (see Scenes and hierarchy).
  2. Set the Background color, or add an Environment with an HDRI to light the scene and show it as the sky.
  3. Add a sky (Sky, Day Sky or Night Sky) for a procedural sky, and Fog to soften the distance.
  4. In a script, reach the root with scene.findEntity(scene.rootId).

Goal: give the scene a dark blue background at startup and log the scene’s root.

BackgroundColor.jx
#pragma lifecycle(startup)
function startup() {
const root = scene.findEntity(scene.rootId);
log('Root entity:', root.name);
const background = root.getComponent(BackgroundComponent);
background.setProps({ color: '#0b1a2e' });
}
  • scene is the scene the script’s entity is in, and scene.rootId is the id of its root. scene.findEntity looks the id up in this scene.
  • getComponent(BackgroundComponent) returns the root’s Background; setProps changes its color.
  • An Environment with background switched on takes precedence over this colour.

Goal: thicken the fog when another script emits weather/fog, and clear it on weather/clear.

The scene root has a Fog component.

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) => {
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));
}
  • In linear mode the fog starts at near and is complete at far, both in metres.
  • near must stay below far, so both are always set in one setProps call.
  • The tween mixes the current distances with the target ones, so a change called off halfway continues from where the fog is.

Goal: pressing N swaps a daytime sky for a night sky.

The scene root carries a Day Sky and a Night Sky, with the Night Sky disabled.

DayNight.jx
#pragma lifecycle(startup, update)
let day;
let night;
function startup() {
const root = scene.findEntity(scene.rootId);
day = root.getComponent(DaySkyComponent);
night = root.getComponent(NightSkyComponent);
}
function update() {
if (!Input.keyboard.isKeyReleased(Key.N)) return;
const toNight = day.enabled;
day.setEnabled(!toNight);
night.setEnabled(toNight);
}
  • Scene-wide components live on the root, so both skies are found there.
  • setEnabled switches a component on or off; enabled reads its current state.
  • Input.keyboard.isKeyReleased(Key.N) is true for one frame after the key goes up.
  • Look the root up once in startup and keep its components; do not search for it every frame.
  • Pick a fog colour close to the sky’s horizon: skies are drawn at infinity and are not fogged.

What a new Scene Root is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.

Component Type Role Per entity What it does
Background comp-background Starts with One Solid background color of the scene.

You can also add 6 optional components, such as Day Sky, Environment, Fog, Light Probe Solver, Night Sky, Sky.

The settings of a new entity, as the runtime creates it. Change them in the Properties panel of XR Creator, or from a script with getComponent(…).setProps({ … }).

Prop Default Range
color "#ffffff"

With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.

It reports the events every entity has: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.

Run it in the browser, read its scripts, and download it as an XR Creator project.

Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My Scene Root.

SceneRootEntity.jx
#pragma lifecycle(startup)
function startup() {
// Find the entity by the name it has in the Hierarchy.
const entity = findEntity('My Scene Root');
if (!(entity instanceof SceneRootEntity)) return;
// Its Background component holds the settings listed under Props.
const component = entity.getComponent(BackgroundComponent);
log(component.props);
}