Skip to content

Text

Updated

Crisp, cheap flat text label in the world.

Type
ent-text
Script class
TextEntity
Family
Media

A Text entity draws flat words in the world: a sign, a name tag, a caption, a score or a timer. Its letters are drawn with a technique that keeps edges sharp at any distance, and changing the string only re-lays out the glyphs, so it is cheap enough to update every second. The text is unlit and looks the same in the editor and in play.

Pick a sibling when it fits better: Text 3D for short titles and logos with depth that catch light and cast shadows, a UI Label for text inside a UI panel, or an Image SVG for a fixed logo drawing.

The props that matter most, on its Text component (TextComponent in scripts):

  • text: the string drawn.
  • fontSize: the letter size in metres. The default, 1, is large; signs usually use 0.1 to 0.3.
  • color, outlineWidth and outlineColor: the fill colour and an outline that keeps text readable on busy backgrounds. outlineWidth takes metres or a percentage of the font size, such as '5%'.
  • font and fontWeight: a font family by name, such as inter, roboto, nunito, montserrat or jetbrains-mono, and its weight, such as bold.
  • maxWidth and textAlign: wrap lines at a width in metres, and align them left, right, center or justify.
  • anchorX and anchorY: which point of the text sits at the entity’s position, such as center and middle.
  1. Add a Text entity to the scene and name it, such as Score.
  2. Type its text, and set fontSize to a size that reads well from where the player stands.
  3. Pick a font, fontWeight and color. Add an outline if the background is busy.
  4. For a paragraph, set maxWidth so lines wrap, and textAlign to align them.
  5. Use anchorX and anchorY to decide which part of the text the entity’s position marks.

Show the score and update it each time a point is scored.

ScoreLabel.jx
#pragma lifecycle(startup)
// Scene: a Text entity "Score". Other scripts emit 'point'.
let score = 0;
let label;
function startup() {
label = findEntity('Score').getComponent(TextComponent);
label.setProps({ text: 'Score: 0' });
on('point', () => {
score += 1;
label.setProps({ text: `Score: ${score}` });
});
}

How it works:

  • getComponent(TextComponent) reaches the text’s settings.
  • setProps({ text }) replaces the string; only the glyph layout runs again, so frequent updates are cheap.

Count down from 60 seconds and turn the text red for the last ten.

Countdown.jx
#pragma lifecycle(startup, update)
// Scene: a Text entity "Timer".
let label;
let remaining = 60;
let shown = -1;
function startup() {
label = findEntity('Timer').getComponent(TextComponent);
}
function update(dt) {
if (remaining <= 0) return;
remaining = Math.max(0, remaining - dt);
const seconds = Math.ceil(remaining);
if (seconds === shown) return;
shown = seconds;
label.setProps({ text: `${seconds}`, color: seconds <= 10 ? '#ef4444' : '#ffffff' });
if (seconds === 0) emit('timer/done');
}

How it works:

  • update(dt) receives dt, the seconds since the last frame; subtracting it each frame counts down in real time.
  • The script calls setProps only when the whole-second value changes, not every frame.
  • At zero it raises timer/done for any other script to react.

Set a long name on a tag and size a background panel to the text once it is laid out.

NameTag.jx
#pragma lifecycle(startup)
// Scene: a Text entity "Name" with anchorX: center, and a box mesh
// "Name Background" behind it.
async function startup() {
const nameEntity = findEntity('Name');
const text = nameEntity.getComponent(TextComponent);
text.setProps({ text: 'Dr. Alexandra Moreau', fontSize: 0.12, outlineWidth: '4%' });
await text.sync();
const size = new THREE.Box3().setFromObject(nameEntity).getSize(new THREE.Vector3());
findEntity('Name Background').setScale(size.x + 0.1, size.y + 0.06, 0.01);
}

How it works:

  • Glyph layout runs in the background, so a new string shows a moment later. await text.sync() waits for it inside the async startup hook.
  • After sync(), the entity’s bounds match the new text; THREE.Box3 measures them.
  • The background is scaled to the text plus a margin, so any name fits.
  • Measure text only after await component.sync(); reading its bounds right after setProps gives the old size.
  • A font loads the first time a text uses it, and the text draws nothing until then, so it never flashes in another font. A family the project does not have draws the default font and warns once.
  • Text entities do not accept a Billboard. To keep a label facing the player, turn it from a script.

What a new Text is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.

Component Type Role Per entity What it does
Layer comp-layer Required One Puts the entity on a render/raycast layer so cameras and rays can include or skip it.
Lock comp-lock Required One Editor-only flag that stops the entity from being selected or moved.
Enable comp-enable Required One Turns the entity and its children on or off: hides them and stops their behavior.
Transform comp-transform Required One Position, rotation and scale of the entity, with optional editor-only per-axis locks.
Text comp-text Required One Crisp, cheap flat text label in the world.

You can also add 15 optional components, such as Animation Graph, Animation Timeline, Attribution, Enable On Signal, Follow Target, Grabbable.

The settings of a new entity, as the runtime creates it. Change them in the Properties panel of XR Creator, or from a script with getComponent(…).setProps({ … }).

Prop Default Range
text "Text"
fontSize 1 0.001 ≤ x ≤ 10000
color "#ffffff"
font "inter"
lineHeight 1 -10 ≤ x ≤ 10
letterSpacing 0 -10 ≤ x ≤ 10
textAlign "left"
anchorX "center"
anchorY "middle"
outlineWidth 0 0 ≤ x ≤ 100
outlineColor "#000000"

With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.

It reports the events every entity has: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.

Run it in the browser, read its scripts, and download it as an XR Creator project.

Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My Text.

TextEntity.jx
#pragma lifecycle(startup)
function startup() {
// Find the entity by the name it has in the Hierarchy.
const entity = findEntity('My Text');
if (!(entity instanceof TextEntity)) return;
// Its Text component holds the settings listed under Props.
const component = entity.getComponent(TextComponent);
log('fontSize is', component.props.fontSize);
component.setProps({ fontSize: 1.5 });
}