View angles: an optional readout under the orientation gizmo and a command to set them (#6032) (#6249)
* claim: #6032 * View angles: an optional readout under the orientation gizmo and a command to set them (#6032) View > View angles (also Preferences > Graphics, or view.angle_readout) shows the view's pitch, yaw and roll in degrees and Ortho/Persp under the axis view or cube, updated live; off by default. Clicking the readout opens a small card to type exact angles, which runs view.apply. The angles are the camera's own (Camera::view_angles / with_view_angles), about the up axis: yaw is the heading of the line of sight (Front 0, Left 90, Right -90, Back 180), pitch the eye's elevation (Top 90, Bottom -90), and a positive roll turns the model clockwise. view.apply documents this convention and now refuses a pitch past +/-pi/2. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -553,6 +553,64 @@ impl Camera {
|
||||
&& self.distance.is_finite()
|
||||
&& self.distance > 0.0
|
||||
}
|
||||
/// The view's orientation in degrees, as the viewport's View angles readout shows it (#6032).
|
||||
/// These are the camera's own angles: yaw in (−180, 180], pitch in [−90, 90], roll in
|
||||
/// (−180, 180]. A camera whose pitch passed a pole is read from its basis, as the same view
|
||||
/// with the angles in range; there, straight up or down, yaw and roll turn the image the same
|
||||
/// way, so roll reads 0 and yaw carries the turn. An invalid camera reads all zeros.
|
||||
pub fn view_angles(&self) -> ViewAngles {
|
||||
use std::f64::consts::FRAC_PI_2;
|
||||
let angles = if !self.is_valid() {
|
||||
(0.0, 0.0, 0.0)
|
||||
} else if self.pitch.abs() <= FRAC_PI_2 {
|
||||
(wrap_half_turn(self.yaw), self.pitch, wrap_half_turn(self.roll))
|
||||
} else {
|
||||
let (right, _, back) = self.basis();
|
||||
let (right, back) = (self.up.to_frame(right), self.up.to_frame(back));
|
||||
let pitch = back.z.clamp(-1.0, 1.0).asin();
|
||||
if back.x.hypot(back.y) < 1e-9 {
|
||||
// Straight up or down: the camera's right vector lies in the ground plane.
|
||||
((-right.y).atan2(right.x), pitch, 0.0)
|
||||
} else {
|
||||
let yaw = (-back.x).atan2(-back.y);
|
||||
let base_right = Vec3::new(yaw.cos(), -yaw.sin(), 0.0);
|
||||
let base_up = back.cross(base_right).normalized().unwrap_or(Vec3::Z);
|
||||
(yaw, pitch, right.dot(base_up).atan2(right.dot(base_right)))
|
||||
}
|
||||
};
|
||||
let degrees = |a: f64| if a.is_finite() { a.to_degrees() + 0.0 } else { 0.0 };
|
||||
ViewAngles { yaw: degrees(wrap_half_turn(angles.0)), pitch: degrees(angles.1).clamp(-90.0, 90.0), roll: degrees(wrap_half_turn(angles.2)) }
|
||||
}
|
||||
/// The same camera turned to `angles` (degrees, as [`Camera::view_angles`] reads them):
|
||||
/// target, distance, projection and up axis are kept. Yaw and roll may be any number of
|
||||
/// turns; pitch is held to ±90°. `None` when an angle is not finite.
|
||||
pub fn with_view_angles(&self, angles: ViewAngles) -> Option<Camera> {
|
||||
let ViewAngles { yaw, pitch, roll } = angles;
|
||||
if !(yaw.is_finite() && pitch.is_finite() && roll.is_finite()) {
|
||||
return None;
|
||||
}
|
||||
let radians = |a: f64| wrap_half_turn(a.to_radians());
|
||||
Some(Camera { yaw: radians(yaw), pitch: pitch.clamp(-90.0, 90.0).to_radians(), roll: radians(roll), ..*self })
|
||||
}
|
||||
}
|
||||
|
||||
/// A view's orientation in degrees about the up axis (#6032). Yaw is the heading of the line of
|
||||
/// sight: 0 looks along +Y (Front), 90 along +X (Left), 180 along −Y (Back), −90 along −X
|
||||
/// (Right). Pitch is the eye's elevation above the ground plane: 90 looks straight down (Top),
|
||||
/// −90 straight up (Bottom). Roll turns the image about the line of sight; positive turns the
|
||||
/// model clockwise on screen. With Y up the same angles turn about Y, the front view looking
|
||||
/// along −Z.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ViewAngles {
|
||||
pub yaw: f64,
|
||||
pub pitch: f64,
|
||||
pub roll: f64,
|
||||
}
|
||||
|
||||
/// An angle in radians brought into (−π, π]; any finite angle, however many turns.
|
||||
fn wrap_half_turn(a: f64) -> f64 {
|
||||
use std::f64::consts::{PI, TAU};
|
||||
if a.is_finite() { PI - (PI - a).rem_euclid(TAU) } else { 0.0 }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -900,4 +958,102 @@ mod tests {
|
||||
let v = Vec3::new(1.0, 2.0, 3.0);
|
||||
assert!(UpAxis::Y.from_frame(UpAxis::Y.to_frame(v)).dist(v) < 1e-12);
|
||||
}
|
||||
|
||||
fn same_view(a: &Camera, b: &Camera) -> bool {
|
||||
let ((ar, au, ab), (br, bu, bb)) = (a.basis(), b.basis());
|
||||
ar.dist(br) < 1e-9 && au.dist(bu) < 1e-9 && ab.dist(bb) < 1e-9
|
||||
}
|
||||
|
||||
/// The View angles readout (#6032): the named views read round numbers, with Front 0/0/0,
|
||||
/// in both up frames.
|
||||
#[test]
|
||||
fn named_views_read_round_view_angles() {
|
||||
let iso_pitch = (1.0_f64 / 3.0_f64.sqrt()).asin().to_degrees();
|
||||
for up in [UpAxis::Z, UpAxis::Y] {
|
||||
for (view, expected) in [
|
||||
(StandardView::Front, (0.0, 0.0, 0.0)),
|
||||
(StandardView::Back, (180.0, 0.0, 0.0)),
|
||||
(StandardView::Left, (90.0, 0.0, 0.0)),
|
||||
(StandardView::Right, (-90.0, 0.0, 0.0)),
|
||||
(StandardView::Top, (0.0, 90.0, 0.0)),
|
||||
(StandardView::Bottom, (0.0, -90.0, 0.0)),
|
||||
(StandardView::Iso, (-45.0, iso_pitch, 0.0)),
|
||||
] {
|
||||
let mut cam = Camera { up, roll: 0.7, ..Default::default() };
|
||||
cam.set_view(view);
|
||||
let a = cam.view_angles();
|
||||
assert!(
|
||||
(a.yaw - expected.0).abs() < 1e-3 && (a.pitch - expected.1).abs() < 1e-3 && (a.roll - expected.2).abs() < 1e-9,
|
||||
"{up:?} {view:?}: {a:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
// Yaw is the heading of the line of sight; positive roll turns the model clockwise.
|
||||
let cam = Camera::default().with_view_angles(ViewAngles { yaw: 90.0, pitch: 0.0, roll: 0.0 }).unwrap();
|
||||
assert!(cam.back().dist(-Vec3::X) < 1e-12, "yaw 90 looks along +X: {:?}", cam.back());
|
||||
let cam = Camera::default().with_view_angles(ViewAngles { yaw: 0.0, pitch: 0.0, roll: 30.0 }).unwrap();
|
||||
let (x, y, _) = cam.to_screen(cam.target + Vec3::X * 10.0, 800.0, 600.0, 50.0).unwrap();
|
||||
assert!(x > 400.0 && y > 300.0, "+X turned clockwise, below the centre: {x}, {y}");
|
||||
}
|
||||
|
||||
/// Angles read from a camera turn a camera to the same view, and set angles read back, for
|
||||
/// any orientation, including straight up and down and cameras whose stored pitch passed a pole.
|
||||
#[test]
|
||||
fn view_angles_round_trip_without_nan_at_the_poles() {
|
||||
use std::f64::consts::{FRAC_PI_2, TAU};
|
||||
for up in [UpAxis::Z, UpAxis::Y] {
|
||||
let base = Camera { up, target: Vec3::new(1.0, -2.0, 3.0), distance: 80.0, fov: 0.6, ..Default::default() };
|
||||
for yaw in [-180.0, -135.0, -90.0, -12.5, 0.0, 45.0, 90.0, 179.0, 180.0] {
|
||||
for pitch in [-90.0, -89.9, -35.0, 0.0, 15.0, 89.9, 90.0] {
|
||||
for roll in [-170.0, -90.0, 0.0, 30.0, 180.0] {
|
||||
let set = ViewAngles { yaw, pitch, roll };
|
||||
let cam = base.with_view_angles(set).unwrap();
|
||||
assert_eq!((cam.target, cam.distance, cam.fov, cam.up), (base.target, base.distance, base.fov, base.up));
|
||||
let read = cam.view_angles();
|
||||
let turn = |a: f64, b: f64| ((a - b + 540.0).rem_euclid(360.0) - 180.0).abs();
|
||||
assert!(
|
||||
turn(read.yaw, yaw) < 1e-9 && (read.pitch - pitch).abs() < 1e-9 && turn(read.roll, roll) < 1e-9,
|
||||
"{set:?} -> {read:?}"
|
||||
);
|
||||
assert!(same_view(&cam, &cam.with_view_angles(read).unwrap()));
|
||||
}
|
||||
}
|
||||
}
|
||||
// Stored angles out of range (a pitch over a pole, many turns) read in range as the same view.
|
||||
for (yaw, pitch, roll) in [
|
||||
(0.3, 2.0, 0.2),
|
||||
(-1.0, -3.0, 1.0),
|
||||
(7.0 * TAU + 0.5, 0.4, -9.0 * TAU),
|
||||
(0.8, FRAC_PI_2 + TAU, 0.3),
|
||||
(-0.6, -FRAC_PI_2 - TAU, 1.1),
|
||||
(1e6, 1e6, -1e6),
|
||||
] {
|
||||
let cam = Camera { yaw, pitch, roll, ..base };
|
||||
let a = cam.view_angles();
|
||||
assert!(a.yaw.is_finite() && a.pitch.is_finite() && a.roll.is_finite(), "{cam:?}: {a:?}");
|
||||
assert!((-180.0..=180.0).contains(&a.yaw) && (-90.0..=90.0).contains(&a.pitch) && (-180.0..=180.0).contains(&a.roll));
|
||||
assert!(same_view(&cam, &cam.with_view_angles(a).unwrap()), "{cam:?}: {a:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_view_angles_are_refused_or_held_in_range() {
|
||||
let cam = Camera::default();
|
||||
for bad in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
|
||||
for a in [
|
||||
ViewAngles { yaw: bad, ..Default::default() },
|
||||
ViewAngles { pitch: bad, ..Default::default() },
|
||||
ViewAngles { roll: bad, ..Default::default() },
|
||||
] {
|
||||
assert_eq!(cam.with_view_angles(a), None);
|
||||
}
|
||||
assert_eq!(Camera { yaw: bad, ..cam }.view_angles(), ViewAngles::default());
|
||||
assert_eq!(Camera { pitch: bad, ..cam }.view_angles(), ViewAngles::default());
|
||||
}
|
||||
let huge = cam.with_view_angles(ViewAngles { yaw: f64::MAX, pitch: f64::MAX, roll: -f64::MAX }).unwrap();
|
||||
assert!(huge.is_valid() && (huge.pitch - std::f64::consts::FRAC_PI_2).abs() < 1e-12, "{huge:?}");
|
||||
let a = huge.view_angles();
|
||||
assert!(a.yaw.is_finite() && a.roll.is_finite() && a.pitch == 90.0, "{a:?}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ pub use backend::{
|
||||
RenderMode, RenderOperation, RenderPolicy, RenderProfile, RenderRequest, ResolveError, ResolvedRequest, ScratchLayout, builtin_backend,
|
||||
resolve_request,
|
||||
};
|
||||
pub use camera::{Camera, CameraAnim, Mat4, Quat, StandardView, UpAxis, ease_in_out};
|
||||
pub use camera::{Camera, CameraAnim, Mat4, Quat, StandardView, UpAxis, ViewAngles, ease_in_out};
|
||||
pub use curve_display::{PixelCurveView, SampledPath, SamplingBudget, SamplingQuality, sample_segment, sample_segment_unclipped};
|
||||
pub use gif::gif;
|
||||
pub use product::{FaceAppearance, ProductMesh, ProductScene};
|
||||
|
||||
Reference in New Issue
Block a user