shader should be good

This commit is contained in:
Arkitu 2024-09-25 20:37:42 +02:00
parent 159e4258e9
commit 0a58032400
2 changed files with 30 additions and 28 deletions

View File

@ -16,18 +16,18 @@ pub struct Vertex {
pub color: [f32; 4], pub color: [f32; 4],
pub tex_coords: [f32; 2], pub tex_coords: [f32; 2],
/// Each bit is used as a flag : /// Each bit is used as a flag :
/// 0: Scaled and moved according to camera /// 1: Scaled and moved according to camera
/// 1: Grayscale /// 2: Grayscale
/// 2: Whiter /// 4: Whiter
/// ///
/// For exemple 0b110 corresponds to Scaled and Grayscale /// For exemple 0b011 corresponds to Scaled and Grayscale
pub effect: u32, pub effect: u32,
} }
impl Vertex { impl Vertex {
const DESC: VertexBufferLayout<'static> = VertexBufferLayout { const DESC: VertexBufferLayout<'static> = VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress, array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex, 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],
}; };
} }

View File

@ -7,16 +7,29 @@ struct Uniforms {
struct VertexOutput { struct VertexOutput {
@location(0) tex_coords: vec2f, @location(0) tex_coords: vec2f,
@location(1) color: vec4f, @location(1) color: vec4f,
@location(2) effect: u32,
@builtin(position) pos: vec4f @builtin(position) pos: vec4f
} }
@vertex @vertex
fn vs_main( fn vs_main(
@location(0) pos: vec2f, @location(0) pos: vec2f,
@location(1) tex_coords: vec2f, @location(1) color: vec4f,
@location(2) effect: u32 @location(2) tex_coords: vec2f,
@location(3) effect: u32
) -> VertexOutput { ) -> 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; return out;
} }
@ -29,27 +42,16 @@ var s_diffuse: sampler;
fn fs_main(in: VertexOutput) -> @location(0) vec4f { fn fs_main(in: VertexOutput) -> @location(0) vec4f {
var tex_col = textureSample(t_diffuse, s_diffuse, in.tex_coords); 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); 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 { // Grayscale
color = vec4f(1.0 - color.r, 1.0 - color.g, 1.0 - color.b, color.a); 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 { // Whiter
case 1u, 3u: { if (in.effect & 4) != 0 {
var v = (color.r*0.299) + (color.g*0.587) + (color.b*0.114); color.r += 0.4;
color = vec4f(v, v, v, color.a); color.g += 0.4;
} color.b += 0.4;
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: {}
} }
return color; return color;
} }