Mesh Usdz
Updated
Lets the entity’s meshes cast and/or receive shadows from lights.
- Type
ent-mesh-usdz- Script class
USDZMeshEntity- Family
- Meshes
Overview
Section titled “Overview”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.usdzfile, as a project asset reference (ASSET-<uuid>) or a URL. Changing it reloads the model;''removes it.castandreceive(Shadow): whether the model casts and receives shadows.- A material component, if you add one, overrides every material of the model with one look.
isLoadedandgetModelRestBounds(): whether the model is in place, and its size as authored.
Set it up
Section titled “Set it up”- Add a Mesh Usdz entity to the scene (see Scenes and hierarchy) and name it.
- Set the loader’s
srcto the.usdzfile. - Place, turn and scale the entity.
- Add a Signal Emitter with a signal to follow its loading from a script.
Examples
Section titled “Examples”Report when the model has loaded
Section titled “Report when the model has loaded”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.
#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.ReadyandEventType.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.
Pop the product in once it has loaded
Section titled “Pop the product in once it has loaded”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.
#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.OutBackovershoots slightly before settling at full size, which reads as a friendly pop.readyis sent again after each reload, so a newsrcpops in too. A running tween is stopped first.
Switch colourways with the number keys
Section titled “Switch colourways with the number keys”Goal: a product configurator. Pressing 1, 2 or 3 loads the matching colourway.
The model Sneaker has a Signal Emitter whose signal is sneaker.
#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.Digit1toKey.Digit3are the number keys along the top of the keyboard.loader.props.srcreads 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
loadingflag ignores key presses while a file downloads.
- Wait for
USDZ::ready, or checkisLoaded, before reading bounds or nodes. - Scripts use the global
USDZComponent, in capitals.ModelLoaderComponentfinds the loader of any model format. - A model file is parsed once and shared by every entity that shows it.
Components
Section titled “Components”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- |
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 |
Events
Section titled “Events”With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.
Shadow::changeUSDZ::readyUSDZ::error
Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.
Live example
Section titled “Live example”Run it in the browser, read its scripts, and download it as an XR Creator project.
A textured USDZ (.usdz) model, the format of AR Quick Look, on a turntable: the script centres it and scales it to 0.8 m once it loads.
The file is about 4 MB, so give it a moment, then watch it turn. Drag to orbit the camera, scroll to zoom.
From a script
Section titled “From a script”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.
#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);}