Skip to main content

Crate wde_terrain

Crate wde_terrain 

Source
Expand description

Terrain system for WaterDropEngine.

The world is a square of CHUNK_COUNT × CHUNK_COUNT chunks of CHUNK_SIZE world units. Each chunk has a heightmap and splat maps blending the ground materials, stored as layers of shared GPU texture arrays.

A terrain lives in a folder of res/, described by its config/terrain.json (TerrainConfig): height encoding, map files, and the rules of its masks. It is a single entity made of:

  • Terrain: the source of truth. Loaded from the heightmaps and splat maps of its folder, it answers height queries (Terrain::height_at) and queues the chunks edited since the last frame for the render and physics. From the heights, it derives a slope and a buildable mask: buildable ground is flat ground at height 0 away from any other (Terrain::is_area_buildable), walkable ground is not steeper than the config allows (Terrain::is_walkable).
  • TerrainRenderer: draws the visible chunks with levels of detail depending on their distance to the camera, optionally tinted by a debug view of the masks, levels of detail, chunk borders or material weights (TerrainDebugView).
  • TerrainPhysics: one heightfield collider per chunk, in HEIGHTMAP_COLLIDER_GROUP.

The TerrainCursorPos resource gives the terrain point under the cursor, in both the main and render worlds.

The ground materials blended by the splat maps are listed in the config. Their textures are loaded when the app starts, from the TerrainMaterialSet resource.

§Example

ⓘ
const TERRAIN_PATH: &str = "terrain/desert";

app.insert_resource(TerrainMaterialSet::from(&TerrainConfig::load(TERRAIN_PATH)))
    .add_systems(PreStartup, init);

fn init(mut commands: Commands, asset_server: Res<AssetServer>) {
    let terrain = Terrain::load(TERRAIN_PATH);
    commands.spawn((
        TerrainRenderer::new(&asset_server, &terrain),
        terrain,
        TerrainPhysics::default()
    ));
}

wde-terrain-editor edits the spawned terrain.

Modules§

render

Structs§

TerrainPlugin

Constants§

HEIGHTMAP_COLLIDER_GROUP
The terrain heightmap.