---
title: Bake a level's lighting
summary: Read before baking a level's lightmap for the first time, or when a bake comes out dark, noisy, seamed or too big — one complete level from an empty project to a baked one (lamps and a glowing panel included), then the order of steps on a real level, which knob decides what, and how to read the bake's own numbers before looking at pixels.
area: 3d
globals: []

---

# Bake a level's lighting

The reference for every call and flag is [lightmap.md](/docs/api/3d/lightmap.md). This recipe is one
complete level from an empty project to a baked one, then the order of work on a real level and the
decisions along it, as proven on two levels of the `lightcheck` project (a school: 1 270 statics,
76 lamps, two atlas pages; a shooting range: 2 036 statics, 42 lamps, one page) on 2026-10-27.

## 1. The level: a scene file that names its bake

In a project (`lecodes init my-level --desktop` makes one), the level is one scene file — a floor,
three walls, three boxes, the sun, two lamps (one of them a ceiling panel) and a glowing panel whose
material is its light — and `main.ts` opens it. These are the two files of
bake-a-level/ next to this page:

```ts
// level.scene.ts
export default defineScene({
  env: { skybox: 'environment', lightmap: bake('./assets/lightmap/level.bake') },
  nodes: {
    eye: { position: [0, 2.5, 9], eulerAngles: [-10, 0, 0], aspects: [use(CameraPlace, { fov: 60, active: true })] },
    sun: { light: { kind: 'sun', direction: [-0.4, -1, -0.3], intensity: 100000 } },
    floor: { mesh: { kind: 'box', size: [20, 0.2, 20] }, material: { lit: { color: '#9a9a9a', roughness: 0.9 } }, position: [0, -0.1, 0] },
    walls: {
      children: {
        back: { mesh: { kind: 'box', size: [20, 4, 0.3] }, material: { lit: { color: '#c8c0b0' } }, position: [0, 2, -10] },
        left: { mesh: { kind: 'box', size: [0.3, 4, 20] }, material: { lit: { color: '#c8c0b0' } }, position: [-10, 2, 0] },
        right: { mesh: { kind: 'box', size: [0.3, 4, 20] }, material: { lit: { color: '#c8c0b0' } }, position: [10, 2, 0] },
      },
    },
    boxes: {
      children: {
        a: { mesh: { kind: 'box', size: 1 }, material: { lit: { color: '#8a5a2b' } }, position: [-2, 0.5, -1] },
        b: { mesh: { kind: 'box', size: [1.5, 2, 1.5] }, material: { lit: { color: '#8a5a2b' } }, position: [2, 1, -3] },
        c: { mesh: { kind: 'box', size: [3, 0.8, 1] }, material: { lit: { color: '#5b6b7a' } }, position: [5, 0.4, -6], eulerAngles: [0, 30, 0] },
      },
    },
    // a bulb: a point light, baked as a small sphere
    lampA: { light: { kind: 'point', intensity: 1500, color: '#ffd9a0', range: 10 }, position: [-4, 3, -6] },
    // a ceiling panel: the bake lights from a 1.2 x 0.3 m rectangle facing down (the node's -Y; eulerAngles tilt it)
    panel: { light: { kind: 'point', intensity: 4000, range: 8, bakeArea: [1.2, 0.3] }, position: [5, 3.8, -7] },
    // a glowing panel: a 1.2 x 0.6 m plane facing down whose material glows at 1600 nits — the bake turns the glow into
    // a rectangle lamp (`emissiveBake: false` would keep it drawn and light nothing)
    glow: {
      mesh: { kind: 'plane', normal: [0, -1, 0] }, scale: [1.2, 1, 0.6], position: [-3, 3.8, 3],
      material: { lit: { color: '#222', emissive: '#ffe9c8', emissiveNits: 1600 } },
    },
  },
})
```

```ts
// main.ts
import level from './level.scene'

level.open()
```

What makes it a baked level:

- **`env.lightmap: bake('./assets/lightmap/level.bake')`** names the bake, relative to the scene
  file. Nothing is there yet, and nothing has to be: before the first bake the level compiles and
  runs unbaked (`Lightmap: no bake at assets/lightmap/level.bake yet`), and the path is where the
  bake writes. The `.bake` the bake writes is a manifest of every file it wrote; the next compile
  reads it, so the scene never lists a page.
- **The statics are derived**: every `mesh` / `model` node that nothing moves (no `Physics`, no
  `CharacterController`) is baked; `lightmap: true | false` overrides the rule per node.
- **Lights in physical units**: the sun in lux, lamps in lumens, the sky as the scene's IBL. A lamp
  is a `light: { kind: 'point' }` node; `bakeArea: [w, h]` makes the bake light from a rectangle
  (a ceiling panel) in the node's XZ plane, emitting along its −Y — `eulerAngles` on the node turn it
  (`[-90, 0, 0]` faces it along +Z, a wall lamp).
- **Lightmap UVs**: none to prepare. Meshes carry them; `lecodes lightmap bake` unwraps every
  referenced GLB that has no `TEXCOORD_1` before it starts (`lecodes assets unwrap` by hand) and
  never touches a file that has them.
- **Emission is light too**: the `glow` node's `lit` material glows (`emissive`) at a luminance of
  its own (`emissiveNits`, cd/m²), and the bake lights the level from it like from the `bakeArea`
  lamp. A GLB's emissive materials do the same at the level's `emissiveNits` (what emission 1.0 is
  worth). Each material has its own switch — `emissiveBake: false` in the `lit` options, the glTF
  extras the Unity importer writes from a material's "Global Illumination" flag — and
  `bake('…', { emissiveBake: false })` turns all of them off when the panels are decoration and the
  lamps under them do the lighting ([lightmap.md › Emission](/docs/api/3d/lightmap.md#emission)).

## 2. Bake it: `lecodes lightmap bake`

In the project folder, with no flags:

```
lecodes lightmap bake
```

The compile finds the `bake()` the entry (`main.ts`) reaches, the app runs in `lecodes-bake` (the
CLI downloads it on first use; it needs a GPU with hardware ray tracing) and the bake fires when
the level has loaded. The log of this level, the lines to read:

```
Target: assets/lightmap/level.bake (the first bake: nothing there yet)
  glow: Material.lit glow of 1600 nits (emission x 0.0417 of the level's 38400)
  1 emitter material(s), 0 vetoed (emissiveBake: false) - emitting: glow
  8 instances in 8 records — a record per mesh, 0 instance(s) of several (8 receivers, 0 without lightmap uvs), 86 triangles (0 alpha-tested over 0 map(s))
  emissive surfaces: 1 lit region(s) on 1 instance(s) became rectangle lamps: 0.7 m2 emitting, 3029 lm in all (3029 lm a region), emission 1.0 = 38400 nits; the regions fill 100 % of their rectangles
  sun 100000 lux, colour (1.00 1.00 1.00), direction (-0.358 -0.894 -0.268), disc radius 0.27°
  3 lamp(s) baked (64 shadow rays each): 1 point, 2 rectangle(s), 3515 lm a rectangle on average
  atlas 4096 x 1 page(s): 8 rects, 5.00 cm texels, 6% of the last page used
  done: 1 page(s) of 4096 at 5.00 cm, 560400 texels, 53 probe(s), 7.8 s
✓ baked → assets/lightmap/level.bake + level-light.ktx2 + level-aux.ktx2 + level-probes.ktx2 (53 probe(s) of 32² at 4 m) + level.lgrid (light grid 21 x 9 x 21, 3951 cell(s) in free space)
```

- `Target:` — where the files go: next to the `.bake` the scene names.
- `N instances in N records` — the statics the bake saw. A static missing here is a node something
  moves, or one built by code that never called `Lightmap.add`.
- **`1 emitter material(s), 0 vetoed`** — the glowing materials the bake found, by name (a Mesh by
  its key, a GLB material by its glTF name), and the ones their own `emissiveBake: false` keeps out.
  `emissive surfaces: 1 lit region(s) … 3029 lm` is what became of them: the glow's lit region as a
  rectangle lamp of 3029 lumens (1600 nits over 0.72 m²).
- **`3 lamp(s) baked: 1 point, 2 rectangle(s)`** — every lamp of the level: the bulb, the panel's
  `bakeArea` and the glowing panel. A lamp is baked only if it exists when the level finishes
  loading; `0 lamp(s)` on a level with lamps is a bug in the level, not in the bake. Read this line
  after every bake.
- `atlas 4096 x 1 page(s): …, 5.00 cm texels` — the pages and the texel the level settled on.
- `N record(s) … get no rect` (not on this level: nothing in it is small or thin) — what left the
  atlas for ambient cubes (section 6).
- `✓ baked → …` — the files written. Nothing to paste anywhere: the manifest names them.

`--logs` forwards the bake host's whole output; `--dry-run` stops after the layout (section 4).

## 3. Run it, and typecheck

```
lecodes desktop run
```

The next compile stages every file the manifest names and the level opens baked (its log says
`[lightmap] bound 1 page(s) of 4096 at 5.00 cm …`). The scene editor and a typecheck see the same
grammar:

```
bunx -p typescript tsc --noEmit
```

Rebake (the same command) after moving a static, a light or the sun: keys in the bake are node
paths, so a rename or a re-nest means a rebake too.

## 4. The layout before the light: `--dry-run`

```
lecodes lightmap bake --dry-run
```

`--dry-run` lays the atlas out and stops: seconds, nothing written. Read three lines of its log:

- `atlas 4096 x 2 page(s): 945 rects, 5.00 cm texels, 28% of the last page used` — pages and the
  texel the level settled on. The bake never takes more pages than `--pages`; it coarsens the texel
  instead and says so (`asked 5.00 cm, … fit 5.95 cm`).
- `fill: … texels in rects … = 76% of the rects` — how much of the rect area holds texels. Under
  50 % means the unwraps are shredded into micro-charts; the bake packs charts per mesh, so this is
  about the files' geometry, not the packing.
- `N record(s) … get no rect` — what leaves the atlas for ambient cubes: the small (`--min-texels`),
  the thin (`--thin-texels`), your `--cube` patterns. Each line names the kinds and counts, so a
  surprise (a door, a cabinet) shows here before it shows in a render.

Then the real bake, the same command without `--dry-run`. Times to expect on an RTX 4070 Ti: the
range 29 s, the school 53 s — the GPU part is a fifth of it, the rest is writing the pages (BC6H,
mips, probe maps). A project whose entry reaches several levels' `bake()` (a menu that opens any of
them) bakes one per run: `--name <stem>` picks it, `--entry <file>` bakes from another entry.

## 5. Texel density and pages

Ask finer than you need with the pages capped: `--texel 0.04 --pages 1` on a level that fills
83 % of one page at 5 cm settles at 4.58 cm (`912 records fit 4.58 cm on 1 page(s)`). Two things
the same log line tells: more rects (a finer texel keeps more records out of the thin rule) and
fewer "finer texels for small surfaces" (`--detail-texels` gives small surfaces up to 3x the
density only in the room the pages have left). If small props matter more than walls, keep the
texel and raise `--detail-texels` instead.

## 6. Round and narrow things: ambient cubes, not texels

A tube 5 cm across at 5 cm texels is a strip of one to three texels around a round surface: it
cannot hold the light's change around the tube, and the two edges of the strip meet on the uv seam
with different light — two probes 5 mm apart on the range's railing read 334 and 253 lux. The bake
takes such meshes out of the atlas by itself (`--thin-texels 4`: no chart wider than four texels
across) and lights them where they stand with ambient cubes: six sides, a cube per metre along a
long one (`--object-slab`), blended per pixel. On the range the rule took 106 records (pipes, fan
blades, gun barrels, floor stripes, the railing), in the school 23 (blinds, one thin pillar), and no
crate or door. To take more by hand — every pipe, say — use the key patterns:

```ts
env: { lightmap: bake('../assets/range/lightmap/range.bake', { cube: ['SM_pipe', 'SM_fence'] }) }
```

or `--cube "SM_pipe|SM_fence"` on the command, or `lightmap: 'cube'` on one node. A cube has no
shadow of the object on itself, so a cabinet with niches is not a candidate; a railing is. Check
the verdicts in the dry run before you bake.

## 7. Reading a bad spot

Find the texel first, then the layer. In a baked level a probe on the surface under the cursor
(`Lightmap.probe(x, y)`, logged as `[probe] key … page 0 texel 2723,1888 uv1 … world …`) names
the record and the texel. Then bake once more with `--debug-dir .lightmap/<level>` (a `.lightmap/`
folder is kept out of the compile) and `lecodes lightmap inspect --key <record> --layer
irradiance,sun,sky,lamps,bounce,visibility`: the PNG per layer shows which source the spot belongs
to. Typical findings: the `sun` layer alone — a crack between modules lets the sun through;
`bounce` alone, one texel — a firefly, gone with another `--seed` or a denoised `--denoise no-sun`;
a bright first row of a rect — the chart's edge texel mixing with its neighbour, the thin rule or
`cube` for that mesh. Two bakes with different `--seed` (the second with `--out`, so it does not
replace the level's) through `lecodes lightmap diff` measure noise per record; a dark room's
bounce reads 100 %+ noise at 64 bounce rays and halves with each doubling, which is what
`--bounce-rays 128` and the denoiser are for.

## 8. Movers and the rest

Everything that moves reads the light grid (`--volume 1`, `<stem>.lgrid`): the baked ambient light
per cubic metre, with links that stop it bleeding through walls; the real-time sun and the unbaked
lamps (`light: { kind: 'point', baked: false }`) stay on movers. Reflection probes
(`--probe-spacing 4`) are placed one per room (`--probe-indoor` keeps only the roofed ones on a
level that is mostly outdoors). Neither needs a rebake when a mover changes — only the statics,
the lights and the sky do.

## 9. Which GPU

`lecodes-bake` traces with OptiX on an NVIDIA card and with Vulkan ray query on any other card that
has hardware ray tracing (Radeon RX 6000 and up, Arc), with the driver alone installed; `--tracer`
forces one. The two agree to float rounding (the school: 0.004 % on per-record means). The AI
denoiser is OptiX's on NVIDIA and Intel Open Image Denoise elsewhere (`--denoiser`); the first
Vulkan run on a machine spends a few seconds compiling the pipeline, cached afterwards.

## Traps

- **A lamp that is not there when the level finishes loading** bakes nothing and warns nothing
  beyond `0 lamp(s) baked`. Read that line every time.
- **A glowing panel outdoors at noon gives next to nothing.** This level's 1600-nit panel puts
  64 lux on the floor under it, against some 170 000 lux of sun and sky there — and under the
  camera's sunlit exposure (EV100 15) it draws as a dim warm rectangle, as a real one would. Its
  light shows in the bake's `lamps` layer (`--debug-dir`, then `lecodes lightmap inspect --layer
  lamps`), and it carries indoors, at dusk, at night. Checked: the floor texel under it reads 64.4
  lux in that layer with the panel baked, 0.0 with `emissiveBake: false` on it.
- **Keys are node paths.** Renaming or re-nesting a node, or giving it physics, loses its records
  silently — rebake after a scene edit.
- **Old bakes do not load after the bake tool changes its files** (`.bake` version, `uv1`): the SDK
  warns `rebake` and leaves those statics unlit. One bake per level fixes it.
- **A file the manifest names that is gone** (a page deleted by hand, a half-copied folder) fails
  the compile of the level, naming the file: rebake.
- **A `.fixed.glb` next to a `.glb`** is a leftover of the old doctor flow; the scene loads the
  original, the bake unwraps it in place. Delete the copies.
- **`--debug-dir` inside the project** is swept into the bundle unless it is a `.lightmap/` folder;
  the layers are gigabytes.
