mirror of
https://github.com/bevyengine/bevy
synced 2024-11-28 15:40:21 +00:00
Improve shader_material example (#10547)
# Objective - The current shader code is misleading since it makes it look like a struct is passed to the bind group 0 but in reality only the color is passed. They just happen to have the exact same memory layout so wgsl doesn't complain and it works. - The struct is defined after the `impl Material` block which is backwards from pretty much every other usage of the `impl` block in bevy. ## Solution - Remove the unnecessary struct in the shader - move the impl block
This commit is contained in:
parent
04ab9a0531
commit
83a358bf33
3 changed files with 17 additions and 23 deletions
|
@ -3,9 +3,7 @@ layout(location = 0) in vec2 v_Uv;
|
||||||
|
|
||||||
layout(location = 0) out vec4 o_Target;
|
layout(location = 0) out vec4 o_Target;
|
||||||
|
|
||||||
layout(set = 1, binding = 0) uniform CustomMaterial {
|
layout(set = 1, binding = 0) uniform vec4 CustomMaterial_color;
|
||||||
vec4 Color;
|
|
||||||
};
|
|
||||||
|
|
||||||
layout(set = 1, binding = 1) uniform texture2D CustomMaterial_texture;
|
layout(set = 1, binding = 1) uniform texture2D CustomMaterial_texture;
|
||||||
layout(set = 1, binding = 2) uniform sampler CustomMaterial_sampler;
|
layout(set = 1, binding = 2) uniform sampler CustomMaterial_sampler;
|
||||||
|
@ -16,5 +14,5 @@ layout(set = 1, binding = 2) uniform sampler CustomMaterial_sampler;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
// o_Target = PbrFuncs::tone_mapping(Color * texture(sampler2D(CustomMaterial_texture,CustomMaterial_sampler), v_Uv));
|
// o_Target = PbrFuncs::tone_mapping(Color * texture(sampler2D(CustomMaterial_texture,CustomMaterial_sampler), v_Uv));
|
||||||
o_Target = Color * texture(sampler2D(CustomMaterial_texture,CustomMaterial_sampler), v_Uv);
|
o_Target = CustomMaterial_color * texture(sampler2D(CustomMaterial_texture,CustomMaterial_sampler), v_Uv);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,17 +2,13 @@
|
||||||
// we can import items from shader modules in the assets folder with a quoted path
|
// we can import items from shader modules in the assets folder with a quoted path
|
||||||
#import "shaders/custom_material_import.wgsl"::COLOR_MULTIPLIER
|
#import "shaders/custom_material_import.wgsl"::COLOR_MULTIPLIER
|
||||||
|
|
||||||
struct CustomMaterial {
|
@group(1) @binding(0) var<uniform> material_color: vec4<f32>;
|
||||||
color: vec4<f32>,
|
@group(1) @binding(1) var material_color_texture: texture_2d<f32>;
|
||||||
};
|
@group(1) @binding(2) var material_color_sampler: sampler;
|
||||||
|
|
||||||
@group(1) @binding(0) var<uniform> material: CustomMaterial;
|
|
||||||
@group(1) @binding(1) var base_color_texture: texture_2d<f32>;
|
|
||||||
@group(1) @binding(2) var base_color_sampler: sampler;
|
|
||||||
|
|
||||||
@fragment
|
@fragment
|
||||||
fn fragment(
|
fn fragment(
|
||||||
mesh: VertexOutput,
|
mesh: VertexOutput,
|
||||||
) -> @location(0) vec4<f32> {
|
) -> @location(0) vec4<f32> {
|
||||||
return material.color * textureSample(base_color_texture, base_color_sampler, mesh.uv) * COLOR_MULTIPLIER;
|
return material_color * textureSample(material_color_texture, material_color_sampler, mesh.uv) * COLOR_MULTIPLIER;
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,17 @@ fn setup(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This struct defines the data that will be passed to your shader
|
||||||
|
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
|
||||||
|
pub struct CustomMaterial {
|
||||||
|
#[uniform(0)]
|
||||||
|
color: Color,
|
||||||
|
#[texture(1)]
|
||||||
|
#[sampler(2)]
|
||||||
|
color_texture: Option<Handle<Image>>,
|
||||||
|
alpha_mode: AlphaMode,
|
||||||
|
}
|
||||||
|
|
||||||
/// The Material trait is very configurable, but comes with sensible defaults for all methods.
|
/// The Material trait is very configurable, but comes with sensible defaults for all methods.
|
||||||
/// You only need to implement functions for features that need non-default behavior. See the Material api docs for details!
|
/// You only need to implement functions for features that need non-default behavior. See the Material api docs for details!
|
||||||
impl Material for CustomMaterial {
|
impl Material for CustomMaterial {
|
||||||
|
@ -50,14 +61,3 @@ impl Material for CustomMaterial {
|
||||||
self.alpha_mode
|
self.alpha_mode
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is the struct that will be passed to your shader
|
|
||||||
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
|
|
||||||
pub struct CustomMaterial {
|
|
||||||
#[uniform(0)]
|
|
||||||
color: Color,
|
|
||||||
#[texture(1)]
|
|
||||||
#[sampler(2)]
|
|
||||||
color_texture: Option<Handle<Image>>,
|
|
||||||
alpha_mode: AlphaMode,
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue