Ambient Light
Updated
Even light from everywhere, with no direction or shadows.
- Type
ent-ambient-light- Script class
AmbientLightEntity- Family
- Lights
Overview
Section titled “Overview”An Ambient Light adds even light from everywhere at once. It has no position that matters, no direction and no shadows: it brightens every lit material by the same amount, so nothing in the scene is ever fully black.
Its settings live in its required Ambient Light component (comp-ambient-light). The editor and play light the scene the same way; in the editor the light also shows an icon in its colour, which play never builds.
Because it lights every surface equally, an ambient light flattens shapes. Keep it low and use it as a last touch of fill. For outdoor fill with a sense of up and down, prefer a Hemisphere Light; for light that comes from somewhere, use a Directional, Point or Spot light. An Environment on the scene root can provide richer ambient light from an HDRI.
The props:
intensityis the brightness, from0to100. Set it to0to switch the light off cheaply.colortints the light. A slightly blue fill reads as shade; a warm one as candle-lit interiors.
Set it up
Section titled “Set it up”- Add an Ambient Light entity to the scene (see Scenes and hierarchy). Where you place it makes no difference.
- Lower
intensityuntil shadowed areas are dark but still readable, often between0.1and0.5when other lights do the main work. - Set
colorto match the mood of the scene. - To react to changes from scripts, add a Signal Emitter to the entity.
Examples
Section titled “Examples”Set a dim, cool fill at startup
Section titled “Set a dim, cool fill at startup”Goal: give the scene a soft blue-grey fill that keeps the shadows from going black.
#pragma lifecycle(startup)
function startup() { const ambient = findEntity('Ambient Light').getComponent(AmbientLightComponent); ambient.setProps({ color: '#b8c4d8', intensity: 0.25 }); log('Ambient intensity is', ambient.props.intensity);}findEntity('Ambient Light')finds the entity by its name in the Hierarchy. Look entities up once, instartup.getComponent(AmbientLightComponent)returns the light’s component;setPropschanges only the keys passed to it.ambient.props.intensityreads the current value back.
Black out the room on a signal
Section titled “Black out the room on a signal”Goal: when another script emits power/off, the ambient fill fades out over a second, and power/on brings it back.
#pragma lifecycle(startup)
const NORMAL = 0.4;let ambient;let fade = null;
function fadeTo(target, seconds) { if (fade) fade.stop(); fade = Tween.value(ambient.props.intensity, target, (value) => { ambient.setProps({ intensity: value }); }, { duration: seconds, ease: Ease.InOutSine });}
function startup() { ambient = findEntity('Ambient Light').getComponent(AmbientLightComponent); on('power/off', () => fadeTo(0, 1)); on('power/on', () => fadeTo(NORMAL, 2));}Tween.value(start, end, onValue, options)animates a plain number and passes each value toonValue, where the script callssetProps.- A running fade is stopped before a new one starts, so a quick off-on never leaves two tweens fighting.
- The light is never disabled: setting
intensityto0turns it off without making lit materials recompile.
Follow changes made by other scripts
Section titled “Follow changes made by other scripts”Goal: log every brightness change of the ambient light, whoever made it.
The Ambient Light entity has a Signal Emitter whose signal is ambient.
#pragma lifecycle(startup)
function startup() { on('ambient', (event) => { if (event.eventType !== EventType.AmbientLight.Change) return; const { changed } = event.data; if ('intensity' in changed) log('Ambient is now', changed.intensity); if ('color' in changed) log('Ambient colour is now', changed.color); });}- The Signal Emitter forwards the entity’s events on the
ambientsignal. The script keeps onlyEventType.AmbientLight.Change. event.data.changedholds only the props that actually changed, with their new values.- A
setPropscall that changes nothing sends no event.
- Too much ambient light makes a scene look flat and washed out. If everything looks the same brightness, lower it and let directional lights shape the objects.
Tween.to(ambient.props, …)changes the numbers without applying them. UseTween.valuewithsetProps, as above.
Components
Section titled “Components”What a new Ambient Light 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 |
|---|---|---|---|---|
| Layer | comp-layer |
Required | One | Puts the entity on a render/raycast layer so cameras and rays can include or skip it. |
| Lock | comp-lock |
Required | One | Editor-only flag that stops the entity from being selected or moved. |
| Enable | comp-enable |
Required | One | Turns the entity and its children on or off: hides them and stops their behavior. |
| Transform | comp- |
Required | One | Position, rotation and scale of the entity, with optional editor-only per-axis locks. |
| Ambient Light | comp- |
Required | One | Even light from everywhere, with no direction or shadows. |
You can also add 6 optional components, such as Animation Graph, Animation Timeline, Motion Path Controller, Networkable, Networkable Trait, Signal Emitter.
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" |
|
intensity |
1 |
0 ≤ x ≤ 100 |
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.
AmbientLight::change
Like every entity, it also reports: 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 point light switched on and off every half second by a script that schedules itself with delay instead of setInterval.
Watch the lamp blink. 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 Ambient Light.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My Ambient Light'); if (!(entity instanceof AmbientLightEntity)) return;
// Its Ambient Light component holds the settings listed under Props. const component = entity.getComponent(AmbientLightComponent); log('intensity is', component.props.intensity); component.setProps({ intensity: 1.5 });}