Skip to content

Directional Light

Updated

Sun-like parallel light aimed by the entity’s rotation.

Type
ent-directional-light
Script class
DirectionalLightEntity
Family
Lights

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:

  • intensity is the brightness, in lux, from 0 to 200000.
  • color tints the light: warm near sunrise and sunset, white at noon, blue for moonlight.
  1. 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.
  2. Rotate the entity so its arrow points the way the light travels: down and across the scene.
  3. Set intensity and color.
  4. 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.
  5. Add a receiving Contact Shadow to the ground so objects do not seem to float.

Goal: tilt the sun 50 degrees down and 30 degrees around, with a warm afternoon colour.

SunSetup.jx
#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_RAD converts 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.

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.

SunFollowsSky.jx
#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.sunDirection is 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.
  • isKeyReleased is true once per key press, so each press advances the sky’s time by a tenth of the day. The light is re-aimed every time the sky changes.

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.

StreetLamps.jx
#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.Change arrives each time a setProps call actually changes the sun. event.data.changed holds 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 intensity rather 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 time or preset: the sky does not move the light for you.
  • lookAt works 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.

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-transform Required One Position, rotation and scale of the entity, with optional editor-only per-axis locks.
Directional Light comp-directional-light 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

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.

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 Directional Light.

DirectionalLightEntity.jx
#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 });
}