diff options
Diffstat (limited to 'assets/shaders/outline_post_process.wgsl')
-rw-r--r-- | assets/shaders/outline_post_process.wgsl | 72 |
1 files changed, 40 insertions, 32 deletions
diff --git a/assets/shaders/outline_post_process.wgsl b/assets/shaders/outline_post_process.wgsl index e5263a1..8f05e01 100644 --- a/assets/shaders/outline_post_process.wgsl +++ b/assets/shaders/outline_post_process.wgsl @@ -1,15 +1,11 @@ #import bevy_core_pipeline::fullscreen_vertex_shader::FullscreenVertexOutput; -#import bevy_pbr::{ rgb9e5 }; -#import bevy_pbr::view_transformations::{ - perspective_camera_near, -}; -#import bevy_pbr::mesh_view_bindings as view_bindings; struct OutlinePostProcessSettings { weight: f32, + color: vec4<f32>, normal_threshold: f32, depth_threshold: f32, - adaptive: u32, + adaptive_threshold: f32, camera_near: f32, } @@ -30,58 +26,70 @@ fn fragment( let outline_width = settings.weight / vec2f(textureDimensions(screen_texture)); let uv_top = vec2f(in.uv.x, in.uv.y - outline_width.y); + let uv_bottom = vec2f(in.uv.x, in.uv.y + outline_width.y); let uv_right = vec2f(in.uv.x + outline_width.x, in.uv.y); + let uv_left = vec2f(in.uv.x - outline_width.x, in.uv.y); let uv_top_right = vec2f(in.uv.x + outline_width.x, in.uv.y - outline_width.y); // NORMAL FACTOR {{{ - let normal = textureSample(normal_texture, normal_sampler, in.uv); - let normal_top = textureSample(normal_texture, normal_sampler, uv_top); - let normal_right = textureSample(normal_texture, normal_sampler, uv_right); - let normal_top_right = textureSample(normal_texture, normal_sampler, uv_top_right); + let normal = textureSample(normal_texture, normal_sampler, in.uv).xyz; + let normal_top = textureSample(normal_texture, normal_sampler, uv_top).xyz; + let normal_right = textureSample(normal_texture, normal_sampler, uv_right).xyz; + let normal_top_right = textureSample(normal_texture, normal_sampler, uv_top_right).xyz; let normal_delta_top = abs(normal - normal_top); let normal_delta_right = abs(normal - normal_right); let normal_delta_top_right = abs(normal - normal_top_right); - let normal_delta_max = max(normal_delta_top, max(normal_delta_right, normal_delta_top_right)); - let normal_delta_raw = max(normal_delta_max.x, max(normal_delta_max.y, normal_delta_max.z)); + let normal_top_sum = max(normal_delta_top.x, max(normal_delta_top.y, normal_delta_top.z)); + let normal_right_sum = max(normal_delta_right.x, max(normal_delta_right.y, normal_delta_right.z)); + let normal_top_right_sum = max(normal_delta_top_right.x, max(normal_delta_top_right.y, normal_delta_top_right.z)); - let show_outline_normal = f32(normal_delta_raw > settings.normal_threshold); - let normal_outline = vec4f(show_outline_normal, show_outline_normal, show_outline_normal, 0.0); + var normal_difference = step(settings.normal_threshold, max(normal_top_sum, max(normal_right_sum, normal_top_right_sum))); + let normal_outline = normal_difference; // }}} - let frag_width = settings.weight / vec2f(textureDimensions(screen_texture)); - let depth_uv_top_right = vec2f(in.uv.x + frag_width.x, in.uv.y - frag_width.y); - let depth_uv_top_left = vec2f(in.uv.x - frag_width.x, in.uv.y - frag_width.y); - let depth_uv_bottom_right = vec2f(in.uv.x + frag_width.x, in.uv.y + frag_width.y); - let depth_uv_bottom_left = vec2f(in.uv.x - frag_width.x, in.uv.y + frag_width.y); // DEPTH FACTOR {{{ let depth_color = textureSample(depth_texture, depth_sampler, in.uv); + let depth_color_top = textureSample(depth_texture, depth_sampler, uv_top); + let depth_color_bottom = textureSample(depth_texture, depth_sampler, uv_bottom); + let depth_color_right = textureSample(depth_texture, depth_sampler, uv_right); + let depth_color_left = textureSample(depth_texture, depth_sampler, uv_left); + let depth = linearize_depth(depth_color); - let depth_top_right = linearize_depth(textureSample(depth_texture, depth_sampler, depth_uv_top_right)); - let depth_top_left = linearize_depth(textureSample(depth_texture, depth_sampler, depth_uv_top_left)); - let depth_bottom_right = linearize_depth(textureSample(depth_texture, depth_sampler, depth_uv_bottom_right)); - let depth_bottom_left = linearize_depth(textureSample(depth_texture, depth_sampler, depth_uv_bottom_left)); + let depth_top = linearize_depth(depth_color_top); + let depth_bottom = linearize_depth(depth_color_bottom); + let depth_right = linearize_depth(depth_color_right); + let depth_left = linearize_depth(depth_color_left); var depth_delta = 0.0; - depth_delta = depth_delta + depth - depth_top_right; - depth_delta = depth_delta + depth - depth_top_left; - depth_delta = depth_delta + depth - depth_bottom_right; - depth_delta = depth_delta + depth - depth_bottom_left; + depth_delta = depth_delta + depth - depth_top; + depth_delta = depth_delta + depth - depth_bottom; + depth_delta = depth_delta + depth - depth_right; + depth_delta = depth_delta + depth - depth_left; - var depth_outline = vec4(step(settings.depth_threshold, depth_delta)); + var depth_outline = step(settings.depth_threshold, depth_delta); if depth_color == 0.0 { - depth_outline = vec4(0.0); + depth_outline = 0.0; } // }}} - var outline = (depth_outline + normal_outline); - if settings.adaptive != 0 && luma < 0.5 { + var outline = saturate(depth_outline + normal_outline); + if (1.0 - luma) > settings.adaptive_threshold { outline = outline * -1; } - return screen_color - outline ; + if (outline == 1.0) { + return settings.color; + } + if (outline == -1.0) { + return vec4(vec3(1.0) - settings.color.xyz, 1.0); + } + return screen_color; } fn linearize_depth(depth: f32) -> f32 { + if depth == 0.0 { + return 0.0; + } return settings.camera_near / depth; } |