Skip to content

Commit 435fb7a

Browse files
committed
Improve shader_material example documentation (#3601)
# Objective While trying to learn how to use custom shaders, I had difficulty figuring out how to use a vertex shader. My confusion was mostly because all the other shader examples used a custom pipeline, but I didn't want a custom pipeline. After digging around I realised that I simply needed to add a function to the `impl Material` block. I also searched what was the default shader used, because it wasn't obvious to me where to find it. ## Solution Added a few comments explaining what is going on in the example and a link to the default shader.
1 parent ac63c49 commit 435fb7a

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

examples/shader/shader_material.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use bevy::{
77
render_asset::{PrepareAssetError, RenderAsset},
88
render_resource::{
99
std140::{AsStd140, Std140},
10-
*,
10+
BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout,
11+
BindGroupLayoutDescriptor, BindGroupLayoutEntry, BindingType, Buffer,
12+
BufferBindingType, BufferInitDescriptor, BufferSize, BufferUsages, ShaderStages,
1113
},
1214
renderer::RenderDevice,
1315
},
@@ -44,6 +46,7 @@ fn setup(
4446
});
4547
}
4648

49+
// This is the struct that will be passed to your shader
4750
#[derive(Debug, Clone, TypeUuid)]
4851
#[uuid = "4ee9c363-1124-4113-890e-199d81b00281"]
4952
pub struct CustomMaterial {
@@ -56,6 +59,7 @@ pub struct GpuCustomMaterial {
5659
bind_group: BindGroup,
5760
}
5861

62+
// The implementation of [`Material`] needs this impl to work properly.
5963
impl RenderAsset for CustomMaterial {
6064
type ExtractedAsset = CustomMaterial;
6165
type PreparedAsset = GpuCustomMaterial;
@@ -91,6 +95,16 @@ impl RenderAsset for CustomMaterial {
9195
}
9296

9397
impl Material for CustomMaterial {
98+
// When creating a custom material, you need to define either a vertex shader, a fragment shader or both.
99+
// If you don't define one of them it will use the default mesh shader which can be found at
100+
// <https://github.com/bevyengine/bevy/blob/latest/crates/bevy_pbr/src/render/mesh.wgsl>
101+
102+
// For this example we don't need a vertex shader
103+
// fn vertex_shader(asset_server: &AssetServer) -> Option<Handle<Shader>> {
104+
// // Use the same path as the fragment shader since wgsl let's you define both shader in the same file
105+
// Some(asset_server.load("shaders/custom_material.wgsl"))
106+
// }
107+
94108
fn fragment_shader(asset_server: &AssetServer) -> Option<Handle<Shader>> {
95109
Some(asset_server.load("shaders/custom_material.wgsl"))
96110
}

0 commit comments

Comments
 (0)