threejs-worldgen
    Preparing search index...

    Class StateField

    A low-resolution world-space map of the four material states over a square region: accumulated wetness and snow cover from the weather, burn scars from wildfires, damage from storms and impacts. Terrain, grass and trees sample it in their shaders (StateField.node) and on the CPU (StateField.sample). Stored as float on the CPU and uploaded as an RGBA8 texture (R = wetness, G = snow, B = burn, A = damage).

    Coordinates are local frame coordinates. With a floating origin, call StateField.setOrigin with the frame position of render-space zero; the GPU lookup stays exact at any distance.

    const field = new StateField({ size: 1024, resolution: 128 })
    field.paint(10, -4, 6, { burn: 1 }) // a scorch mark
    field.updateFromWeather(weather, time) // wetness + snow from the weather (budgeted)
    field.update() // upload changes (once per frame)
    applyMaterialStatesTo(terrainMaterial, field.node())
    Index
    bounds: UniformNode<"vec4", Vector4> = ...

    xy = field minimum relative to render-space zero, zw = size.

    data: Float32Array

    Float states, 4 per texel (RGBA = wetness, snow, burn, damage), row-major from (minX, minZ).

    Resolved settings. Change them with StateField.set.

    texture: DataTexture

    The GPU copy (RGBA8, linear filtering).

    version: number = 0

    Bumped on every write.

    • Calls fn(x, z, i, j) for every texel centre (frame coordinates).

      Parameters

      • fn: (x: number, z: number, i: number, j: number) => void

      Returns void

    • The four states as TSL nodes at a render-space XZ position (default positionWorld.xz). Zero outside the region. Pass the result straight to applyMaterialStates.

      Parameters

      • xz: Node<"vec2"> = positionWorld.xz

      Returns StateNodes

    • Paints a round brush of radius metres at (x, z). falloff (0…1) is the soft share of the radius. Values are clamped to 0…1. Returns the number of texels touched.

      Parameters

      Returns number

    • Sets the frame position at render-space zero (floating origin). [x, y, z] or [x, z].

      Parameters

      • origin: readonly number[]

      Returns this

    • Writes wetness and snow from Weather.surfaceAt at time. The work is spread over calls: each call processes rows until budget milliseconds have passed, and a full pass uses the time it started with, so the whole field is consistent. Returns true when a pass completes.

      Parameters

      • weather: Weather
      • time: number
      • budget: number = 2

      Returns boolean