Thu Nov 21 12:23:45 PM EST 2024 (Introduced Defferred Rendering/Shading Threshold)
This commit is contained in:
parent
68c0010bd4
commit
48382cc39b
@ -1,48 +1,62 @@
|
|||||||
#import bevy_core_pipeline::fullscreen_vertex_shader::FullscreenVertexOutput;
|
#import bevy_core_pipeline::fullscreen_vertex_shader::FullscreenVertexOutput;
|
||||||
|
#import bevy_pbr::{ rgb9e5 };
|
||||||
|
|
||||||
struct OutlinePostProcessSettings {
|
struct OutlinePostProcessSettings {
|
||||||
weight: f32,
|
weight: f32,
|
||||||
threshold: f32,
|
normal_threshold: f32,
|
||||||
adaptive: u32,
|
adaptive: u32,
|
||||||
|
light_threshold: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
@group(0) @binding(0) var screen_texture: texture_2d<f32>;
|
@group(0) @binding(0) var screen_texture: texture_2d<f32>;
|
||||||
@group(0) @binding(1) var screen_sampler: sampler;
|
@group(0) @binding(1) var screen_sampler: sampler;
|
||||||
@group(0) @binding(2) var normal_texture: texture_2d<f32>;
|
@group(0) @binding(2) var normal_texture: texture_2d<f32>;
|
||||||
@group(0) @binding(3) var normal_sampler: sampler;
|
@group(0) @binding(3) var normal_sampler: sampler;
|
||||||
@group(0) @binding(4) var<uniform> settings: OutlinePostProcessSettings;
|
@group(0) @binding(4) var deferred_texture: texture_2d<u32>;
|
||||||
|
@group(0) @binding(5) var<uniform> settings: OutlinePostProcessSettings;
|
||||||
|
|
||||||
@fragment
|
@fragment
|
||||||
fn fragment(
|
fn fragment(
|
||||||
in: FullscreenVertexOutput
|
in: FullscreenVertexOutput
|
||||||
) -> @location(0) vec4<f32> {
|
) -> @location(0) vec4<f32> {
|
||||||
let screen_color = textureSample(screen_texture, screen_sampler, in.uv);
|
let screen_color = textureSample(screen_texture, screen_sampler, in.uv);
|
||||||
let outline_width = settings.weight / vec2f(textureDimensions(screen_texture));
|
let deferred_dimensions = textureDimensions(deferred_texture);
|
||||||
|
let deferred_texel = textureLoad(deferred_texture, vec2u(vec2f(deferred_dimensions) * in.uv), 0);
|
||||||
|
let deferred_color = vec4f(vec3f(unpack4x8unorm(deferred_texel.r).rgb), 1.0);
|
||||||
|
let emissive = vec4f(rgb9e5::rgb9e5_to_vec3_(deferred_texel.g), 1.0);
|
||||||
|
let light = screen_color / ((deferred_color) + vec4f(0.001, 0.001, 0.001, 1.0));
|
||||||
|
|
||||||
let uv_top = 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_top_right = vec2f(in.uv.x + outline_width.x, in.uv.y - outline_width.y);
|
|
||||||
|
|
||||||
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 delta_top = abs(normal - normal_top);
|
|
||||||
let delta_right = abs(normal - normal_right);
|
|
||||||
let delta_top_right = abs(normal - normal_top_right);
|
|
||||||
|
|
||||||
let delta_max = max(delta_top, max(delta_right, delta_top_right));
|
|
||||||
let delta_raw = max(delta_max.x, max(delta_max.y, delta_max.z));
|
|
||||||
|
|
||||||
// let delta_clipped = clamp((delta_raw * 2.0) - settings.threshold, 0.0, 1.0);
|
|
||||||
let show_outline = f32(delta_raw > settings.threshold);
|
|
||||||
|
|
||||||
var outline = vec4f(show_outline, show_outline, show_outline, 0.0);
|
|
||||||
let luma = (0.2126 * screen_color.r + 0.7152 * screen_color.g + 0.0722 * screen_color.b);
|
let luma = (0.2126 * screen_color.r + 0.7152 * screen_color.g + 0.0722 * screen_color.b);
|
||||||
if settings.adaptive != 0 && luma < 0.5 {
|
let light_luma = (0.2126 * light.r + 0.7152 * light.g + 0.0722 * light.b);
|
||||||
outline = outline * -1;
|
let emissive_luma = (0.2126 * emissive.r + 0.7152 * emissive.g + 0.0722 * emissive.b);
|
||||||
}
|
let final_luma = luma + light_luma + emissive_luma;
|
||||||
|
|
||||||
return screen_color - outline;
|
if final_luma > settings.light_threshold {
|
||||||
|
let outline_width = settings.weight / vec2f(textureDimensions(screen_texture));
|
||||||
|
|
||||||
|
let uv_top = 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_top_right = vec2f(in.uv.x + outline_width.x, in.uv.y - outline_width.y);
|
||||||
|
|
||||||
|
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_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 show_outline = f32(normal_delta_raw > settings.normal_threshold);
|
||||||
|
|
||||||
|
var outline = vec4f(show_outline, show_outline, show_outline, 0.0);
|
||||||
|
if settings.adaptive != 0 && luma < 0.5 {
|
||||||
|
outline = outline * -1;
|
||||||
|
}
|
||||||
|
return screen_color - outline + emissive;
|
||||||
|
}
|
||||||
|
return screen_color + emissive;
|
||||||
}
|
}
|
||||||
|
@ -11,18 +11,21 @@ pub struct OutlinePostProcessSettings {
|
|||||||
weight: f32,
|
weight: f32,
|
||||||
/// A threshold for normal differences, values below this threshold will not become outlines.
|
/// A threshold for normal differences, values below this threshold will not become outlines.
|
||||||
/// Higher values will result in more outlines which may look better on smooth surfaces.
|
/// Higher values will result in more outlines which may look better on smooth surfaces.
|
||||||
threshold: f32,
|
normal_threshold: f32,
|
||||||
/// Whether to use adaptive outlines. White outlines will be drawn around darker objects, while black ones will be drawn around lighter ones.
|
/// Whether to use adaptive outlines. White outlines will be drawn around darker objects, while black ones will be drawn around lighter ones.
|
||||||
adaptive: u32,
|
adaptive: u32,
|
||||||
|
/// Threshold of illumination for outlines to apply. Requires deferred prepass.
|
||||||
|
light_threshold: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OutlinePostProcessSettings {
|
impl OutlinePostProcessSettings {
|
||||||
/// Create a new instance with the given settings
|
/// Create a new instance with the given settings
|
||||||
pub fn new(weight: f32, threshold: f32, adaptive: bool) -> Self {
|
pub fn new(weight: f32, normal_threshold: f32, adaptive: bool, light_threshold: f32) -> Self {
|
||||||
Self {
|
Self {
|
||||||
weight,
|
weight,
|
||||||
threshold,
|
normal_threshold,
|
||||||
adaptive: adaptive as u32,
|
adaptive: adaptive as u32,
|
||||||
|
light_threshold,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -31,8 +34,9 @@ impl Default for OutlinePostProcessSettings {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
weight: 1.0,
|
weight: 1.0,
|
||||||
threshold: 0.0,
|
normal_threshold: 0.0,
|
||||||
adaptive: 0
|
adaptive: 0,
|
||||||
|
light_threshold: 0.0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -57,6 +57,11 @@ impl ViewNode for OutlineRenderNode {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let Some(deferred_buffer_view) = view_prepass_textures.deferred_view() else {
|
||||||
|
error!("Failed to get deferred buffer view");
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
let post_process = view_target.post_process_write();
|
let post_process = view_target.post_process_write();
|
||||||
|
|
||||||
let bind_group = render_context.render_device().create_bind_group(
|
let bind_group = render_context.render_device().create_bind_group(
|
||||||
@ -67,6 +72,7 @@ impl ViewNode for OutlineRenderNode {
|
|||||||
&render_pipeline.screen_sampler,
|
&render_pipeline.screen_sampler,
|
||||||
normal_buffer_view,
|
normal_buffer_view,
|
||||||
&render_pipeline.normal_sampler,
|
&render_pipeline.normal_sampler,
|
||||||
|
deferred_buffer_view,
|
||||||
uniform_binding,
|
uniform_binding,
|
||||||
)),
|
)),
|
||||||
);
|
);
|
||||||
|
@ -37,6 +37,7 @@ impl FromWorld for OutlinePostProcessPipeline {
|
|||||||
sampler(SamplerBindingType::Filtering),
|
sampler(SamplerBindingType::Filtering),
|
||||||
texture_2d(TextureSampleType::Float { filterable: true }),
|
texture_2d(TextureSampleType::Float { filterable: true }),
|
||||||
sampler(SamplerBindingType::Filtering),
|
sampler(SamplerBindingType::Filtering),
|
||||||
|
texture_2d(TextureSampleType::Uint),
|
||||||
uniform_buffer::<components::OutlinePostProcessSettings>(false),
|
uniform_buffer::<components::OutlinePostProcessSettings>(false),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user