59 lines
1.8 KiB
WebGPU Shading Language
59 lines
1.8 KiB
WebGPU Shading Language
#import bevy_core_pipeline::fullscreen_vertex_shader::FullscreenVertexOutput
|
|
|
|
struct PostProcessSettings {
|
|
scale: f32,
|
|
}
|
|
|
|
@group(0) @binding(0) var screen_texture: texture_2d<f32>;
|
|
@group(0) @binding(1) var texture_sampler: sampler;
|
|
@group(0) @binding(2) var<uniform> settings: PostProcessSettings;
|
|
|
|
const dither = array<array<f32, 8>, 8>(
|
|
array<f32, 8>( 0.0, 32.0, 8.0, 40.0, 2.0, 34.0, 10.0, 42.0),
|
|
array<f32, 8>(48.0, 16.0, 56.0, 24.0, 50.0, 18.0, 58.0, 26.0),
|
|
array<f32, 8>(12.0, 44.0, 4.0, 36.0, 14.0, 46.0, 6.0, 38.0),
|
|
array<f32, 8>(60.0, 28.0, 52.0, 20.0, 62.0, 30.0, 54.0, 22.0),
|
|
array<f32, 8>( 3.0, 35.0, 11.0, 43.0, 1.0, 33.0, 9.0, 41.0),
|
|
array<f32, 8>(51.0, 19.0, 59.0, 27.0, 49.0, 17.0, 57.0, 25.0),
|
|
array<f32, 8>(15.0, 47.0, 7.0, 39.0, 13.0, 45.0, 5.0, 37.0),
|
|
array<f32, 8>(63.0, 31.0, 55.0, 23.0, 61.0, 29.0, 53.0, 21.0)
|
|
);
|
|
|
|
fn find_closest(x: i32, y: i32, c0: f32) -> f32 {
|
|
|
|
|
|
var limit = 0.0;
|
|
|
|
if (x < 8) {
|
|
limit = (dither[x][y]+1) / 64.0;
|
|
}
|
|
|
|
if (c0 < limit) {
|
|
return 0.0;
|
|
}
|
|
|
|
return 1.0;
|
|
}
|
|
|
|
@fragment
|
|
fn fragment(in: FullscreenVertexOutput) -> @location(0) vec4<f32> {
|
|
let a: f32 = textureSample(screen_texture, texture_sampler, in.uv).a;
|
|
|
|
let gr: f32 = textureSample(screen_texture, texture_sampler, in.uv).r * 0.2989;
|
|
let gg: f32 = textureSample(screen_texture, texture_sampler, in.uv).g * 0.587;
|
|
let gb: f32 = textureSample(screen_texture, texture_sampler, in.uv).b * 0.114;
|
|
let grayscale: f32 = gr + gg + gb;
|
|
|
|
let uvx: f32 = in.position.x;
|
|
let uvy: f32 = in.position.y;
|
|
|
|
let x: i32 = i32(uvx * settings.scale) % 8;
|
|
let y: i32 = i32(uvy * settings.scale) % 8;
|
|
|
|
let final_gray = find_closest(x, y, grayscale);
|
|
|
|
return vec4<f32>(
|
|
final_gray, final_gray, final_gray, 0.0
|
|
);
|
|
}
|