Skip to main content

wde/
lib.rs

1//! WaterDropEngine - A modular game engine.
2//!
3//! # Overview
4//! WaterDropEngine is a modular game engine built on top of Bevy's ECS. It provides a collection of plugins and tools.
5//! The engine is composed of several modules, organized from low-level to high-level:
6//! - High-Level:
7//!    - [`wde_terrain`](wde_terrain): A plugin for generating and rendering procedural terrain.
8//!    - [`wde_terrain_editor`](wde_terrain_editor): A plugin for editing terrain in-game.
9//!    - [`wde_terrain_grid`](wde_terrain_grid): A plugin for managing terrain grids and LOD.
10//!    - [`wde_terrain_navigation`](wde_terrain_navigation): A plugin for pathfinding and navigation on terrain.
11//! - Render:
12//!    - [`wde_pbr`](wde_pbr): A plugin for physically based rendering (PBR) materials and lighting.
13//!    - [`wde_camera`](wde_camera): A plugin for managing cameras and viewports.
14//!    - [`wde_camera_controller`](wde_camera_controller): A plugin for controlling cameras with various input methods.
15//!    - [`wde_gltf`](wde_gltf): A plugin for loading and rendering glTF 3D models.
16//!    - [`wde_gizmos`](wde_gizmos): A plugin for rendering debug gizmos and visualizations.
17//! - Core:
18//!    - [`wde_renderer`](wde_renderer): A plugin for rendering 3D graphics using the `wgpu` graphics API.
19//!    - [`wde_scene`](wde_scene): A plugin for managing scenes, entities, and components.
20//!    - [`wde_editor`](wde_editor): A plugin for creating in-game editors and tools.
21//! - Wrappers:
22//!    - [`wde_wgpu`](wde_wgpu): A wrapper around the `wgpu` graphics API for rendering. It is superseeded by [`wde_renderer`](wde_renderer).
23//!    - [`wde_physics`](wde_physics): A wrapper around the `rapier` physics engine for 3D physics simulation.
24//!    - [`wde_logger`](wde_logger): A wrapper around the `tracing` library for logging and diagnostics.
25//!    - [`wde_egui`](wde_egui): A wrapper around the `egui` library for creating user interfaces. It is superseeded by [`wde_editor`](wde_editor).
26//!
27//! # Getting Started
28//! To get started with WaterDropEngine, add the `wde` crate to your `Cargo.toml`:
29//! ```toml
30//! [dependencies]
31//! wde = "0.1"
32//! ```
33//! Then, in your main Rust file, you can use the default plugins to set up a basic application:
34//! ```rust
35//! app.add_plugins(WdeDefaultPlugins);
36//! ```
37//!
38//! To modify and configure individual plugins, use:
39//! ```rust
40//! app.add_plugins(WdeDefaultPlugins.set(LogPlugin {
41//!     level: LogLevel::DEBUG,
42//!     ..Default::default()
43//! }));
44//! ```
45//!
46//! # Features
47//! WaterDropEngine is designed to be modular and extensible. You can enable or disable features! using Cargo features. For example, to enable the `pbr` feature for physically based rendering, add the following to your `Cargo.toml`:
48//! ```toml
49//! [dependencies]
50//! wde = { version = "0.1", features = ["pbr"] }
51//! ```
52
53use bevy::{
54    app::{ScheduleRunnerPlugin, TaskPoolThreadAssignmentPolicy, plugin_group},
55    diagnostic::FrameCountPlugin,
56    input::InputPlugin,
57    prelude::*,
58    time::TimePlugin
59};
60
61#[cfg(all(feature = "gizmos", feature = "editor"))]
62mod debug;
63
64/// Custom Bevy plugins for WaterDropEngine default plugins.
65#[derive(Default)]
66struct CustomBevyPlugins;
67impl Plugin for CustomBevyPlugins {
68    fn build(&self, app: &mut App) {
69        app.add_plugins((
70            TaskPoolPlugin {
71                task_pool_options: TaskPoolOptions {
72                    min_total_threads: 1,
73                    max_total_threads: usize::MAX,
74
75                    // Use 1 core for IO
76                    io: TaskPoolThreadAssignmentPolicy {
77                        min_threads: 1,
78                        max_threads: 2,
79                        percent: 0.25,
80                        on_thread_spawn: None,
81                        on_thread_destroy: None
82                    },
83
84                    // Use 1 core for async compute
85                    async_compute: TaskPoolThreadAssignmentPolicy {
86                        min_threads: 1,
87                        max_threads: 2,
88                        percent: 0.25,
89                        on_thread_spawn: None,
90                        on_thread_destroy: None
91                    },
92
93                    // Use all remaining cores for compute (at least 1)
94                    compute: TaskPoolThreadAssignmentPolicy {
95                        min_threads: 1,
96                        max_threads: usize::MAX,
97                        percent: 1.0, // This 1.0 here means "whatever is left over"
98                        on_thread_spawn: None,
99                        on_thread_destroy: None
100                    }
101                }
102            },
103            FrameCountPlugin,
104            TimePlugin,
105            ScheduleRunnerPlugin::default(),
106            bevy::prelude::AssetPlugin {
107                mode: AssetMode::Unprocessed,
108                file_path: "res".to_string(),
109                ..Default::default()
110            },
111            InputPlugin,
112            TransformPlugin
113        ));
114    }
115}
116
117/// Custom WaterDropEngine plugins.
118#[derive(Default)]
119struct CustomWdePlugins;
120impl Plugin for CustomWdePlugins {
121    fn build(&self, app: &mut App) {
122        app.add_plugins((
123            wde_logger::LogPlugin::default().auto_level(),
124            wde_renderer::RenderPlugin,
125            wde_gltf::GltfPlugin,
126            wde_camera::CameraPlugin,
127            wde_camera_controller::CameraControllerPlugin,
128            wde_physics::PhysicsPlugin,
129            wde_terrain::TerrainPlugin,
130            wde_terrain_grid::TerrainGridPlugin,
131            wde_terrain_editor::TerrainEditorPlugin,
132            wde_terrain_navigation::TerrainNavigationPlugin,
133            wde_pbr_outline::PbrOutlinePlugin
134        ));
135
136        #[cfg(feature = "gizmos")]
137        app.add_plugins(wde_gizmos::GizmosPlugin);
138        #[cfg(feature = "pbr")]
139        app.add_plugins(wde_pbr::PbrPlugin);
140        #[cfg(feature = "editor")]
141        app.add_plugins(wde_editor::EditorPlugin);
142        #[cfg(all(feature = "gizmos", feature = "editor"))]
143        app.add_plugins(debug::PhysicsDebugPlugin);
144
145        // Always add the scene plugin last
146        app.add_plugins(wde_scene::ScenePlugin);
147    }
148}
149
150plugin_group! {
151    /// Default plugins for WaterDropEngine.
152    pub struct WdeDefaultPlugins {
153        :CustomBevyPlugins,
154        :CustomWdePlugins,
155    }
156}
157
158/// The prelude module of WaterDropEngine.
159/// Import this to get access to the most commonly used types and traits.
160#[doc(hidden)]
161pub mod prelude {
162    // Default plugins
163    pub use crate::WdeDefaultPlugins;
164
165    // Core modules
166    pub use wde_camera::prelude::*;
167    pub use wde_camera_controller::prelude::*;
168    pub use wde_gltf::prelude::*;
169    pub use wde_logger::prelude::*;
170    pub use wde_pbr_outline::prelude::*;
171    pub use wde_physics::prelude::*;
172    pub use wde_renderer::prelude::*;
173    pub use wde_scene::prelude::*;
174    pub use wde_terrain::prelude::*;
175    pub use wde_terrain_grid::prelude::*;
176    pub use wde_terrain_navigation::prelude::*;
177
178    // Optional feature modules
179    #[cfg(all(feature = "gizmos", feature = "editor"))]
180    pub use crate::debug::PhysicsDebugSettings;
181    #[cfg(feature = "editor")]
182    pub use wde_editor::prelude::*;
183    #[cfg(feature = "gizmos")]
184    pub use wde_gizmos::prelude::*;
185    #[cfg(feature = "pbr")]
186    pub use wde_pbr::prelude::*;
187}