[333] render3d: kräftige Umrisslinie in den Kanten-Stilen
Wo die Tiefe zu einem weiter hinten liegenden Körper oder zum Hintergrund springt, zeichnet ein Durchgang in voller Auflösung eine ~2 px kräftige Umrisslinie auf den vorderen Körper (Schattiert mit Kanten, Kanten, Nur Konturen); die Kanten innen bleiben fein. Linienhierarchie wie in einer Zeichnung.
This commit is contained in:
3 files changed
+153
-28
No files matched your search
@@ -2645,14 +2645,20 @@ impl Renderer {
|
||||
// Punktwolken: Viewport/Punktgroesse in die Uniforms (feste Pixelgroesse).
|
||||
self.points.prepare(queue, (w, h));
|
||||
|
||||
// [333] Umgebungsverdeckung nur in den beleuchteten Flächen-Stilen (nicht Verdeckt/Konturen/Drahtgitter).
|
||||
let ao_on = self.ssao.is_some() && self.ssao_on && self.style.draws_faces() && self.style.mesh_mode() < 1.5;
|
||||
// [333] Umgebungsverdeckung nur in den beleuchteten Flächen-Stilen (nicht Verdeckt/Konturen/Drahtgitter); `ao_on`
|
||||
// heisst hier „Nachbearbeitung an“ (Verdeckung und/oder Umriss, gleicher Ablauf mit zwei Durchgängen).
|
||||
let ao_lit = self.ssao.is_some() && self.ssao_on && self.style.draws_faces() && self.style.mesh_mode() < 1.5;
|
||||
// [333] Umrisslinie (Silhouette kräftig) in den Stilen mit Kanten, die Flächen zeichnen.
|
||||
let outline_on = self.ssao.is_some()
|
||||
&& self.style.draws_faces()
|
||||
&& (self.style.draws_edges() || self.style == RenderStyle::Contour);
|
||||
let ao_on = ao_lit || outline_on;
|
||||
let ao_u = ao_on.then(|| {
|
||||
let proj = crate::math::projection_matrix(camera, aspect);
|
||||
let d = crate::math::sub(camera.target, camera.eye);
|
||||
let perspective = matches!(camera.projection, crate::types::Projection::Perspective);
|
||||
let dist = if perspective { crate::math::length(d) } else { camera.ortho_half_height * 2.0 };
|
||||
crate::ssao::uniform(&proj, perspective, dist, (w, h))
|
||||
crate::ssao::uniform(&proj, perspective, dist, (w, h), ao_lit, outline_on)
|
||||
});
|
||||
let depth_view = &self.depth.as_ref().unwrap().view;
|
||||
let msaa_view = &self.msaa.as_ref().unwrap().view;
|
||||
|
||||
+143
-24
@@ -22,6 +22,8 @@ pub(crate) struct AoUniform {
|
||||
pub params: [f32; 4],
|
||||
/// xy = volle Grösse (px), zw = AO-Grösse (px).
|
||||
pub size: [f32; 4],
|
||||
/// [333] x = AO an (1/0), y = Umriss an (1/0), z = Umriss-Stärke (0..1), w = frei.
|
||||
pub flags: [f32; 4],
|
||||
}
|
||||
|
||||
/// Gemeinsamer Kopf beider Shader.
|
||||
@@ -31,6 +33,7 @@ struct AoU {
|
||||
inv_proj : mat4x4<f32>,
|
||||
params : vec4<f32>,
|
||||
size : vec4<f32>,
|
||||
flags : vec4<f32>,
|
||||
};
|
||||
@group(0) @binding(0) var<uniform> u : AoU;
|
||||
@group(0) @binding(1) var depth_ms : texture_depth_multisampled_2d;
|
||||
@@ -131,6 +134,43 @@ fn fs_ao(in : VsOut) -> @location(0) vec4<f32> {
|
||||
}
|
||||
"#;
|
||||
|
||||
/// [333] Umriss je Pixel (R8, volle Auflösung: 1 = kein Umriss): kräftige Umrisslinie, wo die Tiefe zu einem weiter
|
||||
/// hinten liegenden Körper oder zum Hintergrund springt — die Silhouette der Körper wie in einer Zeichnung betont,
|
||||
/// die Kanten innen bleiben fein (eigene 1-px-Linien). Gezeichnet auf dem vorderen Körper: Nachbarn im Abstand 1 voll,
|
||||
/// im Abstand 2 halb (~2 px, aussen weich); Sprung relativ zur Entfernung (Neigung schräger Flächen zählt nicht).
|
||||
const OUTLINE_FS_WGSL: &str = r#"
|
||||
const RING : array<vec3<i32>, 12> = array<vec3<i32>, 12>(
|
||||
vec3<i32>(1, 0, 2), vec3<i32>(-1, 0, 2), vec3<i32>(0, 1, 2), vec3<i32>(0, -1, 2),
|
||||
vec3<i32>(1, 1, 2), vec3<i32>(-1, 1, 2), vec3<i32>(1, -1, 2), vec3<i32>(-1, -1, 2),
|
||||
vec3<i32>(2, 0, 1), vec3<i32>(-2, 0, 1), vec3<i32>(0, 2, 1), vec3<i32>(0, -2, 1),
|
||||
);
|
||||
|
||||
@fragment
|
||||
fn fs_outline(in : VsOut) -> @location(0) vec4<f32> {
|
||||
let fp = vec2<i32>(floor(in.pos.xy));
|
||||
if (depth_at(fp) >= 1.0) {
|
||||
return vec4<f32>(1.0);
|
||||
}
|
||||
let zc = view_at(fp).z;
|
||||
// Toleranz wächst mit der Entfernung (Tiefenauflösung) und mit dem Pixel-Abstand der Probe (schräge Flächen).
|
||||
let tol = 0.03 * abs(zc) + 0.04;
|
||||
var edge = 0.0;
|
||||
for (var k = 0u; k < 12u; k = k + 1u) {
|
||||
let o = RING[k];
|
||||
let q = fp + o.xy;
|
||||
var zq = -1.0e9;
|
||||
if (depth_at(q) < 1.0) {
|
||||
zq = view_at(q).z;
|
||||
}
|
||||
let w = select(0.5, 1.0, o.z == 2);
|
||||
if (zc - zq > tol * f32(3 - o.z)) {
|
||||
edge = max(edge, w);
|
||||
}
|
||||
}
|
||||
return vec4<f32>(1.0 - u.flags.z * edge);
|
||||
}
|
||||
"#;
|
||||
|
||||
/// Multipliziert die Farbe mit der geglätteten Verdeckung (Mischung `Ziel × Quelle`). Liest nur die AO-Textur (die
|
||||
/// Tiefe hängt in Durchgang B als Anhang, darf dort nicht gelesen werden): Gewichte nach der dort abgelegten Tiefe.
|
||||
const COMPOSITE_WGSL: &str = r#"
|
||||
@@ -139,9 +179,11 @@ struct AoU {
|
||||
inv_proj : mat4x4<f32>,
|
||||
params : vec4<f32>,
|
||||
size : vec4<f32>,
|
||||
flags : vec4<f32>,
|
||||
};
|
||||
@group(0) @binding(0) var<uniform> u : AoU;
|
||||
@group(0) @binding(1) var ao_tex : texture_2d<f32>;
|
||||
@group(0) @binding(2) var outline_tex : texture_2d<f32>;
|
||||
|
||||
struct VsOut {
|
||||
@builtin(position) pos : vec4<f32>,
|
||||
@@ -157,12 +199,19 @@ fn vs_main(@builtin(vertex_index) i : u32) -> VsOut {
|
||||
|
||||
@fragment
|
||||
fn fs_composite(in : VsOut) -> @location(0) vec4<f32> {
|
||||
var line = 1.0;
|
||||
if (u.flags.y > 0.5) {
|
||||
line = textureLoad(outline_tex, vec2<i32>(floor(in.pos.xy)), 0).r;
|
||||
}
|
||||
if (u.flags.x < 0.5) {
|
||||
return vec4<f32>(line, line, line, 1.0);
|
||||
}
|
||||
let scale = u.size.xy / u.size.zw;
|
||||
let ao_size = vec2<i32>(u.size.zw);
|
||||
let c = clamp(vec2<i32>(floor(in.pos.xy / scale)), vec2<i32>(0, 0), ao_size - vec2<i32>(1, 1));
|
||||
let z = textureLoad(ao_tex, c, 0).g;
|
||||
if (z > 0.0) {
|
||||
return vec4<f32>(1.0);
|
||||
return vec4<f32>(line, line, line, 1.0);
|
||||
}
|
||||
// 4×4 AO-Texel, gewichtet nach Tiefen-Nähe (relativ zur Entfernung): kein Hof über Silhouetten.
|
||||
var sum = 0.0;
|
||||
@@ -176,7 +225,7 @@ fn fs_composite(in : VsOut) -> @location(0) vec4<f32> {
|
||||
wsum = wsum + w;
|
||||
}
|
||||
}
|
||||
let ao = select(1.0, sum / wsum, wsum > 1e-4);
|
||||
let ao = select(1.0, sum / wsum, wsum > 1e-4) * line;
|
||||
return vec4<f32>(ao, ao, ao, 1.0);
|
||||
}
|
||||
"#;
|
||||
@@ -185,6 +234,10 @@ pub(crate) fn ao_wgsl() -> String {
|
||||
format!("{AO_COMMON_WGSL}{AO_FS_WGSL}")
|
||||
}
|
||||
|
||||
pub(crate) fn outline_wgsl() -> String {
|
||||
format!("{AO_COMMON_WGSL}{OUTLINE_FS_WGSL}")
|
||||
}
|
||||
|
||||
pub(crate) fn composite_wgsl() -> String {
|
||||
COMPOSITE_WGSL.to_string()
|
||||
}
|
||||
@@ -203,8 +256,11 @@ pub(crate) fn ao_radius(view_dist: f32) -> f32 {
|
||||
(0.04 * view_dist).clamp(0.25, 2.5)
|
||||
}
|
||||
|
||||
/// Uniform für ein Bild.
|
||||
pub(crate) fn uniform(proj: &Mat4, perspective: bool, view_dist: f32, full: (u32, u32)) -> AoUniform {
|
||||
/// [333] Stärke der Umrisslinie (Abdunkelung 0..1).
|
||||
pub(crate) const OUTLINE_STRENGTH: f32 = 0.82;
|
||||
|
||||
/// Uniform für ein Bild: `ao`/`outline` = welche Wirkungen an sind.
|
||||
pub(crate) fn uniform(proj: &Mat4, perspective: bool, view_dist: f32, full: (u32, u32), ao: bool, outline: bool) -> AoUniform {
|
||||
let (aw, ah) = ao_size(full.0, full.1);
|
||||
let r = ao_radius(view_dist);
|
||||
AoUniform {
|
||||
@@ -212,9 +268,13 @@ pub(crate) fn uniform(proj: &Mat4, perspective: bool, view_dist: f32, full: (u32
|
||||
inv_proj: crate::math::invert(proj).unwrap_or_else(crate::math::identity),
|
||||
params: [r, 1.1, 0.04 * r, if perspective { 1.0 } else { 0.0 }],
|
||||
size: [full.0 as f32, full.1 as f32, aw as f32, ah as f32],
|
||||
flags: [f32::from(u8::from(ao)), f32::from(u8::from(outline)), OUTLINE_STRENGTH, 0.0],
|
||||
}
|
||||
}
|
||||
|
||||
/// Format der Umriss-Textur (volle Auflösung).
|
||||
pub(crate) const OUTLINE_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::R8Unorm;
|
||||
|
||||
/// Format der AO-Textur: Verdeckung + Tiefe im Kameraraum.
|
||||
pub(crate) const AO_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rg16Float;
|
||||
|
||||
@@ -224,8 +284,11 @@ pub(crate) struct Ssao {
|
||||
ao_layout: wgpu::BindGroupLayout,
|
||||
comp_layout: wgpu::BindGroupLayout,
|
||||
ao_pipeline: wgpu::RenderPipeline,
|
||||
outline_pipeline: wgpu::RenderPipeline,
|
||||
comp_pipeline: wgpu::RenderPipeline,
|
||||
target: Option<(wgpu::TextureView, (u32, u32))>,
|
||||
/// [333] Umriss-Textur (volle Auflösung).
|
||||
outline_target: Option<(wgpu::TextureView, (u32, u32))>,
|
||||
ao_bind: Option<wgpu::BindGroup>,
|
||||
comp_bind: Option<wgpu::BindGroup>,
|
||||
}
|
||||
@@ -274,11 +337,24 @@ impl Ssao {
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 2,
|
||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||
ty: wgpu::BindingType::Texture {
|
||||
sample_type: wgpu::TextureSampleType::Float { filterable: false },
|
||||
view_dimension: wgpu::TextureViewDimension::D2,
|
||||
multisampled: false,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
],
|
||||
});
|
||||
let ao_src = ao_wgsl();
|
||||
let ao_src = if samples > 1 { ao_src } else { ao_src.replace("texture_depth_multisampled_2d", "texture_depth_2d") };
|
||||
let ao_module = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("ssao.ao.wgsl"), source: wgpu::ShaderSource::Wgsl(ao_src.into()) });
|
||||
let ol_src = outline_wgsl();
|
||||
let ol_src = if samples > 1 { ol_src } else { ol_src.replace("texture_depth_multisampled_2d", "texture_depth_2d") };
|
||||
let ol_module = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("ssao.outline.wgsl"), source: wgpu::ShaderSource::Wgsl(ol_src.into()) });
|
||||
let comp_module = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("ssao.comp.wgsl"), source: wgpu::ShaderSource::Wgsl(composite_wgsl().into()) });
|
||||
let layout = |l: &wgpu::BindGroupLayout, name: &str| {
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: Some(name), bind_group_layouts: &[Some(l)], immediate_size: 0 })
|
||||
@@ -301,6 +377,22 @@ impl Ssao {
|
||||
multiview_mask: None,
|
||||
cache: None,
|
||||
});
|
||||
let outline_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("ssao.outline"),
|
||||
layout: Some(&ao_pl),
|
||||
vertex: wgpu::VertexState { module: &ol_module, entry_point: Some("vs_main"), buffers: &[], compilation_options: Default::default() },
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &ol_module,
|
||||
entry_point: Some("fs_outline"),
|
||||
targets: &[Some(wgpu::ColorTargetState { format: OUTLINE_FORMAT, blend: None, write_mask: wgpu::ColorWrites::ALL })],
|
||||
compilation_options: Default::default(),
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState::default(),
|
||||
depth_stencil: None,
|
||||
multisample: wgpu::MultisampleState::default(),
|
||||
multiview_mask: None,
|
||||
cache: None,
|
||||
});
|
||||
// Ziel × Quelle (Farbe), Alpha bleibt.
|
||||
let multiply = wgpu::BlendState {
|
||||
color: wgpu::BlendComponent { src_factor: wgpu::BlendFactor::Zero, dst_factor: wgpu::BlendFactor::Src, operation: wgpu::BlendOperation::Add },
|
||||
@@ -329,7 +421,7 @@ impl Ssao {
|
||||
multiview_mask: None,
|
||||
cache: None,
|
||||
});
|
||||
Self { uniform, ao_layout, comp_layout, ao_pipeline, comp_pipeline, target: None, ao_bind: None, comp_bind: None }
|
||||
Self { uniform, ao_layout, comp_layout, ao_pipeline, outline_pipeline, comp_pipeline, target: None, outline_target: None, ao_bind: None, comp_bind: None }
|
||||
}
|
||||
|
||||
/// Bind-Groups verwerfen (neue Tiefentextur).
|
||||
@@ -338,7 +430,8 @@ impl Ssao {
|
||||
self.comp_bind = None;
|
||||
}
|
||||
|
||||
/// Verdeckung in die AO-Textur rechnen (eigener Durchgang, liest die Tiefe aus Durchgang A).
|
||||
/// Verdeckung (halbe Auflösung) und Umriss (volle Auflösung) rechnen, je nach `u.flags` (eigene Durchgänge, lesen die
|
||||
/// Tiefe aus Durchgang A).
|
||||
pub(crate) fn encode(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
@@ -363,8 +456,23 @@ impl Ssao {
|
||||
self.target = Some((tex.create_view(&wgpu::TextureViewDescriptor::default()), size));
|
||||
self.invalidate();
|
||||
}
|
||||
if self.outline_target.as_ref().map(|t| t.1) != Some(full) {
|
||||
let tex = device.create_texture(&wgpu::TextureDescriptor {
|
||||
label: Some("ssao.outline"),
|
||||
size: wgpu::Extent3d { width: full.0.max(1), height: full.1.max(1), depth_or_array_layers: 1 },
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: OUTLINE_FORMAT,
|
||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
|
||||
view_formats: &[],
|
||||
});
|
||||
self.outline_target = Some((tex.create_view(&wgpu::TextureViewDescriptor::default()), full));
|
||||
self.invalidate();
|
||||
}
|
||||
queue.write_buffer(&self.uniform, 0, bytemuck::bytes_of(u));
|
||||
let ao_view = &self.target.as_ref().unwrap().0;
|
||||
let ol_view = &self.outline_target.as_ref().unwrap().0;
|
||||
if self.ao_bind.is_none() {
|
||||
self.ao_bind = Some(device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("ssao.ao.bind"),
|
||||
@@ -380,25 +488,35 @@ impl Ssao {
|
||||
entries: &[
|
||||
wgpu::BindGroupEntry { binding: 0, resource: self.uniform.as_entire_binding() },
|
||||
wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::TextureView(ao_view) },
|
||||
wgpu::BindGroupEntry { binding: 2, resource: wgpu::BindingResource::TextureView(ol_view) },
|
||||
],
|
||||
}));
|
||||
}
|
||||
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: Some("ssao.pass"),
|
||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||
view: ao_view,
|
||||
depth_slice: None,
|
||||
resolve_target: None,
|
||||
ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color { r: 1.0, g: 1.0, b: 0.0, a: 1.0 }), store: wgpu::StoreOp::Store },
|
||||
})],
|
||||
depth_stencil_attachment: None,
|
||||
timestamp_writes: None,
|
||||
occlusion_query_set: None,
|
||||
multiview_mask: None,
|
||||
});
|
||||
pass.set_pipeline(&self.ao_pipeline);
|
||||
pass.set_bind_group(0, self.ao_bind.as_ref(), &[]);
|
||||
pass.draw(0..3, 0..1);
|
||||
let full_pass = |encoder: &mut wgpu::CommandEncoder, view: &wgpu::TextureView, label: &str, pipeline: &wgpu::RenderPipeline, bind: &wgpu::BindGroup| {
|
||||
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: Some(label),
|
||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||
view,
|
||||
depth_slice: None,
|
||||
resolve_target: None,
|
||||
ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color { r: 1.0, g: 1.0, b: 0.0, a: 1.0 }), store: wgpu::StoreOp::Store },
|
||||
})],
|
||||
depth_stencil_attachment: None,
|
||||
timestamp_writes: None,
|
||||
occlusion_query_set: None,
|
||||
multiview_mask: None,
|
||||
});
|
||||
pass.set_pipeline(pipeline);
|
||||
pass.set_bind_group(0, bind, &[]);
|
||||
pass.draw(0..3, 0..1);
|
||||
};
|
||||
let bind = self.ao_bind.as_ref().unwrap();
|
||||
if u.flags[0] > 0.5 {
|
||||
full_pass(encoder, ao_view, "ssao.pass", &self.ao_pipeline, bind);
|
||||
}
|
||||
if u.flags[1] > 0.5 {
|
||||
full_pass(encoder, ol_view, "ssao.outline.pass", &self.outline_pipeline, bind);
|
||||
}
|
||||
}
|
||||
|
||||
/// Farbe mit der Verdeckung multiplizieren (in Durchgang B, vor Kanten und Linien).
|
||||
@@ -428,7 +546,8 @@ mod tests {
|
||||
#[test]
|
||||
fn uniform_kehrt_die_projektion_um() {
|
||||
let p = crate::math::perspective(0.8, 1.5, 0.1, 500.0);
|
||||
let u = uniform(&p, true, 20.0, (300, 200));
|
||||
let u = uniform(&p, true, 20.0, (300, 200), true, false);
|
||||
assert_eq!(u.flags, [1.0, 0.0, OUTLINE_STRENGTH, 0.0]);
|
||||
let m = crate::math::mul(&u.proj, &u.inv_proj);
|
||||
for (i, v) in m.iter().enumerate() {
|
||||
let want = if i % 5 == 0 { 1.0 } else { 0.0 };
|
||||
@@ -442,7 +561,7 @@ mod tests {
|
||||
#[test]
|
||||
fn wgsl_valide() {
|
||||
use naga::valid::{Capabilities, ValidationFlags, Validator};
|
||||
for (name, src) in [("ao", ao_wgsl()), ("composite", composite_wgsl())] {
|
||||
for (name, src) in [("ao", ao_wgsl()), ("outline", outline_wgsl()), ("composite", composite_wgsl())] {
|
||||
let m = naga::front::wgsl::parse_str(&src).unwrap_or_else(|e| panic!("{name}: {}", e.emit_to_string(&src)));
|
||||
Validator::new(ValidationFlags::all(), Capabilities::all())
|
||||
.validate(&m)
|
||||
|
||||
Reference in new issue
Block a user