FERRETGame engine over Babylon.js
Ferret

@ferret/game

createGame: wires platform, Babylon engine, scene, world, display, input and physics into a running game.

19 exports · source in packages/game/src

Classes

LoadMarks class

class LoadMarks
constructor(o: LoadMarksOptions = {})
add<T>(name: string, p: Promise<T>): Promise<T>register a part of the loading; the promise is returned as it is (a failure stays the caller's to handle: the part is marked failed)
get elapsed(): numberseconds since the marks were made
pending(): string[]the parts not settled yet
failed(): string[]
get ready(): booleanevery registered part has settled (true also when none is registered)
report(): LoadPart[]
text(): stringone line for the splash or the console: what is still awaited
onPart(fn: (p: LoadPart) => void): () => void
whenReady(): Promise<void>resolves when every part registered so far (and any added while waiting) has settled

packages/game/src/load-marks.ts:19

Functions

createGame function

function createGame(opts: GameOptions): Game

Wire the usual pieces for a game: platform (OS Beroun or browser), Babylon engine, scene, world, display (render scale, quality, FOV), input and physics. The game then adds its own services, levels and entities and calls start(). On OS Beroun the engine is made the way /lib/beroun-3d.js does it (the console's 640x480 at density 1, no antialiasing).

packages/game/src/index.ts:79

createGameAsync function

async function createGameAsync(opts: GameAsyncOptions): Promise<GameAsync>

createGame with the engine chosen at run time: WebGPU when asked for (gpu: 'webgpu' or ?gpu=webgpu) and the browser has an adapter, else the WebGL2 engine exactly as createGame makes it. A game runs unchanged on either. Never on OS Beroun (no WebGPU there).

packages/game/src/index.ts:104

registerLoadCommands function

function registerLoadCommands(world: { commands: { register(name: string, help: string, fn: (a?: any) => unknown): unknown } }, marks: LoadMarks): void

the load.report command: the parts and which are not ready (works with any world that has commands.register)

packages/game/src/load-marks.ts:63

releaseWhenReady function

function releaseWhenReady(o: ReleaseOptions): () => void

polls ready each frame; releases once. Returns a function that stops the waiting without releasing.

packages/game/src/ready-gate.ts:22

sceneMaterials function

function sceneMaterials(scene: BABYLON.Scene): Set<BABYLON.Material>

the distinct materials of the scene's meshes (the parts of a multi-material counted, not the multi-material)

packages/game/src/warmup.ts:47

shaderKey function

function shaderKey(m: BABYLON.AbstractMesh): string | null

the key of the shader a mesh makes: meshes with the same key compile the same program

packages/game/src/warmup.ts:27

shaderRepresentatives function

function shaderRepresentatives(scene: BABYLON.Scene, filter?: (m: BABYLON.AbstractMesh) => boolean): BABYLON.AbstractMesh[]

one mesh for each distinct shader of the scene (an instance stands for its source mesh)

packages/game/src/warmup.ts:36

warmUpScene function

async function warmUpScene(scene: BABYLON.Scene, o: WarmUpOptions = {}): Promise<WarmUpResult>

compile one mesh of every kind of shader, all at once, then wait for the scene to be ready

packages/game/src/warmup.ts:54

Interfaces

Game interface

interface Game
setPacing(on: boolean): voideven frame pacing (core FramePacer): draw every k-th refresh of the display, k the smallest the frames keep, and give the world k refreshes of time a frame, so the motion does not jerk when a frame takes a little more than a refresh. Off by default
readonly pacingStep: numberrefreshes per drawn frame now (1 without pacing)
engine: BABYLON.AbstractEngine
scene: BABYLON.Scene
world: World
host: SceneHostthe scene host: second worlds on the shared engine (a game's interlude during a travel)
platform: FerretPlatform
display: Display
input: InputService
physics: PhysicsService
touch?: TouchControls
quality?: QualityServicethe quality regulator (absent when quality: false)
xr?: XrServicethe VR session service, once loaded: only when xr was set and the browser has WebXR (the package is fetched then, not before)
xrReady: Promise<XrService | undefined>resolves when the VR service is added (or at once when it is not wanted)
start(): voidRun the render loop (and with it the fixed-step simulation).
stop(): void
dispose(): void

packages/game/src/index.ts:46

GameAsync interface

interface GameAsync extends Game
gpu: GpuMode
gpuNote?: string

packages/game/src/index.ts:98

GameAsyncOptions interface

interface GameAsyncOptions extends GameOptions
gpu?: GpuMode'webgpu' runs on WebGPU where the browser has an adapter (else WebGL2); default 'webgpu' with an automatic fall back to WebGL2 when the browser has no working WebGPU (scenes made before 2026-11-20 pin 'webgl2'), and ?gpu= on the page overrides it
twgslUrl?: stringwhere the WebGPU shader translator (twgsl.js and twgsl.wasm) is served from; default lib/twgsl/ beside the page
webgpuShaders?: 'glsl' | 'wgsl''glsl' (default): the engine's materials stay on GLSL, translated by twgsl, so Ferret's GLSL plugins and shaders work; 'wgsl': Babylon's native WGSL materials (the GLSL plugins then cannot attach)

packages/game/src/index.ts:90

GameOptions interface

interface GameOptions
canvas: HTMLCanvasElement
input: Omit<InputOptions, 'defaultKind'> & { defaultKind?: InputOptions['defaultKind'] }
platform?: FerretPlatform
storageKey?: stringkey for browser storage (one per game)
physicsLayers?: string[]
tier?: QualityTier
touchControls?: booleanTouch controls on phones and tablets (default true)
clearColor?: [number, number, number]
powerPreference?: 'high-performance' | 'low-power' | 'default'which graphics card the browser is asked for: 'high-performance' (the discrete one on a laptop; the default on desktops), 'low-power' (the integrated one; the default on phones, tablets and OS Beroun). A request only: the browser and the system may decide otherwise
fov?: { baseVertical?: number; minHorizontal?: number }the camera's field of view: the vertical one (radians) at wide aspects, and the horizontal one it never falls below (a game that matches another engine's 90 degrees passes both)
xr?: XrOptionsVR through WebXR: { enabled: true } adds the xr service where the browser has WebXR; elsewhere (OS Beroun) nothing is added
quality?: QualityOptions | falseThe quality regulator: on by default, it holds the frame rate (the headset's during a VR session, else targetHz, 60) by giving up the least visible quality of the side that holds the frame. false leaves it out. The page switch ?quality=off stops it, ?quality=high|medium|low fixes every knob at the top, the middle or the bottom of its range.
neverLowerQuality?: booleanNever lower quality: the picture is held before the frame rate. The quality regulator is off and cannot be switched on, the levels it found on earlier runs are not loaded (nor saved), every knob (render scale, the games' own) stays at its richest, and the page switches ?quality= do not move the knobs. The service and its commands remain (quality.report, quality.set by hand). Replaces resetting each knob by hand. Default false.
workers?: numberworker threads of the system scheduler, at most 8 with the Babylon one; default: the cores less two. 0 runs every kernel on the page; ?workers=N on the page overrides it

packages/game/src/index.ts:13

LoadMarksOptions interface

interface LoadMarksOptions
prefix?: string | nullthe console prefix of the lines ([mygame]); null: nothing is written. Default [load]
t0?: numberseconds are counted from this time in ms (default: the creation of the marks)
now?: () => numberthe clock in ms (default performance.now)
log?: (line: string) => voidthe line sink (default console.info)

packages/game/src/load-marks.ts:8

LoadPart interface

interface LoadPart

Load readiness marks: a game registers the parts of its loading (aircraft, figures, ground ...) as promises; each one that settles is written to the console ([game] ready: <part> at N s), and the splash and the console can ask which are not ready yet. The splash waits for ready (every registered part settled).

name: string
state: 'pending' | 'ready' | 'failed'
seconds?: number
error?: string

packages/game/src/load-marks.ts:6

ReleaseOptions interface

interface ReleaseOptions

The world waits for Ready: nothing happens in game time while the splash is up. A game creates its SimHost with paused: true (it does not step, in a worker it does not run), registers its loading with LoadMarks, and calls releaseWhenReady; when everything is ready (or the wait has run out) the splash leaves and the world starts (SimHost.start()).

ready: () => booleantrue when everything the splash waits for is ready (polled each frame)
release: (why: 'ready' | 'timeout') => voidcalled once, when ready or on timeout: hide the splash, start the simulations (simHost.start()), unpause
timeoutMs?: numberafter this many ms the splash lets go anyway (default 300000)
onTimeout?: (waitedMs: number) => voidcalled on a timeout before release
now?: () => numberthe clock in ms (default performance.now) and the frame scheduler (default requestAnimationFrame)
next?: (cb: () => void) => void

packages/game/src/ready-gate.ts:7

WarmUpOptions interface

interface WarmUpOptions
log?: string | nullthe console line is written with this prefix ([mygame] warmed ...); null: silent. Default null
readyTimeoutMs?: numbergive up waiting for the scene's readiness after this many ms (default 60000)
filter?: (m: BABYLON.AbstractMesh) => booleanwhich meshes count (default: those with a material, a sub-mesh and vertices)

packages/game/src/warmup.ts:17

WarmUpResult interface

interface WarmUpResult

Warm-up before the splash lets go: every shader the scene will draw is compiled and every texture is on the card, so that the first sight of a figure, an impostor or a wreck does not stall a frame (shaders compile and textures upload on first use otherwise).

One mesh is taken for each thing that makes a different shader (material, skeleton, morph targets, instancing, baked vertex animation, vertex colours), and all of them are compiled at once: compiled one mesh after another (each waits a frame for its readiness) thousands of meshes take minutes. Then the scene's own readiness is awaited.

shaders: numbermeshes compiled: one for each distinct shader
meshes: numbermeshes in the scene, materials (sub-materials of multi-materials counted)
materials: number
ms: number

packages/game/src/warmup.ts:9

Types

GpuMode type

type GpuMode = 'webgl2' | 'webgpu'

the graphics API a game asks for: the page switch ?gpu=webgpu (or webgl2) wins over the option; the default is WebGPU with a fall back to WebGL2 (owner 2026-11-20); scenes made before that pin 'webgl2'

packages/game/src/index.ts:89