Viewport: keep the ground grid stable through grazing views (#10014) (#10031)

* claim: #10014

* fix(ui): stabilize the ground grid near the horizon
This commit is contained in:
Colin Faulkingham
2026-10-31 13:28:34 +00:00
committed by GitHub
parent 34c378c063
commit 6047c0d5a1
+51 -7
View File
@@ -5974,15 +5974,16 @@ fn grid_colour(c: [u8; 4], alpha: f32) -> [u8; 4] {
/// Where the ground grid is centred: where the view's axis (`back`, through the camera's target
/// `tf`) meets the plane through the origin with normal `n`, so the middle of the view has grid
/// (#3087). At a grazing slant that point runs far off, and the ground in front of the camera
/// would have none: it stays within `ext` (the grid's half-size) of the point under the target.
/// (#3087). At a grazing slant, return smoothly toward the point under the target: the actual
/// intersection runs beyond the visible grid and jumps to the other side at the horizon.
fn grid_centre(tf: Vec3, back: Vec3, n: Vec3, ext: f64) -> Vec3 {
let under = tf - n * tf.dot(n);
let height = tf.dot(n);
let under = tf - n * height;
let slant = back.dot(n);
if slant.abs() <= 1e-3 {
return under;
}
let along = tf - back * (tf.dot(n) / slant) - under;
// Use the same minimum slant as grid_ext and GridFade. Above it, this is the view-axis
// intersection; below it, the offset tends continuously to zero instead of infinity.
let reach_slant = slant.abs().max(0.25);
let along = (back - n * slant) * (-height * slant / (reach_slant * reach_slant));
let len = along.len();
if len > ext && len > 0.0 { under + along * (ext / len) } else { under + along }
}
@@ -11485,6 +11486,49 @@ mod grid_plane_tests {
assert!(c.len() < 1e-9, "{c:?}");
}
#[test]
fn ground_grid_centre_is_continuous_across_the_horizon() {
for normal in [Vec3::Y, Vec3::Z] {
for height in [-13.0, 0.0, 13.0] {
let target = Vec3::X * 2.0 + normal * height;
for extent in [120.0, 600.0] {
let centre = |step: i32| {
let pitch = f64::from(step) * 0.0001;
grid_centre(target, Vec3::X * pitch.cos() + normal * pitch.sin(), normal, extent)
};
for step in -20..20 {
let (a, b) = (centre(step), centre(step + 1));
assert!(a.dot(normal).abs() < 1e-9 && b.dot(normal).abs() < 1e-9);
assert!(
a.dist(b) < 0.1,
"a tiny orbit must not move the grid away: height {height}, extent {extent}, step {step}: {a:?} -> {b:?}"
);
}
}
}
}
}
#[test]
fn grazing_ground_grid_keeps_the_point_under_the_target_visible() {
let half_height = 124.55908448439891 * 0.4;
let major = grid_step(half_height).1;
for normal in [Vec3::Y, Vec3::Z] {
for height in [-12.966269622652957, 12.966269622652957] {
let target = Vec3::X * 2.1333385556362745 + normal * height;
let under = target - normal * height;
for pitch in [-0.125_f64, -0.08, -0.02, 0.0, 0.02, 0.08, 0.125] {
let back = Vec3::X * pitch.cos() + normal * pitch.sin();
let slant = back.dot(normal);
let ext = grid_ext(half_height, major, slant);
let centre = grid_centre(target, back, normal, ext);
let fade = GridFade::new(half_height, slant, ext, f64::INFINITY);
assert_eq!(fade.alpha(centre.dist(under)), 1.0, "the grid must not recede from the model: height {height}, pitch {pitch}");
}
}
}
}
/// #3087: zoomed in on a target below the ground (an orbit left it there), seen at a slant,
/// the grid still covers the ground in the middle of the view: where the view's axis meets
/// it, some fifty millimetres from the point under the target, round which it was drawn.