Skip to content

Mesh Usdz

Updated

Lets the entity’s meshes cast and/or receive shadows from lights.

Type
ent-mesh-usdz
Script class
USDZMeshEntity
Family
Meshes

A Mesh Usdz shows a model loaded from a USDZ file, the packaged format used by many mobile AR tools. If your products, furniture or exhibits were prepared for phone AR viewers, a Mesh Usdz lets you place the same files in a MIRA world.

The file is named by the entity’s required USDZ loader component (comp-usdz), reached in scripts as USDZComponent. The model draws with the materials stored in the file. A new Mesh Usdz starts with a Shadow component that casts shadows but does not receive them.

When the model is in place, the loader sends USDZ::ready; when the file cannot load, it sends USDZ::error with a message. Both reach scripts through a Signal Emitter on the entity. Changing the source keeps the current model on screen until the new one is ready, and a failed replacement leaves it in place.

Use a Mesh Usdz when the model comes as .usdz. For files you export yourself, a Mesh Gltf is the recommended format for the web.

What matters most:

  • src (USDZ loader): the .usdz file, as a project asset reference (ASSET-<uuid>) or a URL. Changing it reloads the model; '' removes it.
  • cast and receive (Shadow): whether the model casts and receives shadows.
  • A material component, if you add one, overrides every material of the model with one look.
  • isLoaded and getModelRestBounds(): whether the model is in place, and its size as authored.
  1. Add a Mesh Usdz entity to the scene (see Scenes and hierarchy) and name it.
  2. Set the loader’s src to the .usdz file.
  3. Place, turn and scale the entity.
  4. Add a Signal Emitter with a signal to follow its loading from a script.

Goal: log when the USDZ model is ready, and warn with the reason when it fails.

The model Lamp has a Signal Emitter whose signal is lamp.

LampStatus.jx
#pragma lifecycle(startup)
function startup() {
on('lamp', ({ eventType, data }) => {
if (eventType === EventType.USDZ.Ready) log('The lamp has loaded');
if (eventType === EventType.USDZ.Error) console.warn('The lamp failed: ' + data.message);
});
}
  • The USDZ loader sends EventType.USDZ.Ready and EventType.USDZ.Error.
  • A file that is not USDZ content, such as a binary glTF file given by mistake, is reported as an error with a message.

Goal: instead of appearing abruptly, the product grows in with a little bounce as soon as it is ready.

The Behavior entity is a child of the model, which has a Signal Emitter whose signal is product.

PopIn.jx
#pragma lifecycle(startup)
let product;
let popping = null;
function popIn() {
if (popping) popping.stop();
product.setScale(0.001, 0.001, 0.001);
popping = Tween.value(0.001, 1, (s) => product.setScale(s, s, s), {
duration: 0.6,
ease: Ease.OutBack,
});
}
function startup() {
product = self.parent;
if (product.isLoaded) popIn();
on('product', ({ eventType }) => {
if (eventType === EventType.USDZ.Ready) popIn();
});
}
  • setScale(x, y, z) scales the entity. The product starts at a thousandth of its size, almost invisible.
  • Ease.OutBack overshoots slightly before settling at full size, which reads as a friendly pop.
  • ready is sent again after each reload, so a new src pops in too. A running tween is stopped first.

Goal: a product configurator. Pressing 1, 2 or 3 loads the matching colourway.

The model Sneaker has a Signal Emitter whose signal is sneaker.

Colourways.jx
#pragma lifecycle(startup, update)
const COLOURWAYS = {
[Key.Digit1]: 'https://cdn.example.com/products/sneaker-white.usdz',
[Key.Digit2]: 'https://cdn.example.com/products/sneaker-black.usdz',
[Key.Digit3]: 'https://cdn.example.com/products/sneaker-red.usdz',
};
let loader;
let loading = false;
function startup() {
loader = findEntity('Sneaker').getComponent(USDZComponent);
on('sneaker', ({ eventType, data }) => {
if (eventType === EventType.USDZ.Ready) loading = false;
if (eventType === EventType.USDZ.Error) {
loading = false;
console.warn('Colourway failed: ' + data.message);
}
});
}
function update() {
if (loading) return;
for (const key of [Key.Digit1, Key.Digit2, Key.Digit3]) {
if (!Input.keyboard.isKeyReleased(key)) continue;
if (loader.props.src === COLOURWAYS[key]) return;
loading = true;
loader.setProps({ src: COLOURWAYS[key] });
return;
}
}
  • Key.Digit1 to Key.Digit3 are the number keys along the top of the keyboard.
  • loader.props.src reads the current file, so pressing the key of the colourway already shown does nothing.
  • The current sneaker stays on screen until the new file is ready; if it fails, the old one stays and the reason is logged.
  • The loading flag ignores key presses while a file downloads.
  • Wait for USDZ::ready, or check isLoaded, before reading bounds or nodes.
  • Scripts use the global USDZComponent, in capitals. ModelLoaderComponent finds the loader of any model format.
  • A model file is parsed once and shared by every entity that shows it.

What a new Mesh Usdz 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.
Shadow comp-shadow Starts with One Lets the entity’s meshes cast and/or receive shadows from lights.
Transform comp-transform Required One Position, rotation and scale of the entity, with optional editor-only per-axis locks.
Usdz comp-usdz Required One Loads a USDZ model.

You can also add 70 optional components, such as Animation Clip, Animation Graph, Animation Timeline, Attribution, Basic Material, Billboard.

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
cast true
receive false

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

  • Shadow::change
  • USDZ::ready
  • USDZ::error

Like every entity, it also reports: 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 Mesh Usdz.

USDZMeshEntity.jx
#pragma lifecycle(startup)
function startup() {
// Find the entity by the name it has in the Hierarchy.
const entity = findEntity('My Mesh Usdz');
if (!(entity instanceof USDZMeshEntity)) return;
// Its Shadow component holds the settings listed under Props.
const component = entity.getComponent(ShadowComponent);
log(component.props);
}