Skip to content
Work in progress. These docs describe Spawnite at launch, and some parts are still being built.

Internals

It has one portal per UI element to the screen layer; anchored panels project their entity each frame and skip entities beyond a distance; one modal stack; one context each for Game, Scene, Landscape and Entity, with the hooks reading the nearest. Three-mmorpg’s runtime on React Three Fiber and Koota sits behind these components; the wrappers, the hoist, the anchor and the stack are the new engine work.

The engine exposes a devtools API with no UI: the entity tree, selection, pick under the pointer, pause and step, and a registration point for panels. @spawnite/devtools draws the generic overlay and a game adds its own panels. See the devtools API and the debugger.

Every file in packages/engine/src sits in the folder that names what it is. One name means one thing: world is the Landscape level, and the koota instance is runtime. A thing that crosses layers keeps its name in each, so daylight is a render helper, a system and a view.

packages/engine/src/
├── index.ts the barrel; a game imports @spawnite/engine and nothing deeper
├── core.ts the simulation alone, as @spawnite/engine/core: what a room server and the harness import in Node
├── components/ the components: Frameloop, Game, Scene, Landscape, Entity, CharacterSpawn, Camera, Sound, and the headless mount
├── data/ the tables that name the files in packages/assets: character, scatterModels, scatterShapes, openingModels, sounds
├── maths/ geometry with no browser and no React: axes, facing, hash, solids, escape
├── platform/ the browser: keyboard, stick and camera bindings, and the audio context
├── gameplay/ the simulation on koota, never React
│ ├── runtime.ts the koota world factory
│ ├── step.ts the fixed step, the systems it runs and its accumulator
│ ├── harness.ts a headless game: create from a scene and a seed, step seconds, dump state and its hash
│ ├── save.ts the save record read out of the world and put back
│ ├── entity/ what every entity carries: traits, transform, resources
│ ├── character/ traits, movement, path, pose, actions
│ ├── world/ traits, daylight, round, spawn, actions
│ ├── state/ defineMachine(): the state machine on XState, its snapshot in a trait, its waits in the step
│ ├── map/ the ground and what stands on it, baked: surface, scatter, obstacles, navmesh
│ ├── physics/ Rapier: the ground as a heightfield, a hull per obstacle, the floor walk
│ └── camera/ the eye settling out of solids
├── render/ three.js with no React: palette, textures, light maths, VRM prep, the Draco decoder
├── stores/ the zustand stores the hooks read: scenes, modals, wallet, input and time; usePlayer and useMatch query the world
└── views/ React Three Fiber components and their hooks
├── entity/ useEntityRef, StandingModelView
├── vrm/ VrmView
├── world/ GroundView, ScatterView, NavMeshView, DaylightRig, SkyDome, SkyClouds
├── audio/ SoundView
├── texture/ useKtx2
└── character/ CharacterView, CharacterAnimationView, FootstepsView, animation

The line between render and views: a file that calls React or React Three Fiber at runtime is a view; a file that only builds three.js objects, textures or numbers is render code, and a view calls it. views/character/animation.ts calls neither but drives the bones of a VRM a view owns, so it sits with that view.

The layer order runs maths and platform, then render and data, then gameplay, then stores, then views, then components. One import runs the other way: data/character keys the character’s clips by the pose enum in gameplay/character, so a pose and its clip share one name. Lint in packages/engine/oxlint.config.mts holds the lines that matter. It checks a line between layers on the file an import resolves to, so a barrel is refused along with the layer it re-exports:

  • platform imports no React.
  • maths and gameplay import no store, no view and no component, and React only with import type, so a prop a view reads is declared on the simulation’s own interface.
  • stores import no view and no component.
  • No file imports the engine’s own barrel, which re-exports every layer.

A store is reachable from outside React, so a devtools command or a test drives the game without a hook; a game’s own stores live in the game. components sits on top and is the only layer a game is meant to see once it exists.

maths and gameplay are the simulation. The room server and the harness run them in Node with no page, and the same inputs have to build the same world twice. So their lint also refuses the page, the timers, the wall clock, unseeded randomness, three’s loaders, three addons not known to run in Node, and the render barrel. The physics, the character’s steer and turn, the behaviours, the tracks, the ground surface, the scatter and its collision shapes, and the room’s shots also refuse what ECMAScript defines only approximately, whose last digit differs between engines. That is Math functions such as Math.hypot, Math.cos and Math.pow, the ** operator, and the three.js rotations that call them, such as angleTo and rotateTowards.

The engine’s maths folder builds each one they need from what ECMAScript defines exactly, so every engine computes it to the same bytes. A length is measureLength, the square root of the squares’ sum. sine, cosine, arcCosine and exponential are fdlibm’s polynomials, within a last digit of Math’s. measureTurn, turnTowards and yawToQuaternion are three’s rotations on them, and CentripetalCurve is three’s path curve with its quarter power taken as two square roots. A value computed once, such as the cosine of a body’s climb, takes them too: each engine would otherwise compute its own. The lint cannot see a three.js call it does not name, such as the sine inside CylinderGeometry, so the obstacle bake builds its trunk tube itself.

test/gameplay/index.test.ts imports gameplay through its barrel in the node environment and steps a world there on every run. A bake that needs what only a view loads is still a gameplay action: the view calls it with the floor mesh, so a test and a server can call it too.

The scatter’s collision shapes are data: the kit’s model manifest, models.json in @spawnite/assets, baked from the model files, keeps the hull of each scatter model, so the obstacle bake needs no view at all. The manifest is imported dynamically, so it stays out of the entry chunk a page downloads first. The Landscape dresses its ground when the manifest lands rather than waiting for it, and loadRapier loads the manifest with the wasm, so the Landscape, the harness and every caller that awaits the physics has the hulls before its first step or bake.

The character’s files sit in the engine, each in its layer’s folder so lint applies to it: data/character, gameplay/character and views/character.

The engine asks a mesh where a ray meets it and which of its points is nearest through three-mesh-bvh’s MeshBVH: the camera’s eye settle in a world with no physics, the springs’ obstacle collider and the escape query. The pointer’s ray on the ground builds no tree: raycastGround in views/world/groundRaycast.ts walks the grid’s cells under the ray, two triangles each, as a heightfield collider does. A map’s mount builds nothing for it, and a stroke’s heights answer the next ray. Every tree is built by buildQueryTree in maths/queryTree.ts, which picks the tree’s layout from the geometry’s groups:

  • A geometry with several groups is built in three-mesh-bvh’s indirect mode. Without it, three-mesh-bvh builds a separate tree for each group, and every query walks all of them. The obstacle bake keeps one group per placement, because the physics cuts one collider per group: built per group, that is 123 trees for every ray on the mmorpg map, and a short ray costs what a long one does. Indirect mode builds one tree over a buffer of its own and leaves the geometry’s index and groups as the merge wrote them.
  • A geometry with one group or none, such as a model of one material, is one tree either way. It keeps the default, which sorts the index in place and reads each triangle without indirect mode’s extra lookup.
  • An obstacle bake’s tree is built in indirect mode whatever its groups, by buildObstacleTree. On a page that draws, GroundDressing builds it on a worker of its own, so the bake’s geometry lands at once and its tree after: the hulls and the checkpoint have read the index by then, and the worker’s tree is read against that index as it stands. A headless mount and a test’s DOM build it in the call that bakes, and a room builds it at its first query, since nothing in a room need query it.

ObstaclesTrait holds the bake’s geometry and its bounds tree’s state, Building until the worker answers and Ready with the tree. The physics, its checkpoint and the navmesh read the geometry, so the tree’s arrival rebuilds nothing. The loading screen waits for a tree that builds under it. The camera’s eye in a world with no physics reads every ground’s tree and finishes a building one on the page’s thread. The spring bones keep off every ground’s ready tree, each through a collider of its own, and skip a ground until its tree is ready. Their ground collider and the camera’s clearance read the ground under each point by gameplay/world/groundUnder.ts.

A mesh’s detail does not decide the layout: a model of a hundred thousand triangles in one group is one tree without indirect mode.

A game has an entry, an app, scenes, its own components and its HUD:

games/mmorpg/src/
├── main.tsx the entry: preload
├── App.tsx <Game> with one scene and the global HUD
├── scenes/Meadow.tsx <Landscape map> <CharacterSpawn> <Camera follow> and the scene's HUD
├── components/ Character, HealthBar (the bar over the character's head)
└── hud/ DaylightSetting, QualitySetting, SaveIndicator