forestiles/src/shader.wgsl

58 lines
1.4 KiB
WebGPU Shading Language
Raw Normal View History

2024-08-22 17:21:34 +01:00
struct Uniforms {
2024-09-11 20:20:47 +01:00
camera: vec2f,
zooms: vec2f
2024-08-22 17:21:34 +01:00
}
@group(0) @binding(0) var<uniform> uniforms : Uniforms;
struct VertexOutput {
2024-09-25 18:13:50 +01:00
@location(0) tex_coords: vec2f,
@location(1) color: vec4f,
2024-09-25 19:37:42 +01:00
@location(2) effect: u32,
2024-08-22 17:21:34 +01:00
@builtin(position) pos: vec4f
}
2024-08-18 18:21:17 +01:00
@vertex
2024-08-22 17:21:34 +01:00
fn vs_main(
2024-08-28 13:09:57 +01:00
@location(0) pos: vec2f,
2024-09-25 19:37:42 +01:00
@location(1) color: vec4f,
@location(2) tex_coords: vec2f,
@location(3) effect: u32
2024-08-22 17:21:34 +01:00
) -> VertexOutput {
2024-09-25 19:37:42 +01:00
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
);
2024-09-25 18:13:50 +01:00
return out;
}
@group(1) @binding(0)
var t_diffuse: texture_2d<f32>;
@group(1) @binding(1)
var s_diffuse: sampler;
@fragment
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);
2024-09-25 19:37:42 +01:00
// 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);
2024-09-22 20:17:58 +01:00
}
2024-09-25 19:37:42 +01:00
// Whiter
if (in.effect & 4) != 0 {
color.r += 0.4;
color.g += 0.4;
color.b += 0.4;
2024-09-22 20:17:58 +01:00
}
2024-09-25 18:13:50 +01:00
return color;
2024-09-21 18:31:27 +01:00
}