# 3D rendering

Make a 3D game look the way you want: models and shapes, materials, lights and shadows,
the sky, fog, ambient occlusion, bloom, smooth edges and effects. This page is the tour,
with one scene you can copy; every field is in the reference ([3d](../reference/3d.md)
and [render](../reference/render.md)).

![The lit-room sample at step 120](../files/examples/lit-room/shots/dusk.png)

That frame is the lit-room sample, and `frost test`
holds every run of it to the same checksum and this picture. Its project file:

```toml
# example: lit-room/game.toml
# The 3D rendering guide's scene: a courtyard at dusk under the engine's Sky, lit by its
# sun with shadows, two lamps that come on as the sun sets, a glowing orb that blooms,
# ambient occlusion in the corners and smoothed edges. The script moves the time of day.
#
#   frost dev game.toml
#   frost run --headless --steps 120 --shot shot.png game.toml
space = "3d"
scene = "court.toml"
scripts = ["dusk.luau"]
seed = 1
shots = [{ name = "dusk", step = 120 }]
replay = { steps = 120, checksum = "6614dbbec930932f", engine = "0.0.1 r280" }
# The run's checksum on the way there, written by `frost test --write`: a test that
# fails says between which two of these steps the game first went another way.
checkpoints = []

[window]
size = [640, 360]
background = 0x101318

# Four samples a pixel on edges, a soft glow above the threshold, and darker corners.
[render]
msaa = 4
bloom = { threshold = 0.9, strength = 0.8 }
ao = { radius = 1.0, strength = 0.9 }
```

## A 3D game

`space = "3d"` in the project file makes the game 3D: its `Transform` has a place
(`x`, `y`, `z` in metres, y up), a turn (a quaternion, or `turn = { x, y, z }` in degrees
in a scene) and a scale. A `Camera` looks along its own -z; the lowest thing with one
sees. With no `[render]` table the frame is the sky, then the meshes, then the 2D layers
over them (the interface, text).

## Meshes and shapes

`Mesh = { file = "art/hero.glb" }` draws a glTF model (`.glb` or `.gltf`), cooked once
into the engine's own format and kept in a cache. `Mesh = { file = "box" }` draws one of
the engine's shapes with no file: `box`, `sphere`, `plane`, `capsule`, `cylinder`, each a
metre across, so the thing's scale sizes it. A model only a script names goes in the
project's `models`.

## Materials

A model's own glTF material is used as written: base colour, metal and roughness, normal,
occlusion and glow pictures, alpha masks and blending. A thing can change its look like
any value: `Mesh.roughness`, `Mesh.metallic` (0 to 1; below 0, the model's) and
`Mesh.blend`. `Tint` multiplies the colour of a thing and what is under it, and
`Opacity` fades it. `Emission = { color, strength }` is a thing's own light, regardless
of lighting; above 1 it saturates and, with bloom, glows.

For a look no glTF material gives, `[render.materials]` declares up to eight of your own,
each one WGSL function for the vertex, the fragment, or both, and a `Material` component
gives a thing its numbers. A material has no clock: what moves, moves because a script
wrote its numbers this step, so a frame put back is the same picture.

## Lights

`Light = { kind = ..., color, energy }`:

| `kind` | Lights |
| --- | --- |
| `"ambient"` | Everything, evenly |
| `"sun"` | Along its own -z, from infinitely far |
| `"point"` | From its place, fading to nothing at `range` |
| `"spot"` | A cone along its own -z: whole within `inner` degrees, nothing past `outer` |

The first eight points and spots, by thing, are lit; more are ignored. A point or spot
with `night = true` shines only once the Sky's sun is down, smoothly over dusk.

## Shadows

A sun with `shadows = true` (or a `Sky` with `shadows = true`) draws a 2048-pixel shadow
map of the ball `range` (the Sky's `reach`) metres round a point that far ahead of the
camera, soft over one map pixel. One spot can cast shadows too, from a 1024-pixel map of
its cone, and can project a picture (`cookie = "art/mask.png"`). Points cast none.
Shadows are off by default and cost nothing until asked for.

## The sky

`Sky = { time = 15.5 }` is a day: the hour sets the sun's direction, colour and strength,
the sky's colours and the ambient light, with no picture. The sun rises along +x at 6,
stands 50 degrees up towards -z at noon and sets along -x at 18, and `turn` turns that
path about y. At night a dim moon shines from the other side. `time` is a field like any
other, so the sample's script moves it, and a recording, a seek and a checksum all see it:

```lua
-- example: lit-room/dusk.luau
-- The sun goes down: an hour of the game's day every two seconds. The Sky is a
-- component like any other, so a recording, a checksum and a seek all see the time.
local HOURS_A_SECOND = 0.5

function step(dt)
    local sky = find("sky")
    set(sky, "Sky", "time", get(sky, "Sky", "time") + HOURS_A_SECOND * dt)
end
```

`haze` and `haze_far` fade meshes and ground into the horizon's colour between two
distances, melting the world's edge into the sky. Metal mirrors the sky above it,
blurred by its roughness (it reflects no other things). Without a Sky, `Fog = { color,
density }` fades lit meshes towards a colour instead.

A physically based sky (sun and sky colours from the atmosphere itself) is
[coming next](../soon/physical-sky.md).

## Ground

`Ground = { file = "art/hill.ground.toml" }` is a height field from a grey PNG: drawn in
tiles of 64 by 64 cells, each with only as much detail as its distance needs, in one
colour or up to four pictures blended by slope and height. `ground.at(x, z)` gives the
height, the slope and each picture's weight under a point, and `Collider = { shape =
"ground" }` makes the physics walk on it.

## Ambient occlusion

`[render] ao = { radius = 1, strength = 1 }` darkens the ambient light and the sky's
reflection where surfaces close a point in: corners, a crate on a floor. Sun, lamps,
shadows and emission are untouched. Its sample pattern is fixed, so it never shimmers and
every run draws one picture. It is screen-space: what is hidden or off screen shades
nothing.

## Bloom, float light and tone mapping

`[render] bloom = { threshold = 0.85, strength = 1.5 }` spreads what is brighter than the
threshold into a soft glow. For light above 1 kept through the frame, draw the sky and
meshes into a float (`rgba16f`) target and read it with the `aces` effect, which tone
maps it: the [render reference](../reference/render.md) shows the passes.

## Smooth edges

`[render] msaa = 4` takes four samples a pixel on the sky and meshes, with alpha to
coverage on masked materials. `cull = true` and `samplers = true` follow a glTF's
double-sided flags and its texture filters; both are off by default so older games draw
as they did.

## Your own frame graph and effects

`[render]` can name targets and passes: what draws where (`layers`, `meshes`, `sky`) and
effects that read targets (`copy`, `bright`, `blur`, `add`, `aces`, `panini`). Up to
eight effects of your own are WGSL files with one function, `effect(at, size, threshold,
strength)`, that returns a pixel's colour. A `Pass` component sets a pass's numbers from a
script each frame. `render.passes` over the protocol lists the graph that is running.

## Explain a pixel

`frame.at` over the protocol says what is drawn at a pixel and how it is lit: the
triangle of which mesh and material, then one line a light with its share and why.
Asked of the lit-room sample at step 120, at a pixel of its floor:

```json
{"name": "floor", "mesh": "box", "material": "shape", "distance": 11.324,
 "light": [
  {"kind": "sky",   "name": "sky",        "share": 0.586, "why": "the sky's light, everywhere"},
  {"kind": "sun",   "name": "sky",        "share": 0.0,   "why": "the sky's sun: in shadow"},
  {"kind": "point", "name": "lamp_left",  "share": 0.414, "why": "3.123 away, 0.141 of its light left there"},
  {"kind": "point", "name": "lamp_right", "share": 0.0,   "why": "out of its reach: 7.811 away, it reaches 4.5"},
  {"kind": "haze",  "name": "sky",        "share": 0.0,   "why": "11.324 away, nearer than the haze begins (15)"}],
 "unread": ["ambient occlusion (`[render] ao`) is not read; ..."]}
```

It needs no graphics device and changes nothing. `frost blame GAME.toml --pixel X,Y` asks
the same of a 2D frame from the command line; [debugging](debugging.md) has more.

## Every backend, one picture

Native, WebGPU and WebGL2 draw the same picture, with two known exceptions: a smoothed
edge's pixel may be one level off in WebGL2, and two surfaces at exactly one depth (a
pillar standing exactly on a floor) may tie differently, so sink a thing a hundredth of a
unit into what it stands on. Model pictures over 2048 on a side are halved for the web
(`W0455` says which). See [status and limits](../status.md) for what 3D does not do yet.
