Skip to content

Model ​

References a GLTF/GLB asset loaded from path. The model loading system resolves the asset asynchronously and populates ModelComponent.object once available; loading is deferred-tolerant so the entity exists before the model finishes loading.

Properties ​

PropertyTypeDescription
pathstringSource URL or localasset:// path of the GLTF/GLB asset.
objectTHREE.Object3D | nullLoaded model object, populated asynchronously by the model loader. null until loaded.
loadStateModelLoadState"loading" while the asset is being fetched and parsed, "loaded" once it is attached to the entity, "error" when it failed (see loadError). A model with an empty path reports "error". Runtime-only: never saved.
loadErrorstringWhy the load failed, when loadState is "error". Runtime-only.

API reference ​

Scripting examples ​

Component access patterns

Use this.getComponent(X) to access a component on the entity this script is attached to. Use this.world.getEntityByName to find another entity by name, then this.world.getComponent to read its component:

typescript
// Component on the entity this script is on
const model = this.getComponent(ModelComponent);

// Component on another entity, found by its scene name
const other = this.world.getEntityByName("Robot");
if (other) {
  const model = this.world.getComponent(other.entityId, ModelComponent);
}

Fade the model in after it loads ​

Wait for the async model load to complete (loadState === "loaded"), then animate opacity to 1 over one second.

typescript
import { Behaviour } from "@relu-interactives/spatial-ecs";
import { ModelComponent } from "@relu-interactives/spatial-ecs/components";

export default class ModelFadeIn extends Behaviour {
  private fadeProgress = 0;
  private started = false;

  protected onUpdate() {
    const model = this.getComponent(ModelComponent);
    // `object` is an empty anchor until the GLB arrives, so wait on loadState.
    if (!model?.object || model.loadState !== "loaded") return;

    if (!this.started) {
      this.started = true;
      // Initialise all sub-mesh materials to transparent before fading in
      model.object.traverse((child: any) => {
        if (child.isMesh && child.material) {
          child.material.transparent = true;
          child.material.opacity = 0;
        }
      });
    }

    this.fadeProgress = Math.min(1, this.fadeProgress + this.deltaTime);
    model.object.traverse((child: any) => {
      if (child.isMesh && child.material) {
        child.material.opacity = this.fadeProgress;
      }
    });
  }
}

Check whether the model loaded ​

loadState starts as "loading", then becomes "loaded" once the GLB is attached to the entity, or "error" if the fetch or parse failed. loadError holds the reason. A model entity with an empty path reports "error" with the message "No model path set.". Neither field is saved with the project.

typescript
import { Behaviour } from "@relu-interactives/spatial-ecs";
import { ModelComponent } from "@relu-interactives/spatial-ecs/components";

export default class ModelLoadWatcher extends Behaviour {
  private reported = false;

  onUpdate() {
    const model = this.getComponent(ModelComponent);
    if (!model || this.reported) return;

    if (model.loadState === "loaded") {
      this.reported = true;
      console.log("model ready", model.path);
    } else if (model.loadState === "error") {
      this.reported = true;
      console.warn(`model failed: ${model.path}: ${model.loadError}`);
    }
  }
}

Toggle model visibility ​

Show or hide the loaded model at runtime using the Object3DRef visibility flag.

typescript
import { Behaviour } from "@relu-interactives/spatial-ecs";
import { ModelComponent, Object3DRef } from "@relu-interactives/spatial-ecs/components";
import type { ToggleInput } from "@relu-interactives/spatial-ecs";

export default class ModelVisibility extends Behaviour {
  data = {
    visible: true as ToggleInput,
  };

  protected onUpdate() {
    const model = this.getComponent(ModelComponent);
    if (!model?.object) return; // still loading

    const ref = this.getComponent(Object3DRef);
    if (ref) ref.visibility = this.data.visible;
  }
}

Async loading

Always guard before traversing or mutating sub-meshes. Check model.loadState === "loaded", or guard with model?.object. object starts as the empty anchor group, or null on a bare component, until the GLTF/GLB asset finishes loading.