Guides

Model preloading & switching

How the model cache works, why preloadModels makes switching fast, and why switchModel, never placeModel, is the switching call.

Preloading

preloadModels(urls) loads each URL through the engine's GLTF loader and stores the parsed result in the engine's model cache:

await ar.preloadModels(['/models/sofa.glb', '/models/chair.glb', '/models/table.glb']);

Every later placement of a cached URL clones from the parsed in-memory model instead of fetching and parsing again. For a product catalogue, preload the visible products when the AR view is created (or when the product page loads), so the first tap doesn't pay network + parse time.

Things to know about the cache:

  • It is keyed by URL. The cache is LRU-capped (config.modelCacheLimit, default 20); entries with live placements are never evicted, and the engine's clearModelCache() empties it explicitly. (In v1.0.0 there was no eviction, every URL you loaded stayed resident.)
  • Preloading is a Promise.all over the URLs: one failing URL rejects the batch. Load stragglers individually if your catalogue has optional items.
  • Model files are fetched with the page's origin rules, cross-origin models need CORS headers from the host serving them, and URLs must be app-controlled (the loader parses whatever it is given).

Switching models in place

Use switchModel() to change the product a shopper is looking at:

// The shopper placed the sofa and scaled it to fit the corner.
await ar.switchModel('/models/chair.glb');
// The chair appears at the same anchor, with the same scale and rotation.

switchModel(modelUrl, { preserveMaterial = true }):

  • Keeps the existing anchor, position, scale and rotation.
  • With preserveMaterial (the default), carries the current colour/texture over to the new model.
  • Falls back to a fresh placeModel() when nothing is placed yet, so it is safe to call from a product-switcher UI unconditionally.

Warning

Do not use placeModel() to switch. placeModel always performs a fresh hit test and creates a new model alongside the previous one, the placement, scale and rotation you had do not carry over. (Both models stay tracked: getPlacedModels() lists them and clearModels() removes them.) An earlier version of the README's API table (and its React example) claimed placeModel switching "keeps placement, scale and rotation"; that was wrong, and the docs now teach switchModel.

Removing the model

await ar.removeModel(); // removes the active model; no-op if none is placed

ARView tracks every model placed through it, with a single active model. removeModel() removes the active one and clears the active id/URL. getPlacedModels() enumerates everything placed through the view and clearModels() removes all of it, but switching with switchModel() is still the right tool for product changes: it reuses the placement instead of stacking models.

Material carry-over

With preserveMaterial, each material of the new model inherits the colour, and the map, when present, from its counterpart on the old model, matched by material name first, then by traversal order (fixed in 1.1.0; before that, the first material was copied onto every material). Materials without a counterpart keep their own look. If you prefer explicit styling, pass { preserveMaterial: false } and re-apply with setColor / applyTexture after the switch.