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, each behind
6//! a feature of the same name (see [Features](#features)):
7//! - Terrain:
8//!    - [`wde_terrain`](wde_terrain): A plugin for loading, rendering and colliding chunked heightmap terrains.
9//!    - [`wde_terrain_editor`](wde_terrain_editor): A plugin for painting terrains in-game.
10//! - Render:
11//!    - [`wde_pbr`](wde_pbr): A plugin for physically based rendering (PBR) materials and lighting.
12//!    - [`wde_pbr_outline`](wde_pbr_outline): A plugin for drawing outlines around PBR meshes.
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_animation`](wde_animation): A plugin for animating rigid parts of models (e.g. walk cycles).
17//!    - [`wde_gizmos`](wde_gizmos): A plugin for rendering debug gizmos and visualizations.
18//!    - [`wde_ui`](wde_ui): A plugin for laying out and drawing game UI with taffy.
19//! - Core (always included, except the editor):
20//!    - [`wde_renderer`](wde_renderer): A plugin for rendering 3D graphics using the `wgpu` graphics API.
21//!    - [`wde_scene`](wde_scene): A plugin for managing scenes, entities, and components.
22//!    - [`wde_editor`](wde_editor): A plugin for creating in-game editors and tools.
23//! - Wrappers:
24//!    - `wde_wgpu`: A wrapper around the `wgpu` graphics API for rendering. It is superseeded by [`wde_renderer`](wde_renderer).
25//!    - [`wde_physics`](wde_physics): A wrapper around the `rapier` physics engine for 3D physics simulation.
26//!    - [`wde_logger`](wde_logger): A wrapper around the `tracing` library for logging and diagnostics.
27//!    - `wde_egui`: A wrapper around the `egui` library for creating user interfaces. It is superseeded by [`wde_editor`](wde_editor).
28//!
29//! # Getting Started
30//! To get started with WaterDropEngine, add the `wde` crate to your `Cargo.toml`:
31//! ```toml
32//! [dependencies]
33//! wde = "0.1"
34//! ```
35//! Then, in your main Rust file, you can use the default plugins to set up a basic application:
36//! ```rust
37//! app.add_plugins(WdeDefaultPlugins);
38//! ```
39//!
40//! To modify and configure individual plugins, use:
41//! ```rust
42//! app.add_plugins(WdeDefaultPlugins.set(LogPlugin {
43//!     level: LogLevel::DEBUG,
44//!     ..Default::default()
45//! }));
46//! ```
47//!
48//! # Features
49//! Each module is enabled by a Cargo feature, which also enables the modules it depends on. Three
50//! presets are provided:
51//! - `minimal` (default): PBR rendering, cameras with their controllers, and glTF models.
52//! - `full`: every runtime module (`minimal`, `atmosphere`, `pbr-outline`, `gizmos`, `ui`,
53//!   `physics`, `terrain`).
54//! - `everything`: `full`, plus the development tools (`editor`, `terrain-editor`).
55//!
56//! Without default features, only the renderer, the logger and the scenes are included:
57//! ```toml
58//! [dependencies]
59//! wde = { version = "0.1", default-features = false, features = ["pbr", "ui"] }
60//! ```
61
62use bevy::{
63    app::{ScheduleRunnerPlugin, TaskPoolThreadAssignmentPolicy, plugin_group},
64    diagnostic::FrameCountPlugin,
65    input::InputPlugin,
66    prelude::*,
67    time::TimePlugin
68};
69
70#[cfg(all(feature = "gizmos", feature = "editor"))]
71mod debug;
72
73/// Custom Bevy plugins for WaterDropEngine default plugins.
74#[derive(Default)]
75pub struct CustomBevyPlugins;
76impl Plugin for CustomBevyPlugins {
77    fn build(&self, app: &mut App) {
78        app.add_plugins((
79            TaskPoolPlugin {
80                task_pool_options: TaskPoolOptions {
81                    min_total_threads: 1,
82                    max_total_threads: usize::MAX,
83
84                    // Asset loaders (decoding, mesh simplification) run on the IO pool
85                    io: TaskPoolThreadAssignmentPolicy {
86                        min_threads: 1,
87                        max_threads: 4,
88                        percent: 0.25,
89                        on_thread_spawn: None,
90                        on_thread_destroy: None
91                    },
92
93                    async_compute: TaskPoolThreadAssignmentPolicy {
94                        min_threads: 1,
95                        max_threads: 4,
96                        percent: 0.25,
97                        on_thread_spawn: None,
98                        on_thread_destroy: None
99                    },
100
101                    // Use all remaining cores for compute (at least 1)
102                    compute: TaskPoolThreadAssignmentPolicy {
103                        min_threads: 1,
104                        max_threads: usize::MAX,
105                        percent: 1.0, // This 1.0 here means "whatever is left over"
106                        on_thread_spawn: None,
107                        on_thread_destroy: None
108                    }
109                }
110            },
111            FrameCountPlugin,
112            TimePlugin,
113            ScheduleRunnerPlugin::default(),
114            bevy::prelude::AssetPlugin {
115                mode: AssetMode::Unprocessed,
116                file_path: "res".to_string(),
117                ..Default::default()
118            },
119            InputPlugin,
120            TransformPlugin
121        ));
122    }
123}
124
125/// Custom WaterDropEngine plugins. Set it in [`WdeDefaultPlugins`] to configure the window and logs:
126/// ```rust
127/// app.add_plugins(WdeDefaultPlugins.set(CustomWdePlugins {
128///     window_title: "My Game".into(),
129///     ..Default::default()
130/// }));
131/// ```
132pub struct CustomWdePlugins {
133    /// Title shown in the window's title bar.
134    pub window_title: String,
135    /// Initial size (width, height) of the window, in logical pixels.
136    pub window_resolution: (u32, u32),
137    /// Icon shown in the OS taskbar/title bar. See
138    /// [`WindowIcon::from_bytes`](wde_renderer::prelude::WindowIcon::from_bytes).
139    pub window_icon: Option<wde_renderer::prelude::WindowIcon>,
140    /// Application crates logged at the engine's level (see [`wde_logger::LogPlugin::crates`]).
141    pub log_crates: Vec<String>
142}
143impl Default for CustomWdePlugins {
144    fn default() -> Self {
145        let render = wde_renderer::RenderPlugin::default();
146        Self {
147            window_title: render.window_title,
148            window_resolution: render.window_resolution,
149            window_icon: render.window_icon,
150            log_crates: Vec::new()
151        }
152    }
153}
154impl Plugin for CustomWdePlugins {
155    fn build(&self, app: &mut App) {
156        app.add_plugins((
157            wde_logger::LogPlugin {
158                crates: self.log_crates.clone(),
159                ..Default::default()
160            }
161            .auto_level(),
162            wde_renderer::RenderPlugin {
163                window_title: self.window_title.clone(),
164                window_resolution: self.window_resolution,
165                window_icon: self.window_icon.clone()
166            }
167        ));
168
169        #[cfg(feature = "gltf")]
170        app.add_plugins(wde_gltf::GltfPlugin);
171        #[cfg(feature = "animation")]
172        app.add_plugins(wde_animation::AnimationPlugin);
173        #[cfg(feature = "camera")]
174        app.add_plugins(wde_camera::CameraPlugin);
175        #[cfg(feature = "camera-controller")]
176        app.add_plugins(wde_camera_controller::CameraControllerPlugin);
177        #[cfg(feature = "physics")]
178        app.add_plugins(wde_physics::PhysicsPlugin);
179        #[cfg(feature = "terrain")]
180        app.add_plugins(wde_terrain::TerrainPlugin);
181        #[cfg(feature = "terrain-editor")]
182        app.add_plugins(wde_terrain_editor::TerrainEditorPlugin);
183        #[cfg(feature = "pbr-outline")]
184        app.add_plugins(wde_pbr_outline::PbrOutlinePlugin);
185        #[cfg(feature = "gizmos")]
186        app.add_plugins(wde_gizmos::GizmosPlugin);
187        #[cfg(feature = "ui")]
188        app.add_plugins(wde_ui::UiPlugin);
189        #[cfg(feature = "pbr")]
190        app.add_plugins(wde_pbr::PbrPlugin);
191        #[cfg(feature = "editor")]
192        app.add_plugins(wde_editor::EditorPlugin);
193        #[cfg(all(feature = "gizmos", feature = "editor"))]
194        app.add_plugins(debug::CameraDebugPlugin);
195        #[cfg(all(feature = "gizmos", feature = "editor", feature = "physics"))]
196        app.add_plugins(debug::PhysicsDebugPlugin);
197
198        // Always add the scene plugin last
199        app.add_plugins(wde_scene::ScenePlugin);
200    }
201}
202
203plugin_group! {
204    /// Default plugins for WaterDropEngine.
205    pub struct WdeDefaultPlugins {
206        :CustomBevyPlugins,
207        :CustomWdePlugins,
208    }
209}
210
211/// The prelude module of WaterDropEngine.
212/// Import this to get access to the most commonly used types and traits.
213#[doc(hidden)]
214pub mod prelude {
215    // Default plugins
216    pub use crate::WdeDefaultPlugins;
217
218    // Core modules
219    pub use wde_logger::prelude::*;
220    pub use wde_renderer::prelude::*;
221    pub use wde_scene::prelude::*;
222
223    // Optional modules
224    #[cfg(all(feature = "gizmos", feature = "editor"))]
225    pub use crate::debug::CameraDebugSettings;
226    #[cfg(all(feature = "gizmos", feature = "editor", feature = "physics"))]
227    pub use crate::debug::PhysicsDebugSettings;
228    #[cfg(feature = "animation")]
229    pub use wde_animation::prelude::*;
230    #[cfg(feature = "camera")]
231    pub use wde_camera::prelude::*;
232    #[cfg(feature = "camera-controller")]
233    pub use wde_camera_controller::prelude::*;
234    #[cfg(feature = "editor")]
235    pub use wde_editor::prelude::*;
236    #[cfg(feature = "gizmos")]
237    pub use wde_gizmos::prelude::*;
238    #[cfg(feature = "gltf")]
239    pub use wde_gltf::prelude::*;
240    #[cfg(feature = "pbr")]
241    pub use wde_pbr::prelude::*;
242    #[cfg(feature = "pbr-outline")]
243    pub use wde_pbr_outline::prelude::*;
244    #[cfg(feature = "physics")]
245    pub use wde_physics::prelude::*;
246    #[cfg(feature = "terrain")]
247    pub use wde_terrain::prelude::*;
248    #[cfg(feature = "ui")]
249    pub use wde_ui::prelude::*;
250}
251
252// Re-export the modules for external use
253pub use wde_logger;
254pub use wde_renderer;
255pub use wde_scene;
256
257#[cfg(feature = "animation")]
258pub use wde_animation;
259#[cfg(feature = "camera")]
260pub use wde_camera;
261#[cfg(feature = "camera-controller")]
262pub use wde_camera_controller;
263#[cfg(feature = "editor")]
264pub use wde_editor;
265#[cfg(feature = "gizmos")]
266pub use wde_gizmos;
267#[cfg(feature = "gltf")]
268pub use wde_gltf;
269#[cfg(feature = "pbr")]
270pub use wde_pbr;
271#[cfg(feature = "pbr-outline")]
272pub use wde_pbr_outline;
273#[cfg(feature = "physics")]
274pub use wde_physics;
275#[cfg(feature = "terrain")]
276pub use wde_terrain;
277#[cfg(feature = "terrain-editor")]
278pub use wde_terrain_editor;
279#[cfg(feature = "ui")]
280pub use wde_ui;