2021-06-06 11:40:33 +10:00
|
|
|
// Vertex shader bindings
|
|
|
|
|
|
|
|
struct VertexOutput {
|
2022-08-18 10:30:04 +10:00
|
|
|
@location(0) tex_coord: vec2<f32>,
|
|
|
|
@builtin(position) position: vec4<f32>,
|
|
|
|
}
|
2021-06-06 11:40:33 +10:00
|
|
|
|
2022-08-18 10:30:04 +10:00
|
|
|
@vertex
|
2021-06-28 04:09:29 +10:00
|
|
|
fn vs_main(
|
2022-08-18 10:30:04 +10:00
|
|
|
@location(0) position: vec2<f32>,
|
2021-06-28 04:09:29 +10:00
|
|
|
) -> VertexOutput {
|
2021-06-06 11:40:33 +10:00
|
|
|
var out: VertexOutput;
|
2022-01-01 10:40:12 +11:00
|
|
|
out.tex_coord = fma(position, vec2<f32>(0.5, -0.5), vec2<f32>(0.5, 0.5));
|
2021-06-28 04:09:29 +10:00
|
|
|
out.position = vec4<f32>(position, 0.0, 1.0);
|
2021-06-06 11:40:33 +10:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fragment shader bindings
|
|
|
|
|
2022-08-18 10:30:04 +10:00
|
|
|
@group(0) @binding(0) var r_tex_color: texture_2d<f32>;
|
|
|
|
@group(0) @binding(1) var r_tex_sampler: sampler;
|
2022-01-01 09:57:57 +11:00
|
|
|
struct Locals {
|
2022-08-18 10:30:04 +10:00
|
|
|
time: f32,
|
|
|
|
}
|
|
|
|
@group(0) @binding(2) var<uniform> r_locals: Locals;
|
2021-06-06 11:40:33 +10:00
|
|
|
|
2023-01-29 06:00:51 +11:00
|
|
|
const tau = 6.283185307179586476925286766559;
|
|
|
|
const bias = 0.2376; // Offset the circular time input so it is never 0
|
2021-06-06 11:40:33 +10:00
|
|
|
|
|
|
|
// Random functions based on https://thebookofshaders.com/10/
|
2023-01-29 06:00:51 +11:00
|
|
|
const random_scale = 43758.5453123;
|
|
|
|
const random_x = 12.9898;
|
|
|
|
const random_y = 78.233;
|
2021-06-06 11:40:33 +10:00
|
|
|
|
|
|
|
fn random(x: f32) -> f32 {
|
|
|
|
return fract(sin(x) * random_scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn random_vec2(st: vec2<f32>) -> f32 {
|
|
|
|
return random(dot(st, vec2<f32>(random_x, random_y)));
|
|
|
|
}
|
|
|
|
|
2022-08-18 10:30:04 +10:00
|
|
|
@fragment
|
|
|
|
fn fs_main(@location(0) tex_coord: vec2<f32>) -> @location(0) vec4<f32> {
|
2021-06-28 04:09:29 +10:00
|
|
|
let sampled_color = textureSample(r_tex_color, r_tex_sampler, tex_coord);
|
|
|
|
let noise_color = vec3<f32>(random_vec2(tex_coord.xy * vec2<f32>(r_locals.time % tau + bias)));
|
2021-06-06 11:40:33 +10:00
|
|
|
|
|
|
|
return vec4<f32>(sampled_color.rgb * noise_color, sampled_color.a);
|
|
|
|
}
|