Skip to content

Commit 435d9bc

Browse files
authored
Highlight dependency on shader files in examples (#13824)
The examples won't work when copy-pasted to another project, without also copying their shader files. This change adds constants at the top of the files to bring attention to the dependencies. Follow up to [#13624](#13624 (comment))
1 parent f23c686 commit 435d9bc

21 files changed

+91
-26
lines changed

examples/2d/custom_gltf_vertex_attribute.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ use bevy::{
1111
sprite::{Material2d, Material2dKey, Material2dPlugin, MaterialMesh2dBundle, Mesh2dHandle},
1212
};
1313

14+
/// This example uses a shader source file from the assets subdirectory
15+
const SHADER_ASSET_PATH: &str = "shaders/custom_gltf_2d.wgsl";
16+
1417
/// This vertex attribute supplies barycentric coordinates for each triangle.
1518
/// Each component of the vector corresponds to one corner of a triangle. It's
1619
/// equal to 1.0 in that corner and 0.0 in the other two. Hence, its value in
@@ -68,10 +71,10 @@ struct CustomMaterial {}
6871

6972
impl Material2d for CustomMaterial {
7073
fn vertex_shader() -> ShaderRef {
71-
"shaders/custom_gltf_2d.wgsl".into()
74+
SHADER_ASSET_PATH.into()
7275
}
7376
fn fragment_shader() -> ShaderRef {
74-
"shaders/custom_gltf_2d.wgsl".into()
77+
SHADER_ASSET_PATH.into()
7578
}
7679

7780
fn specialize(

examples/3d/irradiance_volumes.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ use bevy::prelude::*;
2222
use bevy::render::render_resource::{AsBindGroup, ShaderRef, ShaderType};
2323
use bevy::window::PrimaryWindow;
2424

25+
/// This example uses a shader source file from the assets subdirectory
26+
const SHADER_ASSET_PATH: &str = "shaders/irradiance_volume_voxel_visualization.wgsl";
27+
2528
// Rotation speed in radians per frame.
2629
const ROTATION_SPEED: f32 = 0.2;
2730

@@ -650,6 +653,6 @@ fn toggle_voxel_visibility(
650653

651654
impl MaterialExtension for VoxelVisualizationExtension {
652655
fn fragment_shader() -> ShaderRef {
653-
"shaders/irradiance_volume_voxel_visualization.wgsl".into()
656+
SHADER_ASSET_PATH.into()
654657
}
655658
}

examples/3d/lines.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ use bevy::{
1414
},
1515
};
1616

17+
/// This example uses a shader source file from the assets subdirectory
18+
const SHADER_ASSET_PATH: &str = "shaders/line_material.wgsl";
19+
1720
fn main() {
1821
App::new()
1922
.add_plugins((DefaultPlugins, MaterialPlugin::<LineMaterial>::default()))
@@ -72,7 +75,7 @@ struct LineMaterial {
7275

7376
impl Material for LineMaterial {
7477
fn fragment_shader() -> ShaderRef {
75-
"shaders/line_material.wgsl".into()
78+
SHADER_ASSET_PATH.into()
7679
}
7780

7881
fn specialize(

examples/3d/ssr.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ use bevy::{
2121
},
2222
};
2323

24+
/// This example uses a shader source file from the assets subdirectory
25+
const SHADER_ASSET_PATH: &str = "shaders/water_material.wgsl";
26+
2427
// The speed of camera movement.
2528
const CAMERA_KEYBOARD_ZOOM_SPEED: f32 = 0.1;
2629
const CAMERA_KEYBOARD_ORBIT_SPEED: f32 = 0.02;
@@ -286,7 +289,7 @@ fn create_text(app_settings: &AppSettings) -> Text {
286289

287290
impl MaterialExtension for Water {
288291
fn deferred_fragment_shader() -> ShaderRef {
289-
"shaders/water_material.wgsl".into()
292+
SHADER_ASSET_PATH.into()
290293
}
291294
}
292295

examples/3d/tonemapping.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ use bevy::{
1313
};
1414
use std::f32::consts::PI;
1515

16+
/// This example uses a shader source file from the assets subdirectory
17+
const SHADER_ASSET_PATH: &str = "shaders/tonemapping_test_patterns.wgsl";
18+
1619
fn main() {
1720
App::new()
1821
.add_plugins((
@@ -600,7 +603,7 @@ impl Default for PerMethodSettings {
600603

601604
impl Material for ColorGradientMaterial {
602605
fn fragment_shader() -> ShaderRef {
603-
"shaders/tonemapping_test_patterns.wgsl".into()
606+
SHADER_ASSET_PATH.into()
604607
}
605608
}
606609

examples/shader/animate_shader.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ use bevy::{
77
render::render_resource::{AsBindGroup, ShaderRef},
88
};
99

10+
/// This example uses a shader source file from the assets subdirectory
11+
const SHADER_ASSET_PATH: &str = "shaders/animate_shader.wgsl";
12+
1013
fn main() {
1114
App::new()
1215
.add_plugins((DefaultPlugins, MaterialPlugin::<CustomMaterial>::default()))
@@ -39,6 +42,6 @@ struct CustomMaterial {}
3942

4043
impl Material for CustomMaterial {
4144
fn fragment_shader() -> ShaderRef {
42-
"shaders/animate_shader.wgsl".into()
45+
SHADER_ASSET_PATH.into()
4346
}
4447
}

examples/shader/array_texture.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use bevy::{
88
render::render_resource::{AsBindGroup, ShaderRef},
99
};
1010

11+
/// This example uses a shader source file from the assets subdirectory
12+
const SHADER_ASSET_PATH: &str = "shaders/array_texture.wgsl";
13+
1114
fn main() {
1215
App::new()
1316
.add_plugins((
@@ -89,6 +92,6 @@ struct ArrayTextureMaterial {
8992

9093
impl Material for ArrayTextureMaterial {
9194
fn fragment_shader() -> ShaderRef {
92-
"shaders/array_texture.wgsl".into()
95+
SHADER_ASSET_PATH.into()
9396
}
9497
}

examples/shader/custom_vertex_attribute.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ use bevy::{
1313
},
1414
};
1515

16+
/// This example uses a shader source file from the assets subdirectory
17+
const SHADER_ASSET_PATH: &str = "shaders/custom_vertex_attribute.wgsl";
18+
1619
fn main() {
1720
App::new()
1821
.add_plugins((DefaultPlugins, MaterialPlugin::<CustomMaterial>::default()))
@@ -65,10 +68,10 @@ struct CustomMaterial {
6568

6669
impl Material for CustomMaterial {
6770
fn vertex_shader() -> ShaderRef {
68-
"shaders/custom_vertex_attribute.wgsl".into()
71+
SHADER_ASSET_PATH.into()
6972
}
7073
fn fragment_shader() -> ShaderRef {
71-
"shaders/custom_vertex_attribute.wgsl".into()
74+
SHADER_ASSET_PATH.into()
7275
}
7376

7477
fn specialize(

examples/shader/extended_material.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ use bevy::{
77
render::render_resource::*,
88
};
99

10+
/// This example uses a shader source file from the assets subdirectory
11+
const SHADER_ASSET_PATH: &str = "shaders/extended_material.wgsl";
12+
1013
fn main() {
1114
App::new()
1215
.add_plugins(DefaultPlugins)
@@ -79,10 +82,10 @@ struct MyExtension {
7982

8083
impl MaterialExtension for MyExtension {
8184
fn fragment_shader() -> ShaderRef {
82-
"shaders/extended_material.wgsl".into()
85+
SHADER_ASSET_PATH.into()
8386
}
8487

8588
fn deferred_fragment_shader() -> ShaderRef {
86-
"shaders/extended_material.wgsl".into()
89+
SHADER_ASSET_PATH.into()
8790
}
8891
}

examples/shader/fallback_image.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ use bevy::{
1111
render::render_resource::{AsBindGroup, ShaderRef},
1212
};
1313

14+
/// This example uses a shader source file from the assets subdirectory
15+
const SHADER_ASSET_PATH: &str = "shaders/fallback_image_test.wgsl";
16+
1417
fn main() {
1518
App::new()
1619
.add_plugins((
@@ -73,6 +76,6 @@ struct FallbackTestMaterial {
7376

7477
impl Material for FallbackTestMaterial {
7578
fn fragment_shader() -> ShaderRef {
76-
"shaders/fallback_image_test.wgsl".into()
79+
SHADER_ASSET_PATH.into()
7780
}
7881
}

examples/shader/gpu_readback.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ use bevy::{
1818
};
1919
use crossbeam_channel::{Receiver, Sender};
2020

21+
/// This example uses a shader source file from the assets subdirectory
22+
const SHADER_ASSET_PATH: &str = "shaders/gpu_readback.wgsl";
23+
2124
// The length of the buffer sent to the gpu
2225
const BUFFER_LEN: usize = 16;
2326

@@ -177,7 +180,7 @@ impl FromWorld for ComputePipeline {
177180
storage_buffer::<Vec<u32>>(false),
178181
),
179182
);
180-
let shader = world.load_asset("shaders/gpu_readback.wgsl");
183+
let shader = world.load_asset(SHADER_ASSET_PATH);
181184
let pipeline_cache = world.resource::<PipelineCache>();
182185
let pipeline = pipeline_cache.queue_compute_pipeline(ComputePipelineDescriptor {
183186
label: Some("GPU readback compute shader".into()),

examples/shader/post_processing.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ use bevy::{
3131
},
3232
};
3333

34+
/// This example uses a shader source file from the assets subdirectory
35+
const SHADER_ASSET_PATH: &str = "shaders/post_processing.wgsl";
36+
3437
fn main() {
3538
App::new()
3639
.add_plugins((DefaultPlugins, PostProcessPlugin))
@@ -249,7 +252,7 @@ impl FromWorld for PostProcessPipeline {
249252
let sampler = render_device.create_sampler(&SamplerDescriptor::default());
250253

251254
// Get the shader handle
252-
let shader = world.load_asset("shaders/post_processing.wgsl");
255+
let shader = world.load_asset(SHADER_ASSET_PATH);
253256

254257
let pipeline_id = world
255258
.resource_mut::<PipelineCache>()

examples/shader/shader_defs.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ use bevy::{
1212
},
1313
};
1414

15+
/// This example uses a shader source file from the assets subdirectory
16+
const SHADER_ASSET_PATH: &str = "shaders/shader_defs.wgsl";
17+
1518
fn main() {
1619
App::new()
1720
.add_plugins((DefaultPlugins, MaterialPlugin::<CustomMaterial>::default()))
@@ -56,7 +59,7 @@ fn setup(
5659

5760
impl Material for CustomMaterial {
5861
fn fragment_shader() -> ShaderRef {
59-
"shaders/shader_defs.wgsl".into()
62+
SHADER_ASSET_PATH.into()
6063
}
6164

6265
fn specialize(

examples/shader/shader_instancing.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ use bevy::{
2626
};
2727
use bytemuck::{Pod, Zeroable};
2828

29+
/// This example uses a shader source file from the assets subdirectory
30+
const SHADER_ASSET_PATH: &str = "shaders/instancing.wgsl";
31+
2932
fn main() {
3033
App::new()
3134
.add_plugins((DefaultPlugins, CustomMaterialPlugin))
@@ -190,7 +193,7 @@ impl FromWorld for CustomPipeline {
190193
let mesh_pipeline = world.resource::<MeshPipeline>();
191194

192195
CustomPipeline {
193-
shader: world.load_asset("shaders/instancing.wgsl"),
196+
shader: world.load_asset(SHADER_ASSET_PATH),
194197
mesh_pipeline: mesh_pipeline.clone(),
195198
}
196199
}

examples/shader/shader_material.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ use bevy::{
66
render::render_resource::{AsBindGroup, ShaderRef},
77
};
88

9+
/// This example uses a shader source file from the assets subdirectory
10+
const SHADER_ASSET_PATH: &str = "shaders/custom_material.wgsl";
11+
912
fn main() {
1013
App::new()
1114
.add_plugins((DefaultPlugins, MaterialPlugin::<CustomMaterial>::default()))
@@ -54,7 +57,7 @@ struct CustomMaterial {
5457
/// You only need to implement functions for features that need non-default behavior. See the Material api docs for details!
5558
impl Material for CustomMaterial {
5659
fn fragment_shader() -> ShaderRef {
57-
"shaders/custom_material.wgsl".into()
60+
SHADER_ASSET_PATH.into()
5861
}
5962

6063
fn alpha_mode(&self) -> AlphaMode {

examples/shader/shader_material_2d.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ use bevy::{
77
sprite::{Material2d, Material2dPlugin, MaterialMesh2dBundle},
88
};
99

10+
/// This example uses a shader source file from the assets subdirectory
11+
const SHADER_ASSET_PATH: &str = "shaders/custom_material_2d.wgsl";
12+
1013
fn main() {
1114
App::new()
1215
.add_plugins((
@@ -53,6 +56,6 @@ struct CustomMaterial {
5356
/// You only need to implement functions for features that need non-default behavior. See the Material2d api docs for details!
5457
impl Material2d for CustomMaterial {
5558
fn fragment_shader() -> ShaderRef {
56-
"shaders/custom_material_2d.wgsl".into()
59+
SHADER_ASSET_PATH.into()
5760
}
5861
}

examples/shader/shader_material_glsl.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ use bevy::{
1212
},
1313
};
1414

15+
/// This example uses shader source files from the assets subdirectory
16+
const VERTEX_SHADER_ASSET_PATH: &str = "shaders/custom_material.vert";
17+
const FRAGMENT_SHADER_ASSET_PATH: &str = "shaders/custom_material.frag";
18+
1519
fn main() {
1620
App::new()
1721
.add_plugins((DefaultPlugins, MaterialPlugin::<CustomMaterial>::default()))
@@ -61,11 +65,11 @@ struct CustomMaterial {
6165
/// When using the GLSL shading language for your shader, the specialize method must be overridden.
6266
impl Material for CustomMaterial {
6367
fn vertex_shader() -> ShaderRef {
64-
"shaders/custom_material.vert".into()
68+
VERTEX_SHADER_ASSET_PATH.into()
6569
}
6670

6771
fn fragment_shader() -> ShaderRef {
68-
"shaders/custom_material.frag".into()
72+
FRAGMENT_SHADER_ASSET_PATH.into()
6973
}
7074

7175
fn alpha_mode(&self) -> AlphaMode {

examples/shader/shader_material_screenspace_texture.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ use bevy::{
66
render::render_resource::{AsBindGroup, ShaderRef},
77
};
88

9+
/// This example uses a shader source file from the assets subdirectory
10+
const SHADER_ASSET_PATH: &str = "shaders/custom_material_screenspace_texture.wgsl";
11+
912
fn main() {
1013
App::new()
1114
.add_plugins((DefaultPlugins, MaterialPlugin::<CustomMaterial>::default()))
@@ -74,6 +77,6 @@ struct CustomMaterial {
7477

7578
impl Material for CustomMaterial {
7679
fn fragment_shader() -> ShaderRef {
77-
"shaders/custom_material_screenspace_texture.wgsl".into()
80+
SHADER_ASSET_PATH.into()
7881
}
7982
}

examples/shader/shader_prepass.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ use bevy::{
1010
render::render_resource::{AsBindGroup, ShaderRef, ShaderType},
1111
};
1212

13+
/// This example uses a shader source file from the assets subdirectory
14+
const PREPASS_SHADER_ASSET_PATH: &str = "shaders/show_prepass.wgsl";
15+
const MATERIAL_SHADER_ASSET_PATH: &str = "shaders/custom_material.wgsl";
16+
1317
fn main() {
1418
App::new()
1519
.add_plugins((
@@ -166,7 +170,7 @@ struct CustomMaterial {
166170
/// function will also be used by the prepass
167171
impl Material for CustomMaterial {
168172
fn fragment_shader() -> ShaderRef {
169-
"shaders/custom_material.wgsl".into()
173+
MATERIAL_SHADER_ASSET_PATH.into()
170174
}
171175

172176
fn alpha_mode(&self) -> AlphaMode {
@@ -208,7 +212,7 @@ struct PrepassOutputMaterial {
208212

209213
impl Material for PrepassOutputMaterial {
210214
fn fragment_shader() -> ShaderRef {
211-
"shaders/show_prepass.wgsl".into()
215+
PREPASS_SHADER_ASSET_PATH.into()
212216
}
213217

214218
// This needs to be transparent in order to show the scene behind the mesh

0 commit comments

Comments
 (0)