voxel_framework/lib.rs
1//! # voxel-framework
2//!
3//! A reusable voxel engine library built on [Bevy](https://bevyengine.org/),
4//! providing all the infrastructure needed to build voxel-based games and
5//! simulations: chunk storage, procedural generation, greedy meshing, world
6//! streaming, PBR rendering, and player interaction.
7//!
8//! ## Overview
9//!
10//! The framework is built around three core abstractions:
11//!
12//! - **[`voxel::VoxelType`]** — a trait that every voxel palette must implement.
13//! It describes whether a voxel is solid, what color it renders as, and how
14//! it serializes to and from a single byte for persistence. The built-in
15//! [`voxel::DefaultVoxel`] palette covers five material types (Air, Rock,
16//! IronOre, SiliconCrystal, RareMineral) and is ready to use out of the box.
17//!
18//! - **[`chunk::Chunk<V>`]** — the fundamental storage unit. A 16×16×16 flat
19//! array of voxels stored on the heap. All generation, meshing, and I/O
20//! operate on chunks identified by a [`chunk::ChunkCoord`] (an `IVec3`
21//! where one unit equals `CHUNK_SIZE` world-space units).
22//!
23//! - **[`generator::ChunkGenerator`]** — an async-safe trait implemented by
24//! procedural generators. Generators run on a Bevy `AsyncComputeTaskPool`
25//! thread and produce fully populated [`chunk::Chunk<V>`] values, which are
26//! then handed back to the main world for meshing and insertion.
27//!
28//! ## Plugin Catalog Architecture
29//!
30//! `voxel-framework` exposes a flat catalog of Bevy plugins grouped by
31//! responsibility. Game crates compose the plugins they need instead of
32//! depending on a hidden monolithic game plugin:
33//!
34//! | Category | Purpose | Examples |
35//! |----------|---------|----------|
36//! | Engine | Foundation for streamable/renderable voxel worlds | [`world::WorldPlugin`], [`mesh::MeshPlugin`], [`render::RenderPlugin`] |
37//! | World Data | Chunk sources, generation, persistence, lifecycle data | [`generator::GeneratorPlugin`], [`world::PersistencePlugin`] |
38//! | Gameplay | Reusable rules and interactions | [`world::mining::MiningPlugin`], [`world::placement::PlacementPlugin`], [`player::PlayerPlugin`] |
39//! | Default Implementation | Replaceable UI/audio/visual defaults | [`ui::UiPlugin`], [`skybox::SkyboxPlugin`], [`audio::DefaultAudioPlugin`] |
40//! | Convenience | Compatibility bundles, input adapters, recipes, prototyping helpers | [`VoxelCorePlugin`], [`VoxelPlugin`] |
41//!
42//! These categories are also represented by marker traits in
43//! [`plugin_category`]. They are documentation and API classification tools;
44//! Bevy composition still uses the normal [`Plugin`] trait.
45//!
46//! [`VoxelCorePlugin`] and [`VoxelPlugin`] remain available as convenience
47//! bundles for compatibility and quick starts, but they are not the primary
48//! architecture. New domain behavior should normally be modeled as an explicit
49//! Engine, World Data, Gameplay, or Default Implementation plugin.
50//!
51//! [`generator::GeneratorPlugin`] is always **opt-in** because it is generic
52//! over `V: VoxelType` and requires the caller to supply a
53//! [`generator::GeneratorResource`] holding a concrete generator. Add it
54//! separately after either a bundle or explicit engine plugins.
55//!
56//! ## The Chunk Pipeline
57//!
58//! The full data flow from viewer movement to rendered geometry:
59//!
60//! ```text
61//! ViewerPosition update
62//! │
63//! ▼
64//! WorldPlugin — streaming system
65//! Computes which chunks are within the configured render distance.
66//! Emits ChunkRequested events for chunks not yet loaded.
67//! │
68//! ▼
69//! GeneratorPlugin — async generation (AsyncComputeTaskPool)
70//! Spawns a task per requested chunk. Each task calls
71//! ChunkGenerator::generate(coord) → Chunk<V>.
72//! When the task completes, inserts the Chunk<V> component and emits
73//! ChunkModified so downstream systems are notified.
74//! │
75//! ▼
76//! MeshPlugin — greedy mesh generation
77//! Reacts to ChunkModified. Iterates the chunk with a configurable LOD
78//! step, runs the greedy meshing algorithm, and uploads a Bevy Mesh asset.
79//! │
80//! ▼
81//! RenderPlugin — PBR material + entity spawn
82//! Attaches a shared chunk PBR material and a MeshBundle to the chunk
83//! entity so Bevy's renderer picks it up on the next frame.
84//! │
85//! ▼
86//! OcclusionPlugin — neighbor-solid occlusion
87//! Reacts to ChunkModified and hides chunks whose six face-adjacent
88//! neighbours are all loaded and fully solid.
89//! ```
90//!
91//! ## Quick Start
92//!
93//! A minimal working world using the built-in asteroid generator:
94//!
95//! ```rust,no_run
96//! use bevy::prelude::*;
97//! use voxel_framework::{
98//! VoxelPlugin,
99//! generator::{GeneratorPlugin, GeneratorResource},
100//! generator::asteroid::{AsteroidConfig, AsteroidGenerator},
101//! voxel::DefaultVoxel,
102//! };
103//! use std::sync::Arc;
104//!
105//! App::new()
106//! .add_plugins(DefaultPlugins)
107//! .add_plugins(VoxelPlugin::<DefaultVoxel>::new())
108//! .add_plugins(GeneratorPlugin::<DefaultVoxel>::new())
109//! .insert_resource(GeneratorResource::<DefaultVoxel> {
110//! generator: Arc::new(AsteroidGenerator::new(AsteroidConfig::default())),
111//! })
112//! .run();
113//! ```
114//!
115//! Use explicit catalog plugins instead of [`VoxelPlugin`] when you want full
116//! control over engine, world data, gameplay, and presentation defaults.
117//!
118//! ## Extending the Framework
119//!
120//! To use a custom voxel palette, implement [`voxel::VoxelType`] on your own type and
121//! substitute it wherever the framework is generic over `V: VoxelType`:
122//!
123//! - [`generator::GeneratorPlugin::<YourVoxel>::new()`][`generator::GeneratorPlugin`]
124//! - [`mesh::MeshPlugin::<YourVoxel>::new()`][`mesh::MeshPlugin`]
125//! - [`render::RenderPlugin::<YourVoxel>::new()`][`render::RenderPlugin`]
126//!
127//! To drive generation, implement [`generator::ChunkGenerator`] and wrap it in
128//! a [`generator::GeneratorResource`].
129//!
130//! See [`prelude`] for the most commonly used re-exports.
131
132#![deny(rustdoc::broken_intra_doc_links)]
133
134pub mod audio;
135pub mod chunk;
136pub mod content;
137#[cfg(feature = "crafting")]
138pub mod crafting;
139pub mod fragments;
140pub mod generator;
141#[cfg(feature = "items")]
142pub mod items;
143pub mod mesh;
144#[cfg(feature = "mobs")]
145pub mod mobs;
146pub mod noise;
147pub mod player;
148pub mod plugin_category;
149pub mod prelude;
150pub mod profile;
151pub mod render;
152pub mod settings;
153pub mod skybox;
154pub mod ui;
155pub mod voxel;
156pub mod world;
157
158use bevy::prelude::*;
159
160use audio::SfxPlugin;
161use fragments::FragmentPlugin;
162#[cfg(feature = "items")]
163use items::hotbar::HotbarPlugin;
164use mesh::MeshPlugin;
165use render::RenderPlugin;
166use skybox::SkyboxPlugin;
167use ui::UiPlugin;
168use voxel::DefaultVoxel;
169use voxel::VoxelType;
170use world::loading::LoadingPlugin;
171use world::mining::MiningPlugin;
172use world::occlusion::OcclusionPlugin;
173use world::placement::PlacementPlugin;
174use world::{WorldConfigPlugin, WorldPlugin};
175
176/// Convenience bundle for the historical core voxel runtime.
177///
178/// `VoxelCorePlugin<V>` wires up the legacy core bundle — world streaming,
179/// chunk lifecycle, meshing, PBR rendering, neighbor-solid occlusion, mining,
180/// placement, and loading state — without the bundled presentation defaults
181/// from [`VoxelPlugin`].
182///
183/// In the plugin catalog taxonomy this type is a [`plugin_category::ConveniencePlugin`]:
184/// it composes stricter Engine and Gameplay plugins for compatibility and quick
185/// starts. New architecture should describe those individual plugins by their
186/// primary categories rather than treating this bundle as the whole model.
187///
188/// The generic parameter `V` selects the voxel type used by the rendering,
189/// meshing, and occlusion systems. Defaults to [`DefaultVoxel`] for backwards
190/// compatibility: `VoxelCorePlugin::default()` and
191/// `VoxelCorePlugin::<DefaultVoxel>::new()` are equivalent.
192///
193/// # Included systems
194///
195/// | Plugin | Responsibility |
196/// |--------|----------------|
197/// | [`WorldPlugin`] | [`world::ChunkManager`], [`world::systems::ViewerPosition`], chunk lifecycle events, and streaming |
198/// | [`WorldConfigPlugin`] | Inserts the [`world::WorldConfig`] resource |
199/// | `RenderPlugin<V>` | Shared chunk PBR material and entity spawn |
200/// | `MeshPlugin<V>` | Greedy mesh generation and chunk despawn |
201/// | `OcclusionPlugin<V>` | Neighbor-solid chunk occlusion |
202/// | [`world::mining::MiningPlugin`] | Gameplay primitive for voxel removal |
203/// | [`world::placement::PlacementPlugin`] | Gameplay primitive for voxel placement |
204/// | [`world::loading::LoadingPlugin`] | World loading state machine |
205///
206/// # Not included (opt-in)
207///
208/// - [`generator::GeneratorPlugin`] — requires the caller to supply a
209/// [`generator::GeneratorResource`].
210/// - [`FragmentPlugin`], [`SkyboxPlugin`], [`SfxPlugin`], [`HotbarPlugin`],
211/// [`UiPlugin`] — presentation/gameplay defaults included by [`VoxelPlugin`].
212///
213/// # Example
214///
215/// ```rust,no_run
216/// use bevy::prelude::*;
217/// use voxel_framework::{VoxelCorePlugin, generator::{GeneratorPlugin, GeneratorResource}};
218/// use voxel_framework::generator::asteroid::{AsteroidConfig, AsteroidGenerator};
219/// use voxel_framework::voxel::DefaultVoxel;
220/// use std::sync::Arc;
221///
222/// App::new()
223/// .add_plugins(DefaultPlugins)
224/// .add_plugins(VoxelCorePlugin::<DefaultVoxel>::new())
225/// .add_plugins(GeneratorPlugin::<DefaultVoxel>::new())
226/// .insert_resource(GeneratorResource::<DefaultVoxel> {
227/// generator: Arc::new(AsteroidGenerator::new(AsteroidConfig::default())),
228/// })
229/// .run();
230/// ```
231///
232/// # See also
233///
234/// - [`VoxelPlugin`] — broader quick-start convenience bundle.
235pub struct VoxelCorePlugin<V: VoxelType = DefaultVoxel> {
236 _m: std::marker::PhantomData<V>,
237}
238
239impl<V: VoxelType> VoxelCorePlugin<V> {
240 /// Creates a new [`VoxelCorePlugin`] for voxel type `V`.
241 pub fn new() -> Self {
242 Self {
243 _m: std::marker::PhantomData,
244 }
245 }
246}
247
248impl<V: VoxelType> Default for VoxelCorePlugin<V> {
249 fn default() -> Self {
250 Self::new()
251 }
252}
253
254impl<V: VoxelType> Plugin for VoxelCorePlugin<V> {
255 fn build(&self, app: &mut App) {
256 app.add_plugins(WorldPlugin);
257 app.add_plugins(WorldConfigPlugin);
258 app.add_plugins(RenderPlugin::<V>::new());
259 app.add_plugins(MeshPlugin::<V>::new());
260 app.add_plugins(OcclusionPlugin::<V>::new());
261 app.add_plugins(MiningPlugin);
262 app.add_plugins(PlacementPlugin);
263 app.add_plugins(LoadingPlugin);
264 }
265}
266
267/// Quick-start convenience bundle for demos and prototypes.
268///
269/// `VoxelPlugin<V>` composes [`VoxelCorePlugin<V>`] with selected presentation
270/// and gameplay defaults. It is useful for examples, demos, and early
271/// prototyping, but it is not the architectural definition of the full
272/// framework. Production games should treat it as a convenience bundle and
273/// switch to explicit catalog plugins when they need finer control.
274///
275/// The generic parameter `V` selects the voxel type. Defaults to
276/// [`DefaultVoxel`] — `VoxelPlugin::default()` produces an asteroid-palette
277/// world, `VoxelPlugin::<DefaultTerrainVoxel>::new()` produces a terrain world.
278///
279/// # Included beyond [`VoxelCorePlugin`]
280///
281/// | Plugin | Responsibility |
282/// |--------|----------------|
283/// | [`FragmentPlugin`] | Floating voxel debris entities ejected on mining |
284/// | [`SkyboxPlugin`] | Procedural starfield and skybox rendering |
285/// | [`SfxPlugin`] | Positional audio effects for mining and placement |
286/// | [`HotbarPlugin`] | Player hotbar, item switching, and held-item state |
287/// | [`UiPlugin`] | Crosshair, loading screen, and HUD overlays |
288///
289/// # Generator not included
290///
291/// [`generator::GeneratorPlugin`] is intentionally excluded because it requires
292/// the caller to supply a concrete [`generator::GeneratorResource`]. Always add
293/// it separately:
294///
295/// ```rust,no_run
296/// use bevy::prelude::*;
297/// use voxel_framework::{VoxelPlugin, generator::{GeneratorPlugin, GeneratorResource}};
298/// use voxel_framework::generator::terrain::{TerrainConfig, TerrainGenerator};
299/// use voxel_framework::voxel::DefaultTerrainVoxel;
300/// use std::sync::Arc;
301///
302/// App::new()
303/// .add_plugins(DefaultPlugins)
304/// .add_plugins(VoxelPlugin::<DefaultTerrainVoxel>::new())
305/// .add_plugins(GeneratorPlugin::<DefaultTerrainVoxel>::new())
306/// .insert_resource(GeneratorResource::<DefaultTerrainVoxel> {
307/// generator: Arc::new(TerrainGenerator::new(TerrainConfig::default())),
308/// })
309/// .run();
310/// ```
311///
312/// # See also
313///
314/// - [`VoxelCorePlugin`] — narrower compatibility/convenience bundle.
315/// - [`prelude`] — commonly used re-exports for convenience imports.
316pub struct VoxelPlugin<V: VoxelType = DefaultVoxel> {
317 _m: std::marker::PhantomData<V>,
318}
319
320impl<V: VoxelType> VoxelPlugin<V> {
321 /// Creates a new [`VoxelPlugin`] for voxel type `V`.
322 pub fn new() -> Self {
323 Self {
324 _m: std::marker::PhantomData,
325 }
326 }
327}
328
329impl<V: VoxelType> Default for VoxelPlugin<V> {
330 fn default() -> Self {
331 Self::new()
332 }
333}
334
335impl<V: VoxelType> Plugin for VoxelPlugin<V> {
336 fn build(&self, app: &mut App) {
337 app.add_plugins(VoxelCorePlugin::<V>::new());
338 app.add_plugins(FragmentPlugin);
339 app.add_plugins(SkyboxPlugin);
340 app.add_plugins(SfxPlugin);
341 #[cfg(feature = "items")]
342 app.add_plugins(HotbarPlugin);
343 app.add_plugins(UiPlugin);
344 }
345}