component

Rain

The weather over a whole world, drawn only around the camera: rain streaks and splashes, snowflakes, and rings on every `WetStreet`'s puddles, as hard as a `weather` function says (`weatherAt` gives the weather as a pure function of a clock). It stays out from under roofs through a depth map of the still world drawn from above in its first seconds. WebGLRenderer only.

⌖ Explore the library in 3D → · View as Markdown

Weather for a whole world: mount it once inside <World> and it falls around the camera.
Tune every prop in the workshop →

Add it

npx @runek/cli add rain

Pulls @react-three/fiber@^9.6.1, @runek/core@^0.18.0, three@^0.184.0.

Use it

import { Rain } from './runek/Rain'

<Rain position={[0, 0, 0]} streaks={4000} splashes={200} />

Or as a node in a world file (worlds as data):

{ "type": "Rain", "props": { "position": [0, 0, 0], "streaks": 4000, "splashes": 200 } }

Props

Prop Type Default Description
position [x, y, z] Accepted for the component contract. Rain follows the camera, so it has no place of its own.
rotation [x, y, z] Accepted for the component contract.
seed number Seeds where each streak, splash and flake starts.
streaks number Streaks in the air around the camera when it rains its hardest. Lighter rain draws fewer.
splashes number Splashes on the ground around the camera in the heaviest rain, 0 for none.
flakes number Snowflakes in the air around the camera when it snows its hardest, 0 for none.
ripples boolean Ring the puddles of every WetStreet.
weather function (code only) The weather now, asked every frame: weatherAt on a clock, most often. Left out, it rains hard and steadily and the streets stay soaked. JSX only.
bounds [number, number, number, number] The part of the world rain can fall on, [minX, minZ, maxX, maxZ] in units. Outside it, rain falls through everything.
cover number Texels along each side of the cover map, which records the highest surface over every spot.
floor number The lowest anything falls, in units: the surface of a sea, which no cover map holds since water is not a still part of the world. Rain ends there and splashes on it, inside the bounds and out. Unset, there is no such floor.
color color Rain color. A cool grey by default, lit as if by the street.
onMemory function (code only) Called with the GPU bytes the cover map holds, and with 0 when it is let go. JSX only.

Registry manifest

registry manifest /r/components/rain.json Self-contained JSON: inlined source plus resolved dependencies, exactly what runek add rain fetches.

Browse the whole catalog in the gallery →.

Source (3 files)

Rain.tsx

import { useFrame, useThree } from '@react-three/fiber'
import {
  DYNAMIC_KEY,
  rng,
  scenePass,
  skipMirrors,
  useNodeRenderer,
  useWorld,
  type Vec3,
} from '@runek/core'
import { useEffect, useMemo, useRef } from 'react'
import {
  AdditiveBlending,
  BufferAttribute,
  BufferGeometry,
  type Camera,
  type Color,
  DepthTexture,
  DoubleSide,
  Group,
  InstancedBufferAttribute,
  InstancedBufferGeometry,
  LineSegments,
  Mesh,
  NearestFilter,
  type Object3D,
  OrthographicCamera,
  Points,
  RedFormat,
  type Scene,
  ShaderMaterial,
  type Texture,
  UnsignedByteType,
  UnsignedShortType,
  Vector2,
  Vector3,
  Vector4,
  type WebGLRenderer,
  WebGLRenderTarget,
} from 'three'
import { along, emissiveColor, useLadder } from './city/glow'
import { RAIN_PERIOD, rainUniforms, type WeatherState } from './city/weather'

// The weather is the rain's own: a world that mounts `Rain` reads what it does from here too.
export type { WeatherKind, WeatherState } from './city/weather'
export {
  makeWeather,
  parseWeatherKind,
  RAIN_PERIOD,
  steadyWeather,
  WEATHER,
  WEATHER_KINDS,
  weatherAt,
} from './city/weather'

/** The part of a `WeatherState` the rain draws from. */
export type RainWeather = Pick<WeatherState, 'rain' | 'snow' | 'wind' | 'wetness'>

export interface RainProps {
  /** Accepted for the component contract. Rain follows the camera, so it has no place of its own. */
  position?: Vec3
  /** Accepted for the component contract. */
  rotation?: Vec3
  /** Seeds where each streak, splash and flake starts. */
  seed?: number
  /** Streaks in the air around the camera when it rains its hardest. Lighter rain draws fewer. */
  streaks?: number
  /** Splashes on the ground around the camera in the heaviest rain, 0 for none. */
  splashes?: number
  /** Snowflakes in the air around the camera when it snows its hardest, 0 for none. */
  flakes?: number
  /** Ring the puddles of every `WetStreet`. */
  ripples?: boolean
  /**
   * The weather now, asked every frame: `weatherAt` on a clock, most often. Left out, it rains
   * hard and steadily and the streets stay soaked. JSX only.
   */
  weather?: () => RainWeather
  /** The part of the world rain can fall on, [minX, minZ, maxX, maxZ] in units. Outside it, rain falls through everything. */
  bounds?: readonly [number, number, number, number]
  /** Texels along each side of the cover map, which records the highest surface over every spot. */
  cover?: number
  /** The lowest anything falls, in units: the surface of a sea, which no cover map holds since
   *  water is not a still part of the world. Rain ends there and splashes on it, inside the
   *  bounds and out. Unset, there is no such floor. */
  floor?: number
  /** Rain color. A cool grey by default, lit as if by the street. */
  color?: string
  /** Called with the GPU bytes the cover map holds, and with 0 when it is let go. JSX only. */
  onMemory?: (bytes: number) => void
}

const DEFAULT_BOUNDS = [-160, -160, 160, 160] as const

/**
 * The weather over the whole world, drawn only around the camera: streaks of rain in the air,
 * splashes where they land, rings on the puddles of every `WetStreet`, and snowflakes. Mount it
 * once inside `<World>`.
 *
 * What falls follows `weather`. Rain from a drizzle (a few short, slow streaks) to a downpour
 * (all of them, long and fast), slanted by the wind and its gusts. Snow as slow flakes that
 * sway and drift. In dry weather nothing is drawn at all. It also tells every `WetStreet` how
 * hard the puddles are ringed and how wet the ground is.
 *
 * It stays out from under roofs. Shortly after mounting it draws the still parts of the world
 * once from straight above into a depth map (the cover map), and a streak dies where it meets
 * the highest surface over its spot, so an awning, a canopy or a ceiling keeps the ground under
 * it dry, and snow stays out the same way. Anything inside a `<Dynamic>` is left out of that
 * map. The map is drawn a few times in the first seconds to catch parts that arrive late, then
 * never again.
 *
 * Streaks, splashes and flakes are a draw call each, only while there are any to draw: two in
 * rain, one in snow, all three for the moments rain turns to snow, none when it is dry. They
 * stay out of the world's mirrors. The cover map is the one extra scene render, a few times in
 * the first seconds and never again, inside `scenePass()`.
 *
 * `WebGLRenderer` only: under `WebGPURenderer` it draws nothing.
 */
export function Rain(props: RainProps) {
  return useNodeRenderer() ? null : <Fall {...props} />
}

Rain.stream = false

/** Seconds after mounting at which the cover map is drawn. The later ones catch parts that build over several frames. */
const BAKE_AT = [0.3, 1.0, 2.2]

/** With no `weather` it rains as it always did: hard, on a light steady wind, on soaked streets. */
const STEADY: RainWeather = { rain: 1, snow: 0, wind: { x: 0.8, z: 0.4 }, wetness: 1 }

function Fall({
  seed = 0,
  streaks = 8000,
  splashes = 400,
  flakes = 4000,
  ripples = true,
  weather,
  bounds = DEFAULT_BOUNDS,
  cover = 2048,
  floor,
  color = '#aab0bd',
  onMemory,
}: RainProps) {
  const { unit } = useWorld()
  const ladder = useLadder()
  const gl = useThree((s) => s.gl) as WebGLRenderer
  const scene = useThree((s) => s.scene) as Scene
  const camera = useThree((s) => s.camera)
  const [minX, minZ, maxX, maxZ] = bounds

  const map = useMemo(
    () => new CoverMap(minX * unit, minZ * unit, maxX * unit, maxZ * unit, cover, unit),
    [minX, minZ, maxX, maxZ, cover, unit],
  )
  const schedule = useRef({ t: 0, next: 0 })
  useEffect(() => {
    schedule.current = { t: 0, next: 0 }
    return () => {
      map.dispose()
      onMemory?.(0)
    }
  }, [map, onMemory])

  // Lit as if by the street: rain shows where light is, and this is the light most of it has.
  const rain = useMemo(
    () =>
      buildRain({
        seed,
        streaks,
        splashes,
        flakes,
        unit,
        streak: emissiveColor(color, along(ladder.streetUnderSign, 0.2)),
        splash: emissiveColor(color, along(ladder.streetUnderSign, 0.35)),
        // Snow is white where rain is clear, so the same street light shows more of it.
        flake: emissiveColor(SNOW_COLOR, along(ladder.streetUnderSign, 0.4)),
        map,
      }),
    [seed, streaks, splashes, flakes, unit, color, ladder.streetUnderSign, map],
  )
  useEffect(() => () => rain.dispose(), [rain])

  // A reflection of a streak is too faint to pay for.
  useEffect(() => skipMirrors(rain.group), [rain])

  // Without a `Rain` the streets go back to what their own props say: still water, as wet as they were built.
  useEffect(
    () => () => {
      rainUniforms.uRain.value = 0
      rainUniforms.uWetness.value = 1
    },
    [],
  )

  useFrame((_, delta) => {
    // A long frame (a tab coming back) moves the rain a tenth of a second, not the whole gap.
    const dt = Math.min(delta, 0.1)
    rainUniforms.uRainTime.value = (rainUniforms.uRainTime.value + dt) % RAIN_PERIOD
    const now = weather ? weather() : STEADY
    rainUniforms.uRain.value = ripples ? Math.min(1, Math.max(0, now.rain)) : 0
    rainUniforms.uWetness.value = now.wetness
    rain.step(dt, now)
    const s = schedule.current
    s.t += dt
    if (s.next < BAKE_AT.length && s.t >= BAKE_AT[s.next]) {
      s.next++
      const first = !map.ready
      map.bake(gl, scene, camera, rain.group)
      if (first) onMemory?.(map.bytes)
    }
    // Nothing falls until the map says where the roofs are.
    rain.cover.value = map.ready ? map.depth : null
    rain.floor.value = floor === undefined ? NO_FLOOR : floor * unit
    rain.group.visible = map.ready
  })

  return <primitive object={rain.group} />
}

/**
 * The highest surface over every spot of a rectangle of the world, as a depth map drawn from
 * straight above. Sixteen bits of depth over the camera's range puts a surface within a
 * centimetre of where it is.
 */
class CoverMap {
  readonly target: WebGLRenderTarget
  readonly depth: DepthTexture
  readonly camera: OrthographicCamera
  /** (minX, maxZ, 1 / sizeX, 1 / sizeZ): turns a world xz into the map's uv. */
  readonly box: Vector4
  /** (camera height, near, far - near, half a texel in uv): turns a depth into a world height. */
  readonly range: Vector4
  readonly bytes: number
  ready = false

  constructor(minX: number, minZ: number, maxX: number, maxZ: number, size: number, unit: number) {
    const sizeX = maxX - minX
    const sizeZ = maxZ - minZ
    const top = 400 * unit
    const near = unit
    const far = top + 40 * unit
    this.depth = new DepthTexture(size, size, UnsignedShortType)
    this.target = new WebGLRenderTarget(size, size, {
      format: RedFormat,
      type: UnsignedByteType,
      minFilter: NearestFilter,
      magFilter: NearestFilter,
      generateMipmaps: false,
      depthBuffer: true,
    })
    this.target.depthTexture = this.depth
    // One byte of color (unused, a target must have one) and two of depth for each texel.
    this.bytes = size * size * 3
    // Looking straight down with -Z up the image: u runs along +X and v along -Z.
    this.camera = new OrthographicCamera(-sizeX / 2, sizeX / 2, sizeZ / 2, -sizeZ / 2, near, far)
    this.camera.position.set((minX + maxX) / 2, top, (minZ + maxZ) / 2)
    this.camera.up.set(0, 0, -1)
    this.camera.lookAt((minX + maxX) / 2, 0, (minZ + maxZ) / 2)
    this.camera.updateProjectionMatrix()
    this.camera.updateMatrixWorld()
    this.box = new Vector4(minX, maxZ, 1 / sizeX, 1 / sizeZ)
    this.range = new Vector4(top, near, far - near, 0.5 / size)
  }

  /** Draw the map: the scene as `viewer` sees it, less `skip` and everything that moves. */
  bake(gl: WebGLRenderer, scene: Scene, viewer: Camera, skip: Object3D) {
    const hidden: Object3D[] = []
    const hide = (o: Object3D) => {
      if (!o.visible) return
      if (o === skip || o.userData[DYNAMIC_KEY]) {
        o.visible = false
        hidden.push(o)
        return
      }
      for (const child of o.children) hide(child)
    }
    hide(scene)
    this.camera.layers.mask = viewer.layers.mask
    const target = gl.getRenderTarget()
    const auto = gl.shadowMap.autoUpdate
    const needs = gl.shadowMap.needsUpdate
    // The shadow maps are for the viewer's camera. Leave them be.
    gl.shadowMap.autoUpdate = false
    gl.shadowMap.needsUpdate = false
    gl.setRenderTarget(this.target)
    gl.clear(true, true, true)
    scenePass(() => gl.render(scene, this.camera))
    gl.setRenderTarget(target)
    gl.shadowMap.autoUpdate = auto
    gl.shadowMap.needsUpdate = needs
    for (const o of hidden) o.visible = true
    this.ready = true
  }

  dispose() {
    this.target.dispose()
    this.depth.dispose()
  }
}

// The box of rain around the camera, in units. Streaks wrap inside it, so the camera never
// walks out of the rain. `BELOW` is how far under the camera they still fall, for a view down
// from a roof.
const BOX = 64
const HEIGHT = 40
const BELOW = 10
/** Streak speeds in the heaviest rain's range, in units a second, in steps that fall a whole number of box heights over `RAIN_PERIOD`. */
const SPEED = { min: 22, steps: 41, step: 0.25 }
/** A streak is as long as the rain moves in this many seconds: the blur of one exposure. */
const EXPOSURE = 0.03
/**
 * What the rain's strength (0 to 1) does to a streak, as [in a drizzle, in a downpour]: how fast
 * it falls against `SPEED`, how long its exposure is against `EXPOSURE`, and how bright it is.
 * How many streaks fall goes straight with the strength.
 */
const STREAK = { speed: [0.45, 1.1], exposure: [0.7, 1.3], light: [0.55, 1] } as const
/** Splashes thin out a little faster than streaks as the rain eases: their count goes with the strength to this power. */
const SPLASH_EASE = 1.3
// Snow has a smaller box of its own, since a flake past 12 units is too small to see, and a
// small box keeps the flakes there are close together.
const SNOW_BOX = 24
const SNOW_HEIGHT = 16
const SNOW_BELOW = 6
/** Flake speeds, in units a second, in steps that fall a whole number of snow box heights over `RAIN_PERIOD`. */
const SNOW_SPEED = { min: 0.9, steps: 13, step: SNOW_HEIGHT / RAIN_PERIOD }
/** The wind's drift wraps here, in units: a whole number of both boxes, so neither sees the wrap. */
const DRIFT_SPAN = BOX * SNOW_BOX
/** How far a flake swings side to side as it falls, in units, and how wide the largest is. */
const SNOW_SWAY = 0.35
const SNOW_SIZE = { min: 0.02, max: 0.045 }
const SNOW_COLOR = '#dfe6f2'
/** Splashes wrap inside a square of this side around the camera, in units. */
const AREA = 36

const f = (n: number) => n.toFixed(4)

/** Where the floor is when a world has none: under anything a cover map could hold. */
const NO_FLOOR = -1000

const COVER_PARS = /* glsl */ `
uniform sampler2D tCover;
uniform float uFloor;
uniform vec4 uCoverBox;
uniform vec4 uCoverDepth;
vec2 coverUv( vec2 xz ) { return vec2( ( xz.x - uCoverBox.x ) * uCoverBox.z, ( uCoverBox.y - xz.y ) * uCoverBox.w ); }
float coverAt( vec2 uv ) { return uCoverDepth.x - ( uCoverDepth.y + texture2D( tCover, uv ).r * uCoverDepth.z ); }
bool coverOutside( vec2 uv ) { return uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0; }
// The height of the highest surface over a spot, or far below the world outside the map.
float coverHeight( vec2 xz ) {
  vec2 uv = coverUv( xz );
  return max( coverOutside( uv ) ? -1000.0 : coverAt( uv ), uFloor );
}
// The same over the four nearest texels, so a streak beside a wall ends at the roof line and not in the wall.
float coverShelter( vec2 xz ) {
  vec2 uv = coverUv( xz );
  if ( coverOutside( uv ) ) return uFloor;
  float h = uCoverDepth.w;
  return max( uFloor, max( max( coverAt( uv + vec2( -h, -h ) ), coverAt( uv + vec2( h, -h ) ) ), max( coverAt( uv + vec2( -h, h ) ), coverAt( uv + vec2( h, h ) ) ) ) );
}
`

const streakVert = (unit: number) => /* glsl */ `
uniform float uFall;
uniform vec2 uDrift;
uniform vec2 uWind;
uniform vec3 uStreak;
uniform vec3 uCam;
attribute float aEnd;
attribute float aSpeed;
varying float vA;
${COVER_PARS}
void main() {
  // Fall and drift, wrapped into the box around the camera. The fall has a clock of its own that
  // runs slow in a drizzle, and the drift is how far the wind has carried everything so far.
  vec3 w;
  w.y = uCam.y + mod( position.y - uFall * aSpeed - uCam.y + ${f(BELOW * unit)}, ${f(HEIGHT * unit)} ) - ${f(BELOW * unit)};
  w.x = uCam.x + mod( position.x + uDrift.x - uCam.x + ${f((BOX / 2) * unit)}, ${f(BOX * unit)} ) - ${f((BOX / 2) * unit)};
  w.z = uCam.z + mod( position.z + uDrift.y - uCam.z + ${f((BOX / 2) * unit)}, ${f(BOX * unit)} ) - ${f((BOX / 2) * unit)};
  // Both ends ask about the head, so a streak goes out whole when its head meets a surface.
  float alive = step( coverShelter( w.xz ), w.y );
  // The tail trails back along the way the drop is moving, so wind slants the streak.
  vec3 velocity = vec3( uWind.x, -aSpeed * uStreak.x, uWind.y );
  w -= velocity * ( aEnd * ${f(EXPOSURE)} * uStreak.y );
  float d = distance( w, uCam );
  // Bright at the head, faint at the tail. Gone right at the lens and again where the box ends.
  vA = uStreak.z * ( 1.0 - aEnd * 0.8 ) * smoothstep( ${f(0.5 * unit)}, ${f(3 * unit)}, d ) * ( 1.0 - smoothstep( ${f(18 * unit)}, ${f((BOX / 2) * unit)}, d ) ) * alive;
  gl_Position = projectionMatrix * viewMatrix * vec4( w, 1.0 );
}`

// No tone mapping here: like the haze, rain is added to the linear image and the post stack grades it.
const STREAK_FRAG = /* glsl */ `
uniform vec3 uColor;
varying float vA;
void main() {
  gl_FragColor = vec4( uColor * vA, 1.0 );
}`

// A flake is one point: it falls slowly, swings from side to side and rides the same drift as the
// rain. The swing turns a whole number of times over the rain clock, so the wrap is seamless.
const flakeVert = (unit: number) => /* glsl */ `
uniform float uRainTime;
uniform vec2 uDrift;
uniform float uPixels;
uniform vec3 uCam;
attribute vec4 aFlake;
varying float vA;
${COVER_PARS}
void main() {
  vec2 sway = vec2( sin( aFlake.y + uRainTime * aFlake.z ), cos( aFlake.y * 1.7 + uRainTime * aFlake.z ) ) * ${f(SNOW_SWAY * unit)};
  vec3 w;
  w.y = uCam.y + mod( position.y - uRainTime * aFlake.x - uCam.y + ${f(SNOW_BELOW * unit)}, ${f(SNOW_HEIGHT * unit)} ) - ${f(SNOW_BELOW * unit)};
  w.x = uCam.x + mod( position.x + uDrift.x + sway.x - uCam.x + ${f((SNOW_BOX / 2) * unit)}, ${f(SNOW_BOX * unit)} ) - ${f((SNOW_BOX / 2) * unit)};
  w.z = uCam.z + mod( position.z + uDrift.y + sway.y - uCam.z + ${f((SNOW_BOX / 2) * unit)}, ${f(SNOW_BOX * unit)} ) - ${f((SNOW_BOX / 2) * unit)};
  float alive = step( coverShelter( w.xz ), w.y );
  vec4 mv = viewMatrix * vec4( w, 1.0 );
  float d = length( mv.xyz );
  // As wide on screen as it would be at its distance. Under a pixel it stays a pixel and fades instead.
  float px = aFlake.w * uPixels / max( -mv.z, 1e-3 );
  gl_PointSize = clamp( px, 1.0, 12.0 );
  vA = min( px, 1.0 ) * smoothstep( ${f(0.4 * unit)}, ${f(1.5 * unit)}, d ) * ( 1.0 - smoothstep( ${f(SNOW_BOX * 0.3 * unit)}, ${f((SNOW_BOX / 2) * unit)}, d ) ) * alive;
  gl_Position = projectionMatrix * mv;
}`

const FLAKE_FRAG = /* glsl */ `
uniform vec3 uColor;
varying float vA;
void main() {
  float r = length( gl_PointCoord - 0.5 ) * 2.0;
  gl_FragColor = vec4( uColor * ( 1.0 - smoothstep( 0.35, 1.0, r ) ) * vA, 1.0 );
}`

// Each splash is two quads: a ring that spreads flat on the surface, and a short spurt that
// stands up and turns to face the camera. When its time is up it starts again somewhere else.
const splashVert = (unit: number) => /* glsl */ `
uniform float uRainTime;
uniform vec3 uCam;
attribute vec4 aSeed;
varying vec2 vP;
varying float vPart;
varying float vLife;
varying float vFade;
${COVER_PARS}
void main() {
  // Rates in eighths, so each splash's count of lives over the rain clock is whole and the wrap is seamless.
  float rate = 1.5 + floor( aSeed.z * 12.0 ) * 0.125;
  float t = uRainTime * rate + floor( aSeed.w * 61.0 );
  float life = fract( t );
  float turn = mod( floor( t ), rate * ${f(RAIN_PERIOD)} );
  vec2 spot = fract( aSeed.xy + turn * vec2( 0.618034, 0.754878 ) ) * ${f(AREA * unit)};
  vec2 xz = uCam.xz + mod( spot - uCam.xz + ${f((AREA / 2) * unit)}, ${f(AREA * unit)} ) - ${f((AREA / 2) * unit)};
  float ground = coverHeight( xz );
  vec3 w = vec3( xz.x, ground, xz.y );
  float d = distance( w, uCam );
  if ( position.z < 0.5 ) {
    float radius = ( 0.02 + ( 0.07 + 0.06 * aSeed.z ) * sqrt( life ) ) * ${f(unit)};
    w.xz += position.xy * radius;
    w.y += ${f(0.02 * unit)};
  } else {
    vec2 toCam = normalize( uCam.xz - xz + 1e-4 );
    float rise = ( 0.04 + 0.08 * aSeed.x ) * sin( min( life * 3.0, 1.0 ) * 3.14159 ) * ${f(unit)};
    w.xz += vec2( -toCam.y, toCam.x ) * position.x * ${f(0.025 * unit)};
    w.y += ${f(0.02 * unit)} + position.y * rise;
  }
  vP = position.xy;
  vPart = position.z;
  vLife = life;
  vFade = ( 1.0 - smoothstep( ${f(AREA * 0.3 * unit)}, ${f(AREA * 0.5 * unit)}, d ) ) * smoothstep( ${f(0.3 * unit)}, ${f(1.5 * unit)}, d ) * step( -500.0, ground );
  gl_Position = projectionMatrix * viewMatrix * vec4( w, 1.0 );
}`

const SPLASH_FRAG = /* glsl */ `
uniform vec3 uColor;
varying vec2 vP;
varying float vPart;
varying float vLife;
varying float vFade;
void main() {
  float a;
  if ( vPart < 0.5 ) {
    float r = length( vP );
    float fade = 1.0 - vLife;
    a = smoothstep( 0.7, 0.88, r ) * ( 1.0 - smoothstep( 0.88, 1.0, r ) ) * fade * fade;
  } else {
    // A sliver, widest at the foot, gone by the time the ring has spread.
    float width = 1.0 - vP.y * 0.8;
    a = ( 1.0 - smoothstep( width * 0.4, width, abs( vP.x ) ) ) * ( 1.0 - smoothstep( 0.0, 0.4, vLife ) );
  }
  gl_FragColor = vec4( uColor * a * vFade, 1.0 );
}`

interface RainParts {
  seed: number
  streaks: number
  splashes: number
  flakes: number
  unit: number
  streak: Color
  splash: Color
  flake: Color
  map: CoverMap
}

const wrap = (v: number, span: number) => ((v % span) + span) % span
const between = (range: readonly [number, number], k: number) =>
  range[0] + (range[1] - range[0]) * k

function buildRain({
  seed,
  streaks,
  splashes,
  flakes,
  unit,
  streak,
  splash,
  flake,
  map,
}: RainParts) {
  const r = rng(seed)
  const group = new Group()
  group.name = 'runek:rain'
  group.visible = false
  const disposables: { dispose(): void }[] = []
  const cover = { value: null as Texture | null }
  const floor = { value: NO_FLOOR }
  const eye = { value: new Vector3() }
  // The streaks' own clock, how far the wind has carried everything, the wind now, and what the
  // rain's strength makes of a streak (speed, exposure, light).
  const fall = { value: 0 }
  const drift = { value: new Vector2() }
  const wind = { value: new Vector2() }
  const streakLook = { value: new Vector3(1, 1, 1) }
  // Screen pixels a world length of 1 covers at a distance of 1, for sizing flakes.
  const pixels = { value: 1 }
  const size = new Vector2()
  const shared = {
    uRainTime: rainUniforms.uRainTime,
    uCam: eye,
    tCover: cover,
    uFloor: floor,
    uCoverBox: { value: map.box },
    uCoverDepth: { value: map.range },
  }
  // Read the eye from whichever camera is about to draw, so the rain never trails the view.
  const follow = (gl: WebGLRenderer, _scene: unknown, camera: Camera) => {
    eye.value.setFromMatrixPosition(camera.matrixWorld)
    const target = gl.getRenderTarget()
    const height = target ? target.height : gl.getDrawingBufferSize(size).y
    pixels.value = height * 0.5 * camera.projectionMatrix.elements[5]
  }
  const additive = (material: ShaderMaterial) => {
    material.transparent = true
    material.blending = AdditiveBlending
    material.depthWrite = false
    disposables.push(material)
    return material
  }
  const place = (object: Object3D) => {
    // The box follows the camera in the shader, so there is no still bound to cull by.
    object.frustumCulled = false
    object.renderOrder = 6
    object.onBeforeRender = follow as Object3D['onBeforeRender']
    // Nothing is drawn until the weather asks for it.
    object.visible = false
    group.add(object)
  }

  let lines: LineSegments | null = null
  let splashMesh: Mesh<InstancedBufferGeometry> | null = null
  let points: Points | null = null

  if (streaks > 0) {
    const position = new Float32Array(streaks * 6)
    const end = new Float32Array(streaks * 2)
    const speed = new Float32Array(streaks * 2)
    for (let i = 0; i < streaks; i++) {
      const x = r() * BOX * unit
      const y = r() * HEIGHT * unit
      const z = r() * BOX * unit
      const fall = (SPEED.min + Math.floor(r() * SPEED.steps) * SPEED.step) * unit
      position.set([x, y, z, x, y, z], i * 6)
      end.set([0, 1], i * 2)
      speed.set([fall, fall], i * 2)
    }
    const geometry = new BufferGeometry()
    geometry.setAttribute('position', new BufferAttribute(position, 3))
    geometry.setAttribute('aEnd', new BufferAttribute(end, 1))
    geometry.setAttribute('aSpeed', new BufferAttribute(speed, 1))
    disposables.push(geometry)
    const material = additive(
      new ShaderMaterial({
        vertexShader: streakVert(unit),
        fragmentShader: STREAK_FRAG,
        uniforms: {
          ...shared,
          uFall: fall,
          uDrift: drift,
          uWind: wind,
          uStreak: streakLook,
          uColor: { value: streak },
        },
      }),
    )
    lines = new LineSegments(geometry, material)
    lines.name = 'runek:rain:streaks'
    place(lines)
  }

  if (splashes > 0) {
    const geometry = new InstancedBufferGeometry()
    // (x, y) is the corner and z says which quad: 0 the flat ring, 1 the upright spurt.
    const corners = [-1, -1, 0, 1, -1, 0, -1, 1, 0, 1, 1, 0, -1, 0, 1, 1, 0, 1, -1, 1, 1, 1, 1, 1]
    geometry.setAttribute('position', new BufferAttribute(new Float32Array(corners), 3))
    geometry.setIndex([0, 1, 2, 2, 1, 3, 4, 5, 6, 6, 5, 7])
    const seeds = new Float32Array(splashes * 4)
    for (let i = 0; i < seeds.length; i++) seeds[i] = r()
    geometry.setAttribute('aSeed', new InstancedBufferAttribute(seeds, 4))
    geometry.instanceCount = splashes
    disposables.push(geometry)
    const material = additive(
      new ShaderMaterial({
        vertexShader: splashVert(unit),
        fragmentShader: SPLASH_FRAG,
        uniforms: { ...shared, uColor: { value: splash } },
        side: DoubleSide,
      }),
    )
    material.polygonOffset = true
    material.polygonOffsetFactor = -2
    material.polygonOffsetUnits = -4
    splashMesh = new Mesh(geometry, material)
    splashMesh.name = 'runek:rain:splashes'
    place(splashMesh)
  }

  if (flakes > 0) {
    const position = new Float32Array(flakes * 3)
    const flakeSeeds = new Float32Array(flakes * 4)
    for (let i = 0; i < flakes; i++) {
      position.set([r() * SNOW_BOX * unit, r() * SNOW_HEIGHT * unit, r() * SNOW_BOX * unit], i * 3)
      // Fall speed, where in its swing it starts, how fast it swings (a whole number of turns over the rain clock), and its width.
      flakeSeeds.set(
        [
          (SNOW_SPEED.min + Math.floor(r() * SNOW_SPEED.steps) * SNOW_SPEED.step) * unit,
          r() * Math.PI * 2,
          ((20 + Math.floor(r() * 40)) * Math.PI * 2) / RAIN_PERIOD,
          (SNOW_SIZE.min + r() * (SNOW_SIZE.max - SNOW_SIZE.min)) * unit,
        ],
        i * 4,
      )
    }
    const geometry = new BufferGeometry()
    geometry.setAttribute('position', new BufferAttribute(position, 3))
    geometry.setAttribute('aFlake', new BufferAttribute(flakeSeeds, 4))
    disposables.push(geometry)
    const material = additive(
      new ShaderMaterial({
        vertexShader: flakeVert(unit),
        fragmentShader: FLAKE_FRAG,
        uniforms: { ...shared, uDrift: drift, uPixels: pixels, uColor: { value: flake } },
      }),
    )
    points = new Points(geometry, material)
    points.name = 'runek:rain:flakes'
    place(points)
  }

  return {
    group,
    cover,
    floor,
    /** Move the weather on by `dt` seconds and draw as much of it as `now` asks for. */
    step(dt: number, now: RainWeather) {
      const rain = Math.min(1, Math.max(0, now.rain))
      const snow = Math.min(1, Math.max(0, now.snow))
      const speed = between(STREAK.speed, rain)
      // Wrapped like the rain clock, and for the same reason: every streak speed repeats over it.
      fall.value = (fall.value + dt * speed) % RAIN_PERIOD
      const span = DRIFT_SPAN * unit
      drift.value.set(
        wrap(drift.value.x + now.wind.x * unit * dt, span),
        wrap(drift.value.y + now.wind.z * unit * dt, span),
      )
      wind.value.set(now.wind.x * unit, now.wind.z * unit)
      streakLook.value.set(speed, between(STREAK.exposure, rain), between(STREAK.light, rain))
      // Every part starts at a random spot, so the first few of them are as evenly spread as all of them.
      if (lines) {
        const count = Math.round(streaks * rain)
        lines.geometry.setDrawRange(0, count * 2)
        lines.visible = count > 0
      }
      if (splashMesh) {
        const count = Math.round(splashes * rain ** SPLASH_EASE)
        splashMesh.geometry.instanceCount = count
        splashMesh.visible = count > 0
      }
      if (points) {
        const count = Math.round(flakes * snow)
        points.geometry.setDrawRange(0, count)
        points.visible = count > 0
      }
    },
    dispose() {
      for (const d of disposables) d.dispose()
    },
  }
}

city/glow.ts

// How bright each kind of city emitter is, as a ladder of rungs, each the lowest and highest
// luminance that kind may have, so a sign face is a sign face whatever its hue and a lamp lens
// always outshines it. The rungs are face values scaled by the world's luminance levels
// (`useLuminance()`): sign faces by `sign`, tube cores by `tube`, lamp lenses by `lamp`, and lit
// glass (windows, shopfronts, ad boards, and the light they leave on the pavement) by `screen`.
// At every level's default of 1 the brightest rungs sit around 1, so they read without bloom; a
// world with bloom raises `sign`, `tube` and `lamp` to make them bloom.
//
// Emissive colors are normalized to unit luminance and then scaled onto a rung, so the hue of a
// color never changes how bright it reads.

import {
  DEFAULT_LUMINANCE,
  glowLook,
  type MaterialLook,
  materialKey,
  retainMaterial,
  sharedMaterial,
  useLuminance,
  type WorldLuminance,
} from '@runek/core'
import { useMemo } from 'react'
import { Color, type Material } from 'three'

/** A rung of the ladder: the lowest and highest luminance that kind of emitter may have. */
export type Rung = readonly [number, number]

export interface Ladder {
  /** A night sky dome lit from below by a city. Face value. */
  skyDome: Rung
  /** A wall with no lamp on it. Face value. */
  unlitWall: Rung
  /** Pavement lit by the glass and signs above it. */
  streetUnderSign: Rung
  /** A lit room seen through its window from the street. */
  windowLit: Rung
  /** Lit shopfront glass, and the white of an ad board. */
  shopGlass: Rung
  /** Sign faces and lit letters. */
  signFace: Rung
  /** The core of a neon tube. */
  tubeCore: Rung
  /** A street lamp's lens seen from below. */
  lampLens: Rung
}

const scale = (r: Rung, k: number): Rung => [r[0] * k, r[1] * k]

/** The rungs at every luminance level's face value (1). */
export const FACE_LADDER: Ladder = {
  skyDome: [0.018, 0.055],
  unlitWall: [0.02, 0.05],
  streetUnderSign: [0.1, 0.5],
  windowLit: [0.15, 0.6],
  shopGlass: [0.4, 1.2],
  signFace: [0.6, 1.6],
  tubeCore: [0.6, 1.7],
  lampLens: [0.67, 1.5],
}

/** The ladder for a world's luminance levels. */
export function ladderFor(l: WorldLuminance): Ladder {
  const f = FACE_LADDER
  return {
    skyDome: f.skyDome,
    unlitWall: f.unlitWall,
    streetUnderSign: scale(f.streetUnderSign, l.screen),
    windowLit: scale(f.windowLit, l.screen),
    shopGlass: scale(f.shopGlass, l.screen),
    signFace: scale(f.signFace, l.sign),
    tubeCore: scale(f.tubeCore, l.tube),
    lampLens: scale(f.lampLens, l.lamp),
  }
}

/** The ladder at the default levels. */
export const DEFAULT_LADDER: Ladder = ladderFor(DEFAULT_LUMINANCE)

/** The world's ladder, from its luminance levels. */
export function useLadder(): Ladder {
  const { screen, sign, tube, lamp, flame } = useLuminance()
  return useMemo(
    () => ladderFor({ screen, sign, tube, lamp, flame }),
    [screen, sign, tube, lamp, flame],
  )
}

/** The typical value of a rung: the geometric middle, since luminance is perceived on a log scale. */
export function mid(rung: Rung): number {
  return Math.sqrt(rung[0] * rung[1])
}

/** A point along a rung, 0 at its bottom and 1 at its top, spaced evenly on a log scale. */
export function along(rung: Rung, t: number): number {
  return rung[0] * (rung[1] / rung[0]) ** t
}

/** Relative luminance (Rec. 709) of a linear color. */
export function luminanceOf(c: Color): number {
  return 0.2126 * c.r + 0.7152 * c.g + 0.0722 * c.b
}

/** `hex` at unit luminance times `luminance`, as a linear (HDR) color. */
export function emissiveColor(hex: string, luminance: number): Color {
  const c = new Color(hex)
  return c.multiplyScalar(luminance / Math.max(1e-4, luminanceOf(c)))
}

/** A shared look's material, kept for the life of the page: city emitters come back often. */
function kept(look: MaterialLook): Material {
  const key = materialKey(look)
  const material = sharedMaterial(look, key)
  if (!material.userData.kept) {
    material.userData.kept = true
    retainMaterial(key)
  }
  return material
}

/**
 * The shared unlit material for an emitter of color `hex` at `luminance`, through core's
 * `glowLook`, so it batches with every other part of that look. Read-only.
 */
export function glowMaterial(hex: string, luminance: number, opacity = 1): Material {
  return kept(
    glowLook(
      emissiveColor(hex, 1),
      luminance,
      opacity < 1 ? { transparent: true, opacity, depthWrite: false } : undefined,
    ),
  )
}

/**
 * The shared unlit material for picture content on a lit board: `hex` keeps its own value and
 * is scaled so that white would shine at `white`. Dark backgrounds stay dark, bright text and
 * shapes glow.
 */
export function screenMaterial(hex: string, white: number): Material {
  return kept(glowLook(hex, white))
}

city/weather.ts

// The weather: what falls, how hard the wind blows and how wet the ground is, as a pure function
// of a clock. Hand `weatherAt` the same Unix time on two machines and both get the same weather,
// so players who share a clock share the sky with nothing sent between them.
//
// Also here, the uniforms `Rain` and `WetStreet` share. `Rain` drives them from the weather it is
// given and the street only listens.
//
// No three.js, React or Runek in this file, so a HUD can import it without pulling in the 3D code.

export type WeatherKind = 'dry' | 'windy' | 'drizzle' | 'light-rain' | 'heavy-rain' | 'snow'

interface WeatherRow {
  /** How likely a spell is to be this kind, against the other rows. Any positive numbers. */
  odds: number
  /** A spell lasts between these, in minutes. Keep the shorter one well above `WEATHER_TIMING.blend`. */
  minutes: readonly [number, number]
  /** How hard it rains, 0 to 1: how many streaks fall, how fast and how long they are, how loud it is. */
  rain: number
  /** How hard it snows, 0 to 1. */
  snow: number
  /** The steady wind is somewhere between these, in units a second. Each spell picks its own speed and heading. */
  wind: readonly [number, number]
  /** How far gusts swing the wind around that speed: 0.4 is 40% either way. */
  gust: number
  /** How wet the ground ends up if the spell lasts, 0 (dry) to 1 (soaked). */
  wet: number
}

/**
 * The tuning table: one row for each kind of weather. Change the mix of weather here.
 *
 * `odds` are relative (these add up to 100, so they read as percentages of spells, not of time:
 * a kind with long spells fills more of the night than its odds say). The same kind never comes
 * twice in a row.
 */
export const WEATHER: Record<WeatherKind, WeatherRow> = {
  dry: { odds: 28, minutes: [5, 12], rain: 0, snow: 0, wind: [0.4, 1.6], gust: 0.3, wet: 0 },
  windy: { odds: 14, minutes: [3, 7], rain: 0, snow: 0, wind: [7, 11], gust: 0.45, wet: 0 },
  drizzle: {
    odds: 16,
    minutes: [4, 9],
    rain: 0.15,
    snow: 0,
    wind: [0.5, 2.5],
    gust: 0.3,
    wet: 0.6,
  },
  'light-rain': {
    odds: 20,
    minutes: [4, 10],
    rain: 0.45,
    snow: 0,
    wind: [1, 3.5],
    gust: 0.35,
    wet: 0.85,
  },
  'heavy-rain': { odds: 12, minutes: [3, 6], rain: 1, snow: 0, wind: [3, 7], gust: 0.5, wet: 1 },
  snow: { odds: 10, minutes: [5, 10], rain: 0, snow: 0.85, wind: [0.5, 2.5], gust: 0.3, wet: 0.5 },
}

/** The clocks behind the table, in seconds unless they say otherwise. */
export const WEATHER_TIMING = {
  /** One spell eases into the next over this long, so nothing pops. */
  blend: 45,
  /** Heavy rain soaks dry ground in about this long. Lighter weather takes longer. */
  soak: 40,
  /** Wet ground loses about two thirds of its water in this long once the rain stops. */
  dry: 480,
  /** The same in a wind, which dries a street faster. */
  dryWindy: 200,
  /** How far back the ground remembers, in minutes. Several times `dry`, or wetness steps. */
  memoryMinutes: 40,
  /** The schedule is dealt in blocks this long, in minutes. A spell never runs across two blocks. */
  blockMinutes: 120,
  /** Changes every spell at once: a different night of weather for the same clock. */
  seed: 2047,
}

export const WEATHER_KINDS = Object.keys(WEATHER) as WeatherKind[]

export interface WeatherState {
  /** The kind of weather this is. Halfway through a change it becomes the new one. */
  kind: WeatherKind
  /** How hard it rains now, 0 to 1. */
  rain: number
  /** How hard it snows now, 0 to 1. */
  snow: number
  /** The wind now, gusts and all, in units a second along x and z. */
  wind: { x: number; z: number }
  /** The length of `wind`. */
  speed: number
  /** How wet the ground is, 0 to 1. It lags the rain: streets stay wet for a while after it stops. */
  wetness: number
}

/** A state to hand to `weatherAt` as `out`, so a caller that asks every frame makes no garbage. */
export function makeWeather(): WeatherState {
  return { kind: 'dry', rain: 0, snow: 0, wind: { x: 0, z: 0 }, speed: 0, wetness: 0 }
}

/** Short names `?weather=` also takes. */
const ALIASES: Record<string, WeatherKind> = {
  clear: 'dry',
  wind: 'windy',
  light: 'light-rain',
  rain: 'light-rain',
  heavy: 'heavy-rain',
  storm: 'heavy-rain',
}

/** The kind a word names, or null when it names none. */
export function parseWeatherKind(word: string | null | undefined): WeatherKind | null {
  if (!word) return null
  const w = word.trim().toLowerCase()
  if (w in WEATHER) return w as WeatherKind
  return ALIASES[w] ?? null
}

// The schedule --------------------------------------------------------------------------------

interface Spell {
  kind: WeatherKind
  /** Unix ms. */
  start: number
  end: number
  rain: number
  snow: number
  /** The steady wind, before gusts. */
  wx: number
  wz: number
  gust: number
  wet: number
  /** Seconds for the ground to take this spell's water up, and to let it go. */
  soak: number
  dry: number
}

const BLOCK_MS = WEATHER_TIMING.blockMinutes * 60_000
const BLEND_MS = WEATHER_TIMING.blend * 1000
const MEMORY_MS = WEATHER_TIMING.memoryMinutes * 60_000
/** A block never ends on a spell shorter than this: the one before runs on to the end instead. */
const SHORTEST_MS = Math.max(
  2 * BLEND_MS,
  Math.min(...WEATHER_KINDS.map((k) => WEATHER[k].minutes[0])) * 60_000,
)

/** A small seeded generator, so the same block deals the same spells on every machine. */
function seeded(seed: number): () => number {
  let a = seed >>> 0
  return () => {
    a = (a + 0x6d2b79f5) >>> 0
    let t = a
    t = Math.imul(t ^ (t >>> 15), t | 1)
    t ^= t + Math.imul(t ^ (t >>> 7), t | 61)
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296
  }
}

function pick(r: () => number, not: WeatherKind | null): WeatherKind {
  let total = 0
  for (const k of WEATHER_KINDS) if (k !== not) total += WEATHER[k].odds
  let at = r() * total
  let last = WEATHER_KINDS[0]
  for (const k of WEATHER_KINDS) {
    if (k === not) continue
    last = k
    at -= WEATHER[k].odds
    if (at < 0) return k
  }
  return last
}

const blocks = new Map<number, Spell[]>()

/** The spells of one block, end to end. Dealt from the block's number alone, so any block can be asked for on its own. */
function spellsOf(block: number): Spell[] {
  const known = blocks.get(block)
  if (known) return known
  const r = seeded(Math.imul(block, 0x9e3779b1) ^ WEATHER_TIMING.seed)
  const from = block * BLOCK_MS
  const spells: Spell[] = []
  let t = 0
  let prev: WeatherKind | null = null
  while (t < BLOCK_MS) {
    const kind = pick(r, prev)
    const row = WEATHER[kind]
    let length = (row.minutes[0] + r() * (row.minutes[1] - row.minutes[0])) * 60_000
    if (BLOCK_MS - (t + length) < SHORTEST_MS) length = BLOCK_MS - t
    const heading = r() * Math.PI * 2
    const speed = row.wind[0] + r() * (row.wind[1] - row.wind[0])
    // No two spells of a kind are quite alike: each falls a little harder or softer than the table says.
    const strength = 0.85 + r() * 0.3
    const rain = Math.min(1, row.rain * strength)
    const snow = Math.min(1, row.snow * strength)
    spells.push({
      kind,
      start: from + t,
      end: from + t + length,
      rain,
      snow,
      wx: Math.cos(heading) * speed,
      wz: Math.sin(heading) * speed,
      gust: row.gust,
      wet: row.wet,
      soak: WEATHER_TIMING.soak / (0.25 + 0.75 * Math.max(rain, snow * 0.3)),
      dry: kind === 'windy' ? WEATHER_TIMING.dryWindy : WEATHER_TIMING.dry,
    })
    t += length
    prev = kind
  }
  if (blocks.size > 6) blocks.clear()
  blocks.set(block, spells)
  return spells
}

// The state at one moment ---------------------------------------------------------------------

const clamp01 = (v: number) => Math.min(1, Math.max(0, v))
const smooth = (v: number) => {
  const x = clamp01(v)
  return x * x * (3 - 2 * x)
}
const mix = (a: number, b: number, k: number) => a + (b - a) * k
/** What falls eases out in the first part of a change and in over the last, so rain is nearly gone before snow starts. */
const fall = (from: number, to: number, x: number) =>
  mix(from, to, to < from ? smooth(x / 0.6) : smooth((x - 0.4) / 0.6))

const TURN = Math.PI * 2

/** Lay gusts over a steady wind. Slow sums of sines of the clock, so every player feels the same gust. */
function blow(out: WeatherState, wx: number, wz: number, gust: number, unixMs: number) {
  const t = unixMs / 1000
  const swell =
    0.5 * Math.sin((t * TURN) / 23.1) +
    0.3 * Math.sin((t * TURN) / 9.7 + 1.3) +
    0.2 * Math.sin((t * TURN) / 4.3 + 4.1)
  const veer = 0.6 * gust * Math.sin((t * TURN) / 37 + 2.2)
  const scale = Math.max(0.15, 1 + gust * swell)
  const cos = Math.cos(veer) * scale
  const sin = Math.sin(veer) * scale
  out.wind.x = wx * cos - wz * sin
  out.wind.z = wx * sin + wz * cos
  out.speed = Math.hypot(out.wind.x, out.wind.z)
}

/** Carry the ground's wetness through the spells of one block that fall inside its memory. -1 means nothing yet. */
function soakThrough(spells: Spell[], since: number, now: number, wet: number): number {
  for (const s of spells) {
    if (s.end <= since || s.start > now) continue
    if (wet < 0) wet = s.wet
    const seconds = (Math.min(s.end, now) - Math.max(s.start, since)) / 1000
    wet = s.wet + (wet - s.wet) * Math.exp(-seconds / (s.wet > wet ? s.soak : s.dry))
  }
  return wet
}

/**
 * The weather at a moment, from the clock alone. `unixMs` is Unix time in milliseconds: hand it
 * the clock the players share. Pure: the same time always gives the same weather, whenever and
 * wherever it is asked. Pass `out` to have it filled in place.
 */
export function weatherAt(unixMs: number, out: WeatherState = makeWeather()): WeatherState {
  const block = Math.floor(unixMs / BLOCK_MS)
  const today = spellsOf(block)
  const before = spellsOf(block - 1)
  let i = today.length - 1
  while (i > 0 && today[i].start > unixMs) i--
  const cur = today[i]
  const prev = i > 0 ? today[i - 1] : before[before.length - 1]
  const x = clamp01((unixMs - cur.start) / BLEND_MS)
  const k = smooth(x)
  out.kind = x < 0.5 ? prev.kind : cur.kind
  out.rain = fall(prev.rain, cur.rain, x)
  out.snow = fall(prev.snow, cur.snow, x)
  blow(out, mix(prev.wx, cur.wx, k), mix(prev.wz, cur.wz, k), mix(prev.gust, cur.gust, k), unixMs)
  const since = unixMs - MEMORY_MS
  out.wetness = clamp01(soakThrough(today, since, unixMs, soakThrough(before, since, unixMs, -1)))
  return out
}

/**
 * One kind of weather that never changes, as if its spell had always been: for `?weather=`, tests
 * and screenshots. The wind still gusts with the clock.
 */
export function steadyWeather(
  kind: WeatherKind,
  unixMs: number,
  out: WeatherState = makeWeather(),
): WeatherState {
  const row = WEATHER[kind]
  const speed = (row.wind[0] + row.wind[1]) / 2
  out.kind = kind
  out.rain = row.rain
  out.snow = row.snow
  blow(out, speed * 0.894, speed * 0.447, row.gust, unixMs)
  out.wetness = row.wet
  return out
}

// What `Rain` and `WetStreet` share -----------------------------------------------------------

/**
 * The rain clock wraps to 0 after this many seconds. Every speed and rate that reads the clock
 * is picked so its motion repeats exactly at the wrap, and nothing jumps.
 */
export const RAIN_PERIOD = 160

/**
 * Read by the streaks, splashes and flakes in `Rain` and by every `WetStreet`. With no `Rain`
 * mounted they stay as they are here: no rings, and streets as wet as their own props say.
 */
export const rainUniforms = {
  /** Seconds of weather so far, wrapped at `RAIN_PERIOD`. */
  uRainTime: { value: 0 },
  /** How hard rain rings the puddles, 0 (still water) to 1 (heavy rain). */
  uRain: { value: 0 },
  /** How wet the weather has left the ground, 0 to 1. It scales each street's own `wetness` and `puddles`. */
  uWetness: { value: 1 },
}