threejs-grass
    Preparing search index...

    Class Grass

    An infinite, camera-centred grass field.

    The world is divided into square tiles that stream in and out around the camera; each tile holds instanced blades placed on the terrain, thinned by distance through four LODs.

    const grass = await Grass.create({ camera, scene, renderer, terrain, tileSize: 25, maxDistance: 200 })
    // That's it — it updates itself whenever `scene` is rendered.
    Index
    camera: Camera

    Camera the field follows. Can be swapped at any time.

    object: Group<Object3DEventMap> = ...

    Root of all grass tiles. Added to scene for you when one is passed to Grass.create.

    sampleHeight: HeightFn

    Terrain height at world (x, z); NaN where there is no terrain. Handy for placing objects on the ground.

    settings: GrassSettings

    Resolved settings (defaults ← preset ← your input). Read-only: change them with Grass.set.

    stats: GrassStats = ...

    Live counters, refreshed on every Grass.update.

    uniforms: {
        baseColor: UniformNode<"color", Color>;
        bladeHeight: UniformNode<"float", number>;
        bladeWidth: UniformNode<"float", number>;
        colorVariation: UniformNode<"float", number>;
        curvature: UniformNode<"float", number>;
        debugLods: UniformNode<"float", number>;
        interactionStrength: UniformNode<"float", number>;
        interactorCount: UniformNode<"int", number>;
        interactors: UniformArrayNode<string>;
        lodDensities: UniformNode<"vec4", Vector4>;
        lodDistances: UniformNode<"vec4", Vector4>;
        map: TextureNode<"vec4">;
        mapBounds: UniformNode<"vec4", Vector4>;
        maxDistance: UniformNode<"float", number>;
        patchiness: UniformNode<"float", number>;
        stiffness: UniformNode<"float", number>;
        sunColor: UniformNode<"color", Color>;
        sunDirection: UniformNode<"vec3", Vector3>;
        tipColor: UniformNode<"color", Color>;
        translucency: UniformNode<"float", number>;
        viewPosition: UniformNode<"vec3", Vector3>;
        windDirection: UniformNode<"vec2", Vector2>;
        windScale: UniformNode<"float", number>;
        windSpeed: UniformNode<"float", number>;
        windStrength: UniformNode<"float", number>;
    } = ...

    Shader uniforms, for advanced tweaks or for reading grass state in your own TSL materials.

    Type Declaration

    • baseColor: UniformNode<"color", Color>

      Blade colour at the root (from baseColor).

    • bladeHeight: UniformNode<"float", number>

      Blade height in metres.

    • bladeWidth: UniformNode<"float", number>

      Blade base width in metres.

    • colorVariation: UniformNode<"float", number>

      Per-blade brightness variation, 0..1.

    • curvature: UniformNode<"float", number>

      Natural droop, 0..1.

    • debugLods: UniformNode<"float", number>

      1 tints grass by LOD, 0 = normal shading.

    • interactionStrength: UniformNode<"float", number>

      Global multiplier on interactor push.

    • interactorCount: UniformNode<"int", number>

      Number of active entries in interactors.

    • interactors: UniformArrayNode<string>

      xyz = world position, w = radius (0 = unused slot).

    • lodDensities: UniformNode<"vec4", Vector4>

      The four LOD density fractions (x, y, z, w = LOD 0..3).

    • lodDistances: UniformNode<"vec4", Vector4>

      The four LOD distances (x, y, z, w = LOD 0..3).

    • map: TextureNode<"vec4">

      Grass map texture (R = coverage, G = height); a neutral 1×1 texture when there is none.

    • mapBounds: UniformNode<"vec4", Vector4>

      minX, minZ, sizeX, sizeZ of the grass map in world space.

    • maxDistance: UniformNode<"float", number>

      Radius with grass; blades fade over its last 15%.

    • patchiness: UniformNode<"float", number>

      Field-scale light/dark and dry patches, 0..1.

    • stiffness: UniformNode<"float", number>

      Resistance to wind and interaction (never below 0.05).

    • sunColor: UniformNode<"color", Color>

      Sun colour × intensity.

    • sunDirection: UniformNode<"vec3", Vector3>

      Direction towards the sun. Drives back-lit translucency.

    • tipColor: UniformNode<"color", Color>

      Blade colour at the tip (from tipColor).

    • translucency: UniformNode<"float", number>

      Back-lit glow strength.

    • viewPosition: UniformNode<"vec3", Vector3>

      Position LOD/fades are measured from. Set from the Grass camera on the CPU so shadow passes (which render from the light) see exactly the same blades as the main view.

    • windDirection: UniformNode<"vec2", Vector2>

      Normalised XZ wind direction.

    • windScale: UniformNode<"float", number>

      Gust noise frequency (1/metres).

    • windSpeed: UniformNode<"float", number>

      Gust travel speed.

    • windStrength: UniformNode<"float", number>

      How far blades bend in the wind.

    • Fraction (0..1) of full blade density at a distance from the camera — the CPU mirror of the shader's LOD curve.

      Parameters

      • dist: number

      Returns number

    • Removes the grass from the scene and frees its GPU resources.

      Returns void

    • TSL nodes reading the grass map at a world XZ position. Use them in your terrain material to show dirt where grass was erased; they follow map edits and grassMap swaps live.

      Parameters

      • worldXZ: Node<"vec2">

      Returns GrassMapSample

      const { coverage } = grass.mapNode(positionWorld.xz)
      terrainMaterial.colorNode = mix(dirtColor, grassyGround, coverage)
    • Replaces all settings with input: anything not given falls back to the preset, then to defaults. Use it to switch presets cleanly after overriding style fields. <Grass> uses it so props behave declaratively (removing a prop reverts it).

      Parameters

      Returns this

    • Changes settings at runtime, merging into what was set before. Visual settings (colours, height, wind, …) apply instantly through uniforms; layout settings (type, density, tileSize, lods, clumping, variation, maxSlope) rebuild tiles. undefined values are ignored; wind merges field by field. To drop earlier overrides (e.g. go back to a preset's colours), use Grass.reset.

      Parameters

      Returns this

    • Swaps the terrain (mesh or height function) and regenerates all tiles.

      Parameters

      Returns void

    • Re-centres the tiles on the camera, streams tiles in/out and refreshes interactors and the sun. Called automatically when a scene was given (and by the <Grass> component); otherwise call it once per frame.

      Parameters

      • budget: number = ...

        Milliseconds allowed for tile (re)builds this frame. Defaults to settings.buildBudget.

      Returns void

    • Creates a grass field and builds every visible tile so the first frame is complete.

      Parameters

      Returns Promise<Grass>

      If renderer is not a WebGPURenderer.