Skip to content

Getting started ​

Wasmotion is a TypeScript 3D game engine built on Three.js for rendering and Bullet physics (Ammo WASM) 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 github:bhirbec/wasmotion three github:bhirbec/bullet-ts#v1.2.0
sh
yarn add github:bhirbec/wasmotion three github:bhirbec/bullet-ts#v1.2.0

The package is still named @maomi/sge, so that is what you import. three and bullet-ts are peer dependencies — your app installs them next to the engine. There must be exactly one copy of each: two three instances break instanceof checks, and bullet-ts patches shape prototypes at load time (that patch must see the same instance the engine uses).

Configure your bundler ​

bullet-ts bootstraps its WASM with a top-level await and locates the binary via new URL('./ammo.wasm.wasm', import.meta.url). Two Vite settings keep both working:

ts
// vite.config.ts
export default defineConfig({
  optimizeDeps: { exclude: ['bullet-ts'] }, // keep it real ESM — don't prebundle
  build: { target: 'es2022' },              // top-level await
});

Without the exclude, esbuild's dep prebundling rewrites the module and the WASM URL breaks. Without the target, production builds refuse the top-level await.

First scene ​

ts
import * as THREE from 'three';
import { FPS } from '@maomi/sge';
import { Player } from '@maomi/sge/player';
import { Box } from '@maomi/sge/object';
import { Grid } from '@maomi/sge/terrain/grid';
import { defaultLights } from '@maomi/sge/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 '@maomi/sge';

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 run in a fullscreen overlay.

Click the canvas 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 '@maomi/sge';
import { Player } from '@maomi/sge/player';   // narrower, tree-shake-friendlier