# Expressions

A small, deterministic expression language for prefab parameters and copies.

Inside a prefab or a repeat, any string that starts with `=` is an expression. It's evaluated when the scene is expanded, so the renderer only ever sees plain values.

```json title="apps/docs/examples/expressions.json"
{
  "camera": { "position": [0, 9, 14], "target": [0, 0, 0], "fov": 40 },
  "lighting": { "rig": "neon_rim" },
  "environment": { "background": { "solid": "#07060c" } },
  "post": { "bloom": 0.9 },
  "prefabs": {
    "bar": {
      "params": { "h": 1, "c": "#ff2ec4" },
      "nodes": {
        "col": {
          "transform": { "position": [0, "=h/2", 0], "scale": [0.6, "=h", 0.6] },
          "mesh": { "geometry": { "box": {} }, "material": "preset:neon" },
          "color": "=c"
        }
      }
    }
  },
  "nodes": {
    "bars": {
      "use": "bar",
      "repeat": { "grid": [9, 1, 9], "spacing": [1, 1, 1] },
      "transform": { "position": [-4, 0, -4] },
      "params": {
        "h": "=0.3 + 2*abs(sin(ix*0.7) * cos(iz*0.7))",
        "c": "=pick(i % 3, '#ff2ec4', '#00e5ff', '#f2c230')"
      }
    }
  }
}
```

![Render of expressions](/renders/expressions.jpg)

## Variables

- Every prefab parameter by name, like `h` or `c`.
- `i`: the copy's index. `n`: the number of copies.
- `ix`, `iy`, `iz`: the copy's grid indices.

## Operators and Values

`+ - * / %`, comparisons `== != < <= > >=`, `&& || !`, `[arrays]` and `'strings'`.

## Functions

| Function | Result |
| --- | --- |
| `floor(x)`, `ceil(x)`, `round(x)`, `abs(x)`, `sqrt(x)` | Rounding and roots |
| `pow(x, y)`, `min(a, b)`, `max(a, b)`, `clamp(x, lo, hi)`, `lerp(a, b, t)` | Arithmetic |
| `sin(x)`, `cos(x)` | Trigonometry, in radians |
| `pick(i, a, b, c, ...)` | The `i`-th choice (from 0), wrapping around |
| `if(cond, a, b)` | `a` when `cond` is true, else `b` |
| `len(list)` | Length of an array or string |
| `rand()`, `rand(k)`, `rand(lo, hi)`, `rand(lo, hi, k)` | A seeded random number in [0, 1) or [lo, hi). Vary `k` for independent streams |
| `str(x)` | A number as a string |

## Nested Prefabs

Expressions in a nested prefab's `params` are evaluated with the outer copy's `i`. Write `==` instead of `=` to defer one to the inner prefab.
