Scene Root
Updated
Solid background color of the scene.
- Type
ent-scene-root- Script class
SceneRootEntity- Family
- Scene
Overview
Section titled “Overview”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.
Set it up
Section titled “Set it up”- Select the root at the top of the scene’s hierarchy (see Scenes and hierarchy).
- Set the Background
color, or add an Environment with an HDRI to light the scene and show it as the sky. - Add a sky (Sky, Day Sky or Night Sky) for a procedural sky, and Fog to soften the distance.
- In a script, reach the root with
scene.findEntity(scene.rootId).
Examples
Section titled “Examples”Change the background colour
Section titled “Change the background colour”Goal: give the scene a dark blue background at startup and log the scene’s root.
#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' });}sceneis the scene the script’s entity is in, andscene.rootIdis the id of its root.scene.findEntitylooks the id up in this scene.getComponent(BackgroundComponent)returns the root’s Background;setPropschanges itscolor.- An Environment with
backgroundswitched on takes precedence over this colour.
Roll fog in and out
Section titled “Roll fog in and out”Goal: thicken the fog when another script emits weather/fog, and clear it on weather/clear.
The scene root has a Fog component.
#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
linearmode the fog starts atnearand is complete atfar, both in metres. nearmust stay belowfar, so both are always set in onesetPropscall.- The tween mixes the current distances with the target ones, so a change called off halfway continues from where the fog is.
Switch between day and night
Section titled “Switch between day and night”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.
#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.
setEnabledswitches a component on or off;enabledreads its current state.Input.keyboard.isKeyReleased(Key.N)istruefor one frame after the key goes up.
- Look the root up once in
startupand 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.
Components
Section titled “Components”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- |
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" |
Events
Section titled “Events”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.
Live example
Section titled “Live example”Run it in the browser, read its scripts, and download it as an XR Creator project.
A Cursor component on the camera draws the runtime Lobby demo's pointer image in place of the system cursor over the view. Clicking the crate switches between the custom and the system cursor.
Move the mouse over the view to see the custom pointer; click the crate to switch to the system cursor and back. Drag to orbit the camera, scroll to zoom.
From a script
Section titled “From a script”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.
#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);}