Skip to content

Mesh Gltf

Updated

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

Type
ent-mesh-gltf
Script class
GLTFMeshEntity
Family
Meshes

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.
  • 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. Removing it puts the file’s materials back.
  • The entity’s members read the loaded model: isLoaded, getAnimationClipNames(), getModelRestBounds(), and setModelOffset(x, y, z) to shift the model inside its entity.
  1. Add a Mesh Gltf entity to the scene (see Scenes and hierarchy).
  2. Set the loader’s src to the model file, and give the entity a clear name, such as Statue.
  3. Place, turn and scale the entity. Turn on the Shadow’s receive if other objects should shade it.
  4. To play its animations, add an Animation Clip. To make it clickable, add a Selectable.
  5. To follow its loading from a script, add a Signal Emitter with a signal.

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.

StatueStatus.jx
#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);
}
});
}
  • isLoaded is a getter, true once 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 an eventType and data, unpacked here in the parameter list.
  • EventType.GLTF.Ready and EventType.GLTF.Error are the constants for 'GLTF::ready' and 'GLTF::error'. Comparing with constants protects you from typos.
  • ready is sent again after every reload, so the listener also reports each change of src.

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.

WaveOnClick.jx
#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 to layer(name).
  • animation.layer('Wave') is the clip named Wave, or undefined before 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.

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.

ChairVariants.jx
#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 loading flag 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::ready or check isLoaded before reading the model: bounds, nodes and clip names are empty until it is in place.
  • Scripts use the global GLTFComponent, in capitals. ModelLoaderComponent finds the loader whatever the model’s format.
  • To show a different file, change src. An entity accepts only its own format’s loader.

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-transform 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

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

  • Shadow::change
  • GLTF::ready
  • GLTF::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 Gltf.

GLTFMeshEntity.jx
#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);
}