Skip to content

Getting started ​

Marble3 is a TypeScript 3D game engine built on Three.js for rendering and Bullet physics, compiled to WebAssembly, for simulation. It is ESM-only and bundler-only: it is built to be consumed by Vite (or an equivalent bundler), not by <script> tags or bare node.

Install ​

sh
npm install marble3
sh
yarn add marble3
sh
pnpm add marble3 three@0.163 @types/three@0.163

That one package is everything: the physics engine and its WebAssembly binary ship inside it, and three with its type definitions (@types/three) installs alongside as a dependency. There is no WASM file to copy and nothing to call before you import.

Marble3 is built against Three.js 0.163, and your scenes import three too (cameras, vectors). npm and yarn hoist the engine's copy, so import * as THREE from 'three' works after the one command; pnpm keeps dependencies private, hence the extra two there. If you add three to your own package.json with any package manager, use 0.163 so your app and the engine share one copy — two copies break instanceof checks.

Bundler setup ​

Importing Marble3 loads the physics WebAssembly before any engine code runs (a top-level await inside the package), and the binary's URL comes from new URL(…, import.meta.url), so your bundler fingerprints and serves it like any other asset, under any base path.

Vitevite buildvite dev server
8works as isworks as is
7works as isneeds optimizeDeps.exclude
5 – 6needs build.targetneeds both

Projects on Vite 7 or older add:

ts
// vite.config.ts
import { defineConfig } from 'vite';

export default defineConfig({
  optimizeDeps: { exclude: ['marble3'] }, // the dev prebundler would move the module away from its .wasm
  build: { target: 'es2022' },              // keep the top-level await
});

Both settings are harmless on Vite 8. vite build prints warnings about fs, path, module and exports in marble3/dist/bullet/ammo.wasm.js: that is the Node.js branch of the Emscripten loader, which never runs in a browser.

The physics binary is about 750 KB (300 KB gzipped). A static import makes your module wait for it; to draw a loading screen first, import the engine dynamically:

ts
const { FPS } = await import('marble3'); // resolves once physics is ready

First scene ​

ts
import * as THREE from 'three';
import { FPS } from 'marble3';
import { Player } from 'marble3/player';
import { Box } from 'marble3/object';
import { Grid } from 'marble3/terrain/grid';
import { defaultLights } from 'marble3/lights';

export const createGame = (): HTMLElement => {
  const camera = new THREE.PerspectiveCamera(70, innerWidth / innerHeight, 0.1, 4000);
  const fps = new FPS();

  fps.scene.add(defaultLights());
  fps.add(new Grid(100));          // a static floor

  const box = new Box();           // a dynamic 1×1×1 crate
  box.position = new THREE.Vector3(0, 5, -4);
  fps.add(box);

  const player = new Player({ camera });
  player.position = new THREE.Vector3(0, 1, 0);
  fps.add(player);

  return fps.start(player);        // a container element — mount it anywhere
};

Mount and unmount:

ts
import { disposeGame } from 'marble3';

const el = createGame();
document.body.appendChild(el);
// …later, e.g. on route change:
disposeGame(el);   // stops the loop, frees every entity, physics world included
el.remove();

The container fills its nearest positioned ancestor (position:absolute; inset:0), and the renderer sizes itself to the window — give the game the viewport, the way the examples' play pages do.

Press W to grab the mouse (pointer lock), walk with WASD, jump with Space. On a touchscreen the player mounts a stick and jump button instead — see Mobile.

Import style ​

Everything is exported from the package root, and every module is also a subpath export — both of these work:

ts
import { FPS, Player, Box } from 'marble3';
import { Player } from 'marble3/player';   // narrower, tree-shake-friendlier