aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-render/src/model_lod.rs
blob: 51b07fdc75f53103607eb6a2b75e4343a375f019 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
                );
            }
        }
    }
}