From ac786241b7506c2e50948a798de2968c5b0b44de Mon Sep 17 00:00:00 2001 From: IceSentry Date: Mon, 13 Nov 2023 15:51:57 -0500 Subject: [PATCH 1/3] improve shader_material example --- assets/shaders/custom_material.wgsl | 8 ++------ examples/shader/shader_material.rs | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/assets/shaders/custom_material.wgsl b/assets/shaders/custom_material.wgsl index 90322438e68d2..84431a7c183d6 100644 --- a/assets/shaders/custom_material.wgsl +++ b/assets/shaders/custom_material.wgsl @@ -2,11 +2,7 @@ // we can import items from shader modules in the assets folder with a quoted path #import "shaders/custom_material_import.wgsl"::COLOR_MULTIPLIER -struct CustomMaterial { - color: vec4, -}; - -@group(1) @binding(0) var material: CustomMaterial; +@group(1) @binding(0) var material_color: vec4; @group(1) @binding(1) var base_color_texture: texture_2d; @group(1) @binding(2) var base_color_sampler: sampler; @@ -14,5 +10,5 @@ struct CustomMaterial { fn fragment( mesh: VertexOutput, ) -> @location(0) vec4 { - return material.color * textureSample(base_color_texture, base_color_sampler, mesh.uv) * COLOR_MULTIPLIER; + return material_color * textureSample(base_color_texture, base_color_sampler, mesh.uv) * COLOR_MULTIPLIER; } diff --git a/examples/shader/shader_material.rs b/examples/shader/shader_material.rs index 59467914d8eaa..eb20a77566fb1 100644 --- a/examples/shader/shader_material.rs +++ b/examples/shader/shader_material.rs @@ -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>, + alpha_mode: AlphaMode, +} + /// 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! impl Material for CustomMaterial { @@ -50,14 +61,3 @@ impl Material for CustomMaterial { 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>, - alpha_mode: AlphaMode, -} From 7c1edf4293286ec2f5f6d193b6fa399c994e2d73 Mon Sep 17 00:00:00 2001 From: IceSentry Date: Mon, 13 Nov 2023 15:57:11 -0500 Subject: [PATCH 2/3] rename base_color --- assets/shaders/custom_material.wgsl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/assets/shaders/custom_material.wgsl b/assets/shaders/custom_material.wgsl index 84431a7c183d6..88d904dd1b87c 100644 --- a/assets/shaders/custom_material.wgsl +++ b/assets/shaders/custom_material.wgsl @@ -3,12 +3,12 @@ #import "shaders/custom_material_import.wgsl"::COLOR_MULTIPLIER @group(1) @binding(0) var material_color: vec4; -@group(1) @binding(1) var base_color_texture: texture_2d; -@group(1) @binding(2) var base_color_sampler: sampler; +@group(1) @binding(1) var material_color_texture: texture_2d; +@group(1) @binding(2) var material_color_sampler: sampler; @fragment fn fragment( mesh: VertexOutput, ) -> @location(0) vec4 { - 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; } From 43414549fc65c714efc414d51f50564b5020c818 Mon Sep 17 00:00:00 2001 From: IceSentry Date: Mon, 13 Nov 2023 15:58:35 -0500 Subject: [PATCH 3/3] update glsl too --- assets/shaders/custom_material.frag | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/assets/shaders/custom_material.frag b/assets/shaders/custom_material.frag index 04b644fda876c..004bdf787ae4a 100644 --- a/assets/shaders/custom_material.frag +++ b/assets/shaders/custom_material.frag @@ -3,9 +3,7 @@ layout(location = 0) in vec2 v_Uv; layout(location = 0) out vec4 o_Target; -layout(set = 1, binding = 0) uniform CustomMaterial { - vec4 Color; -}; +layout(set = 1, binding = 0) uniform vec4 CustomMaterial_color; layout(set = 1, binding = 1) uniform texture2D CustomMaterial_texture; layout(set = 1, binding = 2) uniform sampler CustomMaterial_sampler; @@ -16,5 +14,5 @@ layout(set = 1, binding = 2) uniform sampler CustomMaterial_sampler; void main() { // 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); }