Mesh Gltf
Updated
Lets the entity’s meshes cast and/or receive shadows from lights.
- Type
ent-mesh-gltf- Script class
GLTFMeshEntity- Family
- Meshes
Overview
Section titled “Overview”A glTF mesh shows a 3D model loaded from a glTF or GLB file: a character, a vehicle, a piece of furniture, a whole building. glTF is the recommended model format for the web. It loads fastest, carries animations, skinned characters and morph targets, and accepts Draco and Meshopt compression, which make files much smaller.
The file is named by the entity’s required glTF loader component (comp-gltf), reached in scripts as GLTFComponent. The model draws with the materials stored in its file. A new glTF mesh also starts with a Shadow component that casts shadows but does not receive them.
Loading takes a moment. When the model is in place, the loader sends GLTF::ready; when the file cannot load, it sends GLTF::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 the current model in place.
Use a glTF mesh for any model you can export as glTF or GLB. Pick a Mesh Fbx, Mesh Obj, Mesh Collada or Mesh Usdz only when the file comes in that format, and a Primitive Mesh for simple built-in shapes.
What matters most:
src(glTF loader): the model file, as a project asset reference (ASSET-<uuid>) or a URL. Changing it reloads the model; setting it to''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. Removing it puts the file’s materials back.
- The entity’s members read the loaded model:
isLoaded,getAnimationClipNames(),getModelRestBounds(), andsetModelOffset(x, y, z)to shift the model inside its entity.
Set it up
Section titled “Set it up”- Add a Mesh Gltf entity to the scene (see Scenes and hierarchy).
- Set the loader’s
srcto the model file, and give the entity a clear name, such asStatue. - Place, turn and scale the entity. Turn on the Shadow’s
receiveif other objects should shade it. - To play its animations, add an Animation Clip. To make it clickable, add a Selectable.
- To follow its loading from a script, add a Signal Emitter with a signal.
Examples
Section titled “Examples”Report when the model has loaded
Section titled “Report when the model has loaded”Goal: log when the model is ready, and warn with the reason when it fails.
The model Statue has a Signal Emitter whose signal is statue.
#pragma lifecycle(startup)
function startup() { const statue = findEntity('Statue'); if (statue.isLoaded) log('The statue was already loaded');
on('statue', ({ eventType, data }) => { if (eventType === EventType.GLTF.Ready) { log('The statue has loaded'); } else if (eventType === EventType.GLTF.Error) { console.warn('The statue failed to load: ' + data.message); } });}isLoadedis a getter,trueonce the model is in place. It covers a model that finished loading before the script started.on('statue', …)receives every event the Signal Emitter forwards. Each has aneventTypeanddata, unpacked here in the parameter list.EventType.GLTF.ReadyandEventType.GLTF.Errorare the constants for'GLTF::ready'and'GLTF::error'. Comparing with constants protects you from typos.readyis sent again after every reload, so the listener also reports each change ofsrc.
Play an animation when the model is clicked
Section titled “Play an animation when the model is clicked”Goal: a character waves once each time the player clicks it.
The model Guide has an Animation Clip with autoplay turned off, a Selectable and a Signal Emitter whose signal is guide. Its file holds a clip named Wave.
#pragma lifecycle(startup)
function startup() { const guide = findEntity('Guide'); const animation = guide.getComponent(AnimationClipComponent);
on('guide', (event) => { if (event.eventType === EventType.GLTF.Ready) { log('Clips:', guide.getAnimationClipNames().join(', ')); } if (event.eventType !== EventType.Selectable.Select) return; findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again
const wave = animation.layer('Wave'); if (!wave) return; wave.play({ repeat: 'once', fade: 0.3 }); });}getAnimationClipNames()lists the clips stored in the file: the names to pass tolayer(name).animation.layer('Wave')is the clip namedWave, orundefinedbefore the model loads or when the file has no such clip.play({ repeat: 'once', fade: 0.3 })blends the wave in over 0.3 seconds and plays it once.- One signal carries both the loader’s and the Selectable’s events; the script tells them apart by
eventType.
Cycle through product variants
Section titled “Cycle through product variants”Goal: a product viewer that shows the next variant of a chair each time it is clicked, without ever leaving an empty spot.
The model Chair has a Selectable and a Signal Emitter whose signal is chair.
#pragma lifecycle(startup)
const VARIANTS = [ 'https://cdn.example.com/models/chair-oak.glb', 'https://cdn.example.com/models/chair-walnut.glb', 'https://cdn.example.com/models/chair-white.glb',];
let index = 0;let loading = false;
function startup() { const loader = findEntity('Chair').getComponent(GLTFComponent);
on('chair', (event) => { switch (event.eventType) { case EventType.Selectable.Select: findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again if (loading) return; loading = true; index = (index + 1) % VARIANTS.length; loader.setProps({ src: VARIANTS[index] }); break; case EventType.GLTF.Ready: loading = false; log('Showing variant', index + 1, 'of', VARIANTS.length); break; case EventType.GLTF.Error: loading = false; console.warn('Variant failed, keeping the previous one: ' + event.data.message); break; } });}loader.setProps({ src })starts the reload. The current chair stays on screen until the new one is ready.- The
loadingflag ignores clicks while a variant downloads, so quick clicks do not queue several loads. - On error the previous chair stays visible, so the script only logs the reason.
- Every copy of the same file shares one parsed asset, so going back to a variant already seen is quick.
- Wait for
GLTF::readyor checkisLoadedbefore reading the model: bounds, nodes and clip names are empty until it is in place. - Scripts use the global
GLTFComponent, in capitals.ModelLoaderComponentfinds the loader whatever the model’s format. - To show a different file, change
src. An entity accepts only its own format’s loader.
Components
Section titled “Components”What a new Mesh Gltf 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. |
| Gltf | comp-gltf |
Required | One | Loads a glTF/GLB 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::changeGLTF::readyGLTF::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.
An AI Proximity box round the guide shows her dialog only while the camera is near, and a Behavior script hides the "walk up" hint on the proximity's enter event.
Hold W to walk the camera inside the blue ring: the hint goes and the guide's dialog appears. Hold S to walk back out. The greeting itself is sent to the AI service, which this preview does not have, so the panel shows a short error instead of a reply.
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 Gltf.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My Mesh Gltf'); if (!(entity instanceof GLTFMeshEntity)) return;
// Its Shadow component holds the settings listed under Props. const component = entity.getComponent(ShadowComponent); log(component.props);}