Skip to main content

wde_terrain/
lib.rs

1//! Terrain system for WaterDropEngine.
2//!
3//! The world is a square of `CHUNK_COUNT` × `CHUNK_COUNT` chunks of `CHUNK_SIZE` world units.
4//! Each chunk has a heightmap and splat maps blending the ground materials, stored as layers of
5//! shared GPU texture arrays.
6//!
7//! A terrain lives in a folder of `res/`, described by its `config/terrain.json`
8//! ([`TerrainConfig`](prelude::TerrainConfig)): height encoding, map files, and the rules of its
9//! masks. It is a single entity made of:
10//! - [`Terrain`](prelude::Terrain): the source of truth. Loaded from the heightmaps and splat maps
11//!   of its folder, it answers height queries ([`Terrain::height_at`](prelude::Terrain::height_at))
12//!   and queues the chunks edited since the last frame for the render and physics. From the
13//!   heights, it derives a slope and a buildable mask: buildable ground is flat ground at height 0
14//!   away from any other ([`Terrain::is_area_buildable`](prelude::Terrain::is_area_buildable)),
15//!   walkable ground is not steeper than the config allows
16//!   ([`Terrain::is_walkable`](prelude::Terrain::is_walkable)).
17//! - [`TerrainRenderer`](prelude::TerrainRenderer): draws the visible chunks with levels of detail
18//!   depending on their distance to the camera, optionally tinted by a debug view of the masks,
19//!   levels of detail, chunk borders or material weights
20//!   ([`TerrainDebugView`](prelude::TerrainDebugView)).
21//! - [`TerrainPhysics`](prelude::TerrainPhysics): one heightfield collider per chunk, in
22//!   [`HEIGHTMAP_COLLIDER_GROUP`].
23//!
24//! The [`TerrainCursorPos`](prelude::TerrainCursorPos) resource gives the terrain point under the
25//! cursor, in both the main and render worlds.
26//!
27//! The ground materials blended by the splat maps are listed in the config. Their textures are
28//! loaded when the app starts, from the [`TerrainMaterialSet`](prelude::TerrainMaterialSet)
29//! resource.
30//!
31//! # Example
32//! ```rust,ignore
33//! const TERRAIN_PATH: &str = "terrain/desert";
34//!
35//! app.insert_resource(TerrainMaterialSet::from(&TerrainConfig::load(TERRAIN_PATH)))
36//!     .add_systems(PreStartup, init);
37//!
38//! fn init(mut commands: Commands, asset_server: Res<AssetServer>) {
39//!     let terrain = Terrain::load(TERRAIN_PATH);
40//!     commands.spawn((
41//!         TerrainRenderer::new(&asset_server, &terrain),
42//!         terrain,
43//!         TerrainPhysics::default()
44//!     ));
45//! }
46//! ```
47//! `wde-terrain-editor` edits the spawned terrain.
48
49use bevy::prelude::*;
50use wde_physics::prelude::*;
51
52use crate::{
53    physics::TerrainPhysicsPlugin, render::TerrainRenderPlugin, utils::TerrainUtilsPlugin
54};
55
56/// The terrain heightmap.
57pub const HEIGHTMAP_COLLIDER_GROUP: ColliderGroup = ColliderGroup::GROUP_1;
58
59pub(crate) mod config;
60pub(crate) mod manager;
61pub(crate) mod masks;
62pub(crate) mod physics;
63pub mod render;
64pub(crate) mod utils;
65
66#[doc(hidden)]
67pub mod prelude {
68    pub use crate::HEIGHTMAP_COLLIDER_GROUP;
69    pub use crate::config::{
70        TERRAIN_CONFIG_FILE, TerrainConfig, TerrainMaskRules, TerrainMaterial
71    };
72    pub use crate::manager::*;
73    pub use crate::masks::ChunkMasks;
74    pub use crate::physics::terrain_physics::TerrainPhysics;
75    pub use crate::render::dependencies::materials::TerrainMaterialSet;
76    pub use crate::render::dependencies::terrain_buffer::{
77        TERRAIN_LOD_DEBUG_COLORS, TERRAIN_MATERIAL_COUNT, TERRAIN_MATERIAL_DEBUG_COLORS,
78        TerrainDebugView, TerrainRenderSettings
79    };
80    pub use crate::render::extractor::*;
81    pub use crate::render::renderer::TERRAIN_LOD_COUNT;
82    pub use crate::render::renderer::TerrainRenderer;
83    pub use crate::render::renderer_gpu::{
84        TerrainChunkArrayBg, TerrainComputeArrayBg, TerrainRendererGPU
85    };
86    pub use crate::utils::cursor_pos::TerrainCursorPos;
87}
88
89pub struct TerrainPlugin;
90impl Plugin for TerrainPlugin {
91    fn build(&self, app: &mut App) {
92        app.add_plugins((
93            TerrainRenderPlugin,
94            TerrainPhysicsPlugin,
95            TerrainUtilsPlugin
96        ))
97        .register_type::<prelude::Terrain>()
98        .register_type::<prelude::TerrainRenderer>()
99        .register_type::<prelude::TerrainPhysics>()
100        .register_type::<prelude::TerrainCursorPos>();
101    }
102}