# Walleye scene format, version 1

A scene is one JSON object. `lib/walleye.js` reads it; this page says exactly
what it reads. Fields the viewer does not know are ignored, so a scene can
carry notes of its own.

Every field is optional except `version` and at least one of `points`,
`lines`, `mesh`, `surface`, `pair` or `atlas`. A scene with `"seed": true`
and nothing to draw shows "This example is a seed: there is nothing to draw
yet." instead of an error.

```json
{
  "version": 1,
  "id": "torus",
  "title": "A torus",
  "alt": "A ring-shaped surface seen from slightly above.",
  "caption": "One or two sentences shown under the stage.",
  "credit": "Who made it; data source; licence.",
  "seed": false,

  "view": { "yaw": 30, "pitch": 20, "zoom": 1 },
  "views": [ { "name": "Along the axis", "yaw": 0, "pitch": 85 } ],
  "distance": 3.2,
  "stereo": { "baseline": 1.0 },
  "guides": { "axes": true, "floor": true, "sphere": false },
  "bounds": { "center": [0, 0, 0], "radius": 1 },

  "points": [[0, 0, 0], [1, 0, 0]],
  "pointStyle": { "radius": 4, "color": "ink", "labels": false },
  "lines": [ { "pts": [[0, 0, 0], [1, 0, 0]], "color": "accent", "width": 1.5, "closed": false } ],
  "mesh": { "vertices": [[0,0,0], [1,0,0], [0,1,0]], "faces": [[0, 1, 2]], "color": "surface", "alpha": 1, "wire": false },
  "surface": { "type": "param", "expr": "torus", "params": { "R": 1, "r": 0.4 }, "u": [0, 6.2832, 64], "v": [0, 6.2832, 24] },
  "pair": { "src": "../pairs/name.jpg", "layout": "lr" },
  "atlas": { "points": [[0, 0, 1]], "arcs": [] }
}
```

## Coordinates

Right-handed, **y up**. The front view looks along -z (the camera sits on
+z), x points right. A scene is framed once from its bounding sphere: the
smallest box around everything drawn, then the sphere about the box's
centre that holds every vertex. `bounds` overrides that. For an `atlas`
scene the frame is the ball of radius `atlas.extent` (default 1) about the
origin, as in the atlas viewer.

## Descriptive fields

| field | used for |
|---|---|
| `title` | the viewer page's heading and the stage's accessible name |
| `alt` | the canvas's text alternative: say what the picture shows |
| `caption` | shown under the stage (`options.caption`) |
| `credit` | shown under the caption, smaller |
| `id` | not used by the library; the gallery and `?from=` use it |
| `seed` | marks a placeholder; the gallery shows a "seed" badge |

## View and stereo

| field | default | meaning |
|---|---|---|
| `view.yaw` | 30 | degrees about the vertical axis; positive moves the camera to the right (toward +x) |
| `view.pitch` | 20 | degrees above the horizon; positive looks down. Clamped to -85..85 |
| `view.zoom` | 1 | magnification, clamped to 0.5..4. At 1 the bounding sphere fills about 80% of a pane |
| `views` | none | up to six extra named views `{name, yaw, pitch, zoom}`, shown as buttons and on keys 4 to 9 |
| `distance` | 3.2 | camera distance in bounding-sphere radii, clamped to 1.5..50 |
| `stereo.baseline` | 1 | multiplies the stereo baseline (0..4); 0 gives a flat pair |
| `guides.axes` | true | the axis triad in each pane's lower-left corner |
| `guides.floor` | true | a faint ring under the object with ticks every 30 degrees; the long tick points to the front (+z) |
| `guides.sphere` | false (true for `atlas` unless `atlas.sphere` is false) | a faint sphere: `true` for the bounding sphere (the unit sphere for atlas scenes), or a number for that radius |

The default baseline is 6% of the camera distance (about 3.4 degrees between
the two eyes, as in the atlas viewer). The viewer caps it so that nothing
comes out of the screen further than 8% of a pane's width, and in parallel
mode so the far side never pushes the eyes past parallel. See `api.md`.

## Colours

A colour is a token name or any CSS colour (`"#c0392b"`, `"rgb(10 20 30)"`).
Tokens follow the light and dark themes:

| token | light | for |
|---|---|---|
| `ink` | near black | edges, points, labels |
| `accent` | blue | the thing to look at |
| `up` | rust | a second family; the atlas's upper caps and boundary curves |
| `low` | blue | a third family; the atlas's lower caps; the back of one-sided surfaces is tinted toward it |
| `surface` | warm grey | faces |
| `muted` | grey | secondary lines, mesh edges |
| `plain` | olive brown | the atlas's plain caps |
| `faint` | light grey | guides, zero-weight arcs |

The axis triad uses `axis-x`, `axis-y`, `axis-z`. The tokens are the CSS
custom properties `--wy-scene-<name>` in `lib/walleye.css`.

## Points

`points` is a list of `[x, y, z]`. `pointStyle` applies to all of them:

| field | default | meaning |
|---|---|---|
| `radius` | 3.5 | pixels at the scene centre's depth; nearer points draw larger |
| `color` | `ink` | |
| `labels` | false | `true` numbers the points from 0; a list of strings labels them in order |

## Lines

`lines` is a list of polylines:

| field | default | meaning |
|---|---|---|
| `pts` | | list of `[x, y, z]`; a `null` entry breaks the line |
| `color` | `ink` | |
| `width` | 1.5 | pixels at the middle depth (nearer is thicker, further is thinner and lighter) |
| `alpha` | 1 | |
| `closed` | false | join the last point to the first |
| `tube` | 0 | if above 0, draw a shaded tube of this radius instead of a line (`sides`, default 10) |
| `generator`, `params`, `t` | | instead of `pts`, a named curve from `Walleye.generators` (see below) |

## Mesh

`mesh` is one object or a list of them:

| field | default | meaning |
|---|---|---|
| `vertices` | | list of `[x, y, z]` |
| `faces` | | list of vertex-index lists; any polygon (triangles, quads, ...) |
| `color` | `surface` | front colour |
| `back` | `surface` tinted toward `low` | colour of the side facing away from the normal (index order, right-hand rule) |
| `alpha` | 1 | |
| `wire` | false | `true` draws every edge once (in `edgeColor`, default `muted`, width `edgeWidth`, default 0.9) over the faces; `"only"` draws the edges and no faces |
| `grid` | false | outline each face a little darker, which gives fusion something to hold on large flat faces |

Faces are flat-shaded with one light and drawn back to front (painter's
algorithm), together with lines and points. Very long faces next to short
ones can sort wrongly; split them.

## Surface

`surface` is one object or a list:

```json
{ "type": "param", "expr": "catenoid", "params": { "c": 1 }, "u": [0, 6.2832, 48], "v": [-1.2, 1.2, 24],
  "color": "surface", "alpha": 1, "grid": true, "wire": false }
```

- `expr` (or `generator`, or `name`) names an entry of `Walleye.generators`.
  There is no `eval`; only named generators exist.
- `u` and `v` are `[start, end, steps]`, or a number of steps, or omitted
  (the generator's defaults). Steps are clamped to 2..400.
- `params` override the generator's defaults.
- `grid` is true by default for surfaces (face outlines, a mesh texture).
- `wire`: `true` adds the parameter lines in `muted`; `"only"` draws only
  them, in `color`.
- If the generator is a curve (`trefoil`, `torus-knot`, `helix`, `viviani`,
  `hopf-fibres`), the surface entry draws the curve: as a line (`width`,
  default 2, `color`, default `accent`), or as a tube when `tube` is a
  radius. `t` (or `u`) sets the curve's range and steps.
- `"type": "grid"` and `"type": "catenoid"` are the atlas viewer's surfaces
  (see below) and are accepted here too.

### Generators

| name | kind | params (defaults) | domain |
|---|---|---|---|
| `sphere` | surface | `r` 1 | u 0..2pi, v 0..pi |
| `torus` | surface | `R` 1, `r` 0.4 | u, v 0..2pi |
| `catenoid` | surface | `c` 1 | u 0..2pi, v -1.2..1.2 (height) |
| `helicoid` | surface | `c` 0.3 (rise per radian) | u -pi..pi, v -1..1 |
| `enneper` | surface | none | u 0..1.3 (radius), v 0..2pi (angle) |
| `mobius` | surface | `R` 1 | u 0..2pi, v -0.4..0.4 |
| `klein-bottle` | surface | `r` 2 (figure-eight immersion) | u, v 0..2pi |
| `clifford-torus` | surface | `phase` 0.35 (a turn in 4D before stereographic projection; 0 gives a torus of revolution) | u, v 0..2pi |
| `saddle` | surface | `a` 0.6: y = a(x^2 - z^2) | -1..1 |
| `monkey-saddle` | surface | `a` 0.5: y = a(x^3 - 3xz^2) | -1..1 |
| `helix` | curve | `r` 1, `c` 0.08 | t 0..6pi |
| `trefoil` | curve, closed | `s` 1/3 | t 0..2pi |
| `torus-knot` | curve, closed | `p` 2, `q` 3, `R` 1, `r` 0.4 | t 0..2pi |
| `viviani` | curve, closed | `a` 0.5 | t 0..4pi |
| `hopf-fibres` (alias `hopf-fibration`) | curves | `rings` 3 (or a list of angles in radians), `count` 10 per ring, `n` 120 points each | |

Each generator is `{kind, title, params, u, v, f(u, v, p)}` for a surface,
`{kind: 'curve', t, closed, f(t, p)}` for a curve and
`{kind: 'curves', build(p)}` for a family; a page may add its own to
`Walleye.generators` before mounting.

## Pair (pictures)

```json
"pair": { "src": "../pairs/name.jpg", "layout": "lr", "shift": 0, "dy": 0 }
"pair": { "left": "../pairs/name-left.jpg", "right": "../pairs/name-right.jpg" }
```

- `src`: one side-by-side image. `layout: "lr"` (default) means the left
  eye's picture is on the left (a parallel pair); `"rl"` means it is on the
  right (a cross-eyed pair). The viewer cuts it into two halves.
- `left` and `right`: two separate images instead of `src`.
- Paths are relative to **the scene file** (as in CSS). A gallery scene in
  `examples/scenes/` therefore writes `"../pairs/name.jpg"`. As a
  convenience, when a scene in a folder named `scenes/` writes
  `"pairs/name.jpg"` and that fails, the viewer tries `../pairs/name.jpg`.
- `shift`: source pixels to move the two halves apart (positive pushes the
  picture back behind the screen); `dy`: source pixels to move the right
  image down, to correct a vertical misalignment.
- `width` and `height` may be given for reference; the viewer uses the
  image's own size.
- All five modes work, including anaglyph (both halves are made grey first)
  and wiggle. Rotation, named views, spin and guides are off; drag pans,
  zoom works, Home resets both.

## Atlas

`atlas` takes the atlas viewer's scene object (`assets/atlas/viewer.js`)
unchanged:

| field | drawn as |
|---|---|
| `points` | points on the unit sphere (normalised), `ink` |
| `labels` | number the points from 0 |
| `rattlers` | indices of points drawn hollow |
| `caps` | `[{w, t, kind}]`: the boundary circle of the cap {y : <y, w> >= t}, in `up`, `low` or `plain`; a circle shared by an upper and a lower cap is dashed in both |
| `pointCaps` | a cap of this angular radius (radians) about every point, in `up` |
| `arcs`, `arcWeights` | great-circle arcs; with weights, width 0.7 + 3.3 w/max, zero weights in `faint` |
| `surface` `{type: "catenoid", h, lambda}` | the catenoid with axis z, and its two boundary circles in `up` |
| `surface` `{type: "grid", X, edges, maxEdge, alpha}` | a grid of points (rows by columns; `null` cuts a point away, `maxEdge` drops long quads); the first and last columns (or `edges`) as boundary curves in `up` |
| `sphere` | the unit sphere guide, unless `false` |
| `extent` | radius of the region framed (default 1) |

`inverted` (the atlas's second surface behind a button) is not shown in
version 1. Atlas coordinates are used as they are (the atlas also draws y
up), so an atlas scene looks the same in both viewers.
