Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 110 additions & 34 deletions crates/engine/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,91 @@ pub fn grid_ext(half_height: f64, major: f64, slant: f64) -> f64 {
(ext / slant.abs().max(0.25) / major).ceil() * major
}

/// One grid segment in world coordinates.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct GridSegment {
pub a: Vec3,
pub b: Vec3,
/// The fade to draw it at (0..1); includes the line's cross-fade weight.
pub alpha: f32,
/// How much of the segment is the major look (0..1), for the caller's two colours.
pub major: f32,
}

/// A grid's placement on a plane: the centre, its axes, the plane origin the line offsets are
/// measured from, and the fade.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct GridFrame {
pub centre: Vec3,
pub u: Vec3,
pub v: Vec3,
pub origin: Vec3,
pub fade: GridFade,
}

/// The grid on a frame as world segments.
pub fn grid_segments_on(cam: &Camera, frame: &GridFrame, cap: i64) -> Vec<GridSegment> {
let at_u = (frame.centre - frame.origin).dot(frame.u);
let at_v = (frame.centre - frame.origin).dot(frame.v);
let mut out = Vec::new();
// Two lines through the point `frame.centre + frame.u * t`: one along v, one along u.
for (along, across, at) in [(frame.v, frame.u, at_u), (frame.u, frame.v, at_v)] {
for line in grid_lines(cam.half_height(), at, frame.fade.end, cap) {
for (s0, s1, alpha) in frame.fade.pieces(line.offset) {
out.push(GridSegment {
a: frame.centre + across * line.offset + along * s0,
b: frame.centre + across * line.offset + along * s1,
alpha: alpha * line.weight,
major: line.major,
});
}
}
}
out
}

/// The default viewport grid: the ground, or the upright plane when it is seen edge-on. `far` is
/// the picture's far distance (for the fade); `follows_view` is the preference; `last` is the
/// plane the previous frame drew, which makes the edge-on switch hysteretic.
pub fn grid_segments(cam: &Camera, far: f64, follows_view: bool, last: &mut GridPlane) -> Vec<GridSegment> {
let tf = cam.to_frame(cam.target);
let back = cam.to_frame(cam.back());
let plane = if follows_view {
*last = grid_plane(back, *last);
*last
} else {
GridPlane::Ground
};
let (u, v) = plane.axes();
let n = u.cross(v);
let slant = back.dot(n);
let major = grid_step(cam.half_height()).1;
let ext = grid_ext(cam.half_height(), major, slant);
let centre = grid_centre(tf, back, n, ext);
let snap = |a: Vec3| a * ((centre.dot(a) / major).round() * major);
let c = snap(u) + snap(v);
let far_reach = grid_far_reach(cam, far, cam.from_frame(c), cam.from_frame(n));
let fade = GridFade::new(cam.half_height(), slant, ext, far_reach);
let frame = GridFrame { centre: cam.from_frame(c), u: cam.from_frame(u), v: cam.from_frame(v), origin: Vec3::ZERO, fade };
grid_segments_on(cam, &frame, 300)
}

/// 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, 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.
pub fn grid_centre(tf: Vec3, back: Vec3, n: Vec3, ext: f64) -> Vec3 {
let height = tf.dot(n);
let under = tf - n * height;
let slant = back.dot(n);
// 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 }
}

/// Sketch curves as world polylines with their colour.
pub fn sketch_lines(sk: &Sketch, plane: &Plane, active: bool, determined: &[bool]) -> Vec<(Vec<Vec3>, Rgb, bool)> {
sketch_lines_with_sampler(sk, plane, active, determined, world_curve_parts)
Expand Down Expand Up @@ -783,50 +868,41 @@ fn build_scene(
let major = grid_step(cam.half_height()).1;
// Headless pictures keep the grid on the ground, the viewport's default; an open sketch
// has its own plane's grid (world coordinates, no camera frame).
let (c, u, v, slant, w): (Vec3, Vec3, Vec3, f64, Box<dyn Fn(Vec3) -> Vec3>) = if let Some(pl) = open_sketch_plane(s) {
let sketch_plane = open_sketch_plane(s);
let frame = if let Some(pl) = sketch_plane {
let (c, u, v) = sketch_grid_frame(&pl, cam.target, major);
let n = u.cross(v);
(c, u, v, cam.back().dot(n), Box::new(|p| p))
let slant = cam.back().dot(n);
// The grid reaches as far on the ground as the view is wide (the ground stretches as
// it is seen at a slant) and fades out with the distance, so that it dissolves into the
// distance instead of ending in a cut (#4214).
let ext = grid_ext(cam.half_height(), major, slant);
let far_reach = grid_far_reach(cam, cam.clip(sc.radius).1, c, n);
let fade = GridFade::new(cam.half_height(), slant, ext, far_reach);
GridFrame { centre: c, u, v, origin: pl.origin, fade }
} else {
let tf = cam.to_frame(cam.target);
let (u, v) = (Vec3::X, Vec3::Y);
let back = cam.to_frame(cam.back());
let n = u.cross(v);
let snap = |a: Vec3| a * ((tf.dot(a) / major).round() * major);
(snap(u) + snap(v), u, v, back.dot(n), Box::new(|p| cam.from_frame(p)))
let c = snap(u) + snap(v);
let slant = back.dot(n);
let ext = grid_ext(cam.half_height(), major, slant);
let far_reach = grid_far_reach(cam, cam.clip(sc.radius).1, cam.from_frame(c), cam.from_frame(n));
let fade = GridFade::new(cam.half_height(), slant, ext, far_reach);
GridFrame { centre: cam.from_frame(c), u: cam.from_frame(u), v: cam.from_frame(v), origin: Vec3::ZERO, fade }
};
// The grid reaches as far on the ground as the view is wide (the ground stretches as it
// is seen at a slant) and fades out with the distance, so that it dissolves into the
// distance instead of ending in a cut (#4214).
let ext = grid_ext(cam.half_height(), major, slant);
let far_reach = grid_far_reach(cam, cam.clip(sc.radius).1, w(c), w(u.cross(v)));
let fade = GridFade::new(cam.half_height(), slant, ext, far_reach);
// The grid's centre on each axis, from the plane's origin (the world's, or the sketch
// plane's): a level fading in near a switch of the spacing lies on its own places (#9570).
let origin = open_sketch_plane(s).map_or(Vec3::ZERO, |pl| pl.origin);
let (at_u, at_v) = ((c - origin).dot(u), (c - origin).dot(v));
// Two lines through the point `c + u * t`: one along v, one along u.
for (along, across, at) in [(v, u, at_u), (u, v, at_v)] {
for line in grid_lines(cam.half_height(), at, fade.end, 400) {
let t = line.offset;
// The viewport's widths (pixels). The headless picture keeps the palette's two
// grid colours: a line two levels share with different kinds is drawn as both,
// each as strong as its share.
let width = 0.7 + 0.3 * f64::from(line.major);
for (col, share) in [(p.grid_major, line.major), (p.grid, 1.0 - line.major)] {
if share <= 0.0 {
continue;
}
for (s0, s1, alpha) in fade.pieces(t) {
sc.grid.push(SceneLine {
points: vec![w(c + across * t + along * s0), w(c + across * t + along * s1)],
color: col,
alpha: alpha * line.weight * share,
width,
on_top: false,
});
}
// The viewport's widths (pixels). The headless picture keeps the palette's two grid
// colours: a line two levels share with different kinds is drawn as both, each as strong
// as its share.
for seg in grid_segments_on(cam, &frame, 400) {
let width = 0.7 + 0.3 * f64::from(seg.major);
for (col, share) in [(p.grid_major, seg.major), (p.grid, 1.0 - seg.major)] {
if share <= 0.0 {
continue;
}
sc.grid.push(SceneLine { points: vec![seg.a, seg.b], color: col, alpha: seg.alpha * share, width, on_top: false });
}
}
}
Expand Down
96 changes: 96 additions & 0 deletions crates/engine/src/view_palette_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,102 @@ fn the_grid_never_draws_two_levels_that_do_not_nest_at_once() {
assert!(at(1.0) > 0.3 && at(1.0) < 0.7 && at(2.0) > 0.999, "{} {}", at(1.0), at(2.0));
}

/// The camera frame's direction toward the eye for a view at `yaw` and `pitch` degrees.
fn camera_at(yaw_deg: f64, pitch_deg: f64) -> Camera {
Camera { yaw: yaw_deg.to_radians(), pitch: pitch_deg.to_radians(), ..Camera::default() }
}

/// The viewport's grid as segments: on the ground, finite, and with every fade in 0..1.
#[test]
fn grid_segments_lie_on_the_ground_and_are_finite() {
let cam = camera_at(30.0, 35.0);
let mut last = GridPlane::Ground;
let segments = grid_segments(&cam, 1e6, true, &mut last);
assert!(!segments.is_empty(), "the grid is drawn");
assert_eq!(last, GridPlane::Ground);
for seg in &segments {
for p in [seg.a, seg.b] {
assert!(p.is_finite(), "{p:?} in {seg:?}");
assert!(p.z.abs() < 1e-9, "the ground is Z = 0: {p:?}");
}
assert!((0.0..=1.0).contains(&seg.alpha), "alpha {} in {seg:?}", seg.alpha);
assert!((0.0..=1.0).contains(&seg.major), "major {} in {seg:?}", seg.major);
}
}

/// At the edge-on threshold the grid takes the wall facing the view, and the hysteresis keeps
/// the wall already drawn until the view clearly leaves it (#1993).
#[test]
fn grid_segments_choose_the_side_plane_and_keep_it_at_the_threshold() {
let mut last = GridPlane::Ground;
let edge = camera_at(0.0, 9.0);
let segments = grid_segments(&edge, 1e6, true, &mut last);
assert_eq!(last, GridPlane::Xz, "9 degrees above the ground takes the wall");
assert!(!segments.is_empty());
assert!(segments.iter().all(|s| s.a.y.abs() < 1e-9 && s.b.y.abs() < 1e-9), "Xz is the world's Y = 0 plane");
// 12 degrees: past the entering threshold, but a wall already drawn stays.
let near = camera_at(0.0, 12.0);
let segments = grid_segments(&near, 1e6, true, &mut last);
assert_eq!(last, GridPlane::Xz, "a wobble does not flicker");
assert!(segments.iter().all(|s| s.a.y.abs() < 1e-9 && s.b.y.abs() < 1e-9));
// Coming from the ground, the same view is still the ground.
let mut ground = GridPlane::Ground;
let segments = grid_segments(&near, 1e6, true, &mut ground);
assert_eq!(ground, GridPlane::Ground, "12 degrees is not edge-on enough to enter");
assert!(segments.iter().all(|s| s.a.z.abs() < 1e-9 && s.b.z.abs() < 1e-9));
}

/// With the follow-the-view preference off, the grid stays on the ground and the plane the
/// last frame drew is left alone.
#[test]
fn grid_segments_stay_on_the_ground_when_the_view_does_not_follow() {
// A Right view: following the view would take the YZ wall.
let cam = camera_at(90.0, 0.0);
let mut last = GridPlane::Xz;
let segments = grid_segments(&cam, 1e6, false, &mut last);
assert_eq!(last, GridPlane::Xz, "the remembered plane is unchanged");
assert!(!segments.is_empty());
assert!(segments.iter().all(|s| s.a.z.abs() < 1e-9 && s.b.z.abs() < 1e-9), "the ground is Z = 0");
}

/// At a half height where the spacing cross-fades from 2 to 5, a line the two levels share
/// blends the major and minor looks, and both line families are drawn.
#[test]
fn grid_segments_cross_fade_levels_and_draw_both_families() {
let mut cam = camera_at(0.0, 90.0);
cam.distance = 50.0 / 0.4;
let mut last = GridPlane::Ground;
let segments = grid_segments(&cam, 1e6, true, &mut last);
assert!(segments.iter().any(|s| s.major > 0.0 && s.major < 1.0), "a line shared by the cross-fading levels blends the looks");
assert!(segments.iter().any(|s| (s.a.x - s.b.x).abs() < 1e-9), "a line along Y");
assert!(segments.iter().any(|s| (s.a.y - s.b.y).abs() < 1e-9), "a line along X");
}

/// [`grid_segments_on`] is exactly the grid's lines drawn in pieces at the fade's strength.
#[test]
fn grid_segments_on_matches_grid_lines_and_the_fade() {
let cam = Camera::default();
let fade = GridFade::new(cam.half_height(), 0.8, 100.0, 70.0);
let frame = GridFrame { centre: Vec3::new(3.0, -4.0, 0.0), u: Vec3::X, v: Vec3::Y, origin: Vec3::new(1.0, 2.0, 0.0), fade };
let got = grid_segments_on(&cam, &frame, 7);
let mut want = Vec::new();
let (at_u, at_v) = ((frame.centre - frame.origin).dot(frame.u), (frame.centre - frame.origin).dot(frame.v));
for (along, across, at) in [(frame.v, frame.u, at_u), (frame.u, frame.v, at_v)] {
for line in grid_lines(cam.half_height(), at, frame.fade.end, 7) {
for (s0, s1, alpha) in frame.fade.pieces(line.offset) {
want.push(GridSegment {
a: frame.centre + across * line.offset + along * s0,
b: frame.centre + across * line.offset + along * s1,
alpha: alpha * line.weight,
major: line.major,
});
}
}
}
assert!(!want.is_empty(), "the frame draws a grid");
assert_eq!(got, want);
}

/// No colour here is one of [`colors`] or an appearance below, and each field has its own.
const MARK: Palette = Palette {
background: (Rgb(1, 2, 3), Rgb(4, 5, 6)),
Expand Down
Loading