diff --git a/crates/gpu/src/view.rs b/crates/gpu/src/view.rs index 8169ae920..6ab6fa808 100644 --- a/crates/gpu/src/view.rs +++ b/crates/gpu/src/view.rs @@ -761,6 +761,15 @@ impl BlockBuilder { self.hidden.extend_from_slice(&line_record(a, b, c, width.to_bits())); } + /// A vertex marker: a square `size` pixels across at `p`, the same size at every zoom. It is a + /// line from `p` to itself (the line shader caps each end by half its width), lifted toward + /// the eye by half its size so a face through the vertex seen at a slant does not bury half of + /// it; `hidden` draws it only where the model hides it, as a hidden edge. + pub fn marker(&mut self, p: [f32; 3], c: PaletteIndex, size: f32, hidden: bool) { + let out = if hidden { &mut self.hidden } else { &mut self.lines }; + out.extend_from_slice(&line_record(p, p, c, lifted_width(size, [[0.0; 3]; 2]))); + } + /// How many vertices the block holds so far. pub fn vertices_len(&self) -> usize { self.vertices.len() / TRI_SIZE @@ -4541,6 +4550,63 @@ mod glass_tests { assert_eq!(*block.freeze().lines, expected); } + /// A vertex marker (#7834) is a lifted line from the vertex to itself, among the block's + /// lines or, hidden, among its hidden lines. + #[test] + fn a_marker_is_a_lifted_line_to_itself() { + let (p, c) = ([1.0f32, -2.0, 3.5], PaletteIndex(4)); + let mut block = BlockBuilder::default(); + block.marker(p, c, 4.0, false); + block.marker(p, c, 4.0, true); + let bytes = block.freeze(); + let mut sc = GpuScene::default(); + sc.lifted_line(p, p, c, 4.0, [[0.0; 3]; 2]); + assert_eq!(*bytes.lines, sc.lines); + assert_eq!(*bytes.hidden, sc.lines); + } + + /// A marker shows as a square of its size at its vertex, over a face through the vertex even + /// where the face rises toward the eye beside it (lifted, as a highlight band), and not + /// through a face in front of it; a hidden marker shows only there. Skipped without a GPU. + #[test] + fn a_marker_shows_over_a_slanted_face_and_hides_behind_one() { + let face = |sc: &mut GpuScene, slope: f32, z: f32, x0: f32| { + let grey = sc.color([128, 128, 128, 255]); + // A plane through (x0, 0, z), rising toward the eye (+z) along +x. + let at = |x: f32, y: f32| [x, y, z + slope * (x - x0)]; + let n = [-slope, 0.0, 1.0]; + for t in [[at(-9.0, -9.0), at(9.0, -9.0), at(9.0, 9.0)], [at(-9.0, -9.0), at(9.0, 9.0), at(-9.0, 9.0)]] { + for p in t { + sc.tri(p, n, grey); + } + } + }; + // The middle pixel's centre is 0.16 mm right of the view's centre (0.31 mm a pixel): a + // marker 0.3 mm left of it still covers that pixel, where the face stands 0.69 mm nearer + // the eye than the vertex. + let x0 = -0.3; + let marker = |hidden: bool| { + let mut b = BlockBuilder::default(); + b.marker([x0, 0.0, 0.0], PaletteIndex(0), 4.0, hidden); + SceneBlock { key: u64::from(hidden), bytes: b.freeze() } + }; + let scene = |slope: f32, z: f32, hidden: bool| { + let mut sc = GpuScene::with_palette(Palette::with_stable(vec![[255, 0, 0, 255]])); + face(&mut sc, slope, z, x0); + sc.blocks.push(marker(hidden)); + sc + }; + let red = [255, 0, 0, 255]; + let Some(slanted) = middle_pixel(scene(1.5, 0.0, false), false) else { + eprintln!("skipping: no GPU adapter"); + return; + }; + assert_eq!(slanted, red, "on a slanted face through the vertex"); + assert_ne!(middle_pixel(scene(0.0, 5.0, false), false), Some(red), "behind a face in front"); + assert_eq!(middle_pixel(scene(0.0, 5.0, true), false), Some(red), "a hidden marker behind a face in front"); + assert_ne!(middle_pixel(scene(1.5, 0.0, true), false), Some(red), "no hidden marker where it is seen"); + } + /// Without order-independent transparency, the sorted glass stream is kept while the blocks' /// see-through triangles stay the same: a colour edit (a new palette) uploads palette rows /// and no geometry; a class change rebuilds the stream. diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rs index 3f97777b6..462157207 100644 --- a/crates/ui-egui/src/prefs.rs +++ b/crates/ui-egui/src/prefs.rs @@ -861,6 +861,9 @@ preference_settings! { /// Draw reference geometry such as inactive sketches and datum planes through shaded bodies. /// Off by default to preserve the depth-tested viewport behaviour. reference_geometry_through_bodies: bool = false, PAGE_GRAPHICS => check_id("settings.reference_geometry_through_bodies"); + /// A small dot at every vertex wherever edges are drawn (#7834). Off by default: the viewport + /// draws edges only, as it always has. + vertex_markers: bool = false => check("Vertex markers", "Show a small dot at every vertex wherever edges are drawn"); /// How finely the viewport tessellates curved faces and edges: `coarse`, `normal`, `fine` or /// `ultra` (#1287). Only the display plan the viewport draws; never the design or exports. display_quality: String = "normal".into(), PAGE_GRAPHICS; diff --git a/crates/ui-egui/src/prefs/graphics.rs b/crates/ui-egui/src/prefs/graphics.rs index a421914b7..306bc1cf6 100644 --- a/crates/ui-egui/src/prefs/graphics.rs +++ b/crates/ui-egui/src/prefs/graphics.rs @@ -166,6 +166,7 @@ pub(super) fn show(app: &mut SolveApp, ui: &mut egui::Ui) { crate::prefs::view_angles_checkbox(app, left); setting::reference_geometry_through_bodies(app, left).on_hover_text(app.tr("settings.reference_geometry_through_bodies_help")); setting::sketch_halo(app, left); + setting::vertex_markers(app, left); } if let Some(right) = columns.get_mut(1) { ui_kit::checkbox(right, crate::i18n::tr_label("Ground shadow"), &mut app.ui.ground_shadow); diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index 4a543bda5..5428cdbb9 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -542,6 +542,7 @@ fn scene_key(app: &SolveApp) -> u64 { app.session.colored_components.hash(&mut h); (std::sync::Arc::as_ptr(&crate::open_meshing::shown_world(app)) as usize).hash(&mut h); app.ui.visual_style.hash(&mut h); + app.preferences.vertex_markers.hash(&mut h); app.ui.ground_shadow.hash(&mut h); solidcraft_engine::doc::display_quality::current().hash(&mut h); lod_generation(app).hash(&mut h); @@ -1074,15 +1075,15 @@ struct BodyDraw { impl BodyDraw { /// Everything [`BodyDraw::build`] reads. Colours, opacity and classes are deliberately left /// out: changing them must not change the block's vertices. - fn key(&self, shaded: bool, edges: bool, hidden_edges: bool) -> u64 { + fn key(&self, shaded: bool, edges: bool, hidden_edges: bool, markers: bool) -> u64 { let mut h = std::collections::hash_map::DefaultHasher::new(); (std::sync::Arc::as_ptr(&self.mesh) as usize).hash(&mut h); self.place.iter().flatten().flatten().for_each(|v| v.to_bits().hash(&mut h)); - (self.faded, self.on_gpu, shaded, edges, hidden_edges, self.slot_base, self.edge.0, self.hidden_edge.0).hash(&mut h); + (self.faded, self.on_gpu, shaded, edges, hidden_edges, markers, self.slot_base, self.edge.0, self.hidden_edge.0).hash(&mut h); h.finish() } - fn build(&self, out: &mut crate::gpu::BlockBuilder, shaded: bool, edges: bool, hidden_edges: bool) { + fn build(&self, out: &mut crate::gpu::BlockBuilder, shaded: bool, edges: bool, hidden_edges: bool, markers: bool) { let m = &self.mesh; let color = |ti: usize| m.tri_face.get(ti).and_then(|f| self.face_colors.get(*f as usize)).copied().unwrap_or(self.body_color); let class = |ti: usize| { @@ -1116,9 +1117,43 @@ impl BodyDraw { } } } + // Vertex markers follow the edges: drawn where they are, faint behind the model where + // hidden edges are, and not in plain shaded (#7834). + if edges && markers { + for p in vertex_markers(m) { + let p = at(p); + out.marker(p, self.edge, VERTEX_MARKER_PX, false); + if hidden_edges { + out.marker(p, self.hidden_edge, VERTEX_MARKER_PX, true); + } + } + } } } +/// How large a vertex marker is on the screen, in pixels: a small dot next to the 1.3 px edges, +/// well inside the hover ring drawn over a vertex that can be picked. +const VERTEX_MARKER_PX: f32 = 4.0; + +/// Where `mesh`'s vertices are: the ends of the edges a vertex can be picked from, each once. A +/// mesh body's outlines (no B-rep edges) have none; ends of tangent edges the shaded view leaves +/// out are kept, as they can still be picked. +fn vertex_markers(mesh: &solidcraft_engine::geom::Mesh) -> Vec { + let mut seen = std::collections::HashSet::new(); + let mut out = Vec::new(); + for (ei, e) in mesh.edges.iter().enumerate() { + if !mesh.edge_pickable(ei) { + continue; + } + for p in [e.first(), e.last()].into_iter().flatten() { + if [p.x, p.y, p.z].iter().all(|v| v.is_finite()) && seen.insert([p.x, p.y, p.z].map(|v| (v + 0.0).to_bits())) { + out.push(*p); + } + } + } + out +} + /// Add a block of bodies to the scene: the bytes built for the same bodies last time when /// there were any, else built now. fn push_block( @@ -1352,6 +1387,8 @@ pub(crate) fn build_scene(app: &SolveApp) -> GpuScene { let edge_col = if shaded { c4(tk.body_edge) } else { c4(tk.text) }; // Shaded with hidden edges: edges behind the model show faintly. let hidden_edges = app.ui.visual_style == 3; + // Vertex markers are an option (#7834), off by default. + let markers = app.preferences.vertex_markers; let hidden_col = c4(tk.text.gamma_multiply(0.35)); let shown: Vec<&solidcraft_engine::doc::ModelBody> = st.bodies.iter().filter(|b| app.session.body_visible(&b.name) && !app.preview.effective_replaced().contains(&b.name)).collect(); @@ -1500,7 +1537,7 @@ pub(crate) fn build_scene(app: &SolveApp) -> GpuScene { hidden_edge: slots.hidden_edge, }, }; - let key = body.key(shaded, edges, hidden_edges); + let key = body.key(shaded, edges, hidden_edges, markers); key.hash(&mut run_key); run.push(body); // Blocks end where a body's own key says, so a body added or removed changes only its @@ -1508,11 +1545,11 @@ pub(crate) fn build_scene(app: &SolveApp) -> GpuScene { if key.is_multiple_of(16) || run.len() >= 64 { let draws = std::mem::take(&mut run); let block_key = std::mem::replace(&mut run_key, std::collections::hash_map::DefaultHasher::new()).finish(); - push_block(&mut sc, &mut kept, &mut made, block_key, draws, shaded, |d, out| d.build(out, shaded, edges, hidden_edges)); + push_block(&mut sc, &mut kept, &mut made, block_key, draws, shaded, |d, out| d.build(out, shaded, edges, hidden_edges, markers)); } } if !run.is_empty() { - push_block(&mut sc, &mut kept, &mut made, run_key.finish(), run, shaded, |d, out| d.build(out, shaded, edges, hidden_edges)); + push_block(&mut sc, &mut kept, &mut made, run_key.finish(), run, shaded, |d, out| d.build(out, shaded, edges, hidden_edges, markers)); } // Where each CPU-built body's triangles are in the model's blocks, for whole-body // highlights (see `highlight_block_tris`). @@ -7207,7 +7244,9 @@ mod tests { .filter(|(i, _)| one.edge_pickable(*i) && one.shaded_edge_visible(*i)) .map(|(_, e)| e.len().saturating_sub(1)) .sum(); - assert_eq!(merged.lines.len(), 3 * segments * crate::gpu::LINE_SIZE); + // Vertex markers are lines from a point to itself; count the edges' segments. + let edge_lines = merged.lines.as_chunks::<{ crate::gpu::LINE_SIZE }>().0.iter().filter(|r| r[..12] != r[12..24]).count(); + assert_eq!(edge_lines, 3 * segments); } #[test] @@ -8827,14 +8866,16 @@ mod tests { assert_eq!(mesh.smooth_edges.iter().filter(|f| **f).count(), 512); let smooth = mesh.smooth_edges.iter().position(|f| *f).unwrap(); let expected = mesh.edges.iter().enumerate().filter(|(i, _)| mesh.shaded_edge_visible(*i)).map(|(_, e)| e.len() - 1).sum::(); + // Vertex markers are lines from a point to itself; count the edges' segments. + let edge_lines = |bytes: &[u8]| bytes.as_chunks::<{ crate::gpu::LINE_SIZE }>().0.iter().filter(|r| r[..12] != r[12..24]).count(); let shaded = build_scene(&app).merged(); - assert_eq!(shaded.lines.len() / crate::gpu::LINE_SIZE, expected); + assert_eq!(edge_lines(&shaded.lines), expected); assert!(!shaded.tris.is_empty()); app.ui.visual_style = 2; let wire = build_scene(&app).merged(); let expected_wire = mesh.edges.iter().enumerate().filter(|(i, _)| !mesh.seams.get(*i).copied().unwrap_or(false)).map(|(_, e)| e.len() - 1).sum::(); - assert_eq!(wire.lines.len() / crate::gpu::LINE_SIZE, expected_wire); + assert_eq!(edge_lines(&wire.lines), expected_wire); assert!(expected_wire > expected); assert!(wire.tris.is_empty()); app.ui.visual_style = 0; diff --git a/crates/ui-egui/src/viewport_appearance_tests.rs b/crates/ui-egui/src/viewport_appearance_tests.rs index aa011d424..133e2319e 100644 --- a/crates/ui-egui/src/viewport_appearance_tests.rs +++ b/crates/ui-egui/src/viewport_appearance_tests.rs @@ -510,3 +510,40 @@ fn conflicting_curves_points_glyphs_and_dimensions_take_the_conflict_colour_unti h.call("ui.prefs", json!({"theme": "dark"})); h.frame(); } + +/// Vertex markers follow the edges (#7834): a box's eight corners are marked once each in the +/// edge colour wherever edges are drawn (shaded with edges, wireframe, shaded with hidden +/// edges), faintly behind the model where hidden edges are, and not at all in plain shaded. +#[test] +fn vertex_markers_follow_the_edges_display_mode() { + let mut app = SolveApp::new(Session::default(), Services::default()); + app.ui.ground_shadow = false; + app.set_origin_shown(false); + app.run("solid.box", json!({"length": 20, "width": 15, "height": 10})).unwrap(); + // Off by default: no marker in any style. + assert!(!app.preferences.vertex_markers, "vertex markers are off by default"); + for style in 0..4 { + app.ui.visual_style = style; + let scene = build_scene(&app).merged(); + let dots = |bytes: &[u8]| bytes.as_chunks::<{ crate::gpu::LINE_SIZE }>().0.iter().filter(|r| r[..12] == r[12..24]).count(); + assert_eq!((dots(&scene.lines), dots(&scene.hidden)), (0, 0), "style {style}: no markers by default"); + } + app.preferences.vertex_markers = true; + // A marker is a lifted line from a point to itself. + let markers = |scene: &crate::gpu::GpuScene, bytes: &[u8]| -> Vec<([u8; 12], [u8; 4])> { + let (records, rest) = bytes.as_chunks::<{ crate::gpu::LINE_SIZE }>(); + assert!(rest.is_empty()); + records.iter().filter(|r| r[..12] == r[12..24] && r[31] & 0x80 != 0).map(|r| (r[..12].try_into().unwrap(), line_color(scene, r))).collect() + }; + for (style, drawn, hidden) in [(0, true, false), (1, false, false), (2, true, false), (3, true, true)] { + app.ui.visual_style = style; + let scene = build_scene(&app).merged(); + let (front, back) = (markers(&scene, &scene.lines), markers(&scene, &scene.hidden)); + let edge = scene.lines.as_chunks::<{ crate::gpu::LINE_SIZE }>().0.iter().find(|r| r[..12] != r[12..24]).map(|r| line_color(&scene, r)); + assert_eq!(front.len(), if drawn { 8 } else { 0 }, "style {style}: one marker per corner"); + assert_eq!(back.len(), if hidden { 8 } else { 0 }, "style {style}: hidden markers only with hidden edges"); + let corners: std::collections::HashSet<_> = front.iter().map(|(p, _)| *p).collect(); + assert_eq!(corners.len(), front.len(), "style {style}: each corner once"); + assert!(front.iter().all(|(_, c)| Some(*c) == edge), "style {style}: markers in the edge colour"); + } +}