SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Section Analysis: edges behind the cut no longer show through the cap (#6328) (#6363)
bherbruck2741cd9a8b0b
diff --git a/crates/gpu/src/view.rs b/crates/gpu/src/view.rsindex 966d29c03c..d08ca79aa1 100644--- a/crates/gpu/src/view.rs+++ b/crates/gpu/src/view.rs@@ -1168,23 +1168,24 @@ pub struct Renderer {/// `tri` and `tri_cut` for planned bodies (positions and normals from the GPU evaluation,/// colours per vertex, indexed).tri_planned: wgpu::RenderPipeline,- tri_cut_planned: Option<(wgpu::RenderPipeline, wgpu::RenderPipeline)>,+ tri_cut_planned: Option<(wgpu::RenderPipeline, wgpu::RenderPipeline, wgpu::RenderPipeline)>,/// `tri` and `tri_cut` for instanced meshes (shared triangles, a move and colour per/// placement).tri_instanced: wgpu::RenderPipeline,- tri_cut_instanced: Option<(wgpu::RenderPipeline, wgpu::RenderPipeline)>,+ tri_cut_instanced: Option<(wgpu::RenderPipeline, wgpu::RenderPipeline, wgpu::RenderPipeline)>,/// `tri_instanced` and `tri_cut_instanced` for flat triangles.tri_instanced_flat: wgpu::RenderPipeline,/// `glass_far`, `glass_near` and `trans` for instanced meshes, smooth and flat.glass_far_instanced: (wgpu::RenderPipeline, wgpu::RenderPipeline),glass_near_instanced: (wgpu::RenderPipeline, wgpu::RenderPipeline),trans_instanced: (wgpu::RenderPipeline, wgpu::RenderPipeline),- tri_cut_instanced_flat: Option<(wgpu::RenderPipeline, wgpu::RenderPipeline)>,+ tri_cut_instanced_flat: Option<(wgpu::RenderPipeline, wgpu::RenderPipeline, wgpu::RenderPipeline)>,/// The tessellation compute pipeline, where the device runs compute shaders.tess: Option<crate::TessGpu>,/// While a section is shown (when the depth buffer has a stencil): `tri` marking where the- /// cap shows, then the cap again at the section plane's depth, so it hides the inside.- tri_cut: Option<(wgpu::RenderPipeline, wgpu::RenderPipeline)>,+ /// cap shows, the back faces marking it again where they lie within the faces' push of what+ /// is in front of them, then the cap again at the section plane's depth, so it hides the inside.+ tri_cut: Option<(wgpu::RenderPipeline, wgpu::RenderPipeline, wgpu::RenderPipeline)>,/// Highlight triangles over the model (depth test ≤, no depth write).tri_hl: wgpu::RenderPipeline,/// Whole highlighted bodies, redrawn from the model's planned chunks in the hover and the@@ -1780,6 +1781,17 @@ impl Renderer {stencil: wgpu::StencilState { front: face(Zero), back: face(Replace), read_mask: 0xff, write_mask: 0xff },bias: depth_bias(reversed, FACE_BIAS),};+ // The back faces again, pulled as far forward as the faces are pushed back: where+ // one meets a front face (a pocket's wall and its floor, a fillet and the face it+ // runs into) the push decides which is nearer along their shared edge, and a front+ // face winning there left the cap out, so edges behind the cut showed through it.+ let mark_back = wgpu::DepthStencilState {+ format,+ depth_write_enabled: Some(false),+ depth_compare: Some(depth_compare(reversed, wgpu::CompareFunction::LessEqual)),+ stencil: wgpu::StencilState { front: face(Keep), back: face(Replace), read_mask: 0xff, write_mask: 0xff },+ bias: depth_bias(reversed, CAP_MARK_BIAS),+ };// Then the cap's back faces again, only there, at the section plane's depth.let only = wgpu::StencilFaceState { compare: wgpu::CompareFunction::Equal, ..face(Keep) };let cap = wgpu::DepthStencilState {@@ -1791,6 +1803,7 @@ impl Renderer {};(solid("sc_tris_mark", "fs_solid", None, Some(mark), source),+ solid("sc_tris_mark_back", "fs_solid", Some(wgpu::Face::Front), Some(mark_back), source),solid("sc_tris_cap", "fs_solid_cut", Some(wgpu::Face::Front), Some(cap), source),)})@@ -2004,6 +2017,10 @@ pub const FACE_STEPS: i32 = 8;/// 3.5 px) may lose its outer edge on one side where it lies on a steep face. The section cap,/// which writes its own depth, adds the same in the shader.pub const FACE_SLOPE: f32 = 1.0;+/// The section's second marking of back faces (`Renderer::tri_cut`): pulled toward the eye as far+/// as [`FACE_BIAS`] pushes the faces back, so a back face that meets a front face along an edge+/// still marks the cap there.+const CAP_MARK_BIAS: wgpu::DepthBiasState = wgpu::DepthBiasState { constant: -FACE_STEPS, slope_scale: -FACE_SLOPE, clamp: 0.0 };/// The removed material of a preview, drawn translucent: behind the faces it shares with the new/// shape, so twice [`FACE_BIAS`].const GHOST_BIAS: wgpu::DepthBiasState = wgpu::DepthBiasState { constant: 2 * FACE_STEPS, slope_scale: 2.0 * FACE_SLOPE, clamp: 0.0 };@@ -2418,12 +2435,21 @@ impl Renderer {res.tri_cut_instanced.as_ref(),res.tri_cut_instanced_flat.as_ref(),) {- (Some((mark, cap)), Some((mark_planned, cap_planned)), Some((mark_instanced, cap_instanced)), Some((mark_flat, cap_flat))) => {+ (+ Some((mark, back, cap)),+ Some((mark_planned, back_planned, cap_planned)),+ Some((mark_instanced, back_instanced, cap_instanced)),+ Some((mark_flat, back_flat, cap_flat)),+ ) => {pass.set_stencil_reference(1);tris(pass, mark, &m.tris);tris(pass, mark, &pv.tris);planned(pass, mark_planned);instanced(pass, mark_instanced, mark_flat);+ tris(pass, back, &m.tris);+ tris(pass, back, &pv.tris);+ planned(pass, back_planned);+ instanced(pass, back_instanced, back_flat);tris(pass, cap, &m.tris);tris(pass, cap, &pv.tris);planned(pass, cap_planned);diff --git a/crates/gpu/tests/depth.rs b/crates/gpu/tests/depth.rsindex e95c378b69..6bb6bfc1bf 100644--- a/crates/gpu/tests/depth.rs+++ b/crates/gpu/tests/depth.rs@@ -236,3 +236,87 @@ fn an_edge_on_the_cut_face_draws_over_the_cap() {}}}++/// Pixels of the model's edges lying behind the section plane (recoloured red) that draw over+/// the cap: the cap's pixels are where the picture without lines shows the hatch (warm, with+/// warm pixels 3 px around, so the cut's outline doesn't count).+fn edges_through_the_cap(c: &Capturer, s: &Session, cam: &Camera, (origin, normal): (Vec3, Vec3), (w, h): (u32, u32)) -> usize {+ let mut sc = view::scene(s, cam);+ let behind = |p: &Vec3| (*p - origin).dot(normal) < -0.5;+ let segments = sc+ .lines+ .iter()+ .flat_map(|l| l.points.windows(2).filter(|w| w.iter().all(behind)).map(|w| SceneLine { points: w.to_vec(), color: Rgb(255, 0, 0), ..*l }))+ .collect();+ sc.lines = segments;+ let with = c.rgba(&sc, cam, w, h).unwrap();+ sc.lines.clear();+ let without = c.rgba(&sc, cam, w, h).unwrap();+ let warm = |x: i64, y: i64| {+ let at = (y * i64::from(w) + x) as usize * 4;+ without.get(at..at + 3).is_some_and(|p| p[0] > 60 && p[0] > p[2].saturating_add(15))+ };+ let mut n = 0;+ for y in 3..i64::from(h) - 3 {+ for x in 3..i64::from(w) - 3 {+ let at = (y * i64::from(w) + x) as usize * 4;+ let cap = [(0, 0), (3, 0), (-3, 0), (0, 3), (0, -3)].iter().all(|(dx, dy)| warm(x + dx, y + dy));+ // Redder than the cap there: a red line, whole or blended at its sides.+ let redness = |img: &[u8]| img.get(at..at + 3).map_or(0, |p| i32::from(p[0]) - i32::from(p[1]));+ if cap && redness(&with) > redness(&without) + 30 {+ n += 1;+ }+ }+ }+ n+}++/// The cap hides the edges inside a cut body (#6328). Where a face seen through the cut meets a+/// face turned toward the eye along an edge (a pocket's front wall and its side wall or floor),+/// the faces' push left the cap out beside the edge, and the edge showed through it. A pocket+/// whose front wall is 2 mm behind the cut, in perspective and orthographic.+#[test]+fn edges_of_a_pocket_behind_the_cut_stay_under_the_cap() {+ let mut s = session(&[+ ("solid.box", json!({"length": 40, "width": 40, "height": 30, "corner": [-20, -20, 0]})),+ ("solid.box", json!({"length": 16, "width": 16, "height": 18, "corner": [-14, -14, 12], "operation": "cut"})),+ ]);+ let plane = (Vec3::new(0.0, -16.0, 0.0), Vec3::new(0.0, -1.0, 0.0));+ s.execute("inspect.section", &json!({"plane": {"origin": [0, -16, 0], "normal": [0, -1, 0]}})).unwrap();+ for (mode, c) in capturers() {+ for fov in [0.0, 0.6] {+ let mut cam = view::home_camera(&s);+ cam.fov = fov;+ let n = edges_through_the_cap(c, &s, &cam, plane, (360, 300));+ assert!(n <= 2, "{mode} depth, fov {fov}: the pocket's edges show through the cap in {n} px");+ }+ }+}++/// The same while the view turns: a filleted hub with a bore and a cross hole, cut through the+/// middle, seen from twelve directions. Its fillets' and holes' edges came and went through the+/// cap from one step to the next.+#[test]+fn edges_of_a_cut_hub_stay_under_the_cap_as_the_view_turns() {+ let mut s = session(&[+ ("solid.cylinder", json!({"radius": 20, "height": 40, "body_name": "hub"})),+ ("solid.fillet", json!({"edges": [[20, 0, 40], [20, 0, 0]], "radius": 4})),+ ("solid.cylinder", json!({"radius": 8, "height": 30, "base": [0, 0, 10], "operation": "cut"})),+ ("solid.cylinder", json!({"radius": 3, "height": 50, "base": [-25, 0, 22], "axis": "X", "operation": "cut"})),+ ("solid.fillet", json!({"edges": [[8, 0, 40]], "radius": 1.5})),+ ]);+ let plane = (Vec3::new(0.0, -2.0, 0.0), Vec3::new(0.0, -1.0, 0.0));+ s.execute("inspect.section", &json!({"plane": {"origin": [0, -2, 0], "normal": [0, -1, 0]}})).unwrap();+ for (mode, c) in capturers() {+ for fov in [0.0, 0.6] {+ let mut shown = Vec::new();+ for i in 0..12 {+ let mut cam = view::home_camera(&s);+ (cam.fov, cam.yaw, cam.pitch) = (fov, -1.2 + 0.075 * f64::from(i), 0.35);+ shown.push(edges_through_the_cap(c, &s, &cam, plane, (900, 760)));+ }+ let total: usize = shown.iter().sum();+ assert!(total <= 6, "{mode} depth, fov {fov}: edges show through the cap, px per view {shown:?}");+ }+ }+}