# Pixel Art

Draw the scene at a low resolution, snap it to a palette with ordered dither and outlines, and show it as crisp squares.

`post.pixel_art` turns any scene into low-resolution game art. The scene is drawn at a fraction of the frame, every art pixel is voted into one palette colour and the result is drawn with nearest-neighbour sampling, so edges stay hard at any output size.

```json title="apps/docs/examples/pixel-art.json"
{
  "camera": { "position": [0, 2.2, 9], "target": [0, 1, 0], "fov": 34 },
  "lighting": { "rig": "moonlight" },
  "environment": {
    "background": {
      "linear_gradient": { "from": "#3a2858", "to": "#0d0a14", "angle": 180 }
    }
  },
  "post": {
    "tone_mapping": "none",
    "vignette": 0,
    "bloom": 0.2,
    "pixel_art": {
      "size": 4,
      "supersample": 2,
      "dither": 0.5,
      "outline": 0.8,
      "palette": [
        "#07050b",
        "#15101e",
        "#2a1d42",
        "#4d3470",
        "#8a62a8",
        "#4a1838",
        "#b02e72",
        "#ff4fb0",
        "#1c2630",
        "#465562",
        "#8c9aa0",
        "#b8c0c4",
        "#6a4a26",
        "#f0b45c",
        "#ffe0a0",
        "#243a34"
      ]
    }
  },
  "materials": {
    "ground": { "color": "#243a34", "roughness": 1 },
    "wall": { "color": "#8c9aa0", "roughness": 0.9 },
    "roof": { "color": "#b02e72", "roughness": 0.8 },
    "pine": { "color": "#243a34", "roughness": 1, "shading": "flat" },
    "moon": {
      "color": "#ffe0a0",
      "emissive": "#ffe0a0",
      "emissive_intensity": 2,
      "unlit": true
    },
    "window": {
      "color": "#f0b45c",
      "emissive": "#f0b45c",
      "emissive_intensity": 2,
      "unlit": true
    }
  },
  "nodes": {
    "ground": {
      "mesh": {
        "geometry": { "plane": { "size": [30, 20] } },
        "material": "ground"
      },
      "transform": { "rotation": [-90, 0, 0] }
    },
    "hut": {
      "mesh": {
        "geometry": { "box": { "size": [2.4, 1.6, 2] } },
        "material": "wall"
      },
      "transform": { "position": [0, 0.8, 0] }
    },
    "roof": {
      "mesh": {
        "geometry": { "cone": { "radius": 1.9, "height": 1.1, "segments": 4 } },
        "material": "roof"
      },
      "transform": { "position": [0, 2.15, 0], "rotation": [0, 45, 0] }
    },
    "window": {
      "mesh": {
        "geometry": { "box": { "size": [0.5, 0.5, 0.05] } },
        "material": "window"
      },
      "transform": { "position": [0.5, 0.95, 1.02] }
    },
    "pine": {
      "mesh": {
        "geometry": { "cone": { "radius": 0.9, "height": 2.6, "segments": 6 } },
        "material": "pine"
      },
      "transform": { "position": [-3, 1.3, 0.5] }
    },
    "moon": {
      "mesh": {
        "geometry": { "sphere": { "radius": 0.7 } },
        "material": "moon"
      },
      "transform": { "position": [3.4, 4.6, -4] }
    }
  }
}
```

```jsonc
"post": {
  "tone_mapping": "none",
  "pixel_art": {
    "size": 4,
    "supersample": 2,
    "palette": ["#07050b", "#2a1d42", "#b02e72", "#ffe0a0"],
    "dither": 0.5,
    "outline": 0.8
  }
}
```

## The pixel grid

- `size` is how many frame pixels make one art pixel (4 by default). An 800x800 frame with `size` 2 is 400x400 art pixels. Pick a size that divides the frame so every art pixel is the same square.
- The grid belongs to the frame, not the scene: moving the camera changes what is in each art pixel, never how big a pixel is.
- The scene itself is drawn at `size` times smaller, times `supersample`. That is cheaper than drawing at full size and downscaling: an 800x800 frame at `size` 2 and `supersample` 2 renders 800x800 samples, at `size` 4 only 400x400.
- Bloom, depth of field, fog, grade, vignette and grain all run at the low resolution, so a glow or a grain speck is a whole art pixel.

## Colour

- `palette` lists 2 to 32 colours. Each sample snaps to its nearest palette colour measured in OKLab, so a shaded orange becomes a darker orange, not a grey that happens to be close.
- Without a palette, `levels` reduces each channel to that many steps (2 to 32). With neither, colours are kept and only the pixel grid applies.
- `supersample` (1 to 3) is the number of samples per art pixel along each axis. With `vote` on (the default) every sample snaps to the palette and the pixel takes the colour most of them chose: a border between two materials gets one of them, never a third colour made by averaging. With `vote` off, or with `dither` above 0, the samples are averaged first.
- `dither` (0 to 1) mixes the two nearest palette colours with a 4x4 Bayer pattern fixed to the art grid, in proportion to where the colour sits between them. 0 is a hard snap.

## Outlines

`outline` (0 to 1) gives a pixel at the edge of the drawn scene a darker version of its own colour, lifted to a minimum brightness so it still shows on a near-black background. It looks at where the scene meets the background, so it needs a sky, gradient or solid background to show against; a frame filled edge to edge (a street from kerb to roof) has no silhouettes.

## Tips

- Set `tone_mapping` to `none` or `neutral` and keep the grade modest: the palette does the grading.
- Write the palette once and reuse it. `duskPalette` and `pixelArt(overrides)` in `@motion-sdk/scene` are a ready 28-colour night palette and a starting look.
- Use an even `size` (2, 4, 6) for video: most encoders store colour at half resolution, so odd sizes blur the edges.
- Particles, text and 2D shapes are drawn at the low resolution as well, so a caption becomes pixel text. Keep text at least 8 art pixels tall.

## In TypeScript

```ts
import { defineScene, duskPalette, pixelArt } from "@motion-sdk/scene";

export default defineScene({
  canvas: { width: 800, height: 800 },
  post: { tone_mapping: "none", pixel_art: pixelArt({ size: 2, palette: [...duskPalette], dither: 0.45 }) },
});
```

`examples/ts/rain-street.ts` is a full scene built this way: a wet night street with a neon sign, rain, a parked sedan and a passing wagon (see [the benchmarks](https://github.com/brainfish-ai/motion/tree/main/examples/benchmarks)).

## Where it Runs

Native rendering and export (the CLI) have it, and in the browser the authoring bundle and the WebGL2 fallback do: `createPlayer` loads the authoring bundle for any scene that uses `pixel_art`. The small WebGPU player bundle leaves it out, like [`stylise`](/docs/features/stylise).

## API Reference

### PixelArt

Pixel-art look: the scene is drawn at a low resolution (`size` screen pixels per art pixel) and every art pixel is drawn as a crisp square. Colours can snap to a fixed palette (nearest in OKLab, the majority of the samples in a pixel wins) or to `levels` steps per channel, with an ordered 4x4 Bayer dither and an ink outline on silhouettes.
 The low-resolution grid belongs to the frame, not the scene: moving the camera changes what is in each pixel, never how big a pixel is.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `dither` | `number (0 to 1)` | `0` | Ordered dither between the two nearest colours, 0..1 (0 = off). Uses a 4x4 Bayer pattern fixed to the art grid. |
| `levels` | `integer (0 to 32)` | `0` | Without a palette: reduce each channel to this many steps (0 = off, 2..32). |
| `outline` | `number (0 to 1)` | `0` | Ink outline on silhouettes, 0..1: a pixel at the edge of the drawn scene takes a darker version of its own colour. Needs a background that shows (sky, solid or gradient); frames filled edge to edge have no silhouettes. |
| `palette` | `Color[]` | — | Colours to snap to (2..32). Each sample takes its nearest colour in OKLab. Leave empty to keep the scene's colours (or use `levels`). |
| `size` | `integer (1 to 32)` | `4` | Frame pixels per art pixel (1..32). A 800x800 frame with `size` 4 is 200x200 art pixels. Pick a size that divides the frame so every art pixel is the same square. |
| `supersample` | `integer (1 to 3)` | `2` | Samples per art pixel along each axis (1..3). 2 draws the scene at twice the art resolution and takes a 2x2 vote per pixel, which keeps thin shapes and edges clean; 1 is cheapest. |
| `vote` | `boolean` | `true` | Majority vote: with `supersample` above 1, each sample snaps to its palette colour and the pixel takes the one most samples chose, so a border between two materials never turns into a third colour. Off averages the samples first. Dithering always averages. |
