diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-03 14:03:22 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-03 14:03:22 +0300 |
| commit | 2de48c33dac8b269106cae54cf52410f14ab6217 (patch) | |
| tree | f60dfbf34ef21e13d59f170de5b5d7ff1cdd2342 /crates/fparkan-render/src/model_lod.rs | |
| parent | 670aee00789e21ebe2d01932719fbce44971b089 (diff) | |
| download | fparkan-2de48c33dac8b269106cae54cf52410f14ab6217.tar.xz fparkan-2de48c33dac8b269106cae54cf52410f14ab6217.zip | |
feat(render): cull distant world models using native preset thresholds
Diffstat (limited to 'crates/fparkan-render/src/model_lod.rs')
| -rw-r--r-- | crates/fparkan-render/src/model_lod.rs | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/crates/fparkan-render/src/model_lod.rs b/crates/fparkan-render/src/model_lod.rs new file mode 100644 index 00000000..51b07fdc --- /dev/null +++ b/crates/fparkan-render/src/model_lod.rs @@ -0,0 +1,154 @@ +//! Native world model detail selection from a projected bounding sphere. + +/// Object categories supported by the mission renderer. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum ModelKind { + /// Native kind 3. + Building, + /// Native kind 4. + Robot, + /// Native kind 10 uses the same thresholds as robots. + Static, +} + +/// Result of the world geometry selection, before frustum culling. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum ModelLod { + /// An authored group in the MSH node's slot table. + Group(u8), + /// The native result 8 suppresses the world draw. + Culled, +} + +/// Camera inputs used by the native angular size calculation. +#[derive(Clone, Copy)] +pub struct ModelLodCamera { + /// World-space camera position. + pub position: [f32; 3], + /// Horizontal viewport size in pixels. + pub width: f32, + /// Horizontal field of view in radians. + pub horizontal_fov: f32, +} + +impl ModelLodCamera { + /// Returns the projected angular radius in pixels for finite, valid inputs. + pub fn projected_radius(self, center: [f32; 3], radius: f32) -> Option<f32> { + let distance_squared = self + .position + .into_iter() + .zip(center) + .map(|(camera, center)| (f64::from(camera) - f64::from(center)).powi(2)) + .sum::<f64>(); + if !distance_squared.is_finite() + || !radius.is_finite() + || radius <= 0.0 + || !self.width.is_finite() + || self.width <= 0.0 + || !self.horizontal_fov.is_finite() + || self.horizontal_fov <= 0.0 + { + return None; + } + let radius = f64::from(radius); + let tangent_squared = distance_squared - radius * radius; + if tangent_squared < 0.0 { + return Some(self.width); + } + let angle = radius.atan2(tangent_squared.sqrt()) as f32; + Some(angle * (self.width / self.horizontal_fov)) + } +} + +/// `Terrain.dll` world geometry controls, independent of projected shadow detail. +#[derive(Clone, Copy, Default)] +pub struct ModelDetail(Option<i32>); + +impl ModelDetail { + /// Profile value passed through the original preset mapping. + pub const fn from_render_quality(quality: Option<i32>) -> Self { + Self(quality) + } + + /// Chooses the authored group from `Terrain.dll` RVA `0x2b490`. + pub fn select(self, kind: ModelKind, size: f32) -> ModelLod { + use ModelLod::{Culled, Group}; + + match kind { + ModelKind::Building => { + let detail = if self.0 == Some(0) { 0.7_f32 } else { 1.0 }; + let scaled = f64::from(size) * f64::from(detail); + if scaled < 10.0 { + Culled + } else if (scaled as f32) < 40.0 { + Group(3) + } else if (scaled as f32) < 80.0 { + Group(2) + } else if (scaled as f32) < 120.0 { + Group(1) + } else { + Group(0) + } + } + ModelKind::Robot | ModelKind::Static => { + if size < 4.0 { + return Culled; + } + let best = u8::from(self.0 == Some(0)); + if size >= 70.0 { + return Group(best); + } + let detail = match self.0 { + Some(0) => 0.5, + Some(2) => 1.5, + _ => 1.0, + }; + let scaled = f64::from(size) * detail; + if scaled < 8.0 { + Group(3) + } else if scaled < 30.0 { + Group(2) + } else if scaled < 70.0 { + Group(1) + } else { + Group(best) + } + } + } + } +} + +#[cfg(test)] +mod tests { + use super::{ModelDetail, ModelKind, ModelLod, ModelLodCamera}; + use ModelKind::{Building, Robot, Static}; + use ModelLod::{Culled, Group}; + + #[test] + fn projected_sphere_matches_native_detail_groups() { + let camera = ModelLodCamera { + position: [0.0; 3], + width: 800.0, + horizontal_fov: 1.3, + }; + // GOG Terrain+0x2b490, radius 10 with the sphere center on X. + for (kind, center_x, expected) in [ + (Building, 512.853, [Culled, Group(3), Group(3)]), + (Building, 153.571_17, [Group(3), Group(2), Group(2)]), + (Building, 51.566_074, [Group(1), Group(0), Group(0)]), + (Robot, 3076.928_7, [Culled, Culled, Culled]), + (Robot, 778.989_3, [Group(3), Group(3), Group(2)]), + (Robot, 114.106_5, [Group(2), Group(1), Group(0)]), + (Static, 87.976_82, [Group(1), Group(0), Group(0)]), + (Robot, 0.0, [Group(1), Group(0), Group(0)]), + ] { + let size = camera.projected_radius([center_x, 0.0, 0.0], 10.0).unwrap(); + for (quality, group) in [0, 1, 2].into_iter().zip(expected) { + assert_eq!( + ModelDetail::from_render_quality(Some(quality)).select(kind, size), + group + ); + } + } + } +} |
