Skip to content

Mesh Obj

Updated

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

Type
ent-mesh-obj
Script class
OBJMeshEntity
Family
Meshes

A Mesh Obj shows a model loaded from a Wavefront OBJ file. OBJ is one of the oldest and simplest 3D formats: it holds static geometry only, with no animation or skinning, which makes it a good fit for scanned objects, architectural parts and props from older libraries.

The file is named by the entity’s required OBJ loader component (comp-obj), reached in scripts as OBJComponent. A new Mesh Obj starts with a Shadow component that casts shadows but does not receive them.

An OBJ file describes its materials in separate MTL files, called material libraries. The loader loads the libraries the OBJ file declares: keep each MTL file where the OBJ expects it, and each texture where its MTL expects it. An OBJ without a library draws with plain default materials. A missing library or texture shows a warning, and the geometry still draws with the colours and materials available.

When the model is in place, the loader sends OBJ::ready; when the file cannot load, it sends OBJ::error with a message. Both reach scripts through a Signal Emitter on the entity.

Use a Mesh Obj when the model only exists as OBJ. For animated models, or wherever you can re-export, prefer a Mesh Gltf.

What matters most:

  • src (OBJ loader): the .obj 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: the simplest way to style an OBJ that has no library.
  1. Add a Mesh Obj entity to the scene (see Scenes and hierarchy) and name it.
  2. Set the loader’s src to the .obj file. Keep its MTL files and textures at the paths the files name.
  3. Place, turn and scale the entity.
  4. If the model comes in untextured, add a material component to give it a look.
  5. Add a Signal Emitter with a signal to follow its loading from a script.

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

The model Scan has a Signal Emitter whose signal is scan.

ScanStatus.jx
#pragma lifecycle(startup)
function startup() {
on('scan', ({ eventType, data }) => {
if (eventType === EventType.OBJ.Ready) log('The scan has loaded');
if (eventType === EventType.OBJ.Error) console.warn('The scan failed: ' + data.message);
});
}
  • The OBJ loader sends EventType.OBJ.Ready and EventType.OBJ.Error.
  • The loader refuses content that is not an OBJ model, such as a binary glTF file or a standalone MTL file, and reports it as an error with a message.

Goal: an OBJ file without a material library draws with plain default materials; give it a proper surface at startup.

StyleObj.jx
#pragma lifecycle(startup)
function startup() {
const rock = findEntity('Rock');
if (rock.getComponent(MaterialComponent)) return;
rock.addComponent(new StandardMaterialComponent({
color: '#7d7468',
roughness: 0.95,
metalness: 0,
}));
}
  • getComponent(MaterialComponent) finds a material of any kind. If the entity already has one, the script leaves it alone.
  • A material on a model is an override: one material, built once, drawn on every mesh of the model.
  • roughness near 1 and metalness 0 describe a matte, non-metallic surface such as stone.

Goal: models from different sources come in at very different sizes; scale each one so it is exactly 1.8 m tall.

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

FitHeight.jx
#pragma slot(ref=targetHeight, type=Number, min=0.1, max=100, default=1.8)
#pragma lifecycle(startup)
let model;
function fit() {
const bounds = model.getModelRestBounds();
if (!bounds) return;
const height = bounds.max.y - bounds.min.y;
if (height <= 0) return;
const factor = targetHeight / height;
model.setScale(factor, factor, factor);
log(model.name, 'scaled by', factor.toFixed(3));
}
function startup() {
model = self.parent;
if (model.isLoaded) fit();
on('model', ({ eventType }) => {
if (eventType === EventType.OBJ.Ready) fit();
});
}
  • #pragma slot(...) creates an editor field, targetHeight, that the script reads as a variable.
  • getModelRestBounds() measures the model as authored, in the entity’s own coordinates, before any scaling of the entity.
  • setScale(x, y, z) scales the entity evenly, so the model keeps its proportions. Because the bounds are measured as authored, running fit again gives the same result.
  • isLoaded covers a model that loaded before the script started; the ready listener covers every later load.
  • Keep MTL files next to the OBJ and textures next to their MTL, at the paths the files name, or the model draws with plain materials.
  • Scripts use the global OBJComponent, in capitals. ModelLoaderComponent finds the loader of any model format.
  • Several copies of the same file share one parsed asset, so repeating a model costs little loading time.

What a new Mesh Obj 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.
Obj comp-obj Required One Loads a Wavefront OBJ 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
  • OBJ::ready
  • OBJ::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 Obj.

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