Skip to content

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.

  • Props and setProps. All text changes go through setProps({ text: ... }). See Components.
  • Lifecycle hooks and async. One example awaits a font and a layout inside async function startup(). See Scripting.
  • Text in menus. For text inside a UI panel, use a UI Label instead. See UI.
Text Text 3D
Entity and component ent-text, comp-text ent-text-3d, comp-text-3d
Script class TextComponent Text3DComponent
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-baseline' | 'middle' | 'bottom-baseline' | '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.
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.
getFontWeightChoices() 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.

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.
bevelThickness 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-mono 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.register(family, url, { weight?, label? }) 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.unregister(family) Remove a registered family; texts using it return to their default.
Fonts.has(family) Whether family can be drawn with now.
Fonts.families 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.

Neither component sends events. Scripts change them with setProps and, for Text, wait for layout with sync().

A score that goes up each time any script emits point, and resets on round/start. The scene has a Text entity named Score.

Scoreboard.jx
#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();
});
}
  • 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').

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.

Countdown.jx
#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');
}
  • dt is 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, because dt is then 0.
  • shown remembers the number on screen. The script calls setProps once 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/over tells other scripts the time is up.

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.

NameTag.jx
#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);
}
  • 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 lookAt every frame. Turning an entity is cheap.
  • camera is undefined until rendering starts, hence the guard.

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.

Title.jx
#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 });
}
  • Fonts.register loads the file and makes branda a family every text can use. It rejects when the file is not a usable font; the try/catch keeps the default font in that case.
  • All the shape props are set in one setProps call, so the geometry is built once instead of once per prop.
  • center: true centres the title on its entity, so it is easy to place.
  • Animating color is cheap on Text 3D: it only recolours the material. Animating text or size the same way would rebuild the geometry every frame.

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.

Typewriter.jx
#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) calls type again with the next count. Its return value cancels the pending call, so a new caption interrupts the old one cleanly.
  • maxWidth: 2.4 wraps long captions at 2.4 m. With anchorX: 'left' and anchorY: 'top', the text grows right and down from the entity, so it does not shift as it types.
  • on hands the callback whatever was emitted: emit('caption', 'Welcome!') passes the string.
  • 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 curveSegments or turn off the bevel for long strings or many copies: both multiply the triangle count.
  • Batch Text 3D changes into one setProps call.
  • Fonts are loaded once and shared by every text that uses them.
  • Measuring right after setProps. Layout is asynchronous: await label.sync() first.
  • Using fontSize on Text 3D, or size on Text. Text uses fontSize; Text 3D uses size.
  • Animating Text 3D’s text or size every 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 font must match the name given to Fonts.register or saved in the project exactly.
  • Adding a Billboard. It is not offered on text entities; turn the entity with lookAt as the name tag example does.