FERRETGame engine over Babylon.js
Ferret

Perspective: drawing and animating by size on the screen

@ferret/perspective decides, for each object, how much of it is drawn and animated from how large it is on the screen: a person far away is a dot, a little nearer a flat picture of the model, nearer a simple mesh, and only near the camera the full model. Detail that nobody can see is neither drawn nor posed. The simulation is never touched: the service runs in update, not in the fixed step, so the same inputs give the same run whatever the camera does.

The rule#

The size is the projected size in pixels of the object's bounding sphere, px = tan(asin(R/d)) / tan(fovY/2) * heightPx. With several cameras the largest wins; an orthographic camera uses its view height. A ladder lists the levels, richest first; each has a minPx below which the next one takes over (the last has 0):

kindwhat is drawncost
fullthe modelwhat the model costs
mesha simpler mesh (your lod)fewer triangles
impostora baked picture of the model from 12 x 12 directions, lit by the scene's lights, one draw call for all objects of a modela quad
pointa dot of the object's mean colour and area, hazed like the meshesone instanced quad pass
nonenothingnothing

Switching has hysteresis (a margin of 10 %), a screen-space dither cross-fade between two rungs (every pixel is drawn exactly once), and an optional budget (maxFull, maxMesh). A FrameGovernor scales every threshold up when the measured frame time is over budget and back when there is room (perspective:degrade).

Use#

const backend = new BabylonBackend(scene);
world.addService(new PerspectiveService({ backend, budget: { maxFull: 12 }, governor: { budgetMs: 14, window: 20, down: 0.85, up: 1.06, min: 0.5, max: 4 } }));

backend.represent(entity, 'full', fullRoot);
backend.represent(entity, 'mesh', lodRoot);
await backend.addImpostor(entity, 'impostor', { root: fullRoot, options: { views: 12, cell: 96 }, modelKey: 'mustang@v3', cache });
entity.add(Perspective, { ladder: [
  { kind: 'full', minPx: 120 }, { kind: 'mesh', minPx: 5 }, { kind: 'impostor', minPx: 2.5 }, { kind: 'none', minPx: 0 } ], fade: 0.25, fill: 0.1 });

Rungs that are off are disabled too (the game's own enabling is restored when shown), so the scene does not walk their meshes. Better: backend.representLazy(entity, 'full', () => makeNode()) makes the meshes only when the rung is first shown and disposes them after 120 frames unused (keepFrames); give the size with backend.setBounds (an impostor does it from its atlas). On the 300 aircraft + 200 people scene this took the frame from 48 to 21 ms.

Impostors#

Baked at build time (tools/perspective/bake.mjs writes impostor/<id>.fpia into the library package and an impostor entry into the item; asked of in tools/asset-lib/ids.json) or at run time at the game's start (addImpostor with root; the result is cached in @ferret/assets' persistent cache under bakeKey). Both run the same code, so the two atlases are identical. Lighting is not baked: the shader lights the picture from the scene's lights with the response of Babylon's PBR, which was measured (ambient x1.0, sun x0.347, fog power 2.2).

Animation#

  • An entity's Animator is hooked by the service: Animator.poseGate returns false on frames when this rung does not pose its bones (poseEvery on the level). The graph still runs every frame and root motion is still measured, so states, notifies, clocks and where the object goes are exactly as without the module. Only the bones are skipped.
  • Animated impostors: bake with anim: { frames, pose(f) } (the atlas holds the frames side by side) and give the entity loop: { duration, frames } and optionally clock. Objects without a clock share the service's time, so a far crowd walks in step.

Measured thresholds#

tools/perspective/thresholds.mjs <dir> draws a model every way it has at a ladder of sizes from eight directions in clear air and writes impostor.thresholds (px at which each cheaper level is within tolerance of the richer picture; the none level by the object's contrast). ladderFromThresholds turns them into a ladder. A level the cheaper one covers is left out (orderThresholds).

Honest numbers (RTX 3050, 2026-11-10)#

  • 300 aircraft and 200 people: every object in full 81 ms and 8.4 M triangles; with the module and disabled rungs 48 ms, with lazy rungs 21 ms (1.7 M triangles, 92 draw calls against 466; 4 182 meshes in the scene against 40 738).
  • The impostor differs from the full model by about 0.2-0.3 (a blurred relative luminance error, 0 identical, 1 a missing object); a hand-made lod1 differs by 0.005. So the impostor is used where objects are 3-5 px, and above that a mesh serves better.
  • The governor raises thresholds up to its max and emits perspective:degrade.

In the examples#

examples/dunkirk draws its 170 abandoned lorries and guns through the module: each wreck is an entity with a ladder of the full model (one merged mesh drawn as an instance per wreck), the library item's baked impostor and a point, on the item's measured thresholds (tolerance 0.3, as for the aircraft), and the game runs the world's frame each frame so the service picks the rungs. The library atlas is in the model's own frame, and the wreck holds the model turned and shifted, so the impostor follows a node placed where the model's frame is (node of addImpostor). The burnt wrecks bake theirs at the start from their darkened copy. A mesh that has instances, and an instance, is baked from a plain copy made for the bake (drawn as it is, the instance buffer put every copy's matrix into each view, and the impostor showed an opaque disc).

Commands#

perspective.list, .stats, .force, .release, .thresholds, .debug (tints each level).

Unreal#

A Ferret game that uses the module builds for UE 5.8 with ue_build (see ferret-unreal-build): the pure core (size rule, ladder, hysteresis, budget, governor, thresholds) is translated as it is, and tools/ue-cpp/facade/ue-modules-perspective.ts gives the BabylonBackend of the module on UE's side through the Babylon facade, so the game code does not change.

What is the same on UE: the rung chosen for every entity, the lazy rungs (made on first show, dropped after keepFrames), setBounds, the budget and the counts of perspective.stats.

What differs:

  • The cross-fade is a switch at half the mix, not a dither.
  • An impostor shows the nearest baked view on a textured quad; there is no three-view blend and no ray march (the GLSL is not carried).
  • A point is a billboard disc.
  • Baking and measuring thresholds are editor-only: on UE the atlas is baked in the browser or at build time (tools/perspective/bake.mjs) and cooked into a texture with tools/perspective/ue/cook.mjs (.fpia to a PNG and a meta file).

The proof is node tools/perspective/ue/proof.mjs: it translates tools/perspective/ue/perspective-src/perspective.ts (seven aircraft from 45 to 4200 m), builds the game, plays it on the NVIDIA GPU and checks that each aircraft is on the rung the size rule gives (to 0.15 px), that the counts match and that the picture has sky and the red near aircraft. It passes on the RTX 3050 with UE 5.8.

Not covered#

  • Thresholds are measured for aircraft only; for a person from @ferret/humans take the aircraft's as a start and run perspective.thresholds on the model.
  • The inspector does not show the rung and size of an entity; use perspective.list, perspective.stats and perspective.debug.
  • The governor's return to lower thresholds when there is room is covered by unit tests, not by a long run of a real scene.

Craft growing out of the haze#

The ships and boats of examples/dunkirk (fleet-view.ts) are entities with the ladder full (3 px) and point, created once the model is loaded (the service needs its bounds). Craft are not made in sight: the fleet spawns them at 1.05 times the visibility from the eye, where the fog hides 99% of them, and removes boats only when they are out of sight. They then come out of the haze as the fog lets them through and the module draws the model by size; nothing appears at full colour from nowhere. Check by placing the camera at several distances from a fresh craft: the fog-hidden share at spawn, then screenshots.

Relevance and part culling#

  • RelevanceService (@ferret/view) says how soon each tracked thing can be in the frame: rel.set(id, x, y, z, radius, vx, vy, vz) each frame, rel.refresh(dt) after the camera is final, rel.of(id) is view (in the frame), soon (can be within the horizon), away (cannot) or gone (under half a pixel). Going finer is at once, going coarser waits hold (0.5 s). camStateOf(camera, engine) reads the camera.
    • Turn rate and horizon belong to the camera mode, and the game overrides them: rel.setMode('cockpit', { omega: 4 }) or the manifest's relevance block; what is not set is read from the camera (the observed peak turn rate, with a floor of 2.5 rad/s; the horizon is the time of a half turn, 0.6 to 4 s). Set rel.mode to the mode in force each frame.
    • Use the class for presentation work (skip or thin a pose, an animation, a wake, an effect) and for simulation fidelity that nobody can notice (Air.detail gives the flight model's sub-step rate: 200 Hz for view and soon, 100 for away, 60 for gone; the player's is always 200). Sound is not relevance: a thing behind the player can be heard.
  • PartCuller (@ferret/perspective): a model's small parts (rails, fittings) are separate draw calls however many instances there are. partCuller.add(root) (the library's onModelReady hook calls it for every model) gives each source mesh Babylon's own far level of nothing at the distance where the part covers minPx pixels (default 2; the games use 3). Skinned parts and thin instances are left alone. Switch off with ?minpx=0 to compare.