threejs-worldgen
    Preparing search index...

    Class Corn

    Corn fields: a threejs-grass variant of tall stalks planted in straight rows inside field polygons, with the grass wind, and the grass Interactors pushing plants aside and flattening the ones they drive over (the flattened corn stays down; see Corn.flattened). Plants are built in chunks around the camera, thinned by four levels of detail, and a canopy surface stands in for the plants that thinning drops, so whole fields render from the air.

    const corn = await Corn.create({ camera, scene, renderer, terrain, fields: [{ polygon, rowDirection: 0, rowSpacing: 0.76 }] })
    corn.set({ interactors: tractor.wheels.map((object) => ({ object, radius: 0.4 })) })
    Index
    camera: Camera

    Camera the levels of detail follow. Can be swapped at any time.

    flattenMap: FlattenMap

    Where corn has been flattened. Replaced (cleared) when the fields change.

    object: Group<Object3DEventMap> = ...

    Root of the canopy and all plant chunks. Added to scene for you when one is passed to Corn.create.

    sampleHeight: HeightFn

    Terrain height at world (x, z); NaN where there is no terrain.

    settings: CornSettings

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

    stats: CornStats = ...

    Live counters, refreshed on every Corn.update.

    uniforms: {
        colorVariation: UniformNode<"float", number>;
        debugLods: UniformNode<"float", number>;
        dryColor: UniformNode<"color", Color>;
        flatten: TextureNode<"vec4">;
        flattenBounds: UniformNode<"vec4", Vector4>;
        growth: UniformNode<"float", number>;
        harvested: UniformNode<"float", number>;
        height: UniformNode<"float", number>;
        heightVariation: UniformNode<"float", number>;
        interactionStrength: UniformNode<"float", number>;
        interactorCount: UniformNode<"int", number>;
        interactors: UniformArrayNode<string>;
        leafColor: UniformNode<"color", Color>;
        lodDensities: UniformNode<"vec4", Vector4>;
        lodDistances: UniformNode<"vec4", Vector4>;
        patchiness: UniformNode<"float", number>;
        soilColor: UniformNode<"color", Color>;
        stiffness: UniformNode<"float", number>;
        stubbleHeight: UniformNode<"float", number>;
        sunColor: UniformNode<"color", Color>;
        sunDirection: UniformNode<"vec3", Vector3>;
        tasselColor: 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>;
        youngColor: UniformNode<"color", Color>;
    } = ...

    Shader uniforms, for advanced tweaks.

    Type Declaration

    • colorVariation: UniformNode<"float", number>

      Per-plant brightness variation, 0..1.

    • debugLods: UniformNode<"float", number>

      1 tints corn by level of detail.

    • dryColor: UniformNode<"color", Color>

      Dry leaf, husk and stubble colour.

    • flatten: TextureNode<"vec4">

      Where corn was flattened (see FlattenMap).

    • flattenBounds: UniformNode<"vec4", Vector4>

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

    • growth: UniformNode<"float", number>

      Growth stage: 0 seedling … ~0.65 full height … 0.8 mature … 1 dry.

    • harvested: UniformNode<"float", number>

      1 = harvested: stubble only.

    • height: UniformNode<"float", number>

      Mature stalk height in metres, tassel included.

    • heightVariation: UniformNode<"float", number>

      0..1 random height spread per plant.

    • interactionStrength: UniformNode<"float", number>

      Global multiplier on interactor push (and flattening).

    • interactorCount: UniformNode<"int", number>

      Number of active entries in interactors.

    • interactors: UniformArrayNode<string>

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

    • leafColor: UniformNode<"color", Color>

      Mature leaf colour.

    • lodDensities: UniformNode<"vec4", Vector4>

      The four LOD density fractions.

    • lodDistances: UniformNode<"vec4", Vector4>

      The four LOD distances (x, y, z, w = LOD 0..3); stalks end at w.

    • patchiness: UniformNode<"float", number>

      Field-scale brightness and dryness variation, 0..1.

    • soilColor: UniformNode<"color", Color>

      Bare soil colour between young rows and under the canopy.

    • stiffness: UniformNode<"float", number>

      Resistance to wind and interaction (never below 0.05).

    • stubbleHeight: UniformNode<"float", number>

      Stubble height in metres after harvest.

    • sunColor: UniformNode<"color", Color>

      Sun colour × intensity.

    • sunDirection: UniformNode<"vec3", Vector3>

      Direction towards the sun.

    • tasselColor: UniformNode<"color", Color>

      Tassel colour.

    • translucency: UniformNode<"float", number>

      Back-lit glow of the leaves.

    • viewPosition: UniformNode<"vec3", Vector3>

      Position LOD is measured from (the Corn camera), so shadow passes see the same plants.

    • 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 stalks bend in the wind.

    • youngColor: UniformNode<"color", Color>

      Young leaf colour (seedlings).

    • Fraction (0..1) of plants kept at a distance from the camera: the CPU mirror of the shader's LOD curve.

      Parameters

      • dist: number

      Returns number

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

      Returns void

    • Flattens corn in a disk of radius metres around (x, z), falling along (dirX, dirZ) (e.g. a vehicle's heading; omit to fall outwards). Interactors do this every frame; call it for anything else that knocks corn down. Flattened corn stays down until Corn.clearFlattened.

      Parameters

      • x: number
      • z: number
      • radius: number
      • dirX: number = 0
      • dirZ: number = 0

      Returns void

    • How flattened the corn at world (x, z) is: 0 standing … 1 flat.

      Parameters

      • x: number
      • z: number

      Returns number

    • Changes settings at runtime, merging into what was set before. Looks, growth, harvest, wind and interaction apply instantly through uniforms; fields, spacing, seed and chunk size rebuild the plants. undefined values are ignored; wind merges field by field. Corn.reset drops earlier overrides.

      Parameters

      Returns this

    • Swaps the terrain (mesh or height function) and rebuilds plants and canopy.

      Parameters

      Returns void

    • Streams plant chunks around the camera, flattens corn under interactors and refreshes the shader state. Called automatically when a scene was given (and by the <Corn> component); otherwise call it once per frame.

      Parameters

      • budget: number = ...

        Milliseconds allowed for chunk builds this frame. Defaults to settings.buildBudget.

      Returns void

    • Creates a corn field and builds the chunks in view so the first frame is complete.

      Parameters

      Returns Promise<Corn>

      If renderer is not a WebGPURenderer.