Skip to content

Model ​

Loads a glTF / glb model (with optional Draco compression). The entity is created immediately; the model meshes attach asynchronously once the file is loaded.

ts
import { Behaviour } from "@relu-interactives/spatial-ecs";

export default class SpawnModel extends Behaviour {
  init() {
    const objects = this.world.getObjectManagementSystem();
    objects?.requestCreate("model", {
      name: "Robot",
      path: "/assets/models/robot.glb",
      transform: { position: { x: 0, y: 0, z: 0 } },
    });
  }
}

Payload ​

FieldTypeDefaultDescription
namestring"Model"Display name.
pathstring""URL or local-asset path to a .glb / .gltf. If empty, an anchor is created with no mesh.
transformTransformDataidentityPosition / rotation / scale.
parentIdEntityId | nullnullOptional parent entity.

Components attached ​

Transform, Object3DRef, Name, EntityType, ParentId, Selectable, Model, MeshGeometry. If the loaded file contains animation clips, an Animation component is also attached automatically.

Pivots (0.17.0) ​

A model file's origin is often far from its mesh (a scene exported off-centre, a kit piece at its corner), so rotating the entity swings the model around that far-away point. A pivot makes another point the entity's origin, like Blender's Origin to Geometry, without touching the file.

The pivot lives in the model's MeshGeometryComponent.parameters.pivot: a point { x, y, z } in the file's space, in the entity's local units (before the entity's scale). The loaded model hangs inside the entity moved by -pivot, so the entity's Transform positions, rotates and scales the model about that point, in the editor, in Preview and in the published build alike.

ts
import { Behaviour } from "@relu-interactives/spatial-ecs";
import { MeshGeometryComponent } from "@relu-interactives/spatial-ecs/components";

export default class Spin extends Behaviour {
  protected onUpdate() {
    // With a pivot at the model's centre this spins it in place.
    this.transform.rotation.y += this.deltaTime;
  }
}
  • The editor sets pivots for you (Inspector → Transform → Pivot, or the agent's set_pivot) and moves the entity at the same time, so the model stays where it was. Setting parameters.pivot yourself moves the model by -pivot in its entity: move the entity by the same amount (rotated and scaled) if it should stay put.
  • It takes effect on a live entity as soon as parameters changes (a new object, or an edit in place): no reload. Removing it (parameters = {}) puts the model back on the file's origin.
  • ModelComponent.object is still the loaded model, and its own transform is the file's. The offset is on a group between the entity's object and the model (userData.ecsModelPivot, only there while a pivot is set), so a clip that animates the model's root can't undo it, and bones, bone attachments, raycasts and trimesh colliders all follow.
  • Child entities sit under the entity's origin, which is the pivot. Primitive colliders are placed from the origin too (offsetPosition), so after moving a pivot by hand refit them; the editor shifts them for you.
  • Hosts can check for the feature with SPATIAL_ECS_FEATURES.modelPivot. An engine before 0.17.0 ignores parameters.pivot.