Shadows
Updated
Shadows tie objects to the ground and give a scene depth. Without them, objects seem to float, and distances are hard to judge, especially in a headset. MIRA has three kinds of shadow, which you can combine: real-time shadow maps cast by lights, contact shadows that need no light, and baked shadows stored in light probes. This page covers the components behind them and how to choose the cheapest one that looks right.
Before you start
Section titled “Before you start”- Shadows come from lights: read Lights first. Only Point and Spot lights cast real-time shadows.
- Components are reached with
entity.getComponent(Class)and changed withsetProps; an optional component can be switched withsetEnabled. See Components. - Baked shadows are covered in Light probes.
The three kinds
Section titled “The three kinds”| Kind | Components | Needs a light | Moves in play | Cost in play |
|---|---|---|---|---|
| Real-time shadow maps | Shadow on objects, Light Shadow on a light | Yes | Yes | One render of the casters per frame per light (six for a point light). |
| Contact shadows | Contact Shadow | No | Yes | A small render and two blur passes per receiving surface on screen. |
| Baked shadows | Light Shadow on a light, Light Probe with receiveBaked |
Yes, at bake time | No | One texture read per lit pixel inside the volume. |
A shadow map is a depth image a light renders of everything that blocks it. When the scene is drawn, each pixel checks that image to know whether it is lit. It is accurate and follows moving objects, but every shadow-casting light renders the casters again each frame.
A contact shadow is drawn from straight above, as if by a soft overhead light, onto a receiving surface such as a floor. It needs no light at all, which makes it ideal for unlit scenes, AR and mobile.
Shadow
Section titled “Shadow”ShadowComponent (comp-shadow) decides whether an entity’s meshes cast and receive shadows, for real-time shadow maps and light probe bakes alike. Without a Shadow component, or with it disabled, an entity’s meshes neither cast nor receive.
| Prop | Type | Default | Description |
|---|---|---|---|
cast |
boolean | true |
The entity’s meshes cast shadows. |
receive |
boolean | true |
The entity’s meshes receive shadows. |
Event: Shadow::change (EventType.Shadow.Change), data { changed } with the props that changed.
Geometry entities start with a Shadow component set for their usual role:
| Entity | cast |
receive |
|---|---|---|
| Mesh, Primitive Mesh, Collider Shape | true |
false |
| Floor | false |
true |
| Kit Piece | true |
true |
A material can override the component where it has to. A Shader Material never receives shadows; an additive one (such as a hologram) does not cast either, since it gives light rather than blocking it, and one whose shader says shadows=false never casts. A Matte Shadow material always receives and never casts.
Light Shadow
Section titled “Light Shadow”LightShadowComponent (comp-light-shadow) gives the light beside it shadows. It is offered only on the lights that can cast them: Spot and Point. An enabled Light Shadow gives the light a real-time shadow map, and bakes its shadows into the light probes that receive them. Point and Spot light entities start with one, enabled: disable or remove it for a light that casts nothing.
A Directional Light never casts shadows, real-time or baked, and offers no Light Shadow. Ground objects under a sun with a Contact Shadow instead.
| Prop | Type | Default | Description |
|---|---|---|---|
mapSize |
number | 256 |
Square shadow map resolution in pixels, a whole number from 1 to 2048. Common choices: 256, 512, 1024, 2048. |
bias |
number | 0 |
Depth offset against shadow acne and detached shadows. |
radius |
number | 1 |
Blur radius of the shadow edges, 0–16 texels. |
range |
number | 0 |
How far the shadows reach, in metres, 0–1000. 0 is automatic (500 m, or the light’s own distance when it has one). |
Event: LightShadow::change (EventType.LightShadow.Change), data { changed } with the props that changed.
Tuning
Section titled “Tuning”- Acne (stripes on lit surfaces): set a small negative
bias, such as-0.0004. - Blocky edges: raise
mapSizeor shrinkrange. A shadow map spreads its pixels over its whole range, so covering only the play area gives sharper shadows for free. - Soft edges: raise
radius. Shadow maps are filtered (percentage-closer filtering). - Changing
mapSizefrees the old shadow map; the next frame allocates the new one. The light follows every other Light Shadow change without being rebuilt. - A Spot light with a zero
anglecasts no shadow until it has a usable cone again.
Contact Shadow
Section titled “Contact Shadow”ContactShadowComponent (comp-contact-shadow) draws soft shadows as if lit from straight above. They do not come from a light: they ground objects in unlit scenes and in AR, and they show alongside the lights’ own shadows. It is offered on entities with geometry: meshes, primitives, floors, kit pieces, terrains and 3D text.
Like the Shadow component, it casts, receives or both:
- Casting (the default) marks the entity’s own meshes as contact casters. It costs nothing per frame. Only meshes with a casting Contact Shadow cast: the Shadow component has no say here.
- Receiving draws a shadow on the entity’s own mesh (a floor, a table, a terrain) under the casters standing on it, up to
heightabove it. Closer is darker. It never shadows the entity’s own meshes. On most meshes it is a flat plane on the mesh’s top; a terrain draws it on its own surface, over its hills.
| Prop | Type | Default | Description |
|---|---|---|---|
cast |
boolean | true |
The entity’s meshes cast onto the contact shadows of others. |
receive |
boolean | false |
The entity’s mesh draws a contact shadow of what stands on it. |
height |
number | 1 |
How high above the surface things still cast, in metres, 0.001–1000. |
opacity |
number | 0.8 |
How dark the shadow is right at the surface, 0 to 1. |
blur |
number | 1.5 |
Softness in texels, 0 to 4. 0 skips the blur passes. |
quality |
'low' | 'medium' | 'high' |
'medium' |
Texture size: 128, 256 or 512 texels across. |
range |
number | 0 |
0–1000 metres around the viewer the texture covers, on a large surface. 0 covers the whole surface. |
It sends no events.
Cost. Casting costs nothing per frame. Receiving costs, once per frame while the surface is on screen, one small render of the casters above it and two blur passes, at its quality. Off screen, hidden or disabled, it costs nothing.
Large surfaces. The texture covers the whole surface, so on a very large one each texel is large, and small objects cast faint smudges or nothing. Give such a surface a range: the texture then covers only that many metres around the viewer, moving with it a whole texel at a time so shadows never swim, at the same cost. A 200 m terrain at quality: 'high' has 39 cm texels; with range: 30, 6 cm. Beyond the range there is no contact shadow.
XR. The texture is in world space, so both eyes share it, drawn once per frame.
Matte Shadow
Section titled “Matte Shadow”The Matte Shadow material (comp-matte-shadow-material) is a shadow catcher: an invisible surface that shows only the shadows other objects cast on it. Put it on a floor to ground a virtual object over a camera feed in AR, a photo, a video or a sky.
- It always receives and never casts, but its entity must have an enabled Shadow component, and some light must cast a shadow onto it.
- Its
colorandopacityare the shadow’s: black at 0.5 by default.
See Graphics for materials.
XR shadows
Section titled “XR shadows”In AR, the real surfaces the device detects can receive the shadows of virtual objects, through the receiveShadow prop of the XR Planes and XR Meshes components. See XR.
Quality against cost
Section titled “Quality against cost”| Goal | Cheapest choice |
|---|---|
| Ground objects on mobile or in AR | A receiving Contact Shadow on the floor, quality: 'low' or 'medium'. |
| Ground characters on a large terrain | A receiving Contact Shadow on the terrain with a range of 20 to 40 m. |
| Shadows outdoors | A receiving Contact Shadow on the ground, or one Spot Light with a Light Shadow over the play area. |
| Moving objects in a baked room | Keep the bake, and add a Contact Shadow, or a real-time Light Shadow on one light. |
| Point light shadows | Avoid: six scene renders a frame, even when the shadow is also baked. |
Examples
Section titled “Examples”1. Configure shadows when the scene starts
Section titled “1. Configure shadows when the scene starts”#pragma lifecycle(startup)
function startup() { // A crisp shadow from the lamp that covers only the room. const lampShadow = findEntity('Lamp').getComponent(LightShadowComponent); lampShadow.setEnabled(true); lampShadow.setProps({ mapSize: 1024, range: 12, bias: -0.0004 });
// The crate now also receives shadows from other objects. findEntity('Crate').getComponent(ShadowComponent).setProps({ receive: true });
// The floor draws a cheap, soft contact shadow under what stands on it. findEntity('Floor').getComponent(ContactShadowComponent).setProps({ receive: true, quality: 'low' });}At startup a script switches on a lamp's shadow map, lets a crate receive shadows and gives the floor a soft contact shadow: Light Shadow, Shadow and Contact Shadow components set from a Behavior script.
Orbit around to see the lamp's shadows and the soft contact shadows under each object. Drag to orbit the camera, scroll to zoom.
setEnabled(true)switches the Light Shadow on. It is optional on the light, so it can be switched; the light’s own component cannot.range: 12concentrates the 1024 × 1024 map on a 12 m area, which keeps edges sharp.bias: -0.0004removes the striped “acne” pattern that a shadow map can draw on lit surfaces.- The floor’s Contact Shadow needs
receive: true; casting is already on by default for the objects above it.
2. Tune a shadow live and watch its changes
Section titled “2. Tune a shadow live and watch its changes”Give the lamp a Signal Emitter with the signal lamp. While testing, [ and ] halve and double the shadow map size, and B toggles a bias.
#pragma lifecycle(startup, update)
let shadow;
function startup() { shadow = findEntity('Lamp').getComponent(LightShadowComponent); on('lamp', (event) => { if (event.eventType === EventType.LightShadow.Change) log('shadow changed', event.data.changed); });}
function update() { const size = shadow.props.mapSize; if (Input.keyboard.isKeyReleased(Key.BracketLeft)) shadow.setProps({ mapSize: Math.max(128, size / 2) }); if (Input.keyboard.isKeyReleased(Key.BracketRight)) shadow.setProps({ mapSize: Math.min(2048, size * 2) }); if (Input.keyboard.isKeyReleased(Key.B)) { shadow.setProps({ bias: shadow.props.bias === 0 ? -0.0004 : 0 }); }}The [ and ] keys halve and double a lamp's shadow map size and B toggles a bias; a listener logs every change the Light Shadow reports.
Click the scene, then press [ to blur the shadows with a smaller map, ] to sharpen them, and B to toggle the bias. Drag to orbit the camera, scroll to zoom.
- Each key press calls
setPropsonce. The Light Shadow then sendsLightShadow::changewith only what changed, and the listener logs it. isKeyReleasedis true for one frame per release, so holding a key changes the size once.- The size stays within 128 to 2048; the component accepts 1 to 2048.
Snippets
Section titled “Snippets”1. Pick shadow quality by device
Section titled “1. Pick shadow quality by device”#pragma lifecycle(startup)
#if IS_MOBILE || IS_HEADSETconst SPOT_MAP = 512;const POINT_SHADOWS = false;const CONTACT_QUALITY = 'low';#elseconst SPOT_MAP = 1024;const POINT_SHADOWS = true;const CONTACT_QUALITY = 'medium';#endif
function startup() { for (const entity of scene.entities.values()) { const shadow = entity.getComponent(LightShadowComponent); if (shadow) { if (entity.getComponent(PointLightComponent)) { shadow.setEnabled(POINT_SHADOWS); // six renders a frame: off on mobile } else { shadow.setProps({ mapSize: SPOT_MAP }); } } const contact = entity.getComponent(ContactShadowComponent); if (contact && contact.props.receive) contact.setProps({ quality: CONTACT_QUALITY }); }}#if IS_MOBILE || IS_HEADSETis a device flag: each device compiles only its own branch, so the check costs nothing at run time.scene.entitiesis aMapof every entity in the scene. Walking it once instartupis fine; do not do it every frame.- Point light shadows are switched off on phones and headsets, where their six renders a frame hurt most. This happens once, at startup, so the material recompile happens before play really begins.
- Only receiving Contact Shadows cost anything per frame, so only their
qualityis lowered.
2. Stop small objects casting
Section titled “2. Stop small objects casting”The scene has a group entity named Clutter holding cups, books and other small props. Their shadows barely show, but each one is rendered again for every shadow-casting light.
#pragma lifecycle(startup)
function startup() { const clutter = findEntity('Clutter'); let changed = 0;
for (const child of clutter.children) { if (!isEntity(child)) continue; // skip plain objects const shadow = child.getComponent(ShadowComponent); if (shadow && shadow.props.cast) { shadow.setProps({ cast: false }); changed++; } } log(`${changed} small objects no longer cast shadows`);}clutter.childrenlists what sits under the group.isEntityfilters out plain objects, which have no components.setProps({ cast: false })removes the object from every shadow map. It still receives shadows ifreceiveis on.- For objects that should still look grounded, give the floor a receiving Contact Shadow: contact casters cost nothing per frame.
3. Keep contact shadows sharp on a big terrain
Section titled “3. Keep contact shadows sharp on a big terrain”#pragma lifecycle(startup)
function startup() { const terrain = findEntity('Terrain'); let contact = terrain.getComponent(ContactShadowComponent); if (!contact) { contact = new ContactShadowComponent({ receive: true, cast: false }); terrain.addComponent(contact); } // Cover only 30 m around the viewer: about 6 cm texels at 'high'. contact.setProps({ receive: true, cast: false, quality: 'high', range: 30, height: 2, opacity: 0.7 });}- If the terrain has no Contact Shadow yet, the script adds one with
addComponent(new ContactShadowComponent(...)). cast: falsekeeps the terrain from casting onto other receivers;receive: truedraws the shadows of characters and objects on it.range: 30makes the texture follow the viewer over a 30 m window instead of stretching over the whole terrain, so small objects still cast visible shadows.height: 2lets things up to 2 m above the ground cast, enough for a jumping character.
Common mistakes
Section titled “Common mistakes”- An object casts no shadow. It needs an enabled Shadow component with
cast: true, and a Point or Spot light with an enabled Light Shadow must reach it. - A floor shows no shadows. Floors start with
receive: true, but other entities (meshes, primitives) start withreceive: false. Set it on the surface that should show the shadow. - Adding a Light Shadow to a Directional Light. It does nothing. Use a Contact Shadow or a Spot Light.
- A Matte Shadow floor stays empty. Its entity needs an enabled Shadow component, and some light must cast onto it.
- Toggling shadows often. Turning a light’s shadow on or off recompiles lit materials. Decide once, at startup.
- One huge shadow
range. The map spreads over the whole range and looks blocky. Cover only the play area. - A contact shadow that never appears on a big terrain. Each texel is too large. Set a
range.