Skip to content

Lights

Updated

A real-time light is a light the renderer evaluates every frame, on every pixel of every lit material. MIRA has six of them. Each one is an entity with a light component: the component holds the light’s colour and brightness, and the entity’s transform places and aims it. This page describes each light, what it is good for, and how a script changes it.

  • A light is an entity with a component. You reach the component with entity.getComponent(PointLightComponent) and change it with setProps. See Components.
  • Scripts run in hooks such as startup and update(dt), declared with #pragma lifecycle(...). See Scripting.
  • Components report what happens to them through a Signal Emitter on their entity. A script listens with on(signal, callback) and compares event.eventType with an EventType constant.
  • Read the Lighting overview first for how lights fit with shadows, skies and baked light.
Light Entity Component (script global) Shadows Use it for
Ambient ent-ambient-light AmbientLightComponent No A flat fill so nothing is fully black.
Directional ent-directional-light DirectionalLightComponent No The sun or the moon: parallel light from one direction.
Hemisphere ent-hemisphere-light HemisphereLightComponent No Cheap outdoor ambient: a sky colour from above, a ground colour from below.
Point ent-point-light PointLightComponent Yes A bulb that shines in every direction.
Spot ent-spot-light SpotLightComponent Yes A cone of light: stage lights, torches, lamps.
Rect Area ent-rect-area-light RectAreaLightComponent No A soft glowing rectangle: a window, a panel light, a screen.

The entity classes (PointLightEntity, SpotLightEntity, and so on) are script globals too, so a script can build a new light with scene.add(new PointLightEntity()).

  • Required parts. Each light entity always has a Transform and its own light component. Neither can be removed. The light component can be disabled like any other: light.setEnabled(false) takes its light out of the scene (and any Spot Light Beam with it) until setEnabled(true). You can also set its intensity to 0, or disable the whole entity.
  • Aiming. Directional, Spot and Rect Area lights shine along the entity’s local -Z axis. Rotate the entity to aim them: with the transform gizmo in the editor, or from a script through entity.rotation, setRotation, setLookAt or lookAt.
  • Shadows. Point and Spot light entities start with an enabled Light Shadow. Disable or remove it for a light that should cast nothing. A Directional Light never casts shadows.
  • Defaults. A prop you leave out takes the light’s default. A light keeps only its own props: anything else you pass is dropped.
  • Ranges. distance is limited to 0–1000 m and decay to 0–10. A Spot light’s angle is limited to 0–π/2 radians and penumbra to 0–1. Values outside these ranges are clamped.
  • Change events. When a setProps call actually changes a prop, the light sends <Light>::change on its entity’s Signal Emitter, with data.changed holding only the props that changed and their new values. A setProps that changes nothing sends nothing.
  • Networking. A light’s props can be replicated as a network trait, such as NetworkTraitType.PointLight. See Multiplayer.
  • Editor gizmos. In the editor, a light shows an icon in its colour, and while selected its aim arrow, cone or range. Play builds none of them.

Even light from everywhere, with no direction and no shadows. It brightens every lit material by the same amount, so it flattens the scene: keep it low and let an Environment or a Hemisphere Light do most of the fill.

Prop Type Default Description
color string '#ffffff' Light colour.
intensity number 1 Brightness.

Event: AmbientLight::change (EventType.AmbientLight.Change), data { changed }.

Parallel light, like the sun: every ray has the same direction, wherever the light stands. It shines along the entity’s local -Z, so only the entity’s rotation matters for its lighting. It never casts shadows, real-time or baked: ground the objects under it with a Contact Shadow.

Prop Type Default Description
color string '#ffffff' Light colour.
intensity number 1 Brightness.

Event: DirectionalLight::change (EventType.DirectionalLight.Change), data { changed }.

A Day Sky has a sunDirection and a Night Sky a moonDirection: read-only, world-space directions toward the sun and the moon (the Sky component also has sunDirection). An entity’s lookAt(point) turns its +Z toward a point, so point the light at a point along that direction, and its -Z then shines away from the sun:

sun.lookAt(sun.position.clone().add(sky.sunDirection));

lookAt works in world space, so this is exact when the light’s parent is not rotated, for example a light directly under the scene root. Aim it again whenever you change the sky’s time or preset. See Environment and sky.

A sky colour from above blending into a ground colour from below. It is the cheapest outdoor fill: it gives shapes a sense of up and down without any shadow cost. Its “up” is the entity’s world up axis: moving the entity changes nothing, rotating it tilts the gradient.

Prop Type Default Description
color string '#ffffff' Sky colour, from above.
groundColor string '#444444' Ground colour, from below.
intensity number 1 Brightness.

Event: HemisphereLight::change (EventType.HemisphereLight.Change), data { changed }.

A light bulb: it shines in every direction from the entity’s position.

Prop Type Default Description
color string '#ffffff' Light colour.
intensity number 1 Brightness.
distance number 0 Maximum range in metres, 0–1000. 0 means no limit.
decay number 2 How quickly the light fades with distance, 0–10. 2 is physically correct.

Event: PointLight::change (EventType.PointLight.Change), data { changed }.

With a distance, the light adds nothing past it, and its shadow reaches no further. Giving small lights a distance keeps their light where it belongs.

A cone of light along the entity’s local -Z.

Prop Type Default Description
color string '#ffffff' Light colour.
intensity number 1 Brightness.
distance number 0 Maximum range in metres, 0–1000. 0 means no limit.
decay number 2 Falloff, 0–10. 2 is physically correct.
angle number Math.PI / 6 Half-angle of the cone in radians, 0 to Math.PI / 2.
penumbra number 0 Softness of the cone’s edge, 0 to 1. A new Spot Light entity starts at 0.1.

Event: SpotLight::change (EventType.SpotLight.Change), data { changed }.

Rolling the entity about its aim rolls the light’s shadow and whatever a Spot Light Projector projects, so a light aimed straight down still has a defined orientation. A Spot Light can also show its beam in the air. See Spot light effects.

A soft, physically based rectangle of light. The rectangle lies in the entity’s local XY plane and emits along local -Z.

Prop Type Default Description
color string '#ffffff' Light colour.
intensity number 1 Brightness. A new Rect Area Light entity starts at 5.
width number 1 Width along local X, in metres.
height number 1 Height along local Y, in metres.

Event: RectAreaLight::change (EventType.RectAreaLight.Change), data { changed }.

Every light component has the same small scripting surface:

Member What it does
props The current props, for reading: lamp.props.intensity. Do not assign to it; use setProps.
setProps(patch) Change one or more props. Only the keys you pass change. Sends the change event when something changed.
entity The light entity. Aim and move the light through it.

Each setProps that changes something re-applies the light, sends its change event and, on a networked light, replicates. That is cheap, but for an effect that changes every frame, such as a flicker, change the light only every few frames.

Tween animates numbers on an object directly, which would skip setProps. To fade a light, tween a plain number with Tween.value and pass each value to setProps, as the examples below show.

Give the light’s entity a Signal Emitter with a signal name, for example lamp, then:

on('lamp', (event) => {
if (event.eventType === EventType.PointLight.Change && 'intensity' in event.data.changed) {
log('lamp intensity is now', event.data.changed.intensity);
}
});

The event groups are EventType.AmbientLight, EventType.DirectionalLight, EventType.HemisphereLight, EventType.PointLight, EventType.SpotLight and EventType.RectAreaLight, each with one action, Change. See the event reference.

LampSetup.jx
#pragma lifecycle(startup)
function startup() {
// A warm, dim desk lamp that reaches 4 metres.
const lamp = findEntity('Desk Lamp').getComponent(PointLightComponent);
lamp.setProps({ color: '#ffd8a8', intensity: 0.5, distance: 4 });
// A narrow stage spot with a soft edge, turned 45 degrees.
const spot = findEntity('Stage Spot');
spot.getComponent(SpotLightComponent).setProps({ angle: Math.PI / 8, penumbra: 0.3 });
spot.rotation.y += DEG_TO_RAD(45);
log('Lamp intensity:', lamp.props.intensity);
}
  • findEntity('Desk Lamp') finds the entity by name. Looking up by name walks the scene, so do it once in startup.
  • setProps only changes the keys you pass: the lamp’s decay stays at its value.
  • The spot is aimed by rotating its entity, not by a prop on the component. DEG_TO_RAD converts degrees to radians.
  • lamp.props.intensity reads the current value back.
FadeLight.jx
#pragma lifecycle(startup)
let light;
let fade = null;
function fadeTo(target, seconds) {
if (fade) fade.stop(); // a new fade replaces the old one
fade = Tween.value(light.props.intensity, target, (value) => {
light.setProps({ intensity: value });
}, { duration: seconds, ease: Ease.InOutSine });
return fade;
}
async function startup() {
// This Behavior entity is a child of the light.
light = self.parent.getComponent(SpotLightComponent);
for (;;) {
if (!(await fadeTo(0, 2))) return; // fade out over 2 s
if (!(await fadeTo(3, 2))) return; // and back in
}
}
  • Tween.value(start, end, onValue, options) animates a plain number and hands each value to onValue, where the script calls setProps.
  • A tween is awaitable: await resolves true when it finished and false when it was stopped. The loop ends cleanly when a fade is stopped, for example when the script is released.
  • The light is never disabled, so no material is recompiled.
  • startup is async so it may await. The engine does not wait for it: update (if declared) would already be running.
TorchFlicker.jx
#pragma lifecycle(startup, update)
const BASE = 1.4; // average intensity
const SPREAD = 0.35; // how far it wanders
const STEP = 0.07; // seconds between changes
let torch;
let wait = 0;
let current = BASE;
function startup() {
torch = self.parent.getComponent(PointLightComponent);
torch.setProps({ color: '#ffa040', distance: 6 });
}
function update(dt) {
wait -= dt;
if (wait > 0) return;
wait = STEP;
// Drift toward a random target instead of jumping, for a smoother flame.
const target = BASE + Random.range(-SPREAD, SPREAD);
current += (target - current) * 0.5;
torch.setProps({ intensity: current });
}
  • update(dt) runs every frame, but the light changes only every STEP seconds, so setProps and its change event run about fourteen times a second rather than at the full frame rate.
  • Random.range(min, max) returns a random number between the two.
  • Moving current halfway toward the target smooths the flicker.
  • distance: 6 stops the torch lighting things far away, which also keeps its shadow small.

The switch entity has a Selectable component and a Signal Emitter with the signal switch/clicked. The Behavior entity is a child of the room’s light.

LightSwitch.jx
#pragma lifecycle(startup)
const ON = 2.5;
let light;
let isOn = true;
function startup() {
light = self.parent.getComponent(PointLightComponent);
on('switch/clicked', (event) => {
if (event.eventType !== EventType.Selectable.Select) return;
findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again
isOn = !isOn;
Tween.value(light.props.intensity, isOn ? ON : 0, (value) => light.setProps({ intensity: value }), {
duration: 0.25,
});
});
}
  • on(signal, callback) calls back each time the switch’s Signal Emitter emits. The emitter forwards every event of the switch, so the script checks for EventType.Selectable.Select.
  • A quarter-second tween makes the switch feel physical without being slow.
  • The light stays enabled. Switching with intensity is the cheap way.

The scene root carries a Day Sky; a Directional Light named Sun sits directly under the root. Pressing T advances the time of day.

SunFollowsSky.jx
#pragma lifecycle(startup, update)
let sky;
let sun;
let sunLight;
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));
// Warm and dim near the horizon, white and bright at noon.
const height = Math.max(0, sky.sunDirection.y);
sunLight.setProps({
intensity: 0.4 + height * 2,
color: height < 0.3 ? '#ffb070' : '#fff4e0',
});
}
function startup() {
sky = scene.findEntity(scene.rootId).getComponent(DaySkyComponent);
sun = findEntity('Sun');
sunLight = sun.getComponent(DirectionalLightComponent);
aimSun();
}
function update() {
if (!Input.keyboard.isKeyReleased(Key.T)) return;
const time = (sky.props.time + 0.1) % 1; // 0 is sunrise, 1 is sunset
sky.setProps({ time });
aimSun();
}
  • scene.findEntity(scene.rootId) is the scene root, where scene-wide components such as the Day Sky live.
  • sky.sunDirection is a read-only world-space vector toward the sun. The script re-aims the light every time it changes the sky’s time.
  • The sun’s colour and intensity follow its height, so mornings look warm and noons look bright.
  • The sky re-bakes only when its props change, so stepping time on a key press is cheap.
  • Disabling a light to switch it off. Enabling and disabling lights recompiles every lit material and can cause a hitch. Fade or set intensity to 0 instead.
  • Tweening props directly. Tween.to(light.props, { intensity: 0 }) changes the numbers without applying them. Use Tween.value and setProps.
  • Calling setProps every frame for effects. It works, but each call re-applies the light, sends an event and, on a networked light, replicates. Throttle effects to a few changes per second.
  • Expecting a Directional Light to cast shadows. It never does. Use a Contact Shadow, or a Spot Light with a Light Shadow over the play area.
  • Moving a Directional Light to change its lighting. Only its rotation matters.
  • Forgetting Rect Area limits. It lights only Standard and Physical materials and casts no shadows.
  • Leaving Point light shadows on for mobile and XR. Each one renders the shadow casters six times a frame.
  • Looking up lights by name in update. Find them once in startup and keep the component.