The window.__game convention

Any three.js, React Three Fiber or WebGL page can be opened, looked at and driven without it: open, look, observe (which falls back to the page's named three.js objects), eval, interact with keys, mouse and touch, feel and perf work on a page that knows nothing about the harness. To read and set a game's exact state, the game also exposes window.__game.

Once the page has loaded, it exposes:

window.__game = {
  describe(),        // a short description: name, controls, units
  observe(),         // the state, as plain JSON
  act(input),        // performs one input object: { key, down? } or { action, ...args }
  setState(partial), // sets any of observe()'s keys; keys left out keep their values
  subject,           // optional: the name of the object the game is about
  intentional,       // optional: what look should not flag again
};

observe, look and perf work on any three.js page; window.__game makes the state exact. The optional keys and each rule below in full: references/tools.md.

Rules

templates/plain.html is a complete small game in this shape. In React, keep the state in a plain module and set window.__game in the Canvas's onCreated (templates/r3f.tsx).

The harness sends keys, mouse and touch as real browser events, so the game's own listeners receive them. An { action, ...args } object reaches __game.act whole.