Skip to main content

voxel_framework/render/
mod.rs

1//! Rendering support for voxel chunks.
2//!
3//! Provides either a custom texture-array material ([`ChunkMaterial`]) or a
4//! shared `StandardMaterial` for vertex-colored voxels, depending on whether
5//! [`ChunkTextureArray`] is inserted before the plugin initialises.
6
7use bevy::asset::embedded_asset;
8use bevy::pbr::MaterialPlugin;
9use bevy::prelude::*;
10
11use crate::voxel::VoxelType;
12
13pub mod material;
14
15pub use material::ChunkMaterial;
16pub use material::ChunkMaterialConfig;
17pub use material::ChunkMaterialHandle;
18pub use material::ChunkRendered;
19pub use material::ChunkTextureArray;
20pub use material::build_texture_array;
21pub use material::chunk_aabb;
22pub use material::upgrade_to_texture_material;
23
24/// Plugin that sets up shared rendering resources for voxel chunks.
25///
26/// If [`ChunkTextureArray`] is present as a resource when the plugin builds,
27/// chunks will use the embedded WGSL shader with texture array support.
28/// Otherwise a shared `StandardMaterial` with vertex colors is used.
29pub struct RenderPlugin<V: VoxelType> {
30    _marker: std::marker::PhantomData<V>,
31}
32
33impl<V: VoxelType> RenderPlugin<V> {
34    /// Creates a new `RenderPlugin`.
35    pub fn new() -> Self {
36        Self {
37            _marker: std::marker::PhantomData,
38        }
39    }
40}
41
42impl<V: VoxelType> Default for RenderPlugin<V> {
43    fn default() -> Self {
44        Self::new()
45    }
46}
47
48impl<V: VoxelType> Plugin for RenderPlugin<V> {
49    fn build(&self, app: &mut App) {
50        // Embed the chunk WGSL shader into the binary.
51        // The resulting asset path is: embedded://voxel_framework/render/chunk.wgsl
52        embedded_asset!(app, "chunk.wgsl");
53
54        app.add_plugins(MaterialPlugin::<ChunkMaterial>::default());
55        app.init_resource::<ChunkMaterialHandle>();
56        app.init_resource::<ChunkMaterialConfig>();
57        app.add_systems(Startup, material::setup_chunk_material);
58        app.add_systems(Update, material::upgrade_to_texture_material);
59    }
60}