From acfd60b6b11fb02cf12b4620fd59738e0f07bcb5 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 22 Nov 2026 21:15:47 +0100 Subject: [PATCH 1/6] Highlight texture-set surfaces on hover in the 3D viewport --- crates/render/src/renderer.rs | 49 ++++++++++++++++++----------- crates/render/src/set_highlight.rs | 8 ++--- crates/render/src/shaders/mesh.wgsl | 11 +++++-- crates/ui-egui/src/bake_mode/mod.rs | 5 ++- crates/ui-egui/src/dock.rs | 4 +++ crates/ui-egui/src/lib.rs | 11 +++++++ crates/ui-egui/src/viewport.rs | 13 ++++++-- 7 files changed, 72 insertions(+), 29 deletions(-) diff --git a/crates/render/src/renderer.rs b/crates/render/src/renderer.rs index 5e0561af..8cd7d593 100644 --- a/crates/render/src/renderer.rs +++ b/crates/render/src/renderer.rs @@ -100,6 +100,7 @@ struct GlobalsRaw { refr: [f32; 4], highlight: [f32; 4], highlight_shift: [f32; 4], + set_hover: [f32; 4], bake_high: [f32; 4], bake_matching_error: [f32; 4], bake_cage: [f32; 4], @@ -137,6 +138,9 @@ const HIGHLIGHT_WIDTH_PX: f32 = 1.5; /// Opacity of the highlighted set's edge lines const HIGHLIGHT_OPACITY: f32 = 0.9; +/// Opacity of the texture-set hover tint over the shaded material. +const SET_HOVER_OPACITY: f32 = 0.35; + /// Copies of each highlight line, shifted diagonally to the four corners of its extra width (`vs_wire_highlight`) const HIGHLIGHT_SHIFTS: u32 = 4; @@ -194,6 +198,8 @@ pub struct Shade { pub wireframe_opacity: f32, /// One texture set's mesh edges in a highlight color over the 3D view (the set under a shelf drag) pub highlight: Option, + /// The texture set whose visible surfaces are tinted in the 3D view. + pub set_hover: Option, /// Specular/glossiness shading: base color is the diffuse color and the Specular channel the /// F0 color (roughness arrives as 1 - glossiness from the display conversion). pub spec_gloss: bool, @@ -308,6 +314,7 @@ impl Default for Shade { wireframe_3d: false, wireframe_opacity: 0.4, highlight: None, + set_hover: None, spec_gloss: false, parallax: false, taa: false, @@ -1941,8 +1948,8 @@ impl Renderer { ranges: &'a [(u32, u32, usize)], visible: &'a [bool], only: Option, - ) -> impl Iterator, &'a SetGpu)> + 'a { - drawable_ranges(ranges, &self.material_sets, visible, only).filter_map(|(range, ts)| Some((range, self.sets.get(ts)?))) + ) -> impl Iterator, usize, &'a SetGpu)> + 'a { + drawable_ranges(ranges, &self.material_sets, visible, only).filter_map(|(range, ts)| Some((range, ts, self.sets.get(ts)?))) } /// Keep the key-light shadow map current; returns its view-projection. `light` points to @@ -2003,7 +2010,7 @@ impl Renderer { let geo = m.dense.as_ref().filter(|_| displacing).unwrap_or(&m.geo); pass.set_vertex_buffer(0, geo.vb.slice(..)); pass.set_index_buffer(geo.ib.slice(..), wgpu::IndexFormat::Uint32); - for (range, set) in self.drawable(&geo.draws, visible, None) { + for (range, _, set) in self.drawable(&geo.draws, visible, None) { pass.set_bind_group(1, &set.bind_group, &[]); pass.draw_indexed(range, 0, 0..1); } @@ -2125,8 +2132,9 @@ impl Renderer { fwd: [fwd.x, fwd.y, fwd.z, 0.0], disp, refr: [if refraction { 1.0 } else { 0.0 }, (BEHIND_LEVELS - 1) as f32, REFRACTION_DEPTH * radius, 0.0], - highlight: highlight_globals(s, data), + highlight: highlight_globals(s.highlight, HIGHLIGHT_OPACITY, s, data), highlight_shift: highlight_shift(frame.size), + set_hover: highlight_globals(s.set_hover, SET_HOVER_OPACITY, s, data), ..bake_globals(s.bake) }; let flag = |b: bool| if b { 1.0 } else { 0.0 }; @@ -2214,7 +2222,7 @@ impl Renderer { let geo = self.geometry(m, frame); pass.set_vertex_buffer(0, geo.vb.slice(..)); pass.set_index_buffer(geo.ib.slice(..), wgpu::IndexFormat::Uint32); - for (range, set) in self.drawable(&geo.draws, &frame.visible, None) { + for (range, _, set) in self.drawable(&geo.draws, &frame.visible, None) { pass.set_bind_group(1, &set.bind_group, &[]); pass.draw_indexed(range, 0, 0..1); } @@ -2341,22 +2349,25 @@ impl Renderer { let bake = frame.shade.bake.filter(|_| uv_set.is_none()); let low_poly_shown = bake.is_none_or(|bake| bake.low_poly.shown); let (highlight, highlight_set) = highlight.filter(|(_, set)| low_poly_shown && uv_set.is_none_or(|only| only == *set)).unzip(); + let hover_set = frame.shade.set_hover.filter(|_| uv_set.is_none()).map(|hover| hover.set); // The wireframe and the highlight draw the mesh's own edges (displaced in the vertex shader too) let passes = [ - (low_poly_shown.then_some(mesh_pipeline), &geometry.vb, &geometry.ib, &geometry.draws, uv_set, 1), - (wire.filter(|_| low_poly_shown), &mesh.geo.vb, &mesh.lines, &mesh.line_draws, uv_set, 1), - (highlight, &mesh.geo.vb, &mesh.lines, &mesh.line_draws, highlight_set, HIGHLIGHT_SHIFTS), + (low_poly_shown.then_some(mesh_pipeline), &geometry.vb, &geometry.ib, &geometry.draws, uv_set, 1, hover_set), + (wire.filter(|_| low_poly_shown), &mesh.geo.vb, &mesh.lines, &mesh.line_draws, uv_set, 1, None), + (highlight, &mesh.geo.vb, &mesh.lines, &mesh.line_draws, highlight_set, HIGHLIGHT_SHIFTS, None), ]; - for (pipeline, vertices, indices, ranges, only_set, instances) in passes { + for (pipeline, vertices, indices, ranges, only_set, instances, hover_set) in passes { let Some(pipeline) = pipeline else { continue }; pass.set_pipeline(pipeline); pass.set_vertex_buffer(0, vertices.slice(..)); pass.set_index_buffer(indices.slice(..), wgpu::IndexFormat::Uint32); - for (range, set) in self.drawable(ranges, &frame.visible, only_set) { + for (range, texture_set, set) in self.drawable(ranges, &frame.visible, only_set) { pass.set_bind_group(1, &set.bind_group, &[]); - pass.draw_indexed(range, 0, 0..instances); + // The main mesh shader uses instance index 1 to tint the hovered draw in place. + let first_instance = u32::from(hover_set == Some(texture_set)); + pass.draw_indexed(range, 0, first_instance..first_instance + instances); } } @@ -2370,7 +2381,7 @@ impl Renderer { pass.set_pipeline(&self.uv_missing_seam_pipeline); pass.set_vertex_buffer(0, mesh.geo.vb.slice(..)); pass.set_index_buffer(seams.lines.slice(..), wgpu::IndexFormat::Uint32); - for (range, set) in self.drawable(&seams.line_draws, &frame.visible, Some(texture_set)) { + for (range, _, set) in self.drawable(&seams.line_draws, &frame.visible, Some(texture_set)) { pass.set_bind_group(1, &set.bind_group, &[]); pass.draw_indexed(range, 0, 0..1); } @@ -2404,7 +2415,7 @@ impl Renderer { pass.set_pipeline(&self.missing_seam_pipeline); pass.set_vertex_buffer(0, low_poly.geo.vb.slice(..)); pass.set_index_buffer(seams.lines.slice(..), wgpu::IndexFormat::Uint32); - for (range, set) in self.drawable(&seams.line_draws, &frame.visible, None) { + for (range, _, set) in self.drawable(&seams.line_draws, &frame.visible, None) { pass.set_bind_group(1, &set.bind_group, &[]); pass.draw_indexed(range, 0, 0..1); } @@ -2541,12 +2552,12 @@ fn upload_overlay(device: &wgpu::Device, queue: &wgpu::Queue, view: &mut ViewTar } } -/// Globals `highlight`: the highlight's color and opacity, all zero without one. Data views skip exposure and tone mapping, so they take the color as it is -fn highlight_globals(shade: &Shade, data: bool) -> [f32; 4] { - let Some(highlight) = shade.highlight else { return [0.0; 4] }; +/// The highlight's color and opacity. Data views take the color as it is. +fn highlight_globals(highlight: Option, opacity: f32, shade: &Shade, data: bool) -> [f32; 4] { + let Some(highlight) = highlight else { return [0.0; 4] }; let [r, g, b] = if data { highlight.color } else { highlight.scene_color(shade.exposure_ev, shade.aces) }; - [r, g, b, HIGHLIGHT_OPACITY] + [r, g, b, opacity] } /// The globals of bake mode's overlays and neutral material for `bake` (all zero without it) @@ -2678,8 +2689,8 @@ mod tests { let color = [0.8, 0.25, 0.08]; let shade = Shade { highlight: Some(SetHighlight { set: 0, color }), exposure_ev: 1.0, ..Shade::default() }; - assert_eq!(highlight_globals(&shade, true), [0.8, 0.25, 0.08, HIGHLIGHT_OPACITY]); - assert_ne!(highlight_globals(&shade, false)[..3], color, "the material view undoes exposure and tone mapping"); + assert_eq!(highlight_globals(shade.highlight, HIGHLIGHT_OPACITY, &shade, true), [0.8, 0.25, 0.08, HIGHLIGHT_OPACITY]); + assert_ne!(highlight_globals(shade.highlight, HIGHLIGHT_OPACITY, &shade, false)[..3], color, "the material view undoes exposure and tone mapping"); } #[test] diff --git a/crates/render/src/set_highlight.rs b/crates/render/src/set_highlight.rs index cc29879e..574b5bbd 100644 --- a/crates/render/src/set_highlight.rs +++ b/crates/render/src/set_highlight.rs @@ -1,4 +1,4 @@ -//! A texture set's mesh edges drawn over the 3D view in a highlight color (the set under a shelf drag) +//! Texture-set highlighting in the 3D viewport. /// Brightest on-screen value the tone curve can reach without an infinite scene value const MAX_ACES_SCREEN_VALUE: f32 = 0.99; @@ -6,16 +6,16 @@ const MAX_ACES_SCREEN_VALUE: f32 = 0.99; /// Scene scale the post pass applies before its ACES curve (`aces` in post.wgsl) const ACES_INPUT_SCALE: f32 = 0.6; -/// Draw texture set `set`'s mesh edges over the 3D view in `color` +/// A texture set to highlight in the 3D viewport. #[derive(Clone, Copy, Debug, PartialEq)] pub struct SetHighlight { pub set: usize, - /// The lines' color on screen, linear RGB + /// The highlight color on screen, linear RGB. pub color: [f32; 3], } impl SetHighlight { - /// The scene color the lines are drawn with so that exposure `exposure_ev` and the tone curve (ACES when `aces`) show them in [`Self::color`] + /// The scene color that exposure `exposure_ev` and the tone curve (ACES when `aces`) show as [`Self::color`]. pub fn scene_color(&self, exposure_ev: f32, aces: bool) -> [f32; 3] { scene_color_showing(self.color, exposure_ev, aces) } diff --git a/crates/render/src/shaders/mesh.wgsl b/crates/render/src/shaders/mesh.wgsl index 31fd19df..a98e58f7 100644 --- a/crates/render/src/shaders/mesh.wgsl +++ b/crates/render/src/shaders/mesh.wgsl @@ -26,6 +26,7 @@ struct Globals { refr: vec4, // x screen-space refraction on (1), y lods of the scene behind, z refraction depth (world units), w - highlight: vec4, // rgb highlighted set's edge color in the scene (before exposure and tone mapping), a opacity highlight_shift: vec4, // xy sideways shift of each highlight line copy (clip units), zw - + set_hover: vec4, // rgb hovered set's scene color, a opacity bake_high: vec4, // bake mode: rgb high poly color, a - bake_matching_error: vec4, // rgb color where the bake's rays miss the high poly, a its strength (0 hidden) bake_cage: vec4, // rgb cage surface color, a opacity @@ -70,6 +71,7 @@ struct VOut { @location(1) nrm: vec3, @location(2) tan: vec4, @location(3) uv: vec2, + @location(4) @interpolate(flat) hover: u32, }; // The vertex moved along its normal by the height channel (displacement). @@ -81,7 +83,7 @@ fn displaced(v: VIn) -> vec3 { } @vertex -fn vs_mesh(v: VIn) -> VOut { +fn vs_mesh(v: VIn, @builtin(instance_index) instance: u32) -> VOut { var o: VOut; let p = displaced(v); o.clip = g.view_proj * vec4(p, 1.0); @@ -89,6 +91,7 @@ fn vs_mesh(v: VIn) -> VOut { o.nrm = v.nrm; o.tan = v.tan; o.uv = v.uv; + o.hover = instance; return o; } @@ -359,7 +362,11 @@ fn mesh_color(i: VOut, front: bool) -> vec4 { @fragment fn fs_mesh(i: VOut, @builtin(front_facing) front: bool) -> @location(0) vec4 { - return mesh_color(i, front); + let color = mesh_color(i, front); + if (i.hover == 1u) { + return vec4(mix(color.rgb, g.set_hover.rgb * color.a, g.set_hover.a), color.a); + } + return color; } // The scene behind transmissive surfaces: everything but them (what they refract). diff --git a/crates/ui-egui/src/bake_mode/mod.rs b/crates/ui-egui/src/bake_mode/mod.rs index 2ae7f095..d3ad244d 100644 --- a/crates/ui-egui/src/bake_mode/mod.rs +++ b/crates/ui-egui/src/bake_mode/mod.rs @@ -42,7 +42,7 @@ pub fn texture_sets(app: &mut TextureCraftApp, ui: &mut Ui) { .collect(); ui.push_id(set_index, |ui| { - ui.horizontal(|ui| { + let row = ui.horizontal(|ui| { let button_size = ui.spacing().interact_size.y; if icon_button(ui, if visible { icon::EYE } else { icon::EYE_SLASH }, tr("Show or hide the texture set"), false, button_size).clicked() { app.run("textureSet.visible", json!({ "ts": set_index })); @@ -68,6 +68,9 @@ pub fn texture_sets(app: &mut TextureCraftApp, ui: &mut Ui) { app.start_resize(set_index, selected); } }); + if ui.is_enabled() && ui.rect_contains_pointer(row.response.rect) { + app.next_hovered_texture_set = Some((app.session.project_id, set_index)); + } if !tiles.is_empty() { egui::CollapsingHeader::new(format!("{} ({})", tr("UV tiles (UDIM)"), tiles.len())).default_open(true).show(ui, |ui| { diff --git a/crates/ui-egui/src/dock.rs b/crates/ui-egui/src/dock.rs index 7f8cebfe..7ae97179 100644 --- a/crates/ui-egui/src/dock.rs +++ b/crates/ui-egui/src/dock.rs @@ -85,6 +85,7 @@ pub(crate) fn texture_set_list(app: &mut TextureCraftApp, ui: &mut Ui) { egui::ScrollArea::vertical().id_salt("ts_scroll").show(ui, |ui| { for (i, (name, size, visible)) in sets.into_iter().enumerate() { let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width(), 22.0), Sense::click()); + resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::ListItem, ui.is_enabled(), i == active, &name)); let p = ui.painter(); let bg = if i == active { tk.row_selected @@ -99,6 +100,9 @@ pub(crate) fn texture_set_list(app: &mut TextureCraftApp, ui: &mut Ui) { } let eye = Rect::from_min_size(rect.min + vec2(4.0, 3.0), vec2(16.0, 16.0)); let eye_resp = ui.interact(eye, ui.scope_id().with(("ts_eye", i)), Sense::click()).on_hover_text(tr("Show or hide the texture set")); + if ui.is_enabled() && (resp.contains_pointer() || eye_resp.contains_pointer()) { + app.next_hovered_texture_set = Some((app.session.project_id, i)); + } p.text(eye.center(), egui::Align2::CENTER_CENTER, if visible { ic::EYE } else { ic::EYE_SLASH }, egui::FontId::proportional(13.0), tk.text); // Narrow panels drop the shader label, then the resolution, before cutting the name diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 1a8508eb..3873a915 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -223,6 +223,10 @@ pub struct TextureCraftApp { pub layer_panel: layer_panel::LayerPanel, /// Last frame's viewport rect (logical points), for scripted input. pub viewport_rect: egui::Rect, + /// Project and texture-set index from the last completed UI pass. + hovered_texture_set: Option<(u64, usize)>, + /// The set under the pointer in this pass, independent of dock drawing order. + next_hovered_texture_set: Option<(u64, usize)>, pub fps: f32, /// User smart materials found in the library folder (refreshed on demand). pub library: Option>, @@ -339,6 +343,8 @@ impl TextureCraftApp { thumbs: thumbs::Thumbs::default(), layer_panel: layer_panel::LayerPanel::default(), viewport_rect: egui::Rect::NOTHING, + hovered_texture_set: None, + next_hovered_texture_set: None, fps: 0.0, ui_ms: 0.0, profiler: profile::Profiler::default(), @@ -1078,6 +1084,11 @@ impl eframe::App for TextureCraftApp { // The dock keeps the cards' gutter from the bars and rails around it let panel_frame = if hide_ui { egui::Frame::NONE.fill(tokens.viewport) } else { egui::Frame::NONE.fill(tokens.chrome).inner_margin(theme::GUTTER) }; egui::CentralPanel::default().frame(panel_frame).show(ui, |ui| if hide_ui { viewport::central(self, ui) } else { docking::show(self, ui) }); + let hovered = self.next_hovered_texture_set.take(); + if self.hovered_texture_set != hovered { + self.hovered_texture_set = hovered; + ctx.request_repaint(); + } windows::show(self, &ctx); // Finish after the dock and windows have requested their thumbnails diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index 97edafed..5efee825 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -372,9 +372,12 @@ fn shade(app: &TextureCraftApp) -> Shade { /// The texture set a shelf material or image dragged over the 3D view `response` would drop on, its edges drawn in the accent color fn drop_highlight(app: &TextureCraftApp, ui: &Ui, response: &egui::Response) -> Option { let set = crate::shelf::drop_set(app, ui, response)?; - let accent = egui::Rgba::from(Tokens::get(ui.ctx()).viewport_accent); + Some(set_highlight(ui, set)) +} - Some(SetHighlight { set, color: [accent.r(), accent.g(), accent.b()] }) +fn set_highlight(ui: &Ui, set: usize) -> SetHighlight { + let accent = egui::Rgba::from(Tokens::get(ui.ctx()).viewport_accent); + SetHighlight { set, color: [accent.r(), accent.g(), accent.b()] } } /// Show what the GPU side reported (each message once while its toast is up). @@ -1010,12 +1013,16 @@ fn view_3d(app: &mut TextureCraftApp, ui: &mut Ui, rect: Rect, sync: &mut Pendin } } + let set_hover = app + .hovered_texture_set + .filter(|&(project_id, _)| project_id == app.session.project_id && !app.ui.hide_ui && !crate::docking::input_blocked(app, ui.ctx())) + .map(|(_, set)| set_highlight(ui, set)); let frame = Frame { view_id: crate::viewport::VIEW_3D, size: frame_size_in_pixels(ui, rect), kind: ViewKind::Scene, camera: app.session.camera, - shade: Shade { highlight: drop_highlight(app, ui, &response), ..shade(app) }, + shade: Shade { highlight: drop_highlight(app, ui, &response), set_hover, ..shade(app) }, visible: app.session.doc.project.texture_sets.iter().map(|texture_set| texture_set.visible).collect(), overlay: crate::symmetry::scene_overlay(app, ui, rect), }; From f5e5932539824064b55aa4314cb6d77e27ffcbf7 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 22 Nov 2026 21:19:56 +0100 Subject: [PATCH 2/6] Scope export scroll check to the dialog buttons --- crates/ui-egui/src/windows.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/windows.rs b/crates/ui-egui/src/windows.rs index 0624e1be..ebc2cd76 100644 --- a/crates/ui-egui/src/windows.rs +++ b/crates/ui-egui/src/windows.rs @@ -670,7 +670,7 @@ mod tests { harness.state_mut().ui.windows.export = true; harness.run_steps(5); let footer = harness.get_by_role_and_label(egui::Role::Button, "Reset all").rect(); - let first = harness.get_by_label("Export set 1"); + let first = harness.get_by_role_and_label(egui::Role::Button, "Export set 1"); first.hover(); harness.run_steps(2); @@ -682,7 +682,7 @@ mod tests { source: egui::MouseWheelSource::Wheel, }); harness.run_steps(15); - let last = harness.get_by_label("Export set 39"); + let last = harness.get_by_role_and_label(egui::Role::Button, "Export set 39"); last.click(); harness.run_steps(4); From dd00c4e04d41cfee638ab0ad0268ca5ac74851b2 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 22 Nov 2026 23:22:02 +0100 Subject: [PATCH 3/6] Blend the hover tint with the blurred scattering light and fix the export tree test query --- crates/render/src/shaders/mesh.wgsl | 5 +++++ crates/render/tests/headless.rs | 25 ++++++++++++++++++++++ crates/ui-egui/tests/it/export_progress.rs | 2 +- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/crates/render/src/shaders/mesh.wgsl b/crates/render/src/shaders/mesh.wgsl index a98e58f7..f780c4fc 100644 --- a/crates/render/src/shaders/mesh.wgsl +++ b/crates/render/src/shaders/mesh.wgsl @@ -388,6 +388,11 @@ fn fs_gbuf(i: VOut, @builtin(front_facing) front: bool) -> @location(0) vec4(0.0); if (g.scatter.w > 0.5) { sss = shade_mesh(i, front).sss; + if (i.hover == 1u) { + // fs_mesh mixes the hover tint into the color without this light; the blur pass adds it + // back unblended, so scale it by the share of the color that stays + sss = sss * (1.0 - g.set_hover.a); + } } else if (textureSample(t_base, samp, i.uv).a < hash12(i.clip.xy)) { discard; } diff --git a/crates/render/tests/headless.rs b/crates/render/tests/headless.rs index e4ddad39..dcaeefb2 100644 --- a/crates/render/tests/headless.rs +++ b/crates/render/tests/headless.rs @@ -1072,6 +1072,31 @@ fn subsurface_blur_spreads_only_scattering_light() { assert!((m1 - m0).abs() < 2.0, "the blur moves light, it adds none: {m0} -> {m1}"); } +#[test] +fn the_hover_tint_covers_the_blurred_scattering_light_too() { + let Some((mut h, frame)) = setup() else { return }; + let lit = shaded(&frame, |s| s.light = LIGHT); + let mask = mesh_mask(&mut h, &lit, &[0]); + fill(&mut h, &[0, 1], DisplayKind::BaseColor, &[150, 150, 150, 255]); + fill(&mut h, &[0, 1], DisplayKind::Emissive, &[0, 0, 0, 255]); + let blur = shaded(&lit, |s| s.sss_blur = Some([0.06, 0.024, 0.015])); + let hover = |f: &Frame| shaded(f, |s| s.set_hover = Some(texturecraft_render::SetHighlight { set: 0, color: [1.0, 0.5, 0.0] })); + let mean = |img: &Img| region_mean(&mask, |x, y| img.luma(x, y)); + let plain = mean(&render(&mut h, &lit, "hover_sss_plain")); + let blurred = mean(&render(&mut h, &blur, "hover_sss_blur")); + let hovered_plain = mean(&render(&mut h, &hover(&lit), "hover_sss_plain_hovered")); + let hovered_blur = mean(&render(&mut h, &hover(&blur), "hover_sss_blur_hovered")); + assert!(hovered_plain != plain, "the hover tint changes the set"); + // The blur only moves light around, so it must change a hovered set as little as a plain one + // (the scattering light restored by the blur pass is blended with the tint as well) + assert!( + ((hovered_blur - hovered_plain) - (blurred - plain)).abs() < 3.0, + "the blur changes the plain set by {} and the hovered set by {}", + blurred - plain, + hovered_blur - hovered_plain + ); +} + /// An environment that is bright only around `dir` (world space, rotation 0). fn spot_environment(dir: texturecraft_geom::glam::Vec3) -> ImageRes { let (w, hh) = (256u32, 128u32); diff --git a/crates/ui-egui/tests/it/export_progress.rs b/crates/ui-egui/tests/it/export_progress.rs index 84015102..8f929ca7 100644 --- a/crates/ui-egui/tests/it/export_progress.rs +++ b/crates/ui-egui/tests/it/export_progress.rs @@ -124,7 +124,7 @@ fn the_list_is_a_tree_per_texture_set_with_the_console_beside_it() { h.state_mut().ui.export.tab = 2; h.run_steps(4); let set = h.state().session.doc.project.texture_sets[0].name.clone(); - assert!(h.query_by_label(&set).is_some()); + assert!(h.query_by_role_and_label(egui::Role::DisclosureTriangle, &set).is_some(), "the export tree has a node for the set (the dock list row carries the same name)"); assert!(h.query_all_by_label("256x256 (Texture Set size)").next().is_some()); assert!(h.query_all_by_label("png").next().is_some() && h.query_all_by_label("16 bits").next().is_some()); assert!(h.query_by_label("Include all").is_none() && h.query_by_label("Reset all").is_none()); From d49f75ecd9eb341c179c6c0bd81e7909cc49295b Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 22 Nov 2026 23:41:37 +0100 Subject: [PATCH 4/6] Pass the hovered set to the subsurface G-buffer pass --- crates/render/src/renderer.rs | 7 +++++-- crates/render/tests/headless.rs | 2 +- crates/ui-egui/tests/it/export_progress.rs | 5 ++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/crates/render/src/renderer.rs b/crates/render/src/renderer.rs index 8cd7d593..c91f2404 100644 --- a/crates/render/src/renderer.rs +++ b/crates/render/src/renderer.rs @@ -2222,9 +2222,12 @@ impl Renderer { let geo = self.geometry(m, frame); pass.set_vertex_buffer(0, geo.vb.slice(..)); pass.set_index_buffer(geo.ib.slice(..), wgpu::IndexFormat::Uint32); - for (range, _, set) in self.drawable(&geo.draws, &frame.visible, None) { + // As in the main pass, instance index 1 marks the hovered set (fs_gbuf scales its scattering light) + let hover_set = frame.shade.set_hover.filter(|_| frame.kind == ViewKind::Scene).map(|hover| hover.set); + for (range, texture_set, set) in self.drawable(&geo.draws, &frame.visible, None) { pass.set_bind_group(1, &set.bind_group, &[]); - pass.draw_indexed(range, 0, 0..1); + let first_instance = u32::from(hover_set == Some(texture_set)); + pass.draw_indexed(range, 0, first_instance..first_instance + 1); } } diff --git a/crates/render/tests/headless.rs b/crates/render/tests/headless.rs index dcaeefb2..781933f6 100644 --- a/crates/render/tests/headless.rs +++ b/crates/render/tests/headless.rs @@ -1090,7 +1090,7 @@ fn the_hover_tint_covers_the_blurred_scattering_light_too() { // The blur only moves light around, so it must change a hovered set as little as a plain one // (the scattering light restored by the blur pass is blended with the tint as well) assert!( - ((hovered_blur - hovered_plain) - (blurred - plain)).abs() < 3.0, + ((hovered_blur - hovered_plain) - (blurred - plain)).abs() < 1.0, "the blur changes the plain set by {} and the hovered set by {}", blurred - plain, hovered_blur - hovered_plain diff --git a/crates/ui-egui/tests/it/export_progress.rs b/crates/ui-egui/tests/it/export_progress.rs index 8f929ca7..afb0b320 100644 --- a/crates/ui-egui/tests/it/export_progress.rs +++ b/crates/ui-egui/tests/it/export_progress.rs @@ -124,7 +124,10 @@ fn the_list_is_a_tree_per_texture_set_with_the_console_beside_it() { h.state_mut().ui.export.tab = 2; h.run_steps(4); let set = h.state().session.doc.project.texture_sets[0].name.clone(); - assert!(h.query_by_role_and_label(egui::Role::DisclosureTriangle, &set).is_some(), "the export tree has a node for the set (the dock list row carries the same name)"); + assert!( + h.query_by_role_and_label(egui::Role::DisclosureTriangle, &set).is_some(), + "the export tree has a node for the set (the dock list row carries the same name)" + ); assert!(h.query_all_by_label("256x256 (Texture Set size)").next().is_some()); assert!(h.query_all_by_label("png").next().is_some() && h.query_all_by_label("16 bits").next().is_some()); assert!(h.query_by_label("Include all").is_none() && h.query_by_label("Reset all").is_none()); From b511cad705578c1624cf22371b4e273e3eac3522 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 22 Nov 2026 23:50:47 +0100 Subject: [PATCH 5/6] Dim the hovered set's scattering light at the hovered pixel, not before the blur --- crates/render/src/renderer.rs | 4 ++-- crates/render/src/shaders/fx.wgsl | 29 ++++++++++++++++++----------- crates/render/src/shaders/mesh.wgsl | 9 ++++----- crates/render/src/shaders/post.wgsl | 2 +- crates/render/tests/headless.rs | 17 ++++++++++++++--- 5 files changed, 39 insertions(+), 22 deletions(-) diff --git a/crates/render/src/renderer.rs b/crates/render/src/renderer.rs index c91f2404..1e6d965f 100644 --- a/crates/render/src/renderer.rs +++ b/crates/render/src/renderer.rs @@ -2143,7 +2143,7 @@ impl Renderer { b: [fx(s.vignette), fx(s.sharpen), fx(s.grain), BLOOM_THRESHOLD], c: dof_c, d: sss_d, - e: [flag(cam.ortho), sample as f32, 0.0, 0.0], + e: [flag(cam.ortho), sample as f32, raw.set_hover[3], 0.0], back_top: [s.background[0][0], s.background[0][1], s.background[0][2], 0.0], back_bottom: [s.background[1][0], s.background[1][1], s.background[1][2], 0.0], }; @@ -2222,7 +2222,7 @@ impl Renderer { let geo = self.geometry(m, frame); pass.set_vertex_buffer(0, geo.vb.slice(..)); pass.set_index_buffer(geo.ib.slice(..), wgpu::IndexFormat::Uint32); - // As in the main pass, instance index 1 marks the hovered set (fs_gbuf scales its scattering light) + // As in the main pass, instance index 1 marks the hovered set (fs_gbuf negates its depths for the blur pass) let hover_set = frame.shade.set_hover.filter(|_| frame.kind == ViewKind::Scene).map(|hover| hover.set); for (range, texture_set, set) in self.drawable(&geo.draws, &frame.visible, None) { pass.set_bind_group(1, &set.bind_group, &[]); diff --git a/crates/render/src/shaders/fx.wgsl b/crates/render/src/shaders/fx.wgsl index 333664ce..972de246 100644 --- a/crates/render/src/shaders/fx.wgsl +++ b/crates/render/src/shaders/fx.wgsl @@ -1,7 +1,8 @@ // Scene effects between the scene pass and the post pass, appended to util.wgsl and post.wgsl // (uses `src`, `src_samp`, `post`, `fx_a`): the screen-space subsurface blur and depth of field. // Both read the G-buffer of `fs_gbuf` (mesh.wgsl): rgb the scattering part of the diffuse light, -// a the view depth (0: background). Colors are linear HDR premultiplied by scene coverage. +// a the view depth (0: background; negative where the hovered set shows when the subsurface blur +// is on: read depths with abs). Colors are linear HDR premultiplied by scene coverage. @group(0) @binding(5) var fx_b: texture_2d; @group(0) @binding(6) var fx_c: texture_2d; @@ -31,12 +32,14 @@ fn px_per_world(z: f32) -> f32 { return select(post.d.w / max(z, 1e-4), post.d.w, post.e.x > 0.5); } -// One direction of the blur over `src` (rgb light, a depth) at pixel `p` (same-size target). +// One direction of the blur over `src` (rgb light, a depth) at pixel `p` (same-size target). The +// result keeps the centre's depth, sign included (negative: the hovered set). fn sss_blur(p: vec2, dir: vec2) -> vec4 { let c = texel_at(src, p); - let sigma = post.d.xyz * px_per_world(c.a); + let z = abs(c.a); + let sigma = post.d.xyz * px_per_world(z); let smax = max(sigma.r, max(sigma.g, sigma.b)); - if (c.a <= 0.0 || smax < 0.5) { + if (z <= 0.0 || smax < 0.5) { return c; } let stride = dir * (smax * 3.0 / f32(SSS_TAPS)); @@ -55,7 +58,8 @@ fn sss_blur(p: vec2, dir: vec2) -> vec4 { let o = f32(k) + jitter; let t = texel_at(src, p + stride * o); let x = o * smax * 3.0 / f32(SSS_TAPS); - let wd = select(0.0, max(1.0 - abs(t.a - c.a) / depth_range, 0.0), t.a > 0.0); + let tz = abs(t.a); + let wd = select(0.0, max(1.0 - abs(tz - z) / depth_range, 0.0), tz > 0.0); let w = exp(-x * x * inv); sum += mix(c.rgb, t.rgb, wd) * w; wsum += w; @@ -70,12 +74,15 @@ fn fs_sss_h(i: FsOut) -> @location(0) vec4 { } // src: the horizontal pass, fx_a: the scene (without its scattering light, see `fs_mesh`). Adds -// the blurred scattering light. +// the blurred scattering light. On the hovered set (negative depth) the light is scaled like the +// rest of the color the hover tint mixes in (post.e.z: the tint's opacity), so a neighbouring set +// keeps the light it receives. @fragment fn fs_sss_v(i: FsOut) -> @location(0) vec4 { - let blurred = sss_blur(i.pos.xy, vec2(0.0, 1.0)).rgb; + let blurred = sss_blur(i.pos.xy, vec2(0.0, 1.0)); let scene = texel_at(fx_a, i.pos.xy); - return scene + vec4(blurred * clamp(scene.a, 0.0, 1.0), 0.0); + let keep = select(1.0, 1.0 - post.e.z, blurred.a < 0.0); + return scene + vec4(blurred.rgb * keep * clamp(scene.a, 0.0, 1.0), 0.0); } // ---- Depth of field: a thin lens. Scatter-as-gather over a golden-angle spiral at half @@ -107,7 +114,7 @@ struct DofOut { @fragment fn fs_dof_gather(i: FsOut) -> DofOut { let px = 1.0 / vec2(textureDimensions(src)); - let zc = texel(fx_a, i.uv).a; + let zc = abs(texel(fx_a, i.uv).a); let cc = abs(coc(zc)); let rmax = post.c.z; // Half the spacing of the taps: the soft edge of each tap's reach. @@ -123,7 +130,7 @@ fn fs_dof_gather(i: FsOut) -> DofOut { let r = rmax * sqrt((f32(k) + 0.5) / f32(DOF_TAPS)); let a = f32(k) * GOLDEN_ANGLE + turn; let uv = i.uv + vec2(cos(a), sin(a)) * (r * px); - let z = texel(fx_a, uv).a; + let z = abs(texel(fx_a, uv).a); let nearer = z > 0.0 && (zc <= 0.0 || z < zc); var size = abs(coc(z)); if (!nearer) { @@ -145,7 +152,7 @@ const PI_FX: f32 = 3.14159265; @fragment fn fs_dof(i: FsOut) -> @location(0) vec4 { let sharp = texel_at(src, i.pos.xy); - let f = max(smoothstep(0.5, 1.5, abs(coc(texel_at(fx_b, i.pos.xy).a))), textureSampleLevel(fx_c, src_samp, i.uv, 0.0).r); + let f = max(smoothstep(0.5, 1.5, abs(coc(abs(texel_at(fx_b, i.pos.xy).a)))), textureSampleLevel(fx_c, src_samp, i.uv, 0.0).r); // In focus with nothing blurred in front: the sharp pixel itself (no blend through the // half-resolution blur at all). if (f <= 0.0) { diff --git a/crates/render/src/shaders/mesh.wgsl b/crates/render/src/shaders/mesh.wgsl index f780c4fc..7d2f8bf3 100644 --- a/crates/render/src/shaders/mesh.wgsl +++ b/crates/render/src/shaders/mesh.wgsl @@ -381,17 +381,16 @@ fn fs_opaque(i: VOut, @builtin(front_facing) front: bool) -> @location(0) vec4 @location(0) vec4 { - let z = max(dot(i.wpos - g.eye.xyz, g.fwd.xyz), 1e-4); + var z = max(dot(i.wpos - g.eye.xyz, g.fwd.xyz), 1e-4); var sss = vec3(0.0); if (g.scatter.w > 0.5) { sss = shade_mesh(i, front).sss; if (i.hover == 1u) { - // fs_mesh mixes the hover tint into the color without this light; the blur pass adds it - // back unblended, so scale it by the share of the color that stays - sss = sss * (1.0 - g.set_hover.a); + z = -z; } } else if (textureSample(t_base, samp, i.uv).a < hash12(i.clip.xy)) { discard; diff --git a/crates/render/src/shaders/post.wgsl b/crates/render/src/shaders/post.wgsl index e5df0cd8..c5a5e00f 100644 --- a/crates/render/src/shaders/post.wgsl +++ b/crates/render/src/shaders/post.wgsl @@ -7,7 +7,7 @@ struct Post { b: vec4, // x vignette, y sharpen, z grain, w bloom threshold c: vec4, // depth of field (fx.wgsl): x circle of confusion at infinity (pixels), y focus depth, z largest circle radius (pixels), w accumulation sample d: vec4, // subsurface blur (fx.wgsl): xyz mean free path per channel (world units), w pixels per world unit at depth 1 - e: vec4, // x orthographic camera (1: pixels per world unit don't fall off with depth), y accumulation sample, zw - + e: vec4, // x orthographic camera (1: pixels per world unit don't fall off with depth), y accumulation sample, z hover tint opacity (the subsurface blur scales the hovered set's light), w - back_top: vec4, // rgb background color at the top of the view (linear), a - back_bottom: vec4, // rgb background color at the bottom of the view (linear), a - }; diff --git a/crates/render/tests/headless.rs b/crates/render/tests/headless.rs index 781933f6..8cd807e6 100644 --- a/crates/render/tests/headless.rs +++ b/crates/render/tests/headless.rs @@ -1077,15 +1077,17 @@ fn the_hover_tint_covers_the_blurred_scattering_light_too() { let Some((mut h, frame)) = setup() else { return }; let lit = shaded(&frame, |s| s.light = LIGHT); let mask = mesh_mask(&mut h, &lit, &[0]); + let neighbour = mesh_mask(&mut h, &lit, &[1]); fill(&mut h, &[0, 1], DisplayKind::BaseColor, &[150, 150, 150, 255]); fill(&mut h, &[0, 1], DisplayKind::Emissive, &[0, 0, 0, 255]); let blur = shaded(&lit, |s| s.sss_blur = Some([0.06, 0.024, 0.015])); let hover = |f: &Frame| shaded(f, |s| s.set_hover = Some(texturecraft_render::SetHighlight { set: 0, color: [1.0, 0.5, 0.0] })); let mean = |img: &Img| region_mean(&mask, |x, y| img.luma(x, y)); - let plain = mean(&render(&mut h, &lit, "hover_sss_plain")); - let blurred = mean(&render(&mut h, &blur, "hover_sss_blur")); + let plain_img = render(&mut h, &lit, "hover_sss_plain"); + let blurred_img = render(&mut h, &blur, "hover_sss_blur"); + let hovered_blur_img = render(&mut h, &hover(&blur), "hover_sss_blur_hovered"); + let (plain, blurred, hovered_blur) = (mean(&plain_img), mean(&blurred_img), mean(&hovered_blur_img)); let hovered_plain = mean(&render(&mut h, &hover(&lit), "hover_sss_plain_hovered")); - let hovered_blur = mean(&render(&mut h, &hover(&blur), "hover_sss_blur_hovered")); assert!(hovered_plain != plain, "the hover tint changes the set"); // The blur only moves light around, so it must change a hovered set as little as a plain one // (the scattering light restored by the blur pass is blended with the tint as well) @@ -1095,6 +1097,15 @@ fn the_hover_tint_covers_the_blurred_scattering_light_too() { blurred - plain, hovered_blur - hovered_plain ); + // The set next to the hovered one keeps its light: the blur takes light across the boundary, and + // the hover must not dim what the neighbour receives from the hovered set + let other = |img: &Img| region_mean(&neighbour, |x, y| img.luma(x, y)); + assert!( + (other(&hovered_blur_img) - other(&blurred_img)).abs() < 1.0, + "hovering set 0 changes set 1 from {} to {}", + other(&blurred_img), + other(&hovered_blur_img) + ); } /// An environment that is bright only around `dir` (world space, rotation 0). From c3a52dbfd8b71ad7e6c203985b8b53238eab8f36 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 23 Nov 2026 00:00:32 +0100 Subject: [PATCH 6/6] Test that hovering a set leaves the set next to it unchanged --- crates/render/tests/headless.rs | 34 +++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/crates/render/tests/headless.rs b/crates/render/tests/headless.rs index 8cd807e6..d67afa41 100644 --- a/crates/render/tests/headless.rs +++ b/crates/render/tests/headless.rs @@ -1083,10 +1083,9 @@ fn the_hover_tint_covers_the_blurred_scattering_light_too() { let blur = shaded(&lit, |s| s.sss_blur = Some([0.06, 0.024, 0.015])); let hover = |f: &Frame| shaded(f, |s| s.set_hover = Some(texturecraft_render::SetHighlight { set: 0, color: [1.0, 0.5, 0.0] })); let mean = |img: &Img| region_mean(&mask, |x, y| img.luma(x, y)); - let plain_img = render(&mut h, &lit, "hover_sss_plain"); - let blurred_img = render(&mut h, &blur, "hover_sss_blur"); - let hovered_blur_img = render(&mut h, &hover(&blur), "hover_sss_blur_hovered"); - let (plain, blurred, hovered_blur) = (mean(&plain_img), mean(&blurred_img), mean(&hovered_blur_img)); + let plain = mean(&render(&mut h, &lit, "hover_sss_plain")); + let blurred = mean(&render(&mut h, &blur, "hover_sss_blur")); + let hovered_blur = mean(&render(&mut h, &hover(&blur), "hover_sss_blur_hovered")); let hovered_plain = mean(&render(&mut h, &hover(&lit), "hover_sss_plain_hovered")); assert!(hovered_plain != plain, "the hover tint changes the set"); // The blur only moves light around, so it must change a hovered set as little as a plain one @@ -1098,14 +1097,25 @@ fn the_hover_tint_covers_the_blurred_scattering_light_too() { hovered_blur - hovered_plain ); // The set next to the hovered one keeps its light: the blur takes light across the boundary, and - // the hover must not dim what the neighbour receives from the hovered set - let other = |img: &Img| region_mean(&neighbour, |x, y| img.luma(x, y)); - assert!( - (other(&hovered_blur_img) - other(&blurred_img)).abs() < 1.0, - "hovering set 0 changes set 1 from {} to {}", - other(&blurred_img), - other(&hovered_blur_img) - ); + // the hover must not dim what the neighbour receives from the hovered set. Only the pixels of + // set 1 within the blur's reach of set 0 can differ. + let wide = shaded(&lit, |s| s.sss_blur = Some([0.2, 0.08, 0.05])); + let blurred_img = render(&mut h, &wide, "hover_sss_wide"); + let hovered_blur_img = render(&mut h, &hover(&wide), "hover_sss_wide_hovered"); + const REACH: i32 = 25; + let (w, ht) = (480i32, 360i32); + let near = |i: usize| { + let (x, y) = (i as i32 % w, i as i32 / w); + (-REACH..=REACH) + .any(|dy| (-REACH..=REACH).any(|dx| (0..w).contains(&(x + dx)) && (0..ht).contains(&(y + dy)) && mask[((y + dy) * w + x + dx) as usize])) + }; + let diffs: Vec = (0..neighbour.len()) + .filter(|&i| neighbour[i] && !mask[i] && near(i)) + .map(|i| (hovered_blur_img.luma(i as u32 % 480, i as u32 / 480) - blurred_img.luma(i as u32 % 480, i as u32 / 480)).abs()) + .collect(); + let (count, max) = (diffs.iter().filter(|d| **d > 1.0).count(), diffs.iter().copied().fold(0.0, f32::max)); + assert!(!diffs.is_empty(), "set 1 touches set 0"); + assert!(max <= 1.0, "hovering set 0 changes {count} of {} pixels of set 1 next to it (max {max})", diffs.len()); } /// An environment that is bright only around `dir` (world space, rotation 0).