Skip to content

Viewport: hidden edges show through faces (about 27 mm on a large model in orthographic, 0.16% of the distance in perspective), and thin faces z-fight #1434

Closed

Description

@CamWight

Behavior

Edges behind a face are drawn on top of it, and thin features z-fight. It shows most on large models.

  • Orthographic: edges up to about 27 mm behind a face show through it on a model about 34 m across, however far you zoom in. On a small model it's a fraction of a millimetre. Bolts, washers and nuts behind a plate show through the plate. Corners show short ticks where an edge carries on just behind a face, and circles show hooks near their silhouette, where their back half is only millimetres behind the front.
  • Perspective: the same, by camera distance. At 10 m, edges 12–15 mm behind a face show through it; at 2.5 m they don't.
  • Z-fighting: a face 0.3 mm in front of another speckles at 10 m in perspective, and it's solid at 2.5 m. That's the case for raised lettering and thin plates on a face.

It was seen first on a large private STEP model; the reproduction below is public. Measured on main at 5fa8e7d, Windows 11, NVIDIA Quadro RTX 4000, D3D12.

Reproduction

All in the desktop app through the control channel. Screenshots are in the comment below.

  1. A 20 mm plate with a 30 mm washer 22–25 mm behind its front face, and a 30 m × 8 m slab far away to make the scene large:

    {"commands": [
      {"command": "solid.box", "params": {"length": 30000, "width": 8000, "height": 100, "corner": [-15000, 3000, -3000], "body_name": "slab"}},
      {"command": "solid.box", "params": {"length": 200, "width": 20, "height": 200, "corner": [0, 0, 0], "body_name": "plate"}},
      {"command": "solid.cylinder", "params": {"radius": 8, "height": 60, "base": [100, -20, 100], "axis": "Y", "body_name": "bolt"}},
      {"command": "solid.cylinder", "params": {"radius": 15, "height": 3, "base": [100, 22, 100], "axis": "Y", "body_name": "washer"}}
    ]}

    Camera {"target": [100, 10, 100], "yaw": 0.35, "pitch": 0.25, "distance": 450}:

    • Orthographic: the washer's circle shows through the plate.
    • Perspective: hidden.
    • Orthographic without the slab: hidden.
  2. A 2 m plate 10 mm thick, a 600 mm washer 12–15 mm behind its front face, and a red label 0.3 mm thick on it. The commands are the same apart from sizes; appearance.assign colours the label #cc2222. In perspective with target [1000, 5, 1000]:

    • At 10 m the washer shows through and the label speckles, with or without the slab.
    • At 2.5 m both are right.

Cause

Edges. biased_z (assets/shaders/viewport.wgsl) pulls lines toward the eye by screen.z = 2e-4 (crates/ui-egui/src/gpu.rs, the uniform's 2e-4):

  • Orthographic: that's a fixed step of clip depth. Camera::clip sets the orthographic range to 8r + 2 mm, where r is scene_radius (crates/ui-egui/src/viewport.rs): the model's half-diagonal plus how far its centre is from the target, and at least twice the view's half-height. So the pull is 2e-4 × (8r + 2) mm. For a model 34 m across, r ≈ 17 m and the pull is ≈ 27 mm. Zooming in doesn't shrink r.
  • Perspective: the point moves toward the eye by 8 × 2e-4 = 0.16 % of its distance: 16 mm at 10 m.

Z-fighting. In perspective the near plane is max(d − 2r, d / 1000) (Camera::clip). Since r is at least twice the half-height (about 0.62 × d at the 0.6 rad field of view), d − 2r is always negative, so the near plane is always d / 1000, whatever the model's size. With the 24-bit depth buffer (DEPTH_BITS = 24, Depth24Plus) one depth step at the target is about d × 6e-5: 0.6 mm at 10 m.

Earlier work on the same family: #83 (hidden edges through solids in perspective), #305 (near-plane clipping), #983 (render edges just behind a face). Open PR #1135 adds Depth32Float textures for its ambient-occlusion pass, but the main depth buffer and the edge pull are unchanged there.

Directions (the choice is the maintainer's)

  • Edges: pull them by an amount tied to the pixel size at their depth (a pixel or two), not a fraction of the depth range, so the pull shrinks as you zoom in. The orthographic range could also fit the scene's depth along the view direction rather than 8r.
  • Z-fighting:
    • a reversed-Z projection with a 32-bit float depth buffer (Depth32Float, available on D3D12, Vulkan, Metal, WebGPU and WebGL2); or
    • a near plane taken from the scene's nearest point rather than d / 1000.

Activity

  1. CamWight commented on Oct 15, 2026

    @CamWight
    ContributorAuthor

    Screenshots of the reproduction above, in the desktop app (main at 5fa8e7d with an unrelated meshing fix; Windows 11, D3D12).

    1. The plate, bolt and washer, with the 30 m slab

    Orthographic Perspective Orthographic, no slab
    Orthographic: the washer circle shows through the plate Perspective: hidden Orthographic without the slab: hidden
    The washer, 22–25 mm behind the plate's front face, shows through Hidden Hidden

    2. The 2 m plate: a washer 12–15 mm behind its front face and a 0.3 mm red label on it, in perspective

    10 m, with the slab 2.5 m, with the slab 10 m, no slab
    10 m: the washer shows through and the label speckles 2.5 m: both right 10 m without the slab: both still wrong
    The washer shows through; the label speckles Both right Both still wrong: it's the distance, not the slab
  2. CamWight commented on Oct 16, 2026

    @CamWight
    ContributorAuthor

    Plan for this one (claimed in draft #1768, which also takes #1739: the origin-plane square z-fighting with a face on the same plane comes from the same family).

    A correction to the issue first: #1555 moved the renderer into crates/gpu, so the edge pull's 2e-4 is now in crates/gpu/src/view.rs (uniform_bytes), and the depth format is Depth24PlusStencil8 (the stencil marks section caps). A nice side effect of #1555: headless pictures use the viewport's pipelines, so one fix covers both, and crates/gpu/tests/capture.rs can test it on a real GPU.

    1. A depth range fitted to what's shown. Camera::clip keeps its radius form for the CPU callers; a bounds-aware form fits near and far to the shown bodies along the view direction. In perspective the near plane is never closer than today's d / 1000, and with the eye outside the model it's about 1000× finer at 10 m. In orthographic the range is the scene's depth, not 8r + 2.

    2. Faces pushed back by their own slope, instead of lines pulled forward by a share of the depth range. This is the same rule the CPU renderer has had since #983: a line on a face shows where it's no further behind the face than the face's slope spans across the line's width. On the GPU it's a slope-scaled depth bias on the opaque face pipelines. Section caps write their own depth, so fs_solid_cut adds the same push by hand. biased_z shrinks to a rounding allowance. Edges on a face draw whole at any angle, and edges behind it stay hidden. Anything lying exactly on a face (an origin plane, a sketch) sits cleanly on top, which is #1739.

    3. Reversed-Z with a 32-bit float depth buffer, where the device has it. The viewport always draws into its own target, which the SSAA path already does, so the depth format is chosen at runtime:

    • Depth32FloatStencil8 when the adapter offers it;
    • Depth32Float on the web;
    • otherwise today's format and depth direction.

    One reversed switch drives every compare, the clear value, the bias signs, cap_depth and the near-plane cut in vs_line. CPU code keeps the 0-near convention, so to_screen and the fit check don't change.

    Tests:

    • GPU tests built from both reproductions above, each run in both depth modes;
    • an edge on a face-on face and on a steep face;
    • section caps, highlights, decals, and the Modeling: visual artifacts #1739 case;
    • a fuzz test that the fitted range always contains what's shown.

    After that come planted faults, D3D12, Vulkan and the wasm build, SSAA and MSAA on and off, a frame-time check, and before and after screenshots from the app.

    @CypherNaught-0x, @0xlillith: a heads-up, since #1135 and #1086 add depth passes. With part 3, your Less tests would need to come from the same helper so they flip with it. I'll make that a one-line change for you and mention it on your PRs when it's ready.

    🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions