Skip to content

WebAssembly for AR and VR on the web: where it fits with WebXR

AR and VR in the browser run on WebXR, the JavaScript API for headsets and phone-based AR. WebAssembly (Wasm) is a compact binary format that browsers can compile ahead of time, so code written in C, C++, Rust or C# (via Unity) can run there. The two work together, but Wasm isn’t an XR API in its own right. This post covers what it actually contributes and how the pieces connect.

What WebAssembly gives you, and what it doesn’t

Section titled “What WebAssembly gives you, and what it doesn’t”

Wasm runs in the same sandbox as JavaScript. It has no direct access to the DOM, WebGL, WebGPU or WebXR. Every call to those APIs goes through JavaScript glue code, which is generated by Emscripten for C/C++ or by wasm-bindgen for Rust.

That shapes where it helps:

  • CPU-heavy work in the frame loop. Physics, skinning, mesh processing, pathfinding and decoding can run faster and more predictably in Wasm than in JavaScript, and Wasm has no garbage-collection pauses inside its own linear memory. In VR a dropped frame is noticeable, so steady frame times matter.
  • Reusing native code. Existing C++ engines and libraries can be compiled for the web instead of rewritten. Common examples are the Draco mesh decoder and the Basis Universal texture transcoder (both used by three.js’s DRACOLoader and KTX2Loader), OpenCV.js, and MediaPipe’s web tasks for hand and face tracking.
  • It does not speed up rendering. The GPU does the drawing. Wasm can reduce the CPU cost of preparing a frame, but every WebGL or WebGPU call still crosses into JavaScript. Engines batch these calls to keep that overhead down.

A typical WebXR app has the same structure whatever language it’s written in:

  1. JavaScript checks for WebXR support and, after a user gesture such as a button click, calls navigator.xr.requestSession("immersive-vr") or "immersive-ar".
  2. Each frame, the session’s requestAnimationFrame callback receives the viewer and controller poses.
  3. Game logic, which may live in Wasm, updates the scene from those poses.
  4. The scene is drawn with WebGL into the session’s layer, once per eye.

The feature check is plain JavaScript:

const vrSupported =
"xr" in navigator && (await navigator.xr.isSessionSupported("immersive-vr"));

If you’re writing Rust yourself, web-sys has WebXR bindings (XrSession, XrFrame and so on), but they’re marked unstable. You have to enable each type as a Cargo feature and build with RUSTFLAGS="--cfg=web_sys_unstable_apis".

  • Godot 4. The Web export compiles the engine to Wasm, and the built-in WebXRInterface handles sessions, controllers and reference spaces.
  • Unity. The Web build target compiles C# to Wasm through IL2CPP. Unity’s manual says it has no direct XR support for the Web platform and points to third-party projects (WebXR Export, SimpleWebXR, Needle Engine), which Unity doesn’t test or guarantee.
  • Unreal Engine. HTML5 export was removed from the official engine in 4.24 and handed to a community-maintained project. Don’t plan a WebXR project around it.
  • three.js, Babylon.js, A-Frame. These are JavaScript engines with built-in WebXR support. They load Wasm modules for specific jobs, such as the decoders above or Babylon.js’s Havok physics plugin. For many projects this mix (JS engine plus Wasm where it measurably helps) is simpler than compiling a whole engine.
  • Device support varies. WebXR runs in Chrome on Android (AR via ARCore) and in the Meta Quest browser. Safari on iPhone doesn’t support it. Check isSessionSupported and provide a non-XR fallback.
  • HTTPS is required. WebXR only works in secure contexts (localhost counts during development).
  • Threads need cross-origin isolation. Multithreaded Wasm uses SharedArrayBuffer, which is only available when the page is served with Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp. Those headers can break third-party embeds and scripts.
  • Download size. Whole-engine exports can be tens of megabytes. Serve .wasm with compression and Content-Type: application/wasm so WebAssembly.instantiateStreaming can compile while it downloads.

© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)