Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Display refinement waits until the camera stands still (#3595) (#5446)

SiroArt3f4c6e457197
diff --git a/crates/ui-egui/src/display_lod.rs b/crates/ui-egui/src/display_lod.rs
index 2d39bdae20..4c1dd014c6 100644
--- a/crates/ui-egui/src/display_lod.rs
+++ b/crates/ui-egui/src/display_lod.rs
@@ -5,7 +5,9 @@
//! of the chosen display quality (Preferences ▸ Graphics) in physical pixels, where the body is
//! nearest the eye. [`refinement_for`] turns that into a refinement step (each halves the chord),
//! with hysteresis. A step not made yet is planned off the UI thread and swapped in when it is
-//! ready; until then the body keeps the step it has. Only bodies on screen and big enough get
+//! ready; until then the body keeps the step it has. Nothing is chosen or swapped in while the
+//! camera moves (each swap rebuilds and uploads the whole model scene), only once it has stood
+//! still for a moment (`viewport::REFINE_STILL_MS`). Only bodies on screen and big enough get
//! refined, within a triangle budget (the bodies that need it most first) and a cache size.
//!
//! The model scene draws a refined body's faces from the refined plan (on the GPU, or its CPU
diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs
index b7a404a755..02e119463e 100644
--- a/crates/ui-egui/src/viewport.rs
+++ b/crates/ui-egui/src/viewport.rs
@@ -34,6 +34,10 @@ mod removed_later_tests;
#[path = "viewport_plane_target_tests.rs"]
mod plane_target_tests;
+#[cfg(test)]
+#[path = "viewport_refine_wait_tests.rs"]
+mod refine_wait_tests;
+
#[path = "navigation_cube.rs"]
mod navigation_cube;
@@ -76,6 +80,8 @@ pub struct ViewportState {
pub(crate) lod: std::sync::Mutex<crate::display_lod::Lod>,
/// The view and model the refinement was last chosen for.
lod_view: u64,
+ /// Refinement waits for the camera to stop ([`REFINE_STILL_MS`]).
+ lod_rest: CameraRest,
/// Each body mesh's box (the mesh held, so its address stays its own), from the last state.
mesh_bounds: std::sync::Mutex<MeshBounds>,
/// Each shown body mesh's section index for the cut's normal (the mesh held, so its address
@@ -550,6 +556,27 @@ fn drawn_mesh(app: &SolveApp, b: &solidcraft_engine::doc::ModelBody) -> std::syn
if current() == DisplayQuality::Normal { b.mesh() } else { b.plan_mesh().unwrap_or_else(|| b.mesh()) }
}
+/// How long (ms) the camera must stand still before display refinements are chosen and swapped in.
+const REFINE_STILL_MS: f64 = 200.0;
+
+/// The camera the last frame had and when it last differed from the one before (ms).
+#[derive(Default)]
+struct CameraRest {
+ camera: u64,
+ moved_at: f64,
+}
+
+impl CameraRest {
+ /// Notes this frame's camera (a hash of the view) at `now` (ms); how much longer (ms) the
+ /// camera has to stand still, 0 once it has for [`REFINE_STILL_MS`].
+ fn wait(&mut self, camera: u64, now: f64) -> f64 {
+ if camera != self.camera {
+ *self = CameraRest { camera, moved_at: now };
+ }
+ (REFINE_STILL_MS - (now - self.moved_at)).max(0.0)
+ }
+}
+
/// Choose which bodies to draw finer than the chosen display quality for this view (#1320):
/// each shown body's screen-space error from its box on screen, refinements planned off the UI
/// thread and swapped in when ready. Keeps repainting while refinements are being planned.
@@ -563,6 +590,15 @@ fn update_refinement(app: &mut SolveApp, ctx: &egui::Context, proj: &Proj) {
scene_key(app).hash(&mut h);
crate::sketch_display::projection_key(proj, app.viewport.pixels_per_point()).hash(&mut h);
let view = h.finish();
+ // A moving camera is left alone: every refinement swapped in rebuilds and uploads the whole
+ // model scene, which in a large assembly takes tens of milliseconds a time. The bodies keep
+ // the step they have until the camera has stood still for [`REFINE_STILL_MS`].
+ let camera = crate::sketch_display::projection_key(proj, app.viewport.pixels_per_point());
+ let wait = app.viewport.lod_rest.wait(camera, crate::now_ms());
+ if wait > 0.0 {
+ ctx.request_repaint_after(std::time::Duration::from_millis(wait as u64 + 5));
+ return;
+ }
// While refinements are being planned, the view standing still: only collect finished ones.
if view == app.viewport.lod_view {
let (busy, finished) = app.viewport.lod.lock().map_or((false, false), |mut l| {
diff --git a/crates/ui-egui/src/viewport_refine_wait_tests.rs b/crates/ui-egui/src/viewport_refine_wait_tests.rs
new file mode 100644
index 0000000000..e53f6113d4
--- /dev/null
+++ b/crates/ui-egui/src/viewport_refine_wait_tests.rs
@@ -0,0 +1,32 @@
+//! Display refinement waits for the camera to stand still (#3595): swapping a refinement in
+//! rebuilds and uploads the whole model scene, so it is not done in the middle of an orbit.
+
+use super::{CameraRest, REFINE_STILL_MS};
+
+#[test]
+fn a_camera_that_changes_every_frame_never_gets_refinement() {
+ let mut rest = CameraRest::default();
+ for frame in 0..600_u32 {
+ let now = 1000.0 + f64::from(frame) * 4.0;
+ assert!(rest.wait(u64::from(frame) + 1, now) > 0.0, "frame {frame}: a moving camera must wait");
+ }
+}
+
+#[test]
+fn refinement_is_allowed_once_the_camera_has_stood_still() {
+ let mut rest = CameraRest::default();
+ assert!(rest.wait(7, 1000.0) > 0.0, "the first frame with a new camera waits");
+ let left = rest.wait(7, 1000.0 + REFINE_STILL_MS / 2.0);
+ assert!(left > 0.0 && left < REFINE_STILL_MS, "half way: some of the wait is left, got {left}");
+ assert_eq!(rest.wait(7, 1000.0 + REFINE_STILL_MS), 0.0);
+ assert_eq!(rest.wait(7, 1000.0 + 5.0 * REFINE_STILL_MS), 0.0);
+}
+
+#[test]
+fn moving_again_starts_the_wait_over() {
+ let mut rest = CameraRest::default();
+ rest.wait(7, 1000.0);
+ assert_eq!(rest.wait(7, 2000.0), 0.0);
+ assert_eq!(rest.wait(8, 2010.0), REFINE_STILL_MS);
+ assert_eq!(rest.wait(8, 2010.0 + REFINE_STILL_MS), 0.0);
+}