Skip to content

Lighting

Updated

Light decides how bright the world is, where its shadows fall, and how every surface in it looks: whether a room feels warm or cold, whether an object seems to stand on the floor or float above it, and whether a scene reads clearly on a phone and in a headset. MIRA lights a scene in layers. Each layer is a component, so a scene carries only the layers it needs, and a layer you leave out costs nothing. Scripts reach every layer through the same small set of tools: find the entity, get its component, and change its props with setProps.

This section explains each layer, what it costs, and how to drive it from a Behavior script: dimming lamps, flickering torches, day-and-night cycles, fog that rolls in, spot lights that project images, and light that is baked in the editor.

  • Real-time light. A light the renderer evaluates every frame, for every pixel of every lit material. MIRA has six: Ambient, Directional, Hemisphere, Point, Spot and Rect Area. Each is an entity (for example ent-point-light) with a matching light component (for example PointLightComponent). The entity’s transform places and aims it.
  • Intensity. How bright a light is. It is the cheapest thing to change on a light, so it is also the right way to switch a light off and on during play.
  • Shadow map. A depth picture a light renders of the objects that block it, used to darken what lies behind them. Only Point and Spot lights cast real-time shadows, through a Light Shadow component they start with. Objects opt in through a Shadow component.
  • Contact shadow. A soft shadow drawn under objects as if lit from straight above. It needs no light, so it is the cheapest way to make objects look grounded, especially on mobile and in AR.
  • Image-based lighting. Light and reflections taken from one panoramic HDR image (an HDRI) by the Environment component. It gives shiny and rough materials believable surroundings.
  • Sky and background. What the camera sees behind everything: a solid colour, an HDRI, or a procedural sky (Sky, Day Sky, Night Sky). Skies are visual only: they do not light the scene by themselves.
  • Baked light. Indirect light and soft shadows computed once in the editor and saved into Light Probe volumes. In play they light a room with its own bounced light for almost no cost.
  • Fog. Distance fog that fades objects toward a colour, to soften the horizon or hide where the world ends.
  • Change event. Each light sends <Light>::change (for example PointLight::change, EventType.PointLight.Change in a script) when a setProps call actually changes one of its props, with data.changed holding the props that changed. A script receives it through a Signal Emitter on the light’s entity.
Layer Components What it gives
Direct lights Ambient, Directional, Hemisphere, Point, Spot, Rect Area Real-time light from a source, evaluated per pixel on every lit material. See Lights.
Shadows Shadow, Light Shadow, Contact Shadow, Matte Shadow material Real-time shadow maps from lights, soft contact shadows without a light, and invisible shadow catchers. See Shadows.
Image-based lighting Environment Ambient light and roughness-correct reflections from one HDRI, optionally shown as the sky. See Environment and sky.
Sky and background Background, Sky, Day Sky, Night Sky What the camera sees behind everything.
Baked light Light Probe, Light Probe Solver Indirect light and soft shadows baked in the editor. See Light probes.
Atmosphere Fog Distance fog that fades objects toward a colour.
Light effects Spot Light Beam, Spot Light Projector A visible beam in the air, and an image projected through a spot light. See Spot light effects.
  • Lights are entities: ent-ambient-light, ent-directional-light, ent-hemisphere-light, ent-point-light, ent-spot-light and ent-rect-area-light. Their transform places and aims them. Directional, Spot and Rect Area lights shine along the entity’s local -Z axis.
  • Scene-wide parts go on the scene root: Background (every root starts with one), Environment, Sky, Day Sky, Night Sky, Fog and the Light Probe Solver. A script reaches the root with scene.findEntity(scene.rootId).
  • Light probe volumes are ent-light-probe entities: their transform is the box they light.
  • Shadow flags go on the objects: a Shadow component decides whether an entity’s meshes cast and receive real-time shadows; a Contact Shadow does the same for contact shadows.

When several scenes are loaded, the newest scene’s root decides the background, environment, fog and sky. An older scene’s skies stay dormant, costing nothing, until the scenes above it leave.

Lighting is where most of a scene’s GPU time goes. The costs that matter most:

Part Desktop Mobile and headsets
Each real-time light Per-pixel work on every lit material. The same work on a slower GPU. In XR the scene is drawn once per eye, so every pixel cost counts twice.
A Spot shadow One extra render of the shadow casters per frame. Keep mapSize at 1024 or less and cover only the play area with range.
A Point light shadow Six extra renders per frame, one per cube face. Avoid. Use light probes and contact shadows instead.
Environment HDRI One prefiltered map, built once. A 2k HDRI makes a 512 cube, a 4k one a 1024 cube with four times the memory. Use 1k or 2k HDRIs.
Skies One cube fetch per pixel; the atmosphere re-bakes only when its props change. quality: 'auto' picks the low tier.
Light probes Nothing per frame on the CPU; a few texture reads per lit pixel inside a volume. The cheapest way to get soft indirect light and shadows.
Contact Shadow Receiving: one small render of the casters and two blur passes per frame while on screen. Use low or medium quality.
Fog A few instructions per pixel. Cheap. Its far can also hide a short draw distance.

Every lighting component works per eye or in world space, so both eyes of a headset always agree.

Outdoors: a Day Sky on the root for the visible sky; an Environment with an outdoor HDRI (background: false, since the Day Sky is the backdrop) for reflections and ambient light; one Directional Light as the sun, aimed along the Day Sky’s sunDirection; a receiving Contact Shadow on the ground; and Fog in a colour close to the horizon.

Indoors: an Environment with a low intensity for reflections and a Light Probe Solver on the root; a Light Probe volume around each room, a little larger than its walls; Point, Spot and Rect Area lights for the room, with a Light Shadow kept on Spot lights and removed from Point lights; a bake in the editor; and a Contact Shadow on moving objects, since baked shadows never move.

This example has a sun, a soft ambient fill, objects whose Shadow components make them cast and receive, and a teal point light that a short script moves around them.

You will make a desk lamp that fades on and off when the player presses L, and that flickers very slightly while it is on, like an old bulb.

  1. Add a Point Light entity to the scene and name it Desk Lamp. Place it above a table or an object so you can see its light.

  2. Add a Behavior entity as a child of Desk Lamp. Its script reaches the lamp with self.parent.

  3. Replace the Behavior’s code with this script:

    DeskLamp.jx
    #pragma lifecycle(startup, update)
    const ON_INTENSITY = 2; // brightness when the lamp is on
    const FADE_SECONDS = 0.6; // how long a fade takes
    const FLICKER = 0.08; // how much the bulb wavers while on
    let lamp; // the PointLightComponent
    let level = 1; // 0 = off, 1 = on, values between while fading
    let isOn = true;
    let fade = null; // the running fade tween, if any
    let wait = 0; // seconds until the next flicker step
    function startup() {
    lamp = self.parent.getComponent(PointLightComponent);
    lamp.setProps({ intensity: ON_INTENSITY });
    }
    function update(dt) {
    // Toggle on the frame the L key is released.
    if (Input.keyboard.isKeyReleased(Key.L)) {
    isOn = !isOn;
    if (fade) fade.stop();
    fade = Tween.value(level, isOn ? 1 : 0, (value) => { level = value; }, {
    duration: FADE_SECONDS,
    ease: Ease.InOutSine,
    });
    }
    // Change the light only every few frames: cheap and still lively.
    wait -= dt;
    if (wait > 0) return;
    wait = 0.08;
    const wobble = isOn ? Random.range(-FLICKER, FLICKER) : 0;
    lamp.setProps({ intensity: Math.max(0, ON_INTENSITY * level + wobble) });
    }
  4. Press Play. Press L to fade the lamp out, and again to fade it back in.

What each part does:

  • self.parent.getComponent(PointLightComponent) finds the light component on the lamp entity. PointLightComponent is a script global, like every component class.
  • Input.keyboard.isKeyReleased(Key.L) is true for exactly one frame per release, so one press toggles once. (isKeyPressed would be true on every frame the key is held.)
  • Tween.value(start, end, onValue, options) animates a plain number. The script stores it in level instead of writing the light directly, because a light’s props only change through setProps.
  • The flicker writes intensity at most about twelve times a second. Each setProps that changes something re-applies the light and sends its change event, so effects that run every frame should be throttled like this.
  • The lamp is never disabled: it fades to intensity: 0, which avoids recompiling every lit material.
If you want … Use … Read
A bulb, torch or candle that shines all around Point Light (consider removing its Light Shadow) Lights
The sun or the moon: parallel light from one direction Directional Light, aimed with the sky’s sunDirection or moonDirection Lights
A cone of light: stage, flashlight, lamp shade, with the cheapest real-time shadows Spot Light Lights
A soft glowing rectangle: a window, a panel, a screen Rect Area Light (Standard and Physical materials only) Lights
Cheap fill so nothing is fully black Hemisphere Light (cheap outdoor fill, sky colour above and ground colour below), or a low Ambient Light (a flat fill) Lights
Objects that look grounded on mobile or in AR Contact Shadow Shadows
Sharp shadows that move Spot Light with a Light Shadow, objects with a Shadow Shadows
Realistic reflections and ambient light Environment with an HDRI Environment and sky
A sky with a time of day, or a night sky Day Sky, Night Sky Environment and sky
Baked bounced light and soft shadows in rooms at almost no cost Light Probe volumes and a Light Probe Solver Light probes
A visible beam, or an image projected by a lamp Spot Light Beam, Spot Light Projector Spot light effects

Six of the lights are real-time lights, each an entity whose light component holds its colour and brightness while the entity’s transform places and aims it. The seventh, the Light Probe, holds light baked in the editor. Each type has a page of its components, props, events and a first script:

EntityTypeWhat it is
Ambient Lightent-ambient-lightEven light from everywhere, with no direction or shadows.
Directional Lightent-directional-lightSun-like parallel light aimed by the entity's rotation.
Hemisphere Lightent-hemisphere-lightSky color from above, ground color from below.
Light Probeent-light-probeLights what is inside its box with probes, and shadows, baked by the Light Probe Solver.
Point Lightent-point-lightA light bulb that shines in every direction.
Rect Area Lightent-rect-area-lightA soft glowing rectangle, like a window or a panel light.
Spot Lightent-spot-lightA cone of light aimed by the entity's rotation.

Every lighting component class and every light entity class is a script global. Scripts also use Tween and Ease to fade values, Random for flicker, and scene to reach the scene root.

GlobalWhat it is
AmbientLightComponent
DirectionalLightComponentOwns a THREE.DirectionalLight child. A directional light shines from its position toward its target; we parent the target one unit along the entity's local -Z, so the light aims "forward" and the entity's rotation controls the direction (drive it with the transform gizmo). It never casts shadows: no shadow map, and no part in the Light Probe bake.
HemisphereLightComponentOwns a THREE.HemisphereLight child driven entirely by props.
PointLightComponentOwns a THREE.PointLight child driven entirely by props; its shadows come from a Light Shadow.
SpotLightComponentOwns a THREE.SpotLight child. Like the directional light, the cone points at a target parented one unit along the entity's local -Z, so the entity's rotation aims the spot (drive it with the transform gizmo). Its shadows come from a Light Shadow.
RectAreaLightComponentOwns a THREE.RectAreaLight child — a soft, physically-based area emitter (a glowing rectangle) driven entirely by props. The rectangle lies in the entity's local XY plane and emits along local -Z, so the entity's rotation aims it (drive it with the transform gizmo), matching DirectionalLightComponent's "forward is -Z" convention. RectAreaLight casts no shadows and only affects MeshStandard/MeshPhysical materials.
LightShadowComponentThe shadows of the light beside it (a Spot or Point Light): its real-time shadow map, also baked into the Light Probes that receive them (receiveBaked). range is how far they reach from the light, unless it has a distance of its own. Without it, or disabled, the light casts none and is not baked. The light applies it, and follows its changes without being rebuilt. Sends LightShadow::change on the entity's Signal Emitter when a setting changes.
ShadowComponentWhether the entity's meshes cast and receive shadows. Without it they do neither. While it is on, the entity's material may override either (see MaterialComponent.castShadows). Sends Shadow::change on the entity's Signal Emitter when a setting changes.
ContactShadowComponentSoft shadows as if lit from straight above: they ground objects without any light, and alongside the lights' own shadows. Like the Shadow component, it casts, receives, or both.
EnvironmentComponentImage-based lighting from one HDRI, on the scene root. The HDRI is prefiltered once (PMREM) into the scene's environment, which lights every PBR material with roughness-correct reflections, and the sky is drawn from that same prefiltered map, sharp or blurred: one GPU copy serves both. rotation turns lighting and sky together, so reflections always match the sky.
DaySkyComponentA physically scattered daytime sky with a time of day, and optional drifting clouds, sharing one sun so sky and clouds always agree.
NightSkyComponentA night sky: gradient dome with a Milky Way, nebulae, a phased moon with its halo, fixed twinkling stars and shooting stars.
FogComponentScene-wide distance fog. Lives on the scene root entity and is applied through {@link module :scene/SceneFragment.applyEnvironment} — mirroring BackgroundComponent — driving scene.fog. It also self-applies on attach if already parented, and restores the previous fog on detach.
LightProbeComponentA box of baked irradiance probes that lights what is inside it: their diffuse light replaces the environment's (the Environment's HDRI) there, so a room is lit by the room; reflections stay the environment's. The box is a 1 m cube placed, sized and turned by the entity's transform (a new volume's turns about y only: its Transform locks x and z); it bakes and lights the axis-aligned box around it, with probes 0.25 to 4 m apart by LightProbeProps.lightingQuality.
LightProbeSolverComponentBakes the scene's lighting into its Light Probe volumes: bake renders the scene around every probe and saves what each one sees, and, for the volume that draws the scene's baked shadows, how much of the shadow-casting lights and of the sky reaches each point of its box (soft shadows and ambient occlusion): every light lights the probes, but only one with an enabled Light Shadow casts a baked shadow. Lives on the scene root. Editor only, on request (a host's "Bake Lighting" button; reset, "Reset Lighting", clears it all): it does nothing in play, where the volume uses its saved data. Saved before as comp-light-probe-generator.
PointLightEntityA point light; the point-light component owns the THREE.PointLight.
SpotLightEntityA spot light; the spot-light component owns the THREE.SpotLight.
TweenAnimate any object's numbers: Tween.to(mesh.position, { x: 5 }, { duration: 1 }), from, value, stop; awaitable.
EaseThe easings, for { ease: Ease.OutCubic }: Linear, and In, Out and InOut of each curve (Quad, Cubic, Quart, Sine, Expo, Back, Elastic, Bounce). In starts slow, Out ends slow, InOut does both; Back overshoots a little, Elastic springs, Bounce bounces. Any function of progress 0..1 works as well.
RandomThe shared generator scripts use as Random: every method, with examples, is on RandomGenerator. Frozen, so no script can change it for the others.
ComponentTypeWhat it does
Ambient Lightcomp-ambient-lightEven light from everywhere, with no direction or shadows.
Backgroundcomp-backgroundSolid background color of the scene.
Contact Shadowcomp-contact-shadowSoft shadow from straight above that grounds objects without any light.
Day Skycomp-day-skyPhysically scattered daytime sky with time of day and clouds.
Directional Lightcomp-directional-lightSun-like parallel light aimed by the entity's rotation.
Environmentcomp-environmentLights the scene with an HDRI and shows it as the sky.
Fogcomp-fogDistance fog over the whole scene.
Hemisphere Lightcomp-hemisphere-lightSky color from above, ground color from below.
Light Probecomp-light-probeLights what is inside its box with probes, and shadows, baked by the Light Probe Solver.
Light Probe Solvercomp-light-probe-solverBakes the scene's light probes and shadows in the editor.
Light Shadowcomp-light-shadowAdds real-time and baked shadows to the light beside it.
Night Skycomp-night-skyNight sky with stars, Milky Way, moon and shooting stars.
Point Lightcomp-point-lightA light bulb that shines in every direction.
Rect Area Lightcomp-rect-area-lightA soft glowing rectangle, like a window or a panel light.
Shadowcomp-shadowLets the entity's meshes cast and/or receive shadows from lights.
Skycomp-skyCheap procedural atmosphere sky.
Spot Lightcomp-spot-lightA cone of light aimed by the entity's rotation.
Spot Light Beamcomp-spot-light-beamMakes a spot light's cone visible in the air.
Spot Light Projectorcomp-spot-light-projectorProjects an image through a spot light, like a gobo.
Topic Description
Lights The six real-time lights, their props and change events, aiming, and changing lights from scripts.
Shadows Shadow, Light Shadow and Contact Shadow: which objects cast and receive, tuning, and cost.
Environment and sky Environment HDRIs, Background, Fog, and the Sky, Day Sky and Night Sky.
Light probes Baked indirect light and soft shadows: volumes, the solver, and reading bake state.
Spot light effects A visible beam for a Spot Light, and images projected through it.