Directional Light
Updated
Sun-like parallel light aimed by the entity’s rotation.
- Type
ent-directional-light- Script class
DirectionalLightEntity- Family
- Lights
Overview
Section titled “Overview”A Directional Light is parallel light, like the sun or the moon: every ray has the same direction, wherever the light entity stands. It shines along the entity’s local -Z axis, so only the entity’s rotation changes the lighting; moving it changes nothing.
Its settings live in its required Directional Light component (comp-directional-light). In the editor it shows an icon in its colour and, while selected, an arrow for its aim; play builds none of these. The scene is lit the same way in both.
Use it as the sun or the moon of an outdoor scene. It never casts shadows, real-time or baked: ground the objects under it with a Contact Shadow, or use a Spot Light with a Light Shadow over the play area when you need cast shadows. For light from a point, such as a lamp, use a Point Light. A Water Mesh takes its sun glint from the scene’s first Directional Light.
The props:
intensityis the brightness, in lux, from0to200000.colortints the light: warm near sunrise and sunset, white at noon, blue for moonlight.
Set it up
Section titled “Set it up”- Add a Directional Light entity to the scene (see Scenes and hierarchy) and name it, for example
Sun. Keep it directly under the scene root so its rotation is its world rotation. - Rotate the entity so its arrow points the way the light travels: down and across the scene.
- Set
intensityandcolor. - With a Day Sky on the scene root, aim the light along the sky’s sun from a script (see the examples), so the light and the visible sun agree.
- Add a receiving Contact Shadow to the ground so objects do not seem to float.
Examples
Section titled “Examples”Aim and colour the sun at startup
Section titled “Aim and colour the sun at startup”Goal: tilt the sun 50 degrees down and 30 degrees around, with a warm afternoon colour.
#pragma lifecycle(startup)
function startup() { const sun = findEntity('Sun'); sun.getComponent(DirectionalLightComponent).setProps({ color: '#fff1d6', intensity: 2 });
sun.setRotation(DEG_TO_RAD(-50), DEG_TO_RAD(30), 0);}- The light’s look is on the component; its aim is on the entity.
setRotation(x, y, z)sets the entity’s local rotation in radians. DEG_TO_RADconverts degrees to radians. A negative rotation around x tilts the light’s -Z axis down toward the ground.- Only the rotation matters, so the entity can stay wherever it is easy to select in the editor.
Follow the Day Sky’s sun
Section titled “Follow the Day Sky’s sun”Goal: keep the light aimed at the visible sun, and step the time of day with the T key.
The scene root has a Day Sky; the Directional Light Sun sits directly under the root.
#pragma lifecycle(startup, update)
let sky;let sun;let light;
function aimSun() { // lookAt turns +Z toward the point; the light shines along -Z, away from the sun. sun.lookAt(sun.position.clone().add(sky.sunDirection)); const height = Math.max(0, sky.sunDirection.y); light.setProps({ intensity: 0.4 + height * 2, color: height < 0.3 ? '#ffb070' : '#fff4e0' });}
function startup() { sky = scene.findEntity(scene.rootId).getComponent(DaySkyComponent); sun = findEntity('Sun'); light = sun.getComponent(DirectionalLightComponent); aimSun();}
function update() { if (!Input.keyboard.isKeyReleased(Key.T)) return; sky.setProps({ time: (sky.props.time + 0.1) % 1 }); aimSun();}scene.findEntity(scene.rootId)returns the scene root, where the Day Sky lives.sky.sunDirectionis a read-only world-space direction toward the sun. Looking at a point along it turns the entity’s +Z toward the sun, so its -Z, the way the light shines, points away from it.- The light is warmer and dimmer when the sun is low, brighter when it is high.
isKeyReleasedis true once per key press, so each press advances the sky’stimeby a tenth of the day. The light is re-aimed every time the sky changes.
Switch street lamps on at dusk
Section titled “Switch street lamps on at dusk”Goal: whenever the sun’s intensity drops below a threshold, whoever changed it, the street lamps turn on.
The Sun entity has a Signal Emitter whose signal is sun. The lamps are Point Lights named Street Lamp 1 to Street Lamp 3.
#pragma lifecycle(startup)
const DUSK = 0.8;let lamps = [];let lit = false;
function startup() { lamps = ['Street Lamp 1', 'Street Lamp 2', 'Street Lamp 3'] .map((name) => findEntity(name)) .filter(Boolean) .map((entity) => entity.getComponent(PointLightComponent));
on('sun', (event) => { if (event.eventType !== EventType.DirectionalLight.Change) return; if (!('intensity' in event.data.changed)) return;
const dark = event.data.changed.intensity < DUSK; if (dark === lit) return; lit = dark; for (const lamp of lamps) lamp.setProps({ intensity: lit ? 3 : 0 }); });}EventType.DirectionalLight.Changearrives each time asetPropscall actually changes the sun.event.data.changedholds only the props that changed.filter(Boolean)drops lamp names that are not in the scene.- The lamps change only when the sun crosses the threshold, and they switch with
intensityrather than by disabling the entity.
- Moving the entity does not change the lighting; rotate it instead.
- Aim it again whenever you change a Day Sky’s
timeor preset: the sky does not move the light for you. lookAtworks in world space, so aiming with it is exact when the light’s parent is not rotated, for example a light directly under the scene root.
Components
Section titled “Components”What a new Directional 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. |
| Directional Light | comp- |
Required | One | Sun-like parallel light aimed by the entity’s rotation. |
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 ≤ 200000 |
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.
DirectionalLight::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 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 Directional Light.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My Directional Light'); if (!(entity instanceof DirectionalLightEntity)) return;
// Its Directional Light component holds the settings listed under Props. const component = entity.getComponent(DirectionalLightComponent); log('intensity is', component.props.intensity); component.setProps({ intensity: 1.5 });}