aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/live_basements.rs
blob: bf4a66ca1c23d6acbbf51ebc3bc08ddcb4d49675 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use super::*;
use fparkan_runtime::{mission_basement_footprint, SpawnedUnit};
use fparkan_terrain::TerrainLodPlan;
use fparkan_terrain_format::LandMeshDocument;

pub(super) struct LiveBasements {
    footprints: Vec<MissionBasementFootprint>,
    face_slots: Vec<Option<usize>>,
    near_slot_count: usize,
    band_slot: usize,
    terrain_ranges: Vec<usize>,
    geometry_base: Option<(usize, usize)>,
    upload: Vec<VulkanDynamicDrawRange>,
}

impl LiveBasements {
    pub(super) fn prepare(
        land: &LandMeshDocument,
        terrain: &TerrainWorld,
        mesh: &mut VulkanStaticMesh,
        footprints: Vec<MissionBasementFootprint>,
    ) -> Result<Self, String> {
        // The near projection emits one triangle per foundation fragment, in
        // order. A later fragment inherits the draw slot of its source face.
        let source_faces = match terrain.basement_foundation() {
            Some(foundation) => foundation.fragments.iter().map(|f| f.source_face).collect(),
            None => land
                .face_lod_assignments()
                .map_err(|error| error.to_string())?
                .iter()
                .filter(|face| face.lod == TerrainLod::Lod0)
                .map(|face| Some(face.face_index))
                .collect::<Vec<_>>(),
        };
        let mut face_slots = vec![None; land.faces.len()];
        let mut band_slot = None;
        let mut near_slot_count = 0;
        for (slot, range) in mesh.draw_ranges.iter().enumerate() {
            let start = range.first_index as usize / 3;
            if start >= source_faces.len() {
                break;
            }
            let end = start + range.index_count as usize / 3;
            for &source_face in source_faces
                .get(start..end)
                .ok_or("terrain draw crosses the end of the near projection")?
            {
                if let Some(face) = source_face {
                    face_slots[face].get_or_insert(slot);
                } else {
                    band_slot.get_or_insert(slot);
                }
            }
            near_slot_count = slot + 1;
        }
        let band_slot = match band_slot {
            Some(slot) => slot,
            None => {
                let template = *mesh
                    .draw_ranges
                    .first()
                    .ok_or("terrain has no draw ranges")?;
                let slot = mesh.draw_ranges.len();
                mesh.draw_ranges.push(VulkanStaticDrawRange {
                    first_index: 0,
                    index_count: 0,
                    material_index: 0xff00,
                    terrain_base_only: true,
                    terrain_reflection: false,
                    ..template
                });
                slot
            }
        };
        Ok(Self {
            footprints,
            face_slots,
            near_slot_count,
            band_slot,
            terrain_ranges: Vec::new(),
            geometry_base: None,
            upload: Vec::new(),
        })
    }

    pub(super) fn bind_scene(&mut self, mesh: &VulkanStaticMesh) {
        self.terrain_ranges = mesh
            .draw_ranges
            .iter()
            .enumerate()
            .filter_map(|(index, range)| range.terrain_surface.then_some(index))
            .collect();
    }

    fn rebuild(
        &self,
        terrain: &TerrainWorld,
        footprint: &MissionBasementFootprint,
    ) -> Result<(BasementFoundation, VulkanStaticMesh, Vec<ShadowTriangle>), String> {
        let land = terrain.source_mesh().ok_or("live BAS needs Land.msh")?;
        let rings = self
            .footprints
            .iter()
            .chain(std::iter::once(footprint))
            .map(MissionBasementFootprint::contours)
            .collect::<Vec<_>>();
        let mut foundation = build_basement_foundations(land, &rings).map_err(|e| e.to_string())?;
        let source = project_land_msh_to_static_mesh_in_world_space(land)
            .map_err(|error| error.to_string())?;
        let (mesh, shadows) = project_basement_foundation(land, &source, &mut foundation)?;
        Ok((foundation, mesh, shadows))
    }

    fn draw_upload(
        &self,
        foundation: &BasementFoundation,
        projected: &VulkanStaticMesh,
        vertex_base: usize,
        index_base: usize,
    ) -> Result<(Vec<u32>, Vec<VulkanDynamicDrawRange>), String> {
        let mut batches = vec![Vec::new(); self.near_slot_count.max(self.band_slot + 1)];
        if projected.indices.len() != foundation.fragments.len() * 3 {
            return Err("live BAS projection lost a fragment".into());
        }
        for (tri, fragment) in projected.indices.chunks_exact(3).zip(&foundation.fragments) {
            let slot = match fragment.source_face {
                Some(face) => {
                    self.face_slots[face].ok_or("live BAS source face has no draw slot")?
                }
                None => self.band_slot,
            };
            batches[slot].extend_from_slice(tri);
        }
        let mut indices = Vec::with_capacity(projected.indices.len());
        let mut ranges = Vec::new();
        let slots = (0..self.near_slot_count)
            .chain((self.band_slot >= self.near_slot_count).then_some(self.band_slot));
        let vertex_base = u32::try_from(vertex_base).map_err(|e| e.to_string())?;
        for slot in slots {
            let batch = &batches[slot];
            ranges.push(VulkanDynamicDrawRange {
                range_index: self.terrain_ranges[slot],
                first_index: u32::try_from(index_base + indices.len())
                    .map_err(|e| e.to_string())?,
                index_count: u32::try_from(batch.len()).map_err(|e| e.to_string())?,
            });
            for &index in batch {
                indices.push(
                    vertex_base
                        .checked_add(index)
                        .ok_or("live BAS vertex index overflow")?,
                );
            }
        }
        Ok((indices, ranges))
    }
}

impl StaticVulkanApp {
    pub(super) fn cut_constructed_basements(&mut self) {
        let Some(mut basements) = self.live_basements.take() else {
            return;
        };
        let births = self
            .mission_frame
            .as_ref()
            .map(|frame| frame.constructed_buildings.clone())
            .unwrap_or_default();
        for building in births {
            if let Err(reason) = self.cut_constructed_basement(&mut basements, building) {
                eprintln!(
                    "render_coverage live_basement_stub=1 object={} reason={reason}",
                    building.object_index
                );
            }
        }
        self.live_basements = Some(basements);
    }

    fn cut_constructed_basement(
        &mut self,
        basements: &mut LiveBasements,
        building: SpawnedUnit,
    ) -> Result<(), String> {
        let engine = self.mission_engine.as_mut().ok_or("missing mission")?;
        let frame = self
            .mission_frame
            .as_mut()
            .ok_or("missing construction frame")?;
        let pose =
            live_root_pose(building.handle, &frame.snapshot).ok_or("missing building pose")?;
        let mut root = loaded_mission_object_drafts(engine).ok_or("missing built root")?
            [building.object_index]
            .clone();
        root.position = pose.position.map(f32::from_bits);
        root.orientation_raw = pose.orientation.map(f32::from_bits);
        root.scale = pose.scale.map(f32::from_bits);
        let assets = loaded_mission_assets(engine).ok_or("missing building assets")?;
        let Some(footprint) = mission_basement_footprint(assets, building.object_index, &root)?
        else {
            return Ok(());
        };
        let (foundation, projected, receivers) = basements.rebuild(&self.terrain, &footprint)?;
        let elevation = root.root_position_with_basement_delta(
            foundation
                .placements
                .last()
                .expect("new foundation placement")
                .placement_delta,
        )[2];
        let (vertex_base, index_base) = basements
            .geometry_base
            .unwrap_or((self.shadow_base_vertex, self.shadow_base_index));
        let (indices, upload) =
            basements.draw_upload(&foundation, &projected, vertex_base, index_base)?;
        let plan = self
            .terrain_lod
            .as_ref()
            .map(|_| {
                TerrainLodPlan::new(
                    self.terrain.source_mesh().expect("rebuild used Land.msh"),
                    Some(&foundation),
                )
                .map_err(|error| error.to_string())
            })
            .transpose()?;
        let terrain = self
            .terrain
            .as_ref()
            .clone()
            .with_basement_foundation(foundation)
            .map_err(|error| error.to_string())?;
        fparkan_runtime::place_constructed_basement(engine, frame, building, elevation)
            .map_err(|error| error.to_string())?;

        self.terrain = Arc::new(terrain);
        self.shadow_receivers = receivers;
        if let (Some(lod), Some(plan)) = (&mut self.terrain_lod, plan) {
            lod.replace_plan(plan);
        }
        self.mesh.vertices.truncate(vertex_base);
        self.mesh.indices.truncate(index_base);
        self.mesh.vertices.extend(projected.vertices);
        self.mesh.indices.extend(indices);
        self.shadow_base_vertex = self.mesh.vertices.len();
        self.shadow_base_index = self.mesh.indices.len();
        for range in &upload {
            let draw = &mut self.mesh.draw_ranges[range.range_index];
            draw.first_index = range.first_index;
            draw.index_count = range.index_count;
            self.world_range_index_counts
                .insert(range.range_index, range.index_count);
        }
        basements.geometry_base = Some((vertex_base, index_base));
        basements.footprints.push(footprint);
        basements.upload = upload;
        eprintln!(
            "live_basement_cut object={} elevation={elevation}",
            building.object_index
        );
        Ok(())
    }

    pub(super) fn upload_live_basements(&mut self) -> Result<(), String> {
        let (Some(renderer), Some(basements)) = (&mut self.renderer, &mut self.live_basements)
        else {
            return Ok(());
        };
        if basements.upload.is_empty() {
            return Ok(());
        }
        // The old shadow indices are in the replaced suffix. The normal shadow
        // pass assigns its new segment before presenting this frame.
        let mut ranges = basements.upload.clone();
        ranges.push(VulkanDynamicDrawRange {
            range_index: self.shadow_range.range_index,
            first_index: u32::try_from(self.shadow_base_index).map_err(|e| e.to_string())?,
            index_count: 0,
        });
        let (_, index_base) = basements.geometry_base.expect("pending live BAS upload");
        renderer
            .update_mesh_geometry(
                &self.mesh.vertices,
                &self.mesh.indices,
                &ranges,
                self.dynamic_vertex_start,
                index_base,
            )
            .map_err(|error| format!("upload live building foundation: {error}"))?;
        basements.upload.clear();
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use fparkan_terrain::BasementFoundationFragment;
    use fparkan_terrain_format::FullSurfaceMask;

    #[test]
    fn rebuilt_faces_keep_their_terrain_draw_slots() {
        let basements = LiveBasements {
            footprints: Vec::new(),
            face_slots: vec![Some(1), Some(0)],
            near_slot_count: 2,
            band_slot: 3,
            terrain_ranges: vec![5, 6, 7, 8],
            geometry_base: None,
            upload: Vec::new(),
        };
        let foundation = BasementFoundation {
            fragments: [Some(0), Some(1), None, Some(0)]
                .into_iter()
                .map(|source_face| BasementFoundationFragment {
                    source_face,
                    surface_mask: FullSurfaceMask(0),
                    positions: [[0.0; 3]; 3],
                })
                .collect(),
            placements: Vec::new(),
        };
        let projected = VulkanStaticMesh {
            vertices: Vec::new(),
            indices: (0..12).collect(),
            draw_ranges: Vec::new(),
        };

        let (indices, ranges) = basements
            .draw_upload(&foundation, &projected, 10, 300)
            .unwrap();

        assert_eq!(indices, [13, 14, 15, 10, 11, 12, 19, 20, 21, 16, 17, 18]);
        assert_eq!(
            ranges
                .iter()
                .map(|range| (range.range_index, range.first_index, range.index_count))
                .collect::<Vec<_>>(),
            [(5, 300, 3), (6, 303, 6), (8, 309, 3)]
        );
    }
}