SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Viewport: perspective while orbiting, orthographic on face-on views (#2682) (#6700)
Dahak Builds6f5f64f2d97c
diff --git a/assets/locales/pending/perspective-auto-ortho.json b/assets/locales/pending/perspective-auto-ortho.jsonnew file mode 100644index 0000000000..b95dfcb4a1--- /dev/null+++ b/assets/locales/pending/perspective-auto-ortho.json@@ -0,0 +1,5 @@+{+ "en": { "view.auto_ortho_faces": "Perspective while orbiting, ortho on faces" },+ "ja": { "view.auto_ortho_faces": "回転中は透視投影、面を正面に見ると平行投影" },+ "zh": { "view.auto_ortho_faces": "旋转时透视,正对表面时正交" }+}diff --git a/crates/render/src/camera.rs b/crates/render/src/camera.rsindex e2f641cbe8..d61a321ed8 100644--- a/crates/render/src/camera.rs+++ b/crates/render/src/camera.rs@@ -449,6 +449,37 @@ impl Camera {self.pitch = p.clamp(-lim, lim);self.roll = 0.0;}+ /// The standard axis view this camera faces, if its view direction is within `tol` radians+ /// of one of the six axis views (Front, Back, Left, Right, Top, Bottom). Iso is never+ /// reported. At the poles (Top/Bottom) the yaw is degenerate, so any yaw counts as aligned.+ /// Used by the "perspective while orbiting, orthographic on faces" view mode (#2682).+ pub fn aligned_standard_view(&self, tol: f64) -> Option<StandardView> {+ use std::f64::consts::FRAC_PI_2;+ if !self.yaw.is_finite() || !self.pitch.is_finite() || !(tol > 0.0) {+ return None;+ }+ // Top / Bottom: aligned when the elevation reaches the pole within the tolerance+ // (`set_view` stops a hair short of pi/2, so the slack covers the exact views too).+ if FRAC_PI_2 - self.pitch.abs() <= tol {+ return Some(if self.pitch >= 0.0 { StandardView::Top } else { StandardView::Bottom });+ }+ // The four side views sit at pitch 0 with yaw on a multiple of pi/2.+ if self.pitch.abs() > tol {+ return None;+ }+ let quarter = (self.yaw / FRAC_PI_2).round().rem_euclid(4.0);+ let off = (self.yaw / FRAC_PI_2).rem_euclid(1.0);+ let off = off.min(1.0 - off) * FRAC_PI_2;+ if off > tol {+ return None;+ }+ Some(match quarter as i32 {+ 0 => StandardView::Front,+ 1 => StandardView::Left,+ 2 => StandardView::Back,+ _ => StandardView::Right,+ })+ }/// Frame a bounding box.pub fn fit(&mut self, b: &Aabb3) {if b.is_empty() {@@ -666,6 +697,49 @@ mod tests {assert!((sx - 600.0).abs() < 1e-6 && (sy - 300.0).abs() < 1e-6);}+ /// The #2682 alignment policy: each axis view is detected, off-axis and iso are not, and+ /// the tolerance admits near-aligned cameras (set_view's Top stops a hair short of pi/2).+ #[test]+ fn aligned_standard_view_detects_axis_views_within_tolerance() {+ let tol = 1.0_f64.to_radians();+ for v in [StandardView::Front, StandardView::Back, StandardView::Top, StandardView::Bottom, StandardView::Left, StandardView::Right] {+ let mut cam = Camera::default();+ cam.set_view(v);+ assert_eq!(cam.aligned_standard_view(tol), Some(v), "{v:?}");+ }+ // Iso and off-axis orientations are never aligned.+ let iso = Camera { yaw: StandardView::Iso.angles().0, pitch: StandardView::Iso.angles().1, ..Camera::default() };+ assert_eq!(iso.aligned_standard_view(tol), None);+ let off = Camera { yaw: 0.5, pitch: 0.3, ..Camera::default() };+ assert_eq!(off.aligned_standard_view(tol), None);+ // Within tolerance of Front (yaw and pitch each a half-degree off) it still counts.+ let near = Camera { yaw: 0.5_f64.to_radians(), pitch: 0.5_f64.to_radians(), ..Camera::default() };+ assert_eq!(near.aligned_standard_view(tol), Some(StandardView::Front));+ // Just outside the tolerance it does not.+ let past = Camera { yaw: 2.0_f64.to_radians(), pitch: 0.0, ..Camera::default() };+ assert_eq!(past.aligned_standard_view(tol), None);+ // Yaw wraps: 3*pi/2 is Right, negative yaw works, and at the poles any yaw counts.+ assert_eq!(+ Camera { yaw: 3.0 * std::f64::consts::FRAC_PI_2, pitch: 0.0, ..Camera::default() }.aligned_standard_view(tol),+ Some(StandardView::Right)+ );+ assert_eq!(+ Camera { yaw: -std::f64::consts::FRAC_PI_2, pitch: 0.0, ..Camera::default() }.aligned_standard_view(tol),+ Some(StandardView::Right)+ );+ assert_eq!(+ Camera { yaw: 2.0, pitch: std::f64::consts::FRAC_PI_2 - 1e-6, ..Camera::default() }.aligned_standard_view(tol),+ Some(StandardView::Top)+ );+ assert_eq!(+ Camera { yaw: 2.0, pitch: -std::f64::consts::FRAC_PI_2 + 1e-6, ..Camera::default() }.aligned_standard_view(tol),+ Some(StandardView::Bottom)+ );+ // Non-finite angles and a zero tolerance never align.+ assert_eq!(Camera { yaw: f64::NAN, pitch: 0.0, ..Camera::default() }.aligned_standard_view(tol), None);+ assert_eq!(Camera::default().aligned_standard_view(0.0), None);+ }+#[test]fn large_scene_projection_toggle_keeps_near_and_far_components_in_clip_range() {for scale in [1.0, 100.0] {diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex daa233a312..d38a677896 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -279,6 +279,11 @@ use solidcraft_engine::render::{Camera, CameraAnim, StandardView};pub use control::{ControlRequest, ControlResponse};+/// How close (radians) the camera must be to a standard axis view for the #2682 mode+/// (`auto_ortho_faces`) to snap the projection to orthographic. One degree: exact axis views+/// and Shift-snapped orbits land inside it, free orbiting does not linger in it.+pub const ORTHO_FACE_TOLERANCE: f64 = 1.0_f64.to_radians();+/// UI state that automation can read and set (`ui.set`).#[derive(Clone, Debug, Serialize, Deserialize)]#[serde(default, rename_all = "camelCase")]@@ -298,6 +303,9 @@ pub struct UiState {pub show_timeline: bool,pub show_grid: bool,pub perspective: bool,+ /// Perspective while orbiting, orthographic when the camera faces a standard axis view+ /// (#2682). Off by default; when on it decides the projection instead of `perspective`.+ pub auto_ortho_faces: bool,pub palette_open: bool,/// Dark theme (the default); a preference kept between runs.pub dark: bool,@@ -329,6 +337,7 @@ impl Default for UiState {show_timeline: true,show_grid: true,perspective: false,+ auto_ortho_faces: false,palette_open: false,dark: true,pick_off: Vec::new(),@@ -782,6 +791,7 @@ impl SolveApp {"dark": self.ui.dark,"grid": self.ui.show_grid,"perspective": self.ui.perspective,+ "auto_ortho_faces": self.ui.auto_ortho_faces,"pick_bodies": self.ui.pick_bodies,"auto_project": self.session.auto_project,"visual_style": self.ui.visual_style,@@ -819,6 +829,9 @@ impl SolveApp {if let Some(p) = flag("perspective") {self.ui.perspective = p;}+ if let Some(a) = flag("auto_ortho_faces") {+ self.ui.auto_ortho_faces = a;+ }if let Some(b) = flag("pick_bodies") {self.ui.pick_bodies = b;}@@ -1474,9 +1487,16 @@ impl SolveApp {}/// Is the view drawn in perspective? The perspective preference, except while a sketch is- /// open: a sketch is drawn and measured on its plane, so the view is parallel (no foreshortening).+ /// open (always orthographic) or when the #2682 mode is on: then the view is orthographic+ /// exactly when the camera faces a standard axis view, and perspective otherwise.pub fn perspective_view(&self) -> bool {- self.ui.perspective && self.session.active_sketch.is_none()+ if self.session.active_sketch.is_some() {+ return false;+ }+ if self.ui.auto_ortho_faces {+ return self.cam.aligned_standard_view(ORTHO_FACE_TOLERANCE).is_none();+ }+ self.ui.perspective}pub fn set_status(&mut self, s: impl Into<String>, error: bool) {diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rsindex 3ba554bea2..89710c3245 100644--- a/crates/ui-egui/src/viewport.rs+++ b/crates/ui-egui/src/viewport.rs@@ -5096,6 +5096,9 @@ fn view_menu(app: &mut SolveApp, ui: &mut egui::Ui, button: Rect) -> bool {ui.separator();crate::ui_kit::checkbox(ui, crate::i18n::tr_label("Grid"), &mut app.ui.show_grid);crate::ui_kit::checkbox(ui, crate::i18n::tr_label("Ground shadow"), &mut app.ui.ground_shadow);+ let auto_ortho =+ crate::ui_kit::checkbox(ui, crate::i18n::tr_label("Perspective while orbiting, ortho on faces"), &mut app.ui.auto_ortho_faces);+ crate::scenario::publish_handle("view:auto_ortho_faces", auto_ortho.rect.center());crate::prefs::ambient_occlusion_checkbox(app, ui, "Ambient occlusion");let tool = crate::ui_kit::checkbox(ui, crate::i18n::tr_label("Show tool at cursor"), &mut app.ui.tool_cursor);crate::scenario::publish_handle("view:tool_cursor", tool.rect.center());@@ -9519,6 +9522,40 @@ mod grid_plane_tests {assert!(projection(&app, rect).cam.fov > 0.0);}+ /// #2682: with the auto mode on, a camera facing a standard axis view draws orthographic and+ /// an off-axis one draws perspective; with the mode off the manual preference rules again.+ #[test]+ fn auto_ortho_faces_follows_camera_alignment() {+ let rect = Rect::from_min_size(Pos2::ZERO, egui::vec2(800.0, 600.0));+ let mut app = SolveApp::new(solidcraft_engine::Session::default(), crate::Services::default());+ assert!(!app.ui.auto_ortho_faces, "the option is off by default");+ app.ui.auto_ortho_faces = true;+ for view in [StandardView::Front, StandardView::Top, StandardView::Left] {+ app.cam.set_view(view);+ assert_eq!(projection(&app, rect).cam.fov, 0.0, "{view:?} is orthographic");+ }+ // Orbiting off the face returns to perspective immediately.+ app.cam.yaw += 0.3;+ app.cam.pitch += 0.2;+ assert!(projection(&app, rect).cam.fov > 0.0, "off-axis is perspective");+ // Snapping back to a face (a Shift-snapped orbit or Home-style view) restores it.+ app.cam.set_view(StandardView::Left);+ assert_eq!(projection(&app, rect).cam.fov, 0.0);+ // The preference round-trips through the saved prefs.+ let saved = app.prefs();+ let mut fresh = SolveApp::new(solidcraft_engine::Session::default(), crate::Services::default());+ fresh.load_prefs(&saved);+ assert!(fresh.ui.auto_ortho_faces, "the option is kept between runs");+ // With the mode off, the manual preference rules again (default orthographic).+ app.ui.auto_ortho_faces = false;+ assert_eq!(projection(&app, rect).cam.fov, 0.0);+ app.ui.perspective = true;+ assert!(projection(&app, rect).cam.fov > 0.0, "manual perspective is back");+ // An open sketch still wins: always orthographic.+ app.run("sketch.create", json!({"plane": "XZ"})).unwrap();+ assert_eq!(projection(&app, rect).cam.fov, 0.0);+ }+/// Create Sketch shows the Origin through bodies; the preference opts into that outside the/// tool while remaining off by default.#[test]