Skip to content

fix: renders hide edges just behind a face; lines on steep faces draw whole - #983

Merged
bherbruck merged 1 commit into
tangency-labs:mainfrom
NOWN-DEV:fix/render-depth-tolerance
Oct 14, 2026
Merged

bherbruck merged 1 commit into
tangency-labs:mainfrom
NOWN-DEV:fix/render-depth-tolerance

Conversation

@NOWN-DEV

Copy link
Copy Markdown
Contributor

Change

Wrong. The CPU renderer (snapshot, the MCP screenshot, model renders, the CPU viewport) let a line through its depth test where it lay within 2e-4 of the screen depth range behind the surface. That range comes from the scene's radius, not from what a pixel shows: orthographic views span 8 · radius + 2, so the allowance is about 2 mm on a 2.4 m design and more when the view is zoomed in away from its centre; perspective views put the near plane at 0.1 % of the camera distance, so near the camera the allowance is tens of millimetres. In close views the far edges of thin sheet, and parts a millimetre or two behind it, were drawn on its face.

Now. Each pixel keeps the screen depth slope of the triangle it shows. A line lying on a face is at the face's depth where it passes, while the pixel holds the face's depth at its centre, d away from the line (the distance the line's coverage already uses), so a line passes where it is no further behind than slope · d, plus a few f32 steps for rounding. Where a section cap sets the depth, its plane's slope counts.

  • Edges and sketch lines lying on a face draw at any slope and any zoom. Before, in small renders (a scene of radius 30 seen 40 mm high on 64 pixels, much as in a fitted thumbnail) a line on a face turned 70° from the screen lost the near half of its width.
  • What lies behind a face is hidden once it is further behind than the face's depth changes across the line's width: behind a face square to the view, anything behind it; behind a face in the iso view, anything more than about two pixels' worth of its slope.
  • Only crates/render/src/raster.rs changes; it costs one f32 per render pixel (as the depth buffer). The GPU viewport is unchanged (Section cut faces hide the inside; perspective no longer shows hidden edges through solids #83 fixed its perspective depth bias).

Validation

Two tests in crates/render/src/raster.rs, both on a close front view 40 mm high on 64 pixels (128 render pixels of 0.3125 mm), orthographic and perspective (fov 0.6):

  • edges_behind_a_thin_sheet_stay_hidden_in_close_views: a red edge 1 mm behind a sheet, in a design of radius 1200. The old allowance was 2e-4 · (8 · 1200 + 2) = 1.92 mm, so on main the edge is drawn (64 red pixels, square to the view). Square to the view the sheet's slope is 0; tilted 45° the edge lies 1 / cos 45° = 1.414 mm behind along the view, and the sheet's depth changes 0.3125 mm per pixel, at most 2 · 1.7 · 0.3125 = 1.06 mm over a 1.2 pixel line (1.7 pixels from its middle to the edge of its anti-aliasing). Hidden in all four cases; drawn over everything it is in the picture.
  • edges_on_a_steep_face_draw_whole: the outline of a sheet turned 70° from the view (its depth changes tan 70° · 0.3125 = 0.86 mm per pixel) and a line across it, in designs of radius 30 and 1200, draw exactly as many pixels as the same lines drawn over everything. On main, radius 30 (orthographic) drew 238 of 286.

The pictures below: a 400 × 300 × 1 sheet at z = 200 with a 16 mm boss and a 10 mm hole, a 60 × 60 × 30 box 1 mm under the sheet, and a 4000 × 40 × 40 beam elsewhere that makes the design large; solidcraft-cli mcp --in the script, screenshot with a camera, 1000 × 700.

close view behind the sheet's top face (along the view) old allowance main this change
iso, orthographic, 120 mm high the sheet's bottom edges: 1 / sin 35.26° = 1.73 mm; the box's top edges: 3.46 mm 2e-4 · (8 · 3161 + 2) = 5.06 mm drawn hidden
iso, perspective, 160 mm from the target the same about 2e-4 · 160² / 0.16 = 32 mm at the target drawn hidden
7° above the sheet, perspective, 110 mm the box: 2 / sin 7° = 16.4 mm; the hole's bottom rim: 8.2 mm; the boss's back base: about 16 mm about 22 mm drawn hidden

In those close views only pixels where hidden lines were drawn change (3042 to 6479 per picture, all lighter); in the whole design fitted to the view, 858 pixels are darker where lines on sloped faces now draw their full width.

cargo fmt --all --check and cargo clippy -p solidcraft-render -p solidcraft-engine --all-targets --profile ci --locked -- -D warnings clean (workspace clippy is clean too apart from the Windows-only unused_mut in solidcraft-mcp that main has); cargo test -p solidcraft-render 32 passed; the engine's view, snapshot, render, screenshot and thumbnail tests 33 passed; the UI's render, snapshot, screenshot and section tests 10 passed.

The whole cargo xtask ci on this commit (f7e3ae7, on 2eb292dd), on Windows, merged with the two-line unused_mut fix that draft #907 proposes (since #901, workspace clippy -D warnings stops in solidcraft-mcp on Windows before any test runs). It passes fmt, assets, licences, layers, clippy -D warnings, the wasm32 check, and the render, doc and kernel tests. What failed is not this change's:

  • The tests plain main (8fb5742 with the same two lines) fails on this machine the same day, alone too: the robustness goldens all_0 to all_3, cmd::motion::tests::motion_study_plays_and_exports, and the path-scope tests of the mcp, io and ui-egui libs (this machine's temp folder has an 8.3 short name in its path, which the path scope refuses).
  • press_pull_on_the_enclosure_lid_is_prompt (a timing test) failed beside the other test binaries and passes alone, and the action matrix's sample (Action matrix: every ordinary action on every face, edge, vertex and body of the corpus #553) timed out on a few actions beside them; run alone it passes (1145 of 1195 actions, the rest as in its baseline). This commit changes only crates/render/src/raster.rs.

Screenshots of the behavior (when observable)

Close view, orthographic: main draws the box under the sheet and the sheet's own bottom edges on its top face; this change hides them.

main: hidden edges on the sheet
this change

The sheet's back corner and the boss and hole, enlarged (main left, this change right): the 1 mm sheet's bottom edges, the box's edge and the hole's bottom rim are gone; the boss's base edge stays.

back corner, main and this change
boss and hole, main and this change

Perspective:

main: perspective
this change: perspective

7° above the sheet, where its top face is steepest on screen: the box, the boss's far base and the hole's bottom rim are hidden, while the boss's near base edge, its top rim and the hole's rim are drawn whole.

main: grazing view
this change: grazing view
boss and hole, main and this change

Integration status

Based on origin/main 2eb292dd (#824), one commit, not stacked. It merges cleanly with origin/main b34d4523 (#980), which has changed nothing in crates/render since. Searched upstream issues and pull requests, open and closed: #83 fixed the GPU viewport's perspective depth bias; nothing reports this for the CPU renderer. The open render pull requests #800, #849 and #863 change only line 79 of raster.rs (shade made pub(crate)); this change does not touch it, and their hunk applies on top of it. No new command, parameter or stored field (format unchanged). Action matrix: not run (rendering only).

🤖 Generated with Claude Code

… whole

The CPU renderer (snapshots, MCP screenshots, model renders, the CPU viewport) let a line
through its depth test within 2e-4 of the screen depth range. That is a share of the scene,
not of what a pixel shows: in orthographic views 2e-4 * (8 * radius + 2), about 2 mm on a
2.4 m design, and in perspective, where the near plane sits at 0.1 % of the camera distance,
tens of millimetres near the camera. Close views showed the far edges of thin sheet, and
parts a millimetre or two behind it, on its face.

Each pixel now keeps the screen depth slope of the triangle it shows, and a line passes where
it is no further behind than that slope times its distance from the pixel centre (plus a few
f32 steps). Edges and sketch lines lying on a face pass at any slope (on steep faces in small
renders they used to lose the near half of their width), and what lies behind a face is hidden
once it is further behind than the face's depth changes across the line's width.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bherbruck
bherbruck merged commit 345c088 into tangency-labs:main Oct 14, 2026
15 checks passed
@bherbruck

Copy link
Copy Markdown
Collaborator

Merged, thanks! The slope-based allowance is a much better fit than the fixed share of the depth range, and the before/after close views made the difference easy to see.

CamWight added a commit to CamWight/solidcraft that referenced this pull request Oct 16, 2026
…anar faces stop z-fighting (tangency-labs#1434, tangency-labs#1739)

Edges: lines were pulled toward the eye by a share of the depth range (2e-4), about 27 mm in
orthographic on a design 34 m across and 0.16 % of the distance in perspective, so edges behind
a face showed through it. Opaque faces are now pushed back instead, by a few depth steps and by
their own depth slope over one pixel (FACE_BIAS, a hardware depth bias; the section cap, which
writes its own depth, adds the same in the shader). It is the CPU renderer's rule since tangency-labs#983:
whatever lies on a face (its edges, an origin plane, a sketch, a decal) draws over it at any
angle, and an edge behind it stays hidden. The origin plane's square no longer z-fights with a
face on the same plane (tangency-labs#1739).

Depth precision: where the device has a 32-bit float depth buffer with a stencil, the viewport
draws with reversed depth (1 near, 0 far), whose precision follows the distance from the eye
(about 1 µm at 10 m, where 24 bits gave 0.6 mm), so a 0.3 mm label no longer speckles at 10 m.
reversed_depth decides from the depth format; depth_compare, depth_bias and far_depth turn
every test, bias and clear round with it; the CPU keeps its 0-near projection and only the
GPU's matrix is turned round (gpu_view_proj). egui clears its depth to 1, so the viewport sets
its rectangle to the far plane first (depth_reset). The window asks for the float buffer only
when every adapter it could start has one (eframe makes the depth buffer before it picks the
adapter); devices are opened with DEPTH32FLOAT_STENCIL8 wherever the adapter offers it. Other
devices and safe mode keep Depth24PlusStencil8; the web uses Depth32Float. Headless pictures
make the same choice.

Tests: crates/gpu/tests/depth.rs runs the issue's reproductions and edge checks on a real GPU
with the device's depth and the 24-bit fallback; an_origin_plane_on_a_face_is_tinted_evenly
covers tangency-labs#1739.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants