forestiles/src/shader.wgsl
2024-09-27 19:01:30 +02:00

53 lines
1.1 KiB
WebGPU Shading Language

struct Uniforms {
camera: vec2f,
zooms: vec2f
}
@group(0) @binding(0) var<uniform> uniforms : Uniforms;
struct VertexOutput {
@location(0) color: vec4f,
@location(1) effect: u32,
@builtin(position) pos: vec4f
}
@vertex
fn vs_main(
@location(0) pos: vec2f,
@location(1) color: vec4f,
@location(2) effect: u32
) -> 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(
color,
effect,
screen_pos
);
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 color: vec4f;
if (in.effect & 4) == 0 {
color = in.color;
} else {
color = textureSample(t_diffuse, s_diffuse, in.color.xy);
}
// 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);
}
return color;
}