Text
Updated
Text entities put words in the world: a label on a door, a score above a goal, a player’s name over their head, a glowing title over a stage. MIRA has two kinds. Text draws flat letters that stay sharp at any distance and are cheap to change, so it suits anything that updates often. Text 3D builds solid, bevelled letters that catch the light and cast shadows, so it suits short titles and logos that rarely change. This page explains both, how fonts work, and how scripts update text without wasting work.
Before you start
Section titled “Before you start”- Props and
setProps. All text changes go throughsetProps({ text: ... }). See Components. - Lifecycle hooks and
async. One example awaits a font and a layout insideasync function startup(). See Scripting. - Text in menus. For text inside a UI panel, use a UI Label instead. See UI.
Choosing Text or Text 3D
Section titled “Choosing Text or Text 3D”| Text | Text 3D | |
|---|---|---|
| Entity and component | ent-text, comp-text |
ent-text-3d, comp-text-3d |
| Script class | TextComponent |
Text3 |
| Look | Flat, unlit, optional outline | Solid, lit, bevelled |
| Cost of a change | A fast glyph layout | New geometry for the whole string (a colour change is cheap) |
| Shadows | Not offered | With a Shadow component on the entity |
| Best for | Scores, timers, names, captions, signs | Short titles and logos |
Signed-distance-field text is the technique Text uses: each letter is stored as a map of distances to its outline rather than as pixels, so its edges stay crisp at any size or distance.
| Prop | Type | Default | Description |
|---|---|---|---|
text |
string | 'Text' |
The string drawn. |
fontSize |
number | 1 |
Letter size (the em) in metres. |
color |
string | '#ffffff' |
Fill colour. |
font |
string | 'inter' |
A font family by name: bundled, saved in the project or registered by a script. |
fontWeight |
string | 'normal' |
'light', 'normal', 'medium', 'semi-bold' or 'bold'. A weight the family lacks uses the nearest one it has. |
maxWidth |
number | none | Wrap width in metres. Absent, the text never wraps. |
lineHeight |
number | 1 |
Line spacing, in ems. |
letterSpacing |
number | 0 |
Extra space between letters, in ems. |
textAlign |
'left' | 'right' | 'center' | 'justify' |
'left' |
Alignment of wrapped lines. |
anchorX |
'left' | 'center' | 'right' |
'center' |
Which point of the text sits at the entity’s origin, horizontally. |
anchorY |
'top' | 'top- | 'middle' | 'bottom- | 'bottom' |
'middle' |
The same, vertically. |
outlineWidth |
number or string | 0 |
Outline thickness in metres, or a percentage of the font size such as '5%'. |
outlineColor |
string | '#000000' |
Outline colour. |
Methods
Section titled “Methods”| Method | What it does |
|---|---|
sync() |
Returns a promise that resolves once the latest text, font and layout are drawn. Await it before measuring the text. |
get |
The weights the current font really has, each with a value and a label. |
Layout runs in the background, so a new string or font shows a moment after setProps. That is why sync() exists: measure only after it resolves.
Text 3D
Section titled “Text 3D”| Prop | Type | Default | Description |
|---|---|---|---|
text |
string | 'Text' |
The string extruded. |
font |
string | 'inter' |
A font family by name, as for Text. |
fontWeight |
string | 'normal' |
As for Text. |
size |
number | 1 |
Letter size in metres. |
depth |
number | 0.2 |
Extrusion depth in metres. |
bevelEnabled |
boolean | true |
Round off the front and back edges. |
bevel |
number | 0.03 |
How deep the bevel goes, in metres. |
bevelSize |
number | 0.02 |
How far the bevel reaches from the outline, in metres. |
bevelOffset |
number | 0 |
Bevel offset in metres. |
letterSpacing |
number | 0 |
Extra space between letters, in metres. |
lineHeight |
number | 1 |
Line spacing, as a multiple of the font’s own line height. |
color |
string | '#ffffff' |
Colour of the lit material. |
center |
boolean | false |
Centre the letters on the entity instead of starting at its origin. |
curveSegments sets how many straight segments approximate each curve of a letter, a whole number from 1 to 64. Fewer segments mean fewer triangles.
What a change costs. A color change only recolours the material. Any other change builds new geometry for the whole string. The old letters stay visible until the new ones are ready, so the text never blinks, but the work grows with the length of the string.
Text 3D draws the Latin alphabet with accents, punctuation, currency symbols, arrows and a few marks. Other characters are not drawn.
font takes a family name. Five families come with MIRA:
| Family | Look |
|---|---|
inter |
Neutral sans-serif (the default) |
roboto |
System-style sans-serif |
nunito |
Rounded, friendly |
montserrat |
Geometric display |
jetbrains- |
Monospace |
A project can save its own families, and a script can register one for the session with the Fonts global:
| Call | What it does |
|---|---|
await Fonts. |
Load a .ttf, .otf or .woff file and register it as family, in the weight slot ('normal' by default). Rejects when the file is not a usable font or the name is taken by a bundled family. |
Fonts. |
Remove a registered family; texts using it return to their default. |
Fonts. |
Whether family can be drawn with now. |
Fonts. |
Every family as { name, label, look }, the bundled ones first. |
- A family’s file loads the first time any text uses it, and is then shared. Until it arrives, the text draws nothing, so it never flashes in another font.
- A text that names a family not registered yet draws the default font and switches as soon as the family is registered.
- A registered family lasts for the session and is never saved. Every player runs the same script, so each registers it.
Events
Section titled “Events”Neither component sends events. Scripts change them with setProps and, for Text, wait for layout with sync().
Examples
Section titled “Examples”1. A scoreboard
Section titled “1. A scoreboard”A score that goes up each time any script emits point, and resets on round/start. The scene has a Text entity named Score.
#pragma lifecycle(startup)
let score = 0;let label;
function render() { label.setProps({ text: `Score: ${score}` });}
function startup() { label = findEntity('Score').getComponent(TextComponent); label.setProps({ fontSize: 0.4, font: 'montserrat', fontWeight: 'bold', outlineWidth: '6%' }); render();
on('point', () => { score++; render(); });
on('round/start', () => { score = 0; render(); });}A Text entity shows a score that goes up on every point signal and resets on round/start.
Press Point to score, and New round to reset. Drag to orbit the camera, scroll to zoom.
- The text changes only when the score changes. Text is cheap to update, but it is still wasteful to set the same string every frame.
outlineWidth: '6%'draws an outline 6 percent of the font size wide, which keeps light text readable against a bright sky.- Any script can add a point with
emit('point').
2. A countdown that updates once a second
Section titled “2. A countdown that updates once a second”A round timer. update runs every frame, but the text is only set when the displayed number changes. The Behavior entity is a child of a Text entity.
#pragma lifecycle(startup, update)
const ROUND_SECONDS = 90;
let label;let remaining = ROUND_SECONDS;let shown = -1;
function format(seconds) { const m = Math.floor(seconds / 60); const s = String(seconds % 60).padStart(2, '0'); return `${m}:${s}`;}
function startup() { label = self.parent.getComponent(TextComponent); label.setProps({ font: 'jetbrains-mono', anchorX: 'center' }); on('round/start', () => (remaining = ROUND_SECONDS));}
function update(dt) { if (remaining <= 0) return; remaining = Math.max(0, remaining - dt);
const whole = Math.ceil(remaining); if (whole === shown) return; shown = whole;
label.setProps({ text: format(whole), color: whole <= 10 ? '#f87171' : '#ffffff', }); if (whole === 0) emit('round/over');}A 90-second round timer on a board that redraws its text only once a second and turns red for the last ten: a Text entity updated by a Behavior script under it.
Watch the countdown; the last ten seconds turn red. Drag to orbit the camera, scroll to zoom.
dtis the time since the last frame, in seconds, so the countdown is right whatever the frame rate. It also stops when game time is paused, becausedtis then0.shownremembers the number on screen. The script callssetPropsonce a second instead of sixty times.- A monospace font (
jetbrains-mono) keeps the digits from shifting as they change. - The last ten seconds turn red, and
round/overtells other scripts the time is up.
3. A name tag that faces the player
Section titled “3. A name tag that faces the player”A name tag that floats above a character and always turns towards the camera, with a dark backing plate sized to the text. The Behavior entity is a child of a Text entity named Name Tag, which is itself a child of the character.
#pragma lifecycle(startup, update)
let tag;let label;let plate;const target = new THREE.Vector3();
async function startup() { tag = self.parent; label = tag.getComponent(TextComponent); label.setProps({ text: 'Ada', fontSize: 0.18, anchorX: 'center', anchorY: 'middle' });
// Wait for the layout, then measure it. await label.sync(); const size = new THREE.Box3().setFromObject(tag).getSize(new THREE.Vector3());
plate = new THREE.Mesh( new THREE.PlaneGeometry(size.x + 0.08, size.y + 0.04), new THREE.MeshBasicMaterial({ color: 0x0f172a, transparent: true, opacity: 0.7 }), ); plate.position.z = -0.005; // just behind the letters tag.add(plate);}
function update() { if (!camera) return; camera.getWorldPosition(target); tag.lookAt(target);}A name tag over a character waits for its text layout, measures it to fit a dark backing plate, and turns to the camera every frame.
Drag to orbit the camera: the name tag keeps facing you. Scroll to zoom.
await label.sync()waits until the new string is laid out. Measuring before that would measure the old text, or nothing.THREE.Box3().setFromObject(tag)measures the drawn text; the plate is made slightly larger and placed just behind it.- The Billboard component is not offered on text entities, so the script turns the tag itself with
lookAtevery frame. Turning an entity is cheap. cameraisundefineduntil rendering starts, hence the guard.
4. A title in your own font
Section titled “4. A title in your own font”A Text 3D title that switches to a font the script registers, then glows between two colours. The Behavior entity is a child of a Text 3D entity with a Shadow component. The font file is the one the live example below uses, served with these docs at /example-assets/Branda.ttf.
#pragma lifecycle(startup)
async function startup() { const title = self.parent.getComponent(Text3DComponent);
try { await Fonts.register('branda', '/example-assets/Branda.ttf', { label: 'Branda' }); title.setProps({ font: 'branda' }); } catch (error) { log('Font not available, keeping Inter:', error.message); }
title.setProps({ text: 'GRAND OPENING', size: 0.5, depth: 0.12, center: true, curveSegments: 6, });
const from = new THREE.Color('#f59e0b'); const to = new THREE.Color('#fde68a'); const current = new THREE.Color();
Tween.value(0, 1, (t) => { current.copy(from).lerp(to, t); title.setProps({ color: `#${current.getHexString()}` }); }, { duration: 1.5, ease: Ease.InOutSine, repeat: Infinity, yoyo: true });}A Text 3D title registers the Branda font, switches to it, sets its text and shape in one call, then glows between two ambers by tweening only its colour.
Watch the extruded title glow; orbit to see its depth and its shadow. Drag to orbit the camera, scroll to zoom.
Fonts.registerloads the file and makesbrandaa family every text can use. It rejects when the file is not a usable font; thetry/catchkeeps the default font in that case.- All the shape props are set in one
setPropscall, so the geometry is built once instead of once per prop. center: truecentres the title on its entity, so it is easy to place.- Animating
coloris cheap on Text 3D: it only recolours the material. Animatingtextorsizethe same way would rebuild the geometry every frame.
Snippets
Section titled “Snippets”1. A typewriter caption
Section titled “1. A typewriter caption”A caption that types itself out letter by letter. Any script can call emit('caption', 'Some text'). The Behavior entity is a child of a Text entity.
#pragma lifecycle(startup)
const LETTER_MS = 35;
let label;let cancel = () => {};
function type(full, count = 0) { label.setProps({ text: full.slice(0, count) }); if (count < full.length) { cancel = delay(type, LETTER_MS, full, count + 1); }}
function startup() { label = self.parent.getComponent(TextComponent); label.setProps({ text: '', maxWidth: 2.4, textAlign: 'left', anchorX: 'left', anchorY: 'top' });
on('caption', (message) => { cancel(); type(String(message)); });}delay(fn, ms, ...args)callstypeagain with the next count. Its return value cancels the pending call, so a new caption interrupts the old one cleanly.maxWidth: 2.4wraps long captions at 2.4 m. WithanchorX: 'left'andanchorY: 'top', the text grows right and down from the entity, so it does not shift as it types.onhands the callback whatever was emitted:emit('caption', 'Welcome!')passes the string.
Performance tips
Section titled “Performance tips”- Set text only when it changes. Compare with what is shown, as the countdown does.
- Prefer Text for anything that changes often. Every Text 3D change except colour rebuilds the whole string.
- Keep Text 3D strings short, and lower
curveSegmentsor turn off the bevel for long strings or many copies: both multiply the triangle count. - Batch Text 3D changes into one
setPropscall. - Fonts are loaded once and shared by every text that uses them.
Common mistakes
Section titled “Common mistakes”- Measuring right after
setProps. Layout is asynchronous:await label.sync()first. - Using
fontSizeon Text 3D, orsizeon Text. Text usesfontSize; Text 3D usessize. - Animating Text 3D’s
textorsizeevery frame. Each change builds new geometry. Animate the entity’s scale instead. - Expecting events. Neither text component sends any.
- Characters missing in Text 3D. It draws Latin letters, accents and common symbols only. Use Text for other scripts.
- A custom font not showing. The family name in
fontmust match the name given toFonts.registeror saved in the project exactly. - Adding a Billboard. It is not offered on text entities; turn the entity with
lookAtas the name tag example does.