Concepts

Lifecycle

How sessions are created, ended and torn down on both runtimes, and what is cleaned up in 1.1.0.

WebXR path

Create

ARView.create() calls engine.startSession(). Three.js objects are created once, the first start builds the renderer, scene, camera, lights, loader, plane groups and reticle, and are reused across sessions. The renderer canvas is inserted before your overlay element (or appended to document.body when there is no overlay).

Session end (user or system)

When the XR session ends on its own (user exits, system interrupts):

  • the engine fires sessionEnded to frame listeners,
  • the animation loop stops,
  • session / hit-test-source / reference-space fields are nulled,

…but placed models and the canvas are not removed. That only happens in stopSession(). Listen for sessionEnded and call ar.destroy() (or start again, like the demo's Resume flow) to return to a clean state.

Teardown

ARView.destroy() destroys the gesture controller and awaits engine.dispose(), the full teardown (in 1.0.0 destroy only stopped the session; the rest of this list is 1.1.0 behaviour). Disposal:

  • stops rendering and ends the XR session,
  • removes models, shadow planes and base rings from the scene and clears the anchor/model maps, disposing per-instance GPU resources (removed/switched models are disposed on those paths too, under the engine's ownership rule),
  • disposes plane dots and meshes,
  • hides the reticle and removes the canvas from the DOM,
  • disposes the model cache, the WebGL renderer and its context, and nulls the renderer/scene/camera fields, the same engine instance can be started again afterwards.

React

  • ARPlayground-style components: construct one ARView per mount, destroy() in the effect cleanup.
  • useARCore: checks support on mount; on unmount it stops the session if it started one and runs its listener cleanups. By default all consumers share one lazily-constructed engine, so that stop affects all hook consumers; mount the hook once, or pass a dedicated engine per view with useARCore({ engine }).

Native path

Create

The plugin's load() constructs its managers once; the Filament engine is created lazily on first use (if it cannot initialise, session entry points fail with a coded FILAMENT_UNAVAILABLE instead of leaving the renderer in a broken state). startSession then: camera permission → ARCore availability / install → Session creation and configuration → native view inserted behind the WebView, which is made transparent.

Pause and resume

Wired in 1.1.0 (in v1.0.0 nothing called the pause/resume methods, the gap this section used to describe). The plugin implements Capacitor's handleOnPause / handleOnResume / handleOnDestroy and forwards them to the session manager and renderer:

  • Backgrounding the activity pauses the ARCore session and the render loop, as ARCore's contract requires.
  • A dedicated stop flag (not the pause flag) tracks stopSession, so a session restarted after a stop in the same app run renders normally.

Teardown

stopSession (on the UI thread): clears models/anchors → renderer session-closed → view removed and the WebView's real background restored → session closed. Plugin destroy goes further: it stops the render loop, joins the render thread, then destroys the Filament resources in dependency order, camera background, swap chain, view, scene, renderer, camera entity, and the engine last.