Skip to content

Mesh Fbx

Updated

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

Type
ent-mesh-fbx
Script class
FBXMeshEntity
Family
Meshes

A Mesh Fbx shows a 3D model loaded from an FBX file, a format many modelling and animation tools export. It works like every other model entity: the file is named by its required FBX loader component (comp-fbx), reached in scripts as FBXComponent, and the model draws with the materials stored in the file. A new Mesh Fbx also starts with a Shadow component that casts shadows but does not receive them.

When the model is in place, the loader sends FBX::ready; when the file cannot load, it sends FBX::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.

FBX files work, but they are larger and slower to parse than GLB. If you can re-export a model as glTF or GLB, use a Mesh Gltf instead; keep a Mesh Fbx for files that only exist as FBX.

What matters most:

  • src (FBX loader): the model 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.
  • getModelRestBounds() and setModelOffset(x, y, z): measure the model as authored, and shift it inside its entity, for example to fix a pivot exported in the wrong place.
  1. Add a Mesh Fbx entity to the scene (see Scenes and hierarchy) and name it.
  2. Set the loader’s src to the .fbx file.
  3. Place, turn and scale the entity. Turn on the Shadow’s receive if other objects should shade it.
  4. Add a Signal Emitter with a signal to follow its loading from a script, and an Animation Clip to play the clips it contains.

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

The model Machine has a Signal Emitter whose signal is machine.

MachineStatus.jx
#pragma lifecycle(startup)
function startup() {
on('machine', ({ eventType, data }) => {
if (eventType === EventType.FBX.Ready) log('The machine has loaded');
if (eventType === EventType.FBX.Error) console.warn('The machine failed: ' + data.message);
});
}
  • Each model format has its own event constants. For an FBX model they are EventType.FBX.Ready and EventType.FBX.Error.
  • data.message explains why the file could not load. The ready event carries no data.

Goal: whatever pivot the file was exported with, centre the model horizontally and rest its lowest point on the entity’s origin.

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

GroundFbx.jx
#pragma lifecycle(startup)
let model;
function fit() {
const bounds = model.getModelRestBounds();
if (!bounds) return;
const centerX = (bounds.min.x + bounds.max.x) / 2;
const centerZ = (bounds.min.z + bounds.max.z) / 2;
model.setModelOffset(-centerX, -bounds.min.y, -centerZ);
log(model.name, 'is', (bounds.max.y - bounds.min.y).toFixed(2), 'm tall');
}
function startup() {
model = self.parent;
if (model.isLoaded) fit();
on('model', ({ eventType }) => {
if (eventType === EventType.FBX.Ready) fit();
});
}
  • getModelRestBounds() returns the model’s box as authored, { min, max } in the entity’s own coordinates, or undefined before it loads.
  • setModelOffset(x, y, z) moves the drawn model inside its entity. The entity’s transform and children do not move, and the authored bounds stay the same, so fit can run again safely.
  • A replacement model starts with no offset, so the script refits on every ready.

Highlight a model with a temporary material

Section titled “Highlight a model with a temporary material”

Goal: while a part is selected, draw the whole model in one glowing colour; when it is deselected, give it back its own materials.

The model Engine has a Selectable and a Signal Emitter whose signal is engine.

HighlightFbx.jx
#pragma lifecycle(startup)
let highlight = null;
function startup() {
const engine = findEntity('Engine');
on('engine', (event) => {
if (event.eventType === EventType.Selectable.Select && !highlight) {
highlight = new StandardMaterialComponent({
color: '#ffd166',
emissive: '#ffb000',
emissiveIntensity: 0.6,
roughness: 0.4,
});
engine.addComponent(highlight);
}
if (event.eventType === EventType.Selectable.Deselect && highlight) {
engine.removeComponent(highlight);
highlight = null;
}
});
}
  • A material component on a model is an override: one material, built once, drawn on every mesh of the model.
  • removeComponent(highlight) takes the override away, and the model’s own materials come back. The file’s materials are never changed.
  • Select and Deselect come from the Selectable, through the same Signal Emitter as the loader’s events.
  • Wait for FBX::ready, or check isLoaded, before reading bounds, nodes or clip names.
  • Scripts use the global FBXComponent, in capitals. ModelLoaderComponent finds the loader of any model format.
  • Listen with the FBX constants: a script that waits for EventType.GLTF.Ready never hears an FBX model.

What a new Mesh Fbx 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.
Fbx comp-fbx Required One Loads an FBX 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
  • FBX::ready
  • FBX::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 Fbx.

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