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.
Before you start
Section titled “Before you start”- A light is an entity with a component. You reach the component with
entity.getComponent(PointLightComponent)and change it withsetProps. See Components. - Scripts run in hooks such as
startupandupdate(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 comparesevent.eventTypewith anEventTypeconstant. - Read the Lighting overview first for how lights fit with shadows, skies and baked light.
The six lights
Section titled “The six lights”| Light | Entity | Component (script global) | Shadows | Use it for |
|---|---|---|---|---|
| Ambient | ent- |
Ambient |
No | A flat fill so nothing is fully black. |
| Directional | ent- |
Directional |
No | The sun or the moon: parallel light from one direction. |
| Hemisphere | ent- |
Hemisphere |
No | Cheap outdoor ambient: a sky colour from above, a ground colour from below. |
| Point | ent- |
Point |
Yes | A bulb that shines in every direction. |
| Spot | ent- |
Spot |
Yes | A cone of light: stage lights, torches, lamps. |
| Rect Area | ent- |
Rect |
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()).
What every light shares
Section titled “What every light shares”- 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) untilsetEnabled(true). You can also set itsintensityto0, 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,setLookAtorlookAt. - 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.
distanceis limited to 0–1000 m anddecayto 0–10. A Spot light’sangleis limited to 0–π/2 radians andpenumbrato 0–1. Values outside these ranges are clamped. - Change events. When a
setPropscall actually changes a prop, the light sends<Light>::changeon its entity’s Signal Emitter, withdata.changedholding only the props that changed and their new values. AsetPropsthat 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.
Ambient Light
Section titled “Ambient Light”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 }.
Directional Light
Section titled “Directional Light”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 }.
Aiming at the sun or the moon
Section titled “Aiming at the sun or the moon”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.
Hemisphere Light
Section titled “Hemisphere Light”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 }.
One Hemisphere Light lights everything: tops take its sky colour, undersides its ground colour, with no direction and no shadows. The script steps it through noon, sunset and night.
Watch the tops and undersides of the shapes change colour with each mood. Drag to orbit the camera, scroll to zoom.
Point Light
Section titled “Point Light”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.
Spot Light
Section titled “Spot Light”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.
Rect Area Light
Section titled “Rect Area Light”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 }.
A photo-studio softbox: a Rect Area Light with the runtime Lobby demo's warm settings throws soft, even light from a whole rectangle, and the script cycles its colour and size.
Watch the soft light on the floor and the shapes change as the panel changes colour and size. Drag to orbit the camera, scroll to zoom.
Changing lights from a script
Section titled “Changing lights from a script”Every light component has the same small scripting surface:
| Member | What it does |
|---|---|
props |
The current props, for reading: lamp.. Do not assign to it; use setProps. |
set |
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.
Listening to changes
Section titled “Listening to changes”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.
Examples
Section titled “Examples”1. Set up a lamp at startup
Section titled “1. Set up a lamp at startup”#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);}At startup a script makes a point light a warm, dim desk lamp, narrows a stage spot to a soft-edged beam, and turns the spot 45 degrees.
See the warm lamp on the desk and the narrow spot turned onto the floor. Drag to orbit the camera, scroll to zoom.
findEntity('Desk Lamp')finds the entity by name. Looking up by name walks the scene, so do it once instartup.setPropsonly changes the keys you pass: the lamp’sdecaystays at its value.- The spot is aimed by rotating its entity, not by a prop on the component.
DEG_TO_RADconverts degrees to radians. lamp.props.intensityreads the current value back.
2. Fade a light in and out
Section titled “2. Fade a light in and out”#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 }}A script under a spot light tweens its intensity down to 0 and back up to 3, two seconds each way, for as long as it runs.
Watch the spot fade out and back in. Drag to orbit the camera, scroll to zoom.
Tween.value(start, end, onValue, options)animates a plain number and hands each value toonValue, where the script callssetProps.- A tween is awaitable:
awaitresolvestruewhen it finished andfalsewhen 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.
startupisasyncso it mayawait. The engine does not wait for it:update(if declared) would already be running.
3. A torch that flickers
Section titled “3. A torch that flickers”#pragma lifecycle(startup, update)
const BASE = 1.4; // average intensityconst SPREAD = 0.35; // how far it wandersconst 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 });}A wall torch whose orange point light flickers, drifting toward a new random brightness fourteen times a second: a Point Light changed by a Behavior script under it.
Watch the light wander over the wall and floor. Drag to orbit the camera, scroll to zoom.
update(dt)runs every frame, but the light changes only everySTEPseconds, sosetPropsand 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
currenthalfway toward the target smooths the flicker. distance: 6stops the torch lighting things far away, which also keeps its shadow small.
4. A light switch the player clicks
Section titled “4. A light switch the player clicks”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.
#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, }); });}Clicking the switch tweens the room light's intensity between 2.5 and 0 in a quarter of a second.
Click the switch on the wall to turn the light off and on. Drag elsewhere to orbit the camera, scroll to zoom.
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 forEventType.Selectable.Select.- A quarter-second tween makes the switch feel physical without being slow.
- The light stays enabled. Switching with
intensityis the cheap way.
5. A sun that follows the time of day
Section titled “5. A sun that follows the time of day”The scene root carries a Day Sky; a Directional Light named Sun sits directly under the root. Pressing T advances the time of day.
#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();}Pressing T moves the root's Day Sky on by a tenth of the day; the script turns the Sun directional light to the sky's sun direction and warms and dims it near the horizon.
Click the scene, then press T to advance the time of day and watch the shadows and light follow the sun. Drag to orbit the camera, scroll to zoom.
scene.findEntity(scene.rootId)is the scene root, where scene-wide components such as the Day Sky live.sky.sunDirectionis a read-only world-space vector toward the sun. The script re-aims the light every time it changes the sky’stime.- 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
timeon a key press is cheap.
Common mistakes
Section titled “Common mistakes”- Disabling a light to switch it off. Enabling and disabling lights recompiles every lit material and can cause a hitch. Fade or set
intensityto0instead. - Tweening
propsdirectly.Tween.to(light.props, { intensity: 0 })changes the numbers without applying them. UseTween.valueandsetProps. - Calling
setPropsevery 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 instartupand keep the component.