From 0a58032400c24f979ec60aad2af2afc097cd7179 Mon Sep 17 00:00:00 2001 From: Arkitu <85173315+Arkitu@users.noreply.github.com> Date: Wed, 25 Sep 2024 20:37:42 +0200 Subject: [PATCH] shader should be good --- src/graphics.rs | 10 +++++----- src/shader.wgsl | 48 +++++++++++++++++++++++++----------------------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/graphics.rs b/src/graphics.rs index f24c397..dfb0822 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -16,18 +16,18 @@ pub struct Vertex { pub color: [f32; 4], pub tex_coords: [f32; 2], /// Each bit is used as a flag : - /// 0: Scaled and moved according to camera - /// 1: Grayscale - /// 2: Whiter + /// 1: Scaled and moved according to camera + /// 2: Grayscale + /// 4: Whiter /// - /// For exemple 0b110 corresponds to Scaled and Grayscale + /// For exemple 0b011 corresponds to Scaled and Grayscale pub effect: u32, } impl Vertex { const DESC: VertexBufferLayout<'static> = VertexBufferLayout { array_stride: std::mem::size_of::() as wgpu::BufferAddress, step_mode: wgpu::VertexStepMode::Vertex, - attributes: &wgpu::vertex_attr_array![0 => Float32x2, 1 => Float32x4, 2 => Uint32], + attributes: &wgpu::vertex_attr_array![0 => Float32x2, 1 => Float32x4, 2 => Float32x2, 3 => Uint32], }; } diff --git a/src/shader.wgsl b/src/shader.wgsl index ef0a053..a9682c8 100644 --- a/src/shader.wgsl +++ b/src/shader.wgsl @@ -7,16 +7,29 @@ struct Uniforms { struct VertexOutput { @location(0) tex_coords: vec2f, @location(1) color: vec4f, + @location(2) effect: u32, @builtin(position) pos: vec4f } @vertex fn vs_main( @location(0) pos: vec2f, - @location(1) tex_coords: vec2f, - @location(2) effect: u32 + @location(1) color: vec4f, + @location(2) tex_coords: vec2f, + @location(3) effect: u32 ) -> VertexOutput { - var out: VertexOutput; + var screen_pos: vec4f; + if (effect & 1) == 0 { + screen_pos = vec4f(pos, 0, 1); + } else { + screen_pos = vec4f((pos - uniforms.camera) * uniforms.zooms, 0, 1); + } + var out = VertexOutput( + tex_coords, + color, + effect, + screen_pos + ); return out; } @@ -29,27 +42,16 @@ var s_diffuse: sampler; fn fs_main(in: VertexOutput) -> @location(0) vec4f { var tex_col = textureSample(t_diffuse, s_diffuse, in.tex_coords); var color = vec4f((tex_col.rgb * tex_col.a) + (in.color.rgb * in.color.a), tex_col.a + in.color.a); - if effect & 0100u != 0u { - color = vec4f(1.0 - color.r, 1.0 - color.g, 1.0 - color.b, color.a); + // Grayscale + if (in.effect & 2) != 0 { + var v = (color.r*0.299) + (color.g*0.587) + (color.b*0.114); + color = vec4f(v, v, v, color.a); } - switch effect { - case 1u, 3u: { - var v = (color.r*0.299) + (color.g*0.587) + (color.b*0.114); - color = vec4f(v, v, v, color.a); - } - case 0u, 2u: { - color = color; - } - default: {} - } - switch effect { - case 0u, 1u: { - pos = vec4f(pos, 0, 1); - } - case 2u, 3u: { - pos = vec4f((pos - uniforms.camera) * uniforms.zooms, 0, 1); - } - default: {} + // Whiter + if (in.effect & 4) != 0 { + color.r += 0.4; + color.g += 0.4; + color.b += 0.4; } return color; }