Global illumination
The renderer's indirect lighting - the IBL default, the real-time ProbeClipmap cascaded probe clipmap, the ScreenProbes screen probe gather over it, the Brick4 sparse volumetric lightmap and its bake, and the VolumetricLightMap component that carries one.
The renderer always applies an indirect term. There is no “GI off” switch: the deferred resolve and the forward transparency pass both ask for indirect lighting every frame, and the only question is where the answer comes from. That is one setting on the renderer.
import { ShadeIndirectLightingMode } from "@woosh/meep-engine/src/shade/renderer/ShadeIndirectLightingMode.js";
engine.graphics.renderer.indirect_lighting_mode = ShadeIndirectLightingMode.Brick4;
engine.graphics is the GraphicsEngine facade; .renderer is the escape hatch to the Renderer itself, and it is null before Engine.start() and again after stop(). Set the mode once the engine is running.
| Mode | Value | What produces the indirect light | What you author |
|---|---|---|---|
IBL | 0 | the environment - the scene’s atmosphere, or an environment map in its place - convolved into irradiance and roughness mips. The default. | nothing, or an environment map - see Sky & environment |
Brick4 | 1 | a baked sparse volumetric lightmap, sampled in world space | run the bake, ship the bytes, carry them on a VolumetricLightMap |
ProbeClipmap | 3 | a real-time cascaded octahedral probe clipmap traced on the GPU against the scene’s SDF brick clipmap and its surface material, gathered from per-mesh Model Surface Caches | nothing offline - configure via renderer.probe_clipmap (see below) |
ScreenProbes | 4 | a screen probe gather over the ProbeClipmap world: probes placed on the G-buffer every few pixels, traced through the screen and then the SDF bricks, with the world-space probes answering past their reach. It lights the opaque surfaces’ diffuse; everything else reads the world-space probes | nothing offline - configure via renderer.screen_probes, and renderer.probe_clipmap for the world (see below) |
The enum is a named export and the values above are the literal ones, so indirect_lighting_mode is safe to serialize as a number. 2 names no mode.
The selected mode also supplies the indirect light in the froxel irradiance
volume. Participating media and particles with lighting: true use that volume,
which combines this indirect contribution with shadowed direct lights.
Under ScreenProbes that contribution is the world-space probes’.
A scene with no environment renders unlit
This catches people before any of the modes matter. GraphicsEngine.set_scene gives the scene an environment when it arrives without an environment map:
if (scene.lights.environment === undefined) {
scene.atmosphere.enable_by_default();
}
The comment beside it states the reason plainly: the renderer lights with indirect by default, and a scene with neither an authored environment nor an active atmosphere renders unlit. enable_by_default() turns on scene.atmosphere, the scene’s physical sky, which draws the background and bakes the image-based light from the scene’s sun. It leaves an enabled you assigned yourself alone, so scene.atmosphere.enabled = false on a scene with no map is how you ask for a black environment - turning the sky off is the thing that takes a decision, not turning it on.
A scene that arrives with an environment map keeps it, and the map does both jobs in the atmosphere’s place. And the environment is not only the IBL source: the background is drawn from it in every mode, so a Brick4, ProbeClipmap or ScreenProbes scene still needs one for the sky behind the geometry. Sky & environment covers both sources.
Brick4 - the sparse volumetric lightmap
Brick4 is the technique meep 3 actually ships for baked indirect light. It is a tree of nodes over the scene’s bounds, each node carrying a 4x4x4 grid of probes (BRICK4_PROBE_RESOLUTION), branching 3 per axis (BRICK4_BRANCH_FACTOR, so 27 children per node). Probes hold second-order spherical harmonic colour packed into 28 bytes each, and adjacent nodes share the probes on their shared faces.
What makes it sparse is the expansion rule: a child cell only becomes a candidate for subdivision if it overlaps scene geometry, tested against a BVH over the scene. Empty air is never refined. Expansion runs shallowest-first and stops on whichever comes first - probe spacing that would fall below the requested cell_size, or an estimated GPU footprint that would exceed the memory budget. Density therefore follows surfaces, and the budget is a real ceiling rather than a hint.
Baking
import { brick4_bake_for_scene }
from "@woosh/meep-engine/src/shade/renderer/global_illumination/brick4/cpu/brick4_bake_for_scene.js";
import { MEGABYTE } from "@woosh/meep-engine/src/core/science/units/memory/MEGABYTE.js";
import { EngineHarness } from "@woosh/meep-engine/src/engine/EngineHarness.js";
const { tree, binary } = await brick4_bake_for_scene({
scene: EngineHarness.shadeScene(engine),
renderer: engine.graphics.renderer,
cell_size: 0.5,
max_memory_usage_bytes: 16 * MEGABYTE
});
| Parameter | Default | Meaning |
|---|---|---|
scene | required | the renderer’s Scene, not the ECS dataset. EngineHarness.shadeScene(engine) is the one the harness’s systems draw into. |
renderer | required | the Renderer - engine.graphics.renderer |
cell_size | 0.5 | smallest cell in world units. Capped, never coarsened: the value used is min(cell_size, largest_scene_dimension / 32), so a small scene silently gets a finer grid than asked for. |
max_memory_usage_bytes | 16 * MEGABYTE | ceiling on the estimated GPU footprint of the whole structure, probe data included |
It resolves to { tree, binary } - the intermediate CPU tree, and the ArrayBuffer in brick4’s GPU format. Four things about the call are worth knowing before you wire it into anything:
- It is asynchronous and needs a live device. The tree is built on the CPU; the probes are baked on the GPU. Bake after
Engine.start(), never before. - It downloads a file, unconditionally. The last thing it does before uploading is
downloadAsFile(binary, "lightmap.svlm"). There is no flag to suppress it. This is an authoring tool and belongs in a tools build or a dev-only route, not anywhere a player can reach. - Nothing loads
.svlmfor you. That extension appears exactly once in the engine - in the download call above. There is no asset loader for it and noGameAssetType. Reading the bytes back is the application’s job; anArrayBufferLoaderfetch (GameAssetType.ArrayBuffer) or a plainfetch(...).arrayBuffer()is all it takes. - It uploads the result itself. Before returning, it pushes
binarystraight into the scene’s lightmap buffer, so the picture changes the moment the bake finishes, with no component involved.VolumetricLightMapSystemwill not undo that upload until a claim on the scene actually changes.
The JSDoc is candid about cost: “Requires a fairly beefy GPU to run, confirmed to work on RTX 4090.” Treat it as an offline step run on a workstation, not something a game does at load time.
Carrying a lightmap at runtime
A scene has exactly one lightmap, and the VolumetricLightMap component is a claim on it. That is what gives a lightmap the lifetime an entity already has: it arrives when the level’s entity is built, it is saved with it, and it goes away when the entity does.
import { VolumetricLightMap } from "@woosh/meep-engine/src/engine/graphics3/VolumetricLightMap.js";
import { VolumetricLightMapSystem } from "@woosh/meep-engine/src/engine/graphics3/VolumetricLightMapSystem.js";
import { EngineHarness } from "@woosh/meep-engine/src/engine/EngineHarness.js";
await em.addSystem(new VolumetricLightMapSystem(
engine.graphics,
EngineHarness.shadeScene(engine)
));
const map = new VolumetricLightMap();
map.data = await (await fetch("/levels/keep/lightmap.svlm")).arrayBuffer();
const entity = ecd.createEntity();
ecd.addComponentToEntity(entity, map);
Both the component and the system are named exports. The system takes (graphics, scene) - the GraphicsEngine and the renderer’s Scene - and declares dependencies = [VolumetricLightMap].
| Member | Notes |
|---|---|
data | ArrayBuffer | null. Asserted to be an ArrayBuffer - a Uint8Array will not do. null is a normal state, not a broken one: it means the bytes are still loading, and lights the scene with nothing. |
version | bumped on every assignment to data |
toJSON() / fromJSON() | base64. Correct but expensive; the binary VolumetricLightMapSerializationAdapter (registered by populateEngineSerializationRegistry) is the cheap path. |
hash() | the byte length, deliberately not the bytes |
equals(other) | a real byte comparison |
typeName | "VolumetricLightMap" |
The contract around it has several edges that will otherwise cost you an afternoon:
- First claim wins; a second waits.
system.activeis the first component to link and stays so until it unlinks, at which point the next in line takes over. Last-writer-wins would make a level’s lighting depend on entity construction order. Swapping one map for another is therefore: remove the old component, then add the new one. - The payload is opaque. It is brick4’s own GPU structure - the format
brick4_to_gpu_structurewrites and the indirect-lighting shaders read. The component neither validates it nor knows how to make one. - The entity’s transform is ignored. A brick4 structure begins with the world-space bounds it was baked against, so the map already knows where it is. Moving the entity that carries it moves nothing, and the component does not need a
Transform64at all. - Mutating the buffer in place is invisible. The system watches
version, and only assignment moves it. Publish an in-place edit by assigningdataagain, even to the same buffer. - Unlinking uploads an empty map rather than leaving the last one in place. So do a
nullpayload and a zero-length buffer. There is no way to un-upload - in Brick4 mode the shader reads the buffer unconditionally - so a component that removed itself and left the scene lit exactly as before would make the lifetime a fiction. - The upload happens in
update(), notlink(). A lightmap can arrive before the renderer has a device, and a restart hands out a fresh context whose buffer is empty again. Both are the same question asked once a frame, so a link is eventually consistent by at most one frame. - Uploading a lightmap does not turn Brick4 on. Which technique the frame uses is
renderer.indirect_lighting_mode; a scene in any other mode never reads this buffer no matter what is in it. The upload is necessary and not sufficient.
The fused indirect pass, and screen-space reflections
In Brick4 mode the renderer collapses the diffuse pass, the specular pass and the indirect resolve into a single shader. It reads the G-buffer once, drops the intermediate rgba16float diffuse and specular targets, and shares one brick4 probe pair between the diffuse and specular evaluations - less bandwidth for slightly more cache pressure inside the pass.
There is no switch for it. Fusing is a policy of the frame graph, decided where the passes are built, and it applies whenever the mode is Brick4 and feature_ssr_enabled is false. The only control you have over it is feature_ssr_enabled, because SSR needs the standalone specular target.
Screen-space reflections work in Brick4 mode. Enabling feature_ssr_enabled selects the split path, and SSR uses the baked Brick4 volume for fallback specular lighting where screen-space information is insufficient. This costs the separate reflection and filtering passes; keep SSR off when the fused pass alone gives the result you need. Frame settings covers reprojection and filtering controls.
One neighbouring setting matters here: renderer.feature_ssao_enabled is true by default, and GTAO’s bent normals are what the indirect diffuse is evaluated against in every mode. Switch it off and the indirect passes fall back to the plain G-buffer shading normal. Frame settings catalogues that switch and the rest of them.
ProbeClipmap - real-time cascaded probe clipmap
ShadeIndirectLightingMode.ProbeClipmap (3) computes real-time diffuse and glossy indirect lighting on the GPU without an offline bake. While the mode is active, the renderer maintains these structures on the GPU under per-frame budgets:
- Per-mesh caches - sparse signed-distance-field bricks (
GPUGeometrySDFManager) and Model Surface Caches (GPUMeshSurfaceCaches), baked incrementally on the GPU for geometries in the scene’s SDF world. Bricks with identical bytes share one atlas slot. A surface cache is kept per (geometry, material) pair, in block-coded 4³ blocks where the field can hold surface, and only for the planes (diffuse albedo, metalness and roughness, emissive) the material makes vary; a constant plane is one value in the cache’s record, and a pair that varies in no plane is never baked. EveryShadedGeometryopts in by default throughShadedGeometryFlags.SDFWorld; callgeom.clearFlag(ShadedGeometryFlags.SDFWorld)before linking (or announce a change) to exclude a mesh. - Brick clipmap - cascades of 7³-voxel SDF bricks centred on the camera, scrolled and rebuilt from the per-mesh caches up to
brick_build_budgetcells per frame. Each built brick also gathers a texel of surface material per texel (GPUBrickMaterial) from the surface caches of the surfaces it holds; that is what a probe ray’s hit reads. - Probe clipmap - cascades of octahedral radiance and Chebyshev depth-moment probes centred on the camera, the probes the frame’s pixels want and shading samples. They trace
frustum_ray_budgetrays per frame at most (floor(frustum_ray_budget / rays_per_probe)probes), three quarters ofray_budget. - World cache - a second, denser probe set in the same windows: its spacing is
probe_spacing / 2^world_cache_spacing_shiftand its lattice2^world_cache_spacing_shifttimesprobe_cascade_resolutionper axis, so every frustum probe’s point is one of its points. A probe ray that hits where the frustum has no probe wants the world cache’s probes there, and only those hits sample it, never the shaded pixels. It traces the rest ofray_budget(world_ray_budget) with small4×4probes and never re-samples its neighbours. The renderer exposes it asworld_cachebeside the frustum’sprobe_clipmaponGPUProbeClipmapGI.
Like Brick4, ProbeClipmap uses a fused indirect resolve pass when renderer.feature_ssr_enabled is false, and switches to the split path when SSR is enabled so SSR can use the probe clipmap for fallback specular lighting. Pixels outside the clipmap or with zero probe weight fall back to IBL.
Configure the mode through renderer.probe_clipmap (ProbeClipmapSettings in src/shade/renderer/global_illumination/probe_clipmap/ProbeClipmapSettings.js):
| Field | Default | Meaning |
|---|---|---|
brick_cascade_count | 6 | Brick clipmap cascades, a positive integer; each cascade doubles the voxel size of the one inside it. Checked when the GI is built against the probe rays’ 5-bit cascade field (at most 32) and the device’s storage binding size. Structural (rebuilds GI on change). |
brick_cascade_resolution | 32 | Cells per axis of a brick cascade, a power of two (cells wrap by masking); the brick clipmap checks the rest against its layout and the device when built. Structural. |
brick_voxel_size | 0.125 | Finest brick cascade voxel size in metres. Structural. |
brick_build_budget | 4096 | Maximum brick cells built per frame, a positive integer. |
mesh_sdf_voxel_size | 0.125 | Per-geometry cached SDF voxel size in mesh units. Changing re-bakes cached geometries. |
mesh_sdf_max_bricks | 18 | Even integer from 2 capping bricks per axis of a geometry’s SDF before its voxel coarsens to fit; the SDF manager checks the bake’s ring against the device when it sizes it. Changing re-bakes cached geometries. |
mesh_sdf_bake_budget | 23 | Maximum mesh SDF bricks evaluated per frame while caches fill. |
surface_bake_budget | 512 | Maximum Model Surface Cache bricks surveyed and baked per frame, a positive integer: field surveys first, then new (geometry, material) pairs, then re-bakes of materials that changed. A frame with nothing to bake spends nothing. |
probe_cascade_count | 5 | Probe clipmap cascades, a positive integer checked against the layout and device when built; each cascade doubles probe spacing. Structural. |
probe_cascade_resolution | 32 | Probes per axis of a probe cascade, a power of two. Structural. |
probe_spacing | 1 | Metres between probes in the finest cascade. Structural. |
radiance_resolution | 16 | Octahedral radiance texels per side of a probe (power of two, at least 2; the atlas checks a tile against the device). Structural. |
depth_resolution | 8 | Octahedral depth-moment texels per side of a probe (power of two). Structural. |
rays_per_probe | 256 | Rays a frustum probe traces per update: a power of two, at most 64 · radiance_resolution² and 64 · depth_resolution². Below radiance_resolution² an update covers a spread subset of the texels; above it, each texel averages several rays. |
ray_budget | 131072 | Maximum rays both probe sets trace per frame. The read-only frustum_ray_budget is floor(0.75 · ray_budget) and must be at least rays_per_probe; world_ray_budget is the rest and must be at least world_cache_rays_per_probe. |
world_cache_spacing_shift | 2 | k in the world cache’s spacing probe_spacing / 2^k and lattice 2^k · probe_cascade_resolution; a positive integer. Structural. |
world_cache_radiance_resolution | 4 | Octahedral radiance texels per side of a world cache probe (power of two, at least 2). It is only projected to SH, never sampled directly. Structural. |
world_cache_depth_resolution | 4 | Octahedral depth-moment texels per side of a world cache probe (power of two); a hit’s pick weighs the probe by it. Structural. |
world_cache_rays_per_probe | 16 | Rays a world cache probe traces per update (power of two, at most 64 · each world cache resolution squared). |
response_frames | 30 | Frames over which a neighbour’s history goes stale for history_resampling: a neighbour last updated Δt frames ago weighs e^(−Δt / response_frames). A probe’s own history blends each new sample the same way however long ago its last update was. |
history_resampling | false | Each frustum probe update also re-samples the histories of four of the probe’s 26 neighbours in its cascade, where their depth says they see the same point. Traces no extra rays; trades some spatial blur for stability. Takes effect the next frame. |
ray_resampling | false | Each frustum probe update also takes one neighbour’s rays from the same frame, a neighbour updated this frame chosen by its radiance and how well the probe sees it, keeping every ray whose hit (or sky) the probe’s depth confirms. Traces no extra rays; brings in light the neighbour found this frame. Takes effect the next frame. |
debug_view | ProbeClipmapDebugView.None | Debug view before TAA (None: 0, MeshSDF: 1, MeshSurfaces: 2, BrickClipmap: 3, IndirectDiffuse: 4, IndirectSpecular: 5, BrickMaterial: 6, BrickMetalRough: 7), imported from shade/renderer/global_illumination/probe_clipmap/debug/ProbeClipmapDebugView.js. |
ScreenProbes - screen probe gather
ShadeIndirectLightingMode.ScreenProbes (4) is a Lumen-style screen probe gather run over the ProbeClipmap world (GPUScreenProbeGI in src/shade/renderer/global_illumination/screen_probe/GPUScreenProbeGI.js). It changes one thing about that mode: the diffuse light of the opaque surfaces comes from probes placed on the G-buffer every frame, a few pixels apart, instead of from the world-space probes. Everything ProbeClipmap maintains still runs underneath - the per-mesh caches, the brick clipmap, the world light clusters and both sets of world-space probes - and renderer.probe_clipmap still configures it.
import { ShadeIndirectLightingMode } from "@woosh/meep-engine/src/shade/renderer/ShadeIndirectLightingMode.js";
const renderer = engine.graphics.renderer;
renderer.indirect_lighting_mode = ShadeIndirectLightingMode.ScreenProbes;
renderer.screen_probes.downsample_factor = 8; // four times the probes, and the rays
A frame of the mode, after the world’s own update:
- Place. One uniform probe stands in every tile of
downsample_factorpixels, at a point in the tile that cycles over 8 frames. Adaptive probes follow at half that spacing, then a quarter, down toadaptive_min_downsample_factor, wherever the probes placed so far would not interpolate to a point of the finer grid - depth edges and thin geometry. There are as many adaptive slots as uniform probes, so a frame never holds more than twice the uniform count. - Trace. Each probe casts
octahedron_resolution²rays. Withimportance_sampling, rays are taken from directions the pixels around the probe cannot see and spent where the probe’s light was brightest last frame. Withscreen_traces, a ray is marched through the depth buffer’s hierarchy first, and a hit there is lit by the last frame’s colour of the opaque surfaces - a bounce more each frame. What the screen cannot answer goes on through the SDF bricks, where a front-face hit is shaded as aProbeClipmapprobe ray’s is: emissive, the directional lights shadowed by rays through the bricks, one local light of the hit’s cluster cell, and a bounce from the world-space probes. A ray that passes the probe’s reach unoccluded reads the world-space probes along its direction; where there are none, the environment. - Filter. The rays are composited into each probe’s radiance,
spatial_filter_passespasses blend it with the neighbouring probes’, and each probe is projected to spherical harmonics. - Resolve. Each pixel interpolates the probes around it at its bent normal, and a temporal filter accumulates the result over up to
temporal_framesframes - fewer where the rays saw something move. A pixel no probe covers takes its covered neighbours’ mean, or its history. - Capture. The lit colour of the opaque surfaces, before fog and transparency, is kept for the next frame’s screen hits.
Only the deferred opaque diffuse is the screen probes’. The rest of the frame reads the world-space probes exactly as under ProbeClipmap:
| Reader | Indirect light under ScreenProbes |
|---|---|
| Opaque surfaces, diffuse | the screen probes |
| Opaque surfaces, specular | the world-space probes, the environment where no probe weighs anything; SSR when feature_ssr_enabled is true, falling back to the same probes |
| Transparent surfaces and water | the world-space probes |
| The froxel irradiance volume | the world-space probes |
As with ProbeClipmap and Brick4, the two lobes resolve in one fused pass while renderer.feature_ssr_enabled is false, and on the split path when it is true.
Configure the gather through renderer.screen_probes (ScreenProbeSettings in src/shade/renderer/global_illumination/screen_probe/ScreenProbeSettings.js):
| Field | Default | Meaning |
|---|---|---|
downsample_factor | 16 | Pixels between the uniform probes along each axis, a power of two. Every ray and every pixel’s interpolation scales with the probe count, so the cost goes as the inverse square of this. Structural (rebuilds the screen probes on change). |
octahedron_resolution | 8 | Texels per side of a probe’s radiance, 4, 8 or 16; a probe casts this squared rays a frame. Structural. |
adaptive_min_downsample_factor | 4 | The finest spacing adaptive probes are placed at, in pixels, a power of two. At or above downsample_factor no adaptive probe is placed. Structural. |
importance_sampling | true | Rays follow where a probe’s light comes from: each texel is weighed by the cosine lobes of the pixels around the probe and by the light its history saw there. Off, every texel takes one ray. Takes effect the next frame. |
screen_traces | true | Rays are traced through the screen first and lit by the last frame’s colour where they hit. Off, every ray is traced through the bricks alone. Takes effect the next frame. |
spatial_filter_passes | 3 | Passes of the filter between neighbouring probes, a non-negative integer; 0 runs none. Takes effect the next frame. |
temporal_frames | 10 | The most frames the temporal filter averages a pixel’s diffuse over, at least 1: a pixel whose history holds blends its new light by 1 / (1 + frames). Takes effect the next frame. |
debug_view | ScreenProbeDebugView.None | Debug view before TAA (None: 0, IndirectDiffuse: 1, ProbePlacement: 2, TraceKinds: 3), imported from shade/renderer/global_illumination/screen_probe/ScreenProbeDebugView.js. While it is None, renderer.probe_clipmap.debug_view applies in this mode too. |
Three things to plan around:
- Its history is tied to its structure and its view. The screen probes’ resources are made for the view’s internal resolution and the three structural settings; a change to any of them - a step of the render resolution included - makes them again, and the gather starts from nothing.
renderer.indicate_view_change()drops the history too, for a camera cut. - The probe count is bounded by the device. The probes’ atlas is
ceil(width / downsample_factor) · octahedron_resolutiontexels wide and must fitmaxTextureDimension2D, and each per-probe buffer must fitmaxStorageBufferBindingSize; both are asserted when the resources are made. A largerdownsample_factoror a smalleroctahedron_resolutionis the way under. - It costs the whole of
ProbeClipmapand then its own passes. The world it traces is not optional, so nothing inrenderer.probe_clipmapgets cheaper for running the gather on top.
Related
- Real-time Global Illumination example - live scene demonstrating
ShadeIndirectLightingMode.ProbeClipmapand local point light bounce on the Flying World island - Sky & environment - the atmosphere that lights a scene by default, and the octahedral environment map that replaces it
- Lights & shadows - the direct half of the lighting
- Rendering overview - the frame the renderer builds, and the
GraphicsEnginefacade