diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-02 21:23:02 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-02 21:23:02 +0300 |
| commit | fd159dbfa3e0ae399658a2cbea305d0a35debf77 (patch) | |
| tree | b8770bce80101c13e0c27fe6aedb39a2000aeddf /crates/fparkan-render | |
| parent | 31d770f66c89ce23ca0a9510aa1259e8505179dd (diff) | |
| download | fparkan-fd159dbfa3e0ae399658a2cbea305d0a35debf77.tar.xz fparkan-fd159dbfa3e0ae399658a2cbea305d0a35debf77.zip | |
fix(ui): match cockpit miniature camera fitting
Diffstat (limited to 'crates/fparkan-render')
| -rw-r--r-- | crates/fparkan-render/src/cockpit_projection.rs | 55 | ||||
| -rw-r--r-- | crates/fparkan-render/src/lib.rs | 1 |
2 files changed, 56 insertions, 0 deletions
diff --git a/crates/fparkan-render/src/cockpit_projection.rs b/crates/fparkan-render/src/cockpit_projection.rs new file mode 100644 index 00000000..697fcbb4 --- /dev/null +++ b/crates/fparkan-render/src/cockpit_projection.rs @@ -0,0 +1,55 @@ +//! Camera framing for the cockpit's object miniatures. + +/// Returns the camera distance and field of view. The original fits the +/// object sphere against the current scene's planes before drawing it with +/// the miniature camera. +#[must_use] +pub fn fit_sphere(radius: f32, scene_planes: [f32; 2]) -> Option<(f32, f32)> { + let radius = f64::from(radius); + let [near, far] = scene_planes.map(f64::from); + if !radius.is_finite() + || radius <= 0.0 + || !near.is_finite() + || near <= 0.0 + || !far.is_finite() + || radius * 2.0 > far - near + { + return None; + } + + // iron3d+0x41c65 fits the diameter into the current scene depth interval. + let distance = (2.0 * radius).clamp(near + radius, far - radius); + let fov = if distance == 2.0 * radius { + 75.0_f32.to_radians() + } else { + // Twice the angle subtended by the sphere, plus the native 25% margin. + let sine = f64::from((radius / distance) as f32); + ((1.0 - 2.0 * sine * sine).acos() * 1.25) as f32 + }; + Some((distance as f32, fov)) +} + +#[cfg(test)] +mod tests { + use super::fit_sphere; + + #[test] + fn targeter_uses_native_fits_on_both_clip_planes() { + let planes = [0.5, 700.0]; + for (radius, distance, angle) in [ + ( + f32::from_bits(0x4258_71df), + f32::from_bits(0x42d8_71df), + 0x3fa7_8d36, + ), + (0.25, 0.75, 0x3f59_7ee0), + (250.0, 450.0, 0x3fbc_7d6b), + ] { + let (fit_distance, fit_angle) = fit_sphere(radius, planes).unwrap(); + assert_eq!(fit_distance, distance); + assert!((fit_angle - f32::from_bits(angle)).abs() < 2e-7); + } + assert!(fit_sphere(349.75, planes).is_some()); + assert!(fit_sphere(350.0, planes).is_none()); + } +} diff --git a/crates/fparkan-render/src/lib.rs b/crates/fparkan-render/src/lib.rs index 8eaab4f9..0d55697c 100644 --- a/crates/fparkan-render/src/lib.rs +++ b/crates/fparkan-render/src/lib.rs @@ -3,6 +3,7 @@ pub mod cockpit_course; pub mod cockpit_lighting; +pub mod cockpit_projection; pub mod cockpit_radar; pub mod designer_lighting; pub mod water; |
