Skip to content

Commit 690e2d3

Browse files
authored
Embed shaders into crate (#5)
1 parent a44d1f6 commit 690e2d3

File tree

10 files changed

+50
-42
lines changed

10 files changed

+50
-42
lines changed

Cargo.lock

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "bevy-volumetric-clouds"
33
description = "A plugin for Bevy that renders clouds using raymarching"
44
repository = "https://github.com/evroon/bevy-volumetric-clouds"
55
readme = "README.md"
6-
version = "0.1.1"
6+
version = "0.1.2"
77
publish = true
88
authors = ["evroon"]
99
edition = "2024"
@@ -12,8 +12,7 @@ keywords = ["bevy", "gamedev", "clouds", "volumetric-clouds", "raymarching"]
1212
categories = ["game-development"]
1313

1414
[features]
15-
# dev = ["bevy/dynamic_linking"]
16-
dev = ["bevy/file_watcher"]
15+
dev = ["bevy/dynamic_linking", "bevy/file_watcher", "bevy/embedded_watcher"]
1716
debug = ["bevy/debug", "bevy_egui", "bevy/bevy_text"]
1817
fly_camera = []
1918

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ There are a few features:
6464
- `debug`: enables an `egui` UI that allows you to tweak shader uniforms (parameters) in-game.
6565
- `fly_camera`: adds a `fly_camera` module that controls the camera using keyboard and mouse.
6666

67+
## Bevy version compatibility
68+
69+
| bevy | bevy-volumetric-clouds |
70+
|------|------------------------|
71+
| 0.17 | 0.1.* |
72+
6773
## Credits
6874

6975
1. "The real-time volumetric cloudscapes of Horizon Zero Dawn" by Andrew Schneider and Nathan Vos ([article](https://www.guerrilla-games.com/read/the-real-time-volumetric-cloudscapes-of-horizon-zero-dawn))

src/compute.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
/// Controls the compute shader which renders the volumetric clouds.
2-
use std::borrow::Cow;
3-
41
use bevy::{
2+
asset::load_embedded_asset,
53
ecs::system::ResMut,
64
prelude::*,
75
render::{
@@ -18,6 +16,8 @@ use bevy::{
1816
texture::GpuImage,
1917
},
2018
};
19+
/// Controls the compute shader which renders the volumetric clouds.
20+
use std::borrow::Cow;
2121

2222
use crate::config::CloudsConfig;
2323

@@ -121,6 +121,10 @@ fn prepare_textures_bind_group(
121121
commands.insert_resource(CloudsImageBindGroup(bind_group));
122122
}
123123

124+
/// The compute shading pipeline
125+
///
126+
/// Note that the compute shader is loaded in [`CloudsShaderPlugin`] so this resource depends on
127+
/// that plugin.
124128
#[derive(Resource)]
125129
struct CloudsPipeline {
126130
texture_bind_group_layout: BindGroupLayout,
@@ -133,9 +137,7 @@ impl FromWorld for CloudsPipeline {
133137
fn from_world(world: &mut World) -> Self {
134138
let render_device = world.resource::<RenderDevice>();
135139
let texture_bind_group_layout = CloudsImage::bind_group_layout(render_device);
136-
let shader = world
137-
.resource::<AssetServer>()
138-
.load("shaders/clouds_compute.wgsl");
140+
let shader = load_embedded_asset!(world, "shaders/clouds_compute.wgsl");
139141
let pipeline_cache = world.resource::<PipelineCache>();
140142

141143
let entries = BindGroupLayoutEntries::sequential(

src/lib.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ pub mod config;
99
pub mod fly_camera;
1010
mod images;
1111
mod render;
12-
mod shader_utils;
1312
mod skybox;
1413
#[cfg(feature = "debug")]
1514
mod ui;
@@ -25,8 +24,7 @@ use crate::{
2524
compute::CameraMatrices,
2625
config::CloudsConfig,
2726
images::build_images,
28-
render::CloudsMaterial,
29-
shader_utils::ShaderCommonPlugin,
27+
render::{CloudsMaterial, CloudsShaderPlugin},
3028
skybox::system::{SkyboxMaterials, init_skybox_mesh, setup_daylight, update_skybox_transform},
3129
uniforms::CloudsImage,
3230
};
@@ -41,11 +39,7 @@ pub struct CloudsPlugin;
4139
impl Plugin for CloudsPlugin {
4240
fn build(&self, app: &mut App) {
4341
app.insert_resource(CloudsConfig::default())
44-
.add_plugins((
45-
CloudsComputePlugin,
46-
ShaderCommonPlugin,
47-
MaterialPlugin::<CloudsMaterial>::default(),
48-
))
42+
.add_plugins((CloudsComputePlugin, CloudsShaderPlugin))
4943
.add_systems(Startup, (clouds_setup, setup_daylight))
5044
.add_systems(Update, (update_skybox_transform, update_camera_matrices));
5145
#[cfg(feature = "debug")]

src/render.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use bevy::{
2-
prelude::*, reflect::TypePath, render::render_resource::AsBindGroup, shader::ShaderRef,
2+
asset::{AssetPath, embedded_asset, embedded_path},
3+
prelude::*,
4+
reflect::TypePath,
5+
render::render_resource::AsBindGroup,
6+
shader::{ShaderRef, load_shader_library},
37
};
48

5-
const SHADER_ASSET_PATH: &str = "shaders/clouds.wgsl";
6-
79
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
810
pub(crate) struct CloudsMaterial {
911
#[texture(100, visibility(vertex, fragment))]
@@ -25,6 +27,21 @@ pub(crate) struct CloudsMaterial {
2527

2628
impl Material for CloudsMaterial {
2729
fn fragment_shader() -> ShaderRef {
28-
SHADER_ASSET_PATH.into()
30+
ShaderRef::Path(
31+
AssetPath::from_path_buf(embedded_path!("shaders/clouds.wgsl")).with_source("embedded"),
32+
)
33+
}
34+
}
35+
36+
pub(crate) struct CloudsShaderPlugin;
37+
38+
impl Plugin for CloudsShaderPlugin {
39+
fn build(&self, app: &mut App) {
40+
load_shader_library!(app, "shaders/common.wgsl");
41+
42+
embedded_asset!(app, "shaders/clouds.wgsl");
43+
embedded_asset!(app, "shaders/clouds_compute.wgsl");
44+
45+
app.add_plugins(MaterialPlugin::<CloudsMaterial>::default());
2946
}
3047
}

src/shader_utils/mod.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)