Images
Updated
An image surface puts a picture in the world: a poster on a wall, a company logo, a 360 panorama around the player, an animated sticker. MIRA has one entity and one component for each image format, and they all share the same idea: a flat surface that shows a texture, sized in metres, with a src that names the file. This page explains each format, the props they share, how they size themselves, how animated formats play, and how a Behavior script swaps, fades and controls them.
Before you start
Section titled “Before you start”This page relies on a few ideas explained elsewhere:
- Entities and components. An image is an entity whose component holds its settings as
props. Scripts readcomponent.propsand change them withcomponent.setProps({ ... }). See Components. - Lifecycle hooks. Scripts declare
startupandupdatewith#pragma lifecycle(...). See Scripting. - Signals. A component’s events reach scripts only through a Signal Emitter on the same entity. The script listens with
on(signal, callback)and comparesevent.eventTypewith anEventTypeconstant. See Events and signals. - Media basics. The overview of Media and text introduces surfaces, sources and sizing.
The image formats
Section titled “The image formats”Each format has its own entity and component. An entity draws only its own component’s picture.
| Entity | Component | Script class | Shows | Use it for |
|---|---|---|---|---|
ent-image |
comp-image |
Image |
A still image: PNG, JPEG, WebP, AVIF where the browser supports it, BMP, a static GIF. | Most pictures. |
ent- |
comp-ktx2 |
KTX2Component |
A GPU-compressed .ktx2 texture (Basis UASTC or ETC1S). |
Large or numerous images, especially on phones and headsets. |
ent-image-svg |
comp-svg |
SVGComponent |
A vector SVG drawn as flat shapes. | Logos, icons and signs that must stay sharp up close. |
ent-image-gif |
comp-gif |
GIFComponent |
An animated GIF. | Short looping animations. |
ent-lottie |
comp-lottie |
Lottie |
A Lottie (Bodymovin) JSON vector animation. | Crisp animated stickers, icons and celebrations. |
Why several formats? Each one trades something. A plain image is the simplest. A KTX2 file stays compressed in GPU memory, so a gallery of a hundred KTX2 posters uses a fraction of the memory the same posters take as JPEGs. An SVG is made of shapes rather than pixels, so it never blurs. GIF and Lottie move.
Every image entity also accepts a Frame and an Attribution, plus Grabbable, Selectable, Billboard, Snappable and the outline components. Image and KTX2 entities also accept a Cache Mesh (see Batching copies).
Surfaces
Section titled “Surfaces”Image, KTX2, GIF and Lottie share one kind of surface:
- Planar. A flat plane in the entity’s local XY plane,
widthbyheightmetres. - Equirectangular. A sphere around the entity, for 360 images.
widthbecomes the sphere’s radius.
The texture is cover-fitted: it fills the plane without stretching, and the side that overflows is cropped. A wide photo on a square plane shows its middle.
Shared props
Section titled “Shared props”These props are the same on comp-image, comp-ktx2, comp-gif and comp-lottie.
| Prop | Type | Default | Description |
|---|---|---|---|
src |
string | none | The image: a URL or an asset from the project. |
width |
number | 1 |
Plane width in metres. For equirectangular, the sphere’s radius. |
height |
number | 1 |
Plane height in metres. Ignored for equirectangular. |
sizing |
'manual' | 'auto' |
none (manual) | auto fits the image’s own proportions inside width × height. Not on Lottie. |
color |
string | '#ffffff' |
A tint multiplied onto the texture. White shows the image unchanged. |
zoom |
number | 1 |
Texture scale. Above 1 zooms in. |
grayscale |
number | 0 |
Desaturation, from 0 (full colour) to 1 (grey). |
opacity |
number | 1 |
Overall opacity, from 0 (invisible) to 1. Fully transparent pixels never hide what is behind them. |
projection |
'planar' | 'equirectangular' |
'planar' |
A flat plane, or a sphere for a 360 image. |
side |
'double' | 'front' | 'back' |
'double' |
Which faces are drawn. Use back to see a 360 image from inside its sphere. |
Changing src loads the new picture. The other props update the surface that is already showing, without reloading.
Intrinsic sizing
Section titled “Intrinsic sizing”Intrinsic sizing means letting the picture decide its own shape. With sizing: 'auto', width and height become bounds: the plane takes the image’s aspect ratio and fits inside them.
- A 16:9 image in 1 × 1 bounds becomes 1 × 0.5625. A portrait image in the same bounds fits by height.
- Without
sizing, or with'manual', the plane is exactlywidth×heightand the image is cropped to fill it. - The fitted size comes from the loaded file. It is never written back into the props or the transform, and the entity’s scale still applies on top.
- Until the file has loaded, the plane uses the bounds.
- A 360 surface ignores sizing. Lottie and SVG do not support it.
Use auto when a script swaps pictures of different shapes onto one frame, so none of them is cropped.
360 panoramas
Section titled “360 panoramas”A 360 panorama (an equirectangular image) is a photo that covers every direction. To surround the player with one:
- Set
projection: 'equirectangular'. - Set
side: 'back', so the inside of the sphere is drawn. - Set
width(the radius) large enough to surround the player, for example50.
comp-image decodes through the browser, so it shows whatever the browser’s image decoder supports. It takes only the shared props. Its props can be shared with other players as a network trait, NetworkTraitType.Image (see Multiplayer).
comp-ktx2 shows a GPU-compressed .ktx2 texture. Every surface that shows the same file shares one copy of the texture. Prefer KTX2 for large or numerous images on mobile and XR: it loads fast and stays compressed in GPU memory. It takes only the shared props.
A framed picture on an easel shows a .ktx2 texture, which stays compressed in GPU memory. The script logs its pixel size when it is ready.
The picture on the easel is a KTX2 texture; the log gives its size. Drag to orbit the camera, scroll to zoom.
comp-svg turns the SVG’s filled paths into flat shapes, so it stays sharp at any distance. It has one prop:
| Prop | Type | Default | Description |
|---|---|---|---|
src |
string | none | The SVG file: a URL or an asset from the project. |
The drawing is normalized to a 1 × 1 box: size it with the entity’s scale, not with width and height. Copies of the same file share one parsed drawing.
GIF and Lottie
Section titled “GIF and Lottie”comp-gif plays an animated GIF and comp-lottie plays a Lottie JSON animation. Both decode their frames off the main thread, so the render loop only uploads finished frames. They add these props to the shared ones:
| Prop | Type | Default | Description |
|---|---|---|---|
autoplay |
boolean | true |
Start playing once the frames are decoded. |
repeat |
'once' | 'repeat' |
'repeat' |
once stops on the last frame; repeat starts again. |
speed |
number | 1 |
Playback speed multiplier, above 0 and at most 16. |
maxFps |
number | 30 |
The most frames per second the animation updates, 1 to 60. The scene still renders at full rate. |
controls |
boolean | true on GIF, false on Lottie |
A play/pause and repeat pill under a planar surface. |
quality |
number | 1 |
Lottie only. Raster resolution scale. Higher is sharper and costs more per frame. |
On a Lottie, changing src or quality reloads the animation; the other props apply to the running one.
Methods
Section titled “Methods”| Method | On | What it does |
|---|---|---|
play() |
GIFComponent, Lottie |
Ask the animation to play. |
pause() |
GIFComponent, Lottie |
Ask it to hold its current frame. |
set |
every image component | Change props. A new src reloads. |
When animations play
Section titled “When animations play”A GIF or Lottie advances only while it can be seen:
- In play, while its entity is active and shown. A hidden entity, or one under a hidden parent, decodes no frames.
- In the editor, only while its entity is selected, so a scene full of animations costs nothing while you author it.
play() and pause() set what you want. The visibility rule still applies on top: a playing GIF on a hidden entity is paused until it is shown again, and sends GIF::pause and GIF::play as that happens.
Batching copies
Section titled “Batching copies”A Cache Mesh component on an Image or KTX2 entity draws it together with every identical copy in a single draw call: same source, size, tint, zoom, grayscale, opacity and side. Use it for repeated static images, such as a row of identical posters or a tiled sign. See LOD and caching.
- Planar surfaces only: a 360 sphere draws on its own.
- Do not cache a surface under a mask.
- Picking, frames and selection still work on each copy.
Events
Section titled “Events”Each component sends these through the Signal Emitters on its entity.
| Event | EventType |
Data | When |
|---|---|---|---|
Image::ready |
Event |
{ width, height }, the texture’s pixels |
The texture is shown. |
Image::error |
Event |
{ message } |
The image cannot load. |
KTX2::ready |
Event |
{ width, height } |
The texture is shown. |
KTX2::error |
Event |
{ message } |
The file cannot load. |
SVG::ready |
Event |
{} |
The drawing is shown. |
SVG::error |
Event |
{ message } |
The SVG cannot load. |
GIF::ready, Lottie::ready |
Event, Event |
{ width, height, animated } |
The animation is decoded. animated is false for a single frame. |
GIF::error, Lottie::error |
Event, Event |
{ message } |
The file cannot load or decode. |
GIF::play, Lottie::play |
Event, Event |
{} |
Playback actually starts. |
GIF::pause, Lottie::pause |
Event, Event |
{} |
Playback actually stops: paused, its entity hidden or disabled, or its scene left. |
GIF::end, Lottie::end |
Event, Event |
{} |
An animation set to play once reaches its last frame. |
Examples
Section titled “Examples”Every example below assumes the entity names given in its introduction. Where a script listens for events, the entity carries a Signal Emitter whose signal is the one the script names.
1. Cycle posters on click
Section titled “1. Cycle posters on click”A poster that shows the next picture each time it is clicked. The scene has an Image entity named Poster with a Selectable (so it can be clicked) and a Signal Emitter with the signal poster. The three paths are the pictures of the live example below, served with these docs under /example-assets/; use your own files or project assets in their place.
#pragma lifecycle(startup)
const POSTERS = [ '/example-assets/lemon-decal.png', '/example-assets/bang.png', '/example-assets/alpha.png',];
let poster;let index = 0;
function startup() { poster = findEntity('Poster').getComponent(ImageComponent); poster.setProps({ src: POSTERS[0], sizing: 'auto', width: 1.6, height: 1.6 });
on('poster', (event) => { if (event.eventType === EventType.Selectable.Select) { findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again index = (index + 1) % POSTERS.length; poster.setProps({ src: POSTERS[index] }); } if (event.eventType === EventType.Image.Ready) { log(`Poster ${index + 1}: ${event.data.width} x ${event.data.height} px`); } if (event.eventType === EventType.Image.Error) { console.warn(`Poster ${index + 1} failed: ${event.data.message}`); } });}A poster on a gallery wall shows the next picture each time it is clicked, fitted inside 1.6 × 1.6 m with sizing auto, and the script logs each image's pixel size from its ready event.
Click the poster to show the next picture; the log gives each one's size. Drag to orbit the camera, scroll to zoom.
findEntity('Poster')runs once, instartup, and the component is kept in a variable. Looking it up every frame would walk the scene each time.sizing: 'auto'with 1.6 × 1.6 bounds lets a landscape and a portrait poster both fit without cropping.- One
on('poster', ...)listener handles the click (Selectable::select) and the image’s ownreadyanderrorevents, because the Signal Emitter forwards every event of the entity. event.dataholds the event’s details: the pixel size forready, amessageforerror.
2. A slideshow that fades between pictures
Section titled “2. A slideshow that fades between pictures”A picture frame that holds each slide for four seconds, fades it out, swaps the file, waits for it to load and fades back in. The Behavior entity is a child of an Image entity that has a Signal Emitter with the signal slides. The slides are the live example’s own pictures, under /example-assets/.
#pragma lifecycle(startup)
const SLIDES = [ '/example-assets/lemon-decal.png', '/example-assets/bang.png', '/example-assets/alpha.png',];const HOLD_MS = 4000;const FADE_SECONDS = 0.6;
let image;
function wait(ms) { return new Promise((done) => delay(done, ms));}
// Resolves on the next ready or error event of this image.function nextLoad() { return new Promise((done) => { const stop = on('slides', (event) => { if (event.eventType === EventType.Image.Ready || event.eventType === EventType.Image.Error) { stop(); done(); } }); });}
function fade(from, to) { return Tween.value(from, to, (value) => image.setProps({ opacity: value }), { duration: FADE_SECONDS, ease: Ease.InOutSine, });}
async function startup() { image = self.parent.getComponent(ImageComponent); let index = 0;
for (;;) { await wait(HOLD_MS); await fade(1, 0);
index = (index + 1) % SLIDES.length; const loaded = nextLoad(); image.setProps({ src: SLIDES[index] }); await loaded;
await fade(0, 1); }}A picture frame holds each slide for four seconds, fades it out, swaps the file, waits for the new image's ready event and fades back in, all from an async loop on the Image entity.
Watch the frame fade from one picture to the next every few seconds. Drag to orbit the camera, scroll to zoom.
- Scripts have no
setTimeout.waitwrapsdelayin a promise so anasynchook canawaitit. nextLoadstarts listening beforesrcchanges, so it cannot miss a fastImage::ready. The functiononreturns stops the listener once it has fired.Tween.valueanimates a plain number and hands each value tosetProps({ opacity }). Opacity updates the live surface without reloading, so this is cheap.- The loop never ends on its own. When the script is released, its tweens and delays stop and the
awaits simply never resume.
3. Confetti when a goal is reached
Section titled “3. Confetti when a goal is reached”A Lottie celebration that appears, plays once and hides itself. The scene has a Lottie entity named Confetti, saved disabled, with a Signal Emitter whose signal is confetti. Another script calls emit('goal/reached').
#pragma lifecycle(startup)
let entity;let lottie;
function startup() { entity = findEntity('Confetti'); lottie = entity.getComponent(LottieComponent); lottie.setProps({ repeat: 'once', autoplay: false, speed: 1.5 });
on('goal/reached', () => { entity.setEnabled(true); lottie.play(); });
on('confetti', (event) => { if (event.eventType === EventType.Lottie.End) entity.setEnabled(false); if (event.eventType === EventType.Lottie.Error) log('Confetti failed:', event.data.message); });}In the live example, a button sends the goal. Its entity has a Selectable and a Signal Emitter whose signal is goal/button:
#pragma lifecycle(startup)
function startup() { on('goal/button', (event) => { if (event.eventType !== EventType.Selectable.Select) return; findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again emit('goal/reached'); });}Press the goal button and a Lottie confetti burst appears, plays once and hides itself: a Lottie entity saved disabled, played by a script when another script emits goal/reached.
Click the amber button to reach the goal. Click again for another burst. Drag to orbit the camera, scroll to zoom.
repeat: 'once'makes the animation stop on its last frame and sendLottie::end.autoplay: falsekeeps it from starting by itself; the script callsplay()when the goal is reached.- A disabled entity decodes nothing, so a hidden celebration costs nothing until it is needed.
setEnabled(false)onLottie::endhides it again, ready for the next goal.
4. A GIF sticker that pauses on click
Section titled “4. A GIF sticker that pauses on click”A GIF that the player can pause and resume by clicking it, with a lower frame rate on phones and headsets. The Behavior entity is a child of a GIF entity with a Selectable and a Signal Emitter whose signal is sticker.
#pragma lifecycle(startup)
#if IS_MOBILE || IS_HEADSETconst FPS = 12;#elseconst FPS = 24;#endif
let gif;let playing;
function startup() { gif = self.parent.getComponent(GIFComponent); gif.setProps({ maxFps: FPS, controls: false }); playing = gif.props.autoplay; // an autoplaying GIF may start before this script does
on('sticker', (event) => { switch (event.eventType) { case EventType.GIF.Ready: if (!event.data.animated) log('This GIF has a single frame'); break; case EventType.GIF.Play: playing = true; break; case EventType.GIF.Pause: playing = false; break; case EventType.Selectable.Select: findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again if (playing) gif.pause(); else gif.play(); break; } });}An animated GIF on an easel that the player pauses and resumes by clicking it; the script follows the GIF’s play and pause events and lowers the frame rate on phones and headsets.
Click the horses to pause them; click again to resume. Drag to orbit the camera, scroll to zoom.
#if IS_MOBILE || IS_HEADSETpicks the frame rate when the script is compiled, so the other branch costs nothing at run time.playingstarts fromautoplay: a GIF that is decoded while the scene loads starts playing before the script’sstartupruns, so the script never sees that firstGIF::play.- After that,
playingfollows theGIF::playandGIF::pauseevents rather than the script’s own calls. The events report what actually happened, including a pause caused by the entity being hidden. controls: falseremoves the built-in pill, since a click on the sticker now does the same job.
5. A logo that pulses
Section titled “5. A logo that pulses”An SVG logo that breathes gently. The Behavior entity is a child of an SVG entity. Because an SVG is sized by its entity’s scale, the script animates the scale.
#pragma lifecycle(startup)
function startup() { const logo = self.parent; logo.scale.set(1.2, 1.2, 1.2); // the 1 x 1 drawing becomes 1.2 m wide
Tween.to(logo.scale, { x: 1.3, y: 1.3, z: 1.3 }, { duration: 1.4, ease: Ease.InOutSine, repeat: Infinity, yoyo: true, });}An SVG logo that breathes gently for ever: an SVG image sized by its entity's scale, tweened by a Behavior script under it.
Watch the logo pulse. Drag to orbit the camera, scroll to zoom.
- An SVG drawing always fits a 1 × 1 box, so
scalesets its size in metres. repeat: Infinitywithyoyo: trueplays the tween forwards and backwards for ever.
Snippets
Section titled “Snippets”1. Switch 360 panoramas
Section titled “1. Switch 360 panoramas”A viewer that surrounds the player with a 360 photo and switches to the next one when the player presses the right arrow key. The scene has an Image entity named Panorama.
#pragma lifecycle(startup, update)
const PLACES = [ { name: 'Lighthouse', src: '/assets/360/lighthouse.jpg' }, { name: 'Canyon', src: '/assets/360/canyon.jpg' }, { name: 'Old library', src: '/assets/360/library.jpg' },];
let panorama;let index = 0;
function startup() { panorama = findEntity('Panorama').getComponent(ImageComponent); panorama.setProps({ src: PLACES[0].src, projection: 'equirectangular', side: 'back', width: 50, // the sphere's radius, in metres }); log(`Now in: ${PLACES[0].name}`);}
function update() { if (!Input.keyboard.isKeyReleased(Key.ArrowRight)) return; index = (index + 1) % PLACES.length; panorama.setProps({ src: PLACES[index].src }); log(`Now in: ${PLACES[index].name}`);}projection,sideandwidthtogether turn the plane into a sphere the player stands inside.isKeyReleasedistruefor one frame after the key goes up, so one press moves one panorama.isKeyPressedwould betrueon every frame the key is held.- Only
srcchanges afterwards, so the sphere keeps its shape.
Performance tips
Section titled “Performance tips”- Prefer KTX2 for many or large images on phones and headsets. It loads faster and uses far less GPU memory.
- Lower
maxFpson animations that do not need it: a sticker at 15 frames a second costs half the uploads of one at 30. - Keep Lottie
qualityat 1 unless the surface fills much of the view. - Hide animations that are out of sight. A disabled or hidden GIF or Lottie decodes nothing.
- Batch identical posters with a Cache Mesh.
- Images are unlit: they show their own colours and are not affected by scene lights, so lighting changes cost nothing for them.
Common mistakes
Section titled “Common mistakes”- Changing
srcevery frame. Each newsrcloads the file again. Change it only when the picture really changes. - Sizing an SVG with
widthandheight. An SVG has onlysrc; size it with the entity’s scale. - Expecting events without a Signal Emitter.
Image::ready,GIF::endand the rest reach scripts only through a Signal Emitter on the image’s entity. - Listening for
readyafter changingsrc. A cached file can be ready almost at once. Start listening first, as the slideshow example does. - A 360 image that looks inside out or invisible. Set
side: 'back'so the inside of the sphere is drawn, and makewidth(the radius) large enough to surround the player. - Calling
play()on a hidden GIF and expecting it to move. Visibility wins: the animation plays only once its entity is enabled and shown. - Using
sizingon Lottie or SVG. Neither supports it.