@ferret/game
createGame: wires platform, Babylon engine, scene, world, display, input and physics into a running game.
Classes
LoadMarks class
class LoadMarksconstructor(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(): number | seconds since the marks were made |
pending(): string[] | the parts not settled yet |
failed(): string[] | |
get ready(): boolean | every registered part has settled (true also when none is registered) |
report(): LoadPart[] | |
text(): string | one 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): GameWire 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): voidthe 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): () => voidpolls 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 | nullthe 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 GamesetPacing(on: boolean): void | even 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: number | refreshes per drawn frame now (1 without pacing) |
engine: BABYLON.AbstractEngine | |
scene: BABYLON.Scene | |
world: World | |
host: SceneHost | the 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?: QualityService | the quality regulator (absent when quality: false) |
xr?: XrService | the 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(): void | Run 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 Gamegpu: GpuMode | |
gpuNote?: string |
packages/game/src/index.ts:98
GameAsyncOptions interface
interface GameAsyncOptions extends GameOptionsgpu?: 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?: string | where 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 GameOptionscanvas: HTMLCanvasElement | |
input: Omit<InputOptions, 'defaultKind'> & { defaultKind?: InputOptions['defaultKind'] } | |
platform?: FerretPlatform | |
storageKey?: string | key for browser storage (one per game) |
physicsLayers?: string[] | |
tier?: QualityTier | |
touchControls?: boolean | Touch 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?: XrOptions | VR through WebXR: { enabled: true } adds the xr service where the browser has WebXR; elsewhere (OS Beroun) nothing is added |
quality?: QualityOptions | false | The 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?: boolean | Never 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?: number | worker 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 LoadMarksOptionsprefix?: string | null | the console prefix of the lines ([mygame]); null: nothing is written. Default [load] |
t0?: number | seconds are counted from this time in ms (default: the creation of the marks) |
now?: () => number | the clock in ms (default performance.now) |
log?: (line: string) => void | the line sink (default console.info) |
packages/game/src/load-marks.ts:8
LoadPart interface
interface LoadPartLoad 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 ReleaseOptionsThe 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: () => boolean | true when everything the splash waits for is ready (polled each frame) |
release: (why: 'ready' | 'timeout') => void | called once, when ready or on timeout: hide the splash, start the simulations (simHost.start()), unpause |
timeoutMs?: number | after this many ms the splash lets go anyway (default 300000) |
onTimeout?: (waitedMs: number) => void | called on a timeout before release |
now?: () => number | the 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 WarmUpOptionslog?: string | null | the console line is written with this prefix ([mygame] warmed ...); null: silent. Default null |
readyTimeoutMs?: number | give up waiting for the scene's readiness after this many ms (default 60000) |
filter?: (m: BABYLON.AbstractMesh) => boolean | which meshes count (default: those with a material, a sub-mesh and vertices) |
packages/game/src/warmup.ts:17
WarmUpResult interface
interface WarmUpResultWarm-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: number | meshes compiled: one for each distinct shader |
meshes: number | meshes 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