# Water and Floods

Rolling waves and a real planar reflection for seas, lakes and wet roads, plus simulated flood water that flows and floats things.

Add `water` to the material of a flat plane. It ripples and mirrors the scene above it.

```json title="apps/docs/examples/water.json"
{
  "camera": { "position": [0, 3, 12], "target": [0, 1.5, -10], "fov": 50 },
  "lighting": { "rig": "moonlight" },
  "environment": {
    "background": {
      "sky": {
        "zenith": "#0a1030",
        "horizon": "#c86a4a",
        "sun": { "elevation": 3, "azimuth": 0 },
        "clouds": null
      }
    }
  },
  "post": { "bloom": 0.8, "tone_mapping": "agx" },
  "materials": {
    "sea": {
      "color": "#0b1a2a",
      "roughness": 0.25,
      "water": { "waves": 0.25, "scale": 4, "speed": 1, "reflect": 1 }
    },
    "beacon": { "preset": "neon", "color": "#ff5a36", "emissive_intensity": 6 },
    "stone": { "preset": "rock" }
  },
  "nodes": {
    "sea": {
      "mesh": {
        "geometry": { "plane": { "size": [200, 200] } },
        "material": "sea"
      },
      "transform": { "rotation": [-90, 0, 0] }
    },
    "tower": {
      "transform": { "position": [2, 1.6, -4] },
      "mesh": {
        "geometry": { "cylinder": { "radius": 0.4, "height": 3.2 } },
        "material": "stone"
      }
    },
    "lamp": {
      "transform": { "position": [2, 3.4, -4] },
      "mesh": {
        "geometry": { "sphere": { "radius": 0.3 } },
        "material": "beacon"
      }
    }
  },
  "timeline": { "duration": 4, "loop": "loop" }
}
```

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

## Settings

- `waves`: ripple strength. `0` is a still mirror, for wet tarmac or a calm pond.
- `scale`: the wavelength of the biggest waves, in world units. Large for open sea.
- `speed`: how fast the waves roll.
- `reflect`: how much it mirrors, 0 to 1.

Give the material a dark `color` and `roughness` 0.2 to 0.4. Where nothing is mirrored, the water shows the horizon or fog colour.

## Limits

- One water level per scene: the first water plane's height.
- Reflections cost a second, half-resolution render pass. Use them where they matter.

## Flood Water

`water` on a material is a surface. `flood` on a node is simulated water that moves. It pours in from `sources`, spills from `fill` blocks like a breaking dam, or starts still at `level`. It runs downhill over terrain, piles up against `static` bodies, and floats `dynamic` ones.

```json title="apps/docs/examples/flood.json"
{
  "camera": { "position": [0, 9, 16], "target": [0, 0, 0], "fov": 40 },
  "lighting": { "rig": "golden_hour" },
  "environment": { "background": { "sky": {} } },
  "physics": { "ground": 0 },
  "materials": {
    "flood": {
      "color": "#2a4a52",
      "roughness": 0.3,
      "water": { "waves": 0.2, "scale": 2 }
    },
    "ground": { "preset": "matte_clay", "color": "#8a7a66" },
    "crate": { "preset": "matte_clay", "color": "#c8873a" }
  },
  "nodes": {
    "ground": {
      "mesh": {
        "geometry": { "plane": { "size": [60, 60] } },
        "material": "ground"
      },
      "transform": { "rotation": [-90, 0, 0] }
    },
    "block": {
      "transform": { "position": [3, 1, -1] },
      "mesh": {
        "geometry": { "box": { "size": [2, 2, 2] } },
        "material": "preset:stucco"
      },
      "body": { "type": "static" }
    },
    "crate": {
      "transform": { "position": [-6, 0.4, 1] },
      "mesh": {
        "geometry": { "box": { "size": [0.8, 0.8, 0.8] } },
        "material": "crate"
      },
      "body": { "buoyancy": 1.5 }
    },
    "water": {
      "flood": {
        "size": [24, 10],
        "cells": 64,
        "material": "flood",
        "fill": [{ "from": [-12, -5], "to": [-6, 5], "height": 3 }]
      }
    }
  },
  "timeline": { "duration": 6, "loop": "once" }
}
```

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

- The area is `size` wide on x and z, centred on the node and aligned with the world. Put the node at the floor's height. The basin floor is the physics `ground`.
- `cells` sets the detail along the longer side, from 8 to 160. The default is 64. More is finer and slower.
- `edges: "open"` drains water at the border. The default `closed` keeps it in.
- A dynamic body's `buoyancy` sets how it floats. 1 rides half under, 2 bobs high, and below 0.5 sinks.
- Give the surface a `water` material.
- Like [physics](/docs/features/physics), the flood is simulated once when the scene loads, so it scrubs like keyframes. In the browser it needs the authoring bundle.

See `examples/physics-flood.json` for a flash flood through a street.

## API Reference

### Water

Waves and reflections for a water (or wet, mirror-like) surface.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `reflect` | `number (0 to 1)` | `1` | Reflection strength (0 to 1). |
| `scale` | `number (≥ 0.01)` | `3` | Wavelength of the biggest waves, in world units. |
| `speed` | `number (0 to 20)` | `1` | How fast the waves move (1 is a gentle swell). |
| `waves` | `number (0 to 2)` | `0.35` | Wave strength: 0 is a still mirror (wet tarmac, a calm pond), 1 is choppy. |

### Flood

Simulated water over an area centred on the node and aligned with world x and z: it pours in from `sources`, spills from `fill` blocks, flows downhill around static bodies and over terrain, and floats dynamic bodies (see `buoyancy`). Simulated once on load with the bodies, so it scrubs like keyframes. The basin floor is the physics `ground` (or the node's height without one); put the node at the floor's height.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `cells` | `integer` | `64` | Grid cells along the longer side (8..160). More is finer and slower. |
| `edges` | `FloodEdges` | — | What the area's border does: walls keep water in, `open` drains. |
| `fill` | `FloodFill[]` | — | Blocks of water standing at the start, held by nothing: a dam breaking, a wave from one side. |
| `level` | `number` | — | Starting still-water surface height above the node (none: dry). |
| `material` | `string` | — | Material for the surface (a `water` material looks best). |
| `size` (required) | `[x, y]` | — | Width (x) and depth (z) of the area, world units. |
| `sources` | `FloodSource[]` | — | Places water pours in: a burst main, a river mouth, rain on a spot. |

### FloodFill

A box of standing water at the start of a [`Flood`], in the node's local x/z.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `from` (required) | `[x, y]` | — | Corners `[x, z]`. |
| `height` (required) | `number` | — | Surface height above the node. |
| `to` (required) | `[x, y]` | — | The opposite corner `[x, z]`. Must be past `from` on both axes. |

### FloodSource

Water pouring into a [`Flood`] over a disc.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `duration` | `number` | — | How long it runs, seconds (none: to the end). |
| `position` (required) | `[x, y]` | — | Centre `[x, z]` in the node's local space. |
| `radius` | `number` | `1` | Radius of the disc the water pours in over, in world units. |
| `rate` | `number` | `4` | Cubic metres per second. |
| `start` | `number` | — | When it starts, seconds. |

### FloodEdges

The border of a [`Flood`].

| Value | Description |
| --- | --- |
| `"closed"` | Water stays in (it piles up against the border). |
| `"open"` | Water runs off and is gone. |
