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 /apps/fparkan-game | |
| parent | 670aee00789e21ebe2d01932719fbce44971b089 (diff) | |
| download | fparkan-2de48c33dac8b269106cae54cf52410f14ab6217.tar.xz fparkan-2de48c33dac8b269106cae54cf52410f14ab6217.zip | |
feat(render): cull distant world models using native preset thresholds
Diffstat (limited to 'apps/fparkan-game')
| -rw-r--r-- | apps/fparkan-game/src/live_factory_scene.rs | 1 | ||||
| -rw-r--r-- | apps/fparkan-game/src/main.rs | 109 | ||||
| -rw-r--r-- | apps/fparkan-game/src/mission_startup.rs | 3 | ||||
| -rw-r--r-- | apps/fparkan-game/src/object_lod.rs | 127 | ||||
| -rw-r--r-- | apps/fparkan-game/src/object_render.rs | 34 |
5 files changed, 205 insertions, 69 deletions
diff --git a/apps/fparkan-game/src/live_factory_scene.rs b/apps/fparkan-game/src/live_factory_scene.rs index e5d6fbe0..dd35871a 100644 --- a/apps/fparkan-game/src/live_factory_scene.rs +++ b/apps/fparkan-game/src/live_factory_scene.rs @@ -415,6 +415,7 @@ impl StaticVulkanApp { .extend(parts.control_animation_instances_by_object); self.dynamic_shadow_casters .extend(parts.dynamic_shadow_casters); + self.model_lod_bounds.extend(parts.model_lod_bounds); self.sun_occlusion_range_indices .extend(parts.sun_occlusion_range_indices); self.shadow_casters.extend(parts.shadow_casters); diff --git a/apps/fparkan-game/src/main.rs b/apps/fparkan-game/src/main.rs index f67b1369..55a0091e 100644 --- a/apps/fparkan-game/src/main.rs +++ b/apps/fparkan-game/src/main.rs @@ -82,6 +82,7 @@ mod shield_effects; mod shot_sound; use projectile_render::{prepare_projectile_draw, ProjectileDraw}; mod live_basements; +mod object_lod; mod scene_point_lights; mod script_coverage; mod sun_visibility; @@ -2232,36 +2233,11 @@ fn refresh_dynamic_shadow_casters( dynamic.caster_index )); } - let component_spheres = dynamic - .component_spheres - .iter() - .filter_map(|source| { - let mount_pose = if source.mounted { - mount_poses - .get(dynamic.object_index) - .and_then(|items| items.get(source.component_index)) - .copied() - .flatten() - } else { - None - }; - if source.mounted && mount_pose.is_none() { - return None; - } - let node1_pose = if source.mounted { - pose_buffers - .get(dynamic.object_index) - .and_then(|items| items.get(source.component_index)) - .and_then(Option::as_ref) - .and_then(|hierarchy| { - node38_pose_relative_to_root_from_hierarchy(hierarchy, 1) - }) - } else { - None - }; - mounted_shadow_sphere(source.center, source.radius, mount_pose, node1_pose) - }) - .collect::<Vec<_>>(); + let component_spheres = object_lod::sample_component_spheres( + &dynamic.component_spheres, + pose_buffers.get(dynamic.object_index).map(Vec::as_slice), + mount_poses.get(dynamic.object_index).map(Vec::as_slice), + ); if component_spheres.len() != dynamic.component_spheres.len() { return Err(format!( "dynamic shadow caster {} lost one or more component sphere poses", @@ -2435,26 +2411,7 @@ fn aggregate_shadow_caster( if triangles.is_empty() { return None; } - let ShadowComponentSphere { - center: aggregate_center, - radius, - } = aggregate_component_sphere(component_spheres)?; - let max_scale = scale.iter().copied().fold(f32::NEG_INFINITY, f32::max); - if !max_scale.is_finite() || max_scale <= 0.0 { - return None; - } - // The native scale flag applies to the aggregate exactly once. The - // mission Euler transform then rotates/translates that scaled sphere. - let scaled_center = [ - aggregate_center[0] * scale[0], - aggregate_center[1] * scale[1], - aggregate_center[2] * scale[2], - ]; - let center = transform.try_transform_scaled_point(scaled_center, [1.0; 3])?; - let radius = radius * max_scale; - if !center.iter().all(|value| value.is_finite()) || !radius.is_finite() { - return None; - } + let sphere = object_lod::world_sphere(LivePlacement { transform, scale }, component_spheres)?; let world_axes = transform .try_row_major() .map(|matrix| { @@ -2468,7 +2425,7 @@ fn aggregate_shadow_caster( Some(ShadowCaster { id, world_axes, - sphere: ShadowSphere { center, radius }, + sphere, native_kind, triangles, }) @@ -4514,6 +4471,8 @@ fn multiply_row_major(left: [f32; 16], right: [f32; 16]) -> [f32; 16] { struct StaticPreviewScene { fixed_water_clock_ms: Option<u32>, + model_lod_bounds: HashMap<usize, object_lod::ObjectLodBounds>, + model_detail: fparkan_render::model_lod::ModelDetail, mesh: VulkanStaticMesh, dynamic_vertex_start: usize, terrain_lod: Option<terrain_lod::TerrainLodDraws>, @@ -7882,6 +7841,7 @@ fn static_preview_mesh_and_materials( dynamic_shadow_casters, sun_occlusion_range_indices, shadow_casters, + model_lod_bounds, mesh_components, } = builder.parts; let free_camera = if legacy_camera.is_none() { @@ -7920,6 +7880,8 @@ fn static_preview_mesh_and_materials( Ok(StaticPreviewScene { fixed_water_clock_ms, robot_shadow_quality, + model_lod_bounds, + model_detail: Default::default(), mesh, dynamic_vertex_start, terrain_lod, @@ -8566,6 +8528,8 @@ fn player_movement_key(physical: PhysicalKey, logical: &Key) -> Option<KeyCode> } struct StaticVulkanApp { + model_lod_bounds: HashMap<usize, object_lod::ObjectLodBounds>, + model_detail: fparkan_render::model_lod::ModelDetail, mission_engine: Option<Engine>, mission_frame: Option<FrameResult>, mission_render_roots: Vec<Option<MissionRenderRoot>>, @@ -8741,6 +8705,8 @@ impl StaticVulkanApp { cockpit_geometry: cockpit_geometry::CockpitGeometry::default(), demo_camera: None, mesh: preview.mesh, + model_lod_bounds: preview.model_lod_bounds, + model_detail: preview.model_detail, dynamic_vertex_start: preview.dynamic_vertex_start, terrain_lod, live_basements, @@ -8992,21 +8958,25 @@ impl StaticVulkanApp { horizontal_fov: f32, active_materials: &HashMap<usize, ActiveMaterialState>, inactive_building_ranges: &HashSet<usize>, + lod_hidden_objects: &HashSet<usize>, renderer: &mut VulkanSmokeRenderer, ) -> Result<(), String> { for &range_index in world_range_indices { - if inactive_building_ranges.contains(&range_index) { - renderer - .set_draw_range_index_count(range_index, 0) - .map_err(|error| { - format!("hide inactive building stage range {range_index}: {error}") - })?; - continue; - } let range = mesh .draw_ranges .get(range_index) .ok_or_else(|| "world draw range index is out of bounds".to_string())?; + if inactive_building_ranges.contains(&range_index) + || range + .owner_scope + .checked_sub(1) + .is_some_and(|owner| lod_hidden_objects.contains(&(owner as usize))) + { + renderer + .set_draw_range_index_count(range_index, 0) + .map_err(|error| format!("hide world draw range {range_index}: {error}"))?; + continue; + } let state = active_materials.get(&range_index); let material_alpha = state.map_or_else( || { @@ -9845,6 +9815,12 @@ impl StaticVulkanApp { &mut self.mesh, &placements, )?; + object_lod::refresh_bounds( + &mut self.model_lod_bounds, + &self.component_pose_buffers, + &self.component_mount_poses, + &placements, + ); for binding in &mut self.building_collision_bindings { if let Some(sample) = placements.get(&binding.object_index) { let pose = sample.pose; @@ -10352,6 +10328,16 @@ impl StaticVulkanApp { &self.dynamic_component_geometry, ) }); + let view_fov = horizontal_fov(&frame.camera, frame.aspect); + let lod_hidden_objects = object_lod::hidden_objects( + &self.model_lod_bounds, + self.model_detail, + fparkan_render::model_lod::ModelLodCamera { + position: frame.camera.position, + width: frame.viewport[0], + horizontal_fov: view_fov, + }, + ); Self::update_world_transparency( &self.mesh, &self.materials, @@ -10361,9 +10347,10 @@ impl StaticVulkanApp { &self.native_handedness_by_range, self.terrain_lod.as_ref(), frame.camera.position, - horizontal_fov(&frame.camera, frame.aspect), + view_fov, &frame.material_update.active_materials, &inactive_building_ranges, + &lod_hidden_objects, renderer, )?; self.cockpit_geometry.apply(renderer, frame.viewport)?; @@ -14775,6 +14762,8 @@ mod tests { let view_camera = free_camera.preview_frame(16.0 / 9.0); let preview = StaticPreviewScene { robot_shadow_quality: RobotShadowQuality::default(), + model_lod_bounds: Default::default(), + model_detail: Default::default(), fixed_water_clock_ms: None, terrain_lod: None, live_basements: None, diff --git a/apps/fparkan-game/src/mission_startup.rs b/apps/fparkan-game/src/mission_startup.rs index 05fdc36e..cbe9bedc 100644 --- a/apps/fparkan-game/src/mission_startup.rs +++ b/apps/fparkan-game/src/mission_startup.rs @@ -278,6 +278,9 @@ pub(super) fn prepare( settings.integer("RENDER_QUALITY"), ), )?; + preview.model_detail = fparkan_render::model_lod::ModelDetail::from_render_quality( + settings.integer("RENDER_QUALITY"), + ); let terrain_microtexturing = settings.terrain_microtexturing(); let water_reflections = settings.water_reflections(); for range in &mut preview.mesh.draw_ranges { diff --git a/apps/fparkan-game/src/object_lod.rs b/apps/fparkan-game/src/object_lod.rs new file mode 100644 index 00000000..24912124 --- /dev/null +++ b/apps/fparkan-game/src/object_lod.rs @@ -0,0 +1,127 @@ +use super::{ + aggregate_component_sphere, mounted_shadow_sphere, node38_pose_relative_to_root_from_hierarchy, + DynamicShadowSphereSource, LivePlacement, NodePoseBuffer, SampledPlacement, + ShadowComponentSphere, ShadowSphere, VulkanNodePose, +}; +use fparkan_render::model_lod::{ModelDetail, ModelKind, ModelLod, ModelLodCamera}; +use std::collections::{HashMap, HashSet}; + +pub(super) struct ObjectLodBounds { + kind: ModelKind, + sphere: ShadowSphere, + initial: LivePlacement, + animated: bool, + sources: Vec<DynamicShadowSphereSource>, +} + +impl ObjectLodBounds { + pub(super) fn new( + native_kind: u32, + placement: LivePlacement, + animated: bool, + spheres: &[ShadowComponentSphere], + sources: Vec<DynamicShadowSphereSource>, + ) -> Option<Self> { + let kind = match native_kind { + 3 => ModelKind::Building, + 4 => ModelKind::Robot, + 10 => ModelKind::Static, + _ => return None, + }; + Some(Self { + kind, + sphere: world_sphere(placement, spheres)?, + initial: placement, + animated, + sources, + }) + } +} + +pub(super) fn refresh_bounds( + bounds: &mut HashMap<usize, ObjectLodBounds>, + pose_buffers: &[Vec<Option<NodePoseBuffer>>], + mount_poses: &[Vec<Option<VulkanNodePose>>], + placements: &HashMap<usize, SampledPlacement>, +) { + for (&index, bound) in bounds.iter_mut() { + let placement = placements.get(&index); + if !bound.animated && !placement.is_some_and(|sample| sample.changed) { + continue; + } + let pose = placement.map_or(bound.initial, |sample| sample.pose); + let spheres = sample_component_spheres( + &bound.sources, + pose_buffers.get(index).map(Vec::as_slice), + mount_poses.get(index).map(Vec::as_slice), + ); + if spheres.len() != bound.sources.len() { + continue; + } + if let Some(sphere) = world_sphere(pose, &spheres) { + bound.sphere = sphere; + } + } +} + +pub(super) fn hidden_objects( + bounds: &HashMap<usize, ObjectLodBounds>, + quality: ModelDetail, + camera: ModelLodCamera, +) -> HashSet<usize> { + bounds + .iter() + .filter_map(|(&index, bound)| { + let size = camera.projected_radius(bound.sphere.center, bound.sphere.radius)?; + (quality.select(bound.kind, size) == ModelLod::Culled).then_some(index) + }) + .collect() +} + +pub(super) fn sample_component_spheres( + sources: &[DynamicShadowSphereSource], + pose_buffers: Option<&[Option<NodePoseBuffer>]>, + mount_poses: Option<&[Option<VulkanNodePose>]>, +) -> Vec<ShadowComponentSphere> { + sources + .iter() + .filter_map(|source| { + let (mount_pose, node1_pose) = if source.mounted { + let mount = mount_poses? + .get(source.component_index) + .copied() + .flatten()?; + let node1 = pose_buffers + .and_then(|items| items.get(source.component_index)) + .and_then(Option::as_ref) + .and_then(|hierarchy| { + node38_pose_relative_to_root_from_hierarchy(hierarchy, 1) + }); + (Some(mount), node1) + } else { + (None, None) + }; + mounted_shadow_sphere(source.center, source.radius, mount_pose, node1_pose) + }) + .collect() +} + +pub(super) fn world_sphere( + placement: LivePlacement, + components: &[ShadowComponentSphere], +) -> Option<ShadowSphere> { + let aggregate = aggregate_component_sphere(components)?; + let scale = placement.scale; + let max_scale = scale.iter().copied().fold(f32::NEG_INFINITY, f32::max); + if !max_scale.is_finite() || max_scale <= 0.0 { + return None; + } + let center = placement + .transform + .try_transform_scaled_point(aggregate.center, scale)?; + let radius = aggregate.radius * max_scale; + if !center.iter().all(|value| value.is_finite()) || !radius.is_finite() { + return None; + } + Some(ShadowSphere { center, radius }) +} diff --git a/apps/fparkan-game/src/object_render.rs b/apps/fparkan-game/src/object_render.rs index de26313e..e689b9d0 100644 --- a/apps/fparkan-game/src/object_render.rs +++ b/apps/fparkan-game/src/object_render.rs @@ -18,6 +18,7 @@ pub(super) struct ObjectRenderParts { pub(super) dynamic_shadow_casters: Vec<DynamicShadowCaster>, pub(super) sun_occlusion_range_indices: Vec<usize>, pub(super) shadow_casters: Vec<ShadowCaster>, + pub(super) model_lod_bounds: HashMap<usize, object_lod::ObjectLodBounds>, pub(super) mesh_components: usize, } @@ -366,7 +367,7 @@ impl<'a> ObjectRenderBuilder<'a> { range.pipeline_state.cull = native_msh_cull_mode(range.batch_flags, component_handedness); } - if root_projected_shadow_kind.is_some() { + if root_native_kind.is_some() { let node1_pose = mount_pose.and_then(|_| match pose_buffer { Some(poses) => node38_pose_relative_to_root_from_hierarchy(poses, 1), None => node38_pose_relative_to_root( @@ -474,13 +475,32 @@ impl<'a> ObjectRenderBuilder<'a> { self.parts.mesh_components += 1; } } + let initial = LivePlacement { + transform: LegacyIron3dEulerTransform { + translation: mission_root.position, + orientation_radians: mission_root.orientation_raw, + }, + scale: mission_root.scale, + }; + let animated = dynamic_component_geometry + .iter() + .flatten() + .any(|c| c.animated); + if let Some(bounds) = root_native_kind.and_then(|kind| { + object_lod::ObjectLodBounds::new( + kind, + initial, + animated, + &root_shadow_spheres, + root_shadow_sphere_sources.clone(), + ) + }) { + self.parts.model_lod_bounds.insert(object_index, bounds); + } if let Some(native_kind) = root_projected_shadow_kind { let caster_id = u32::try_from(self.caster_start + self.parts.shadow_casters.len()) .map_err(|_| "shadow caster id exceeds u32".to_string())?; - let root_transform = LegacyIron3dEulerTransform { - translation: mission_root.position, - orientation_radians: mission_root.orientation_raw, - }; + let root_transform = initial.transform; if let Some(caster) = aggregate_shadow_caster( caster_id, native_kind, @@ -489,10 +509,6 @@ impl<'a> ObjectRenderBuilder<'a> { root_shadow_triangles, &root_shadow_spheres, ) { - let animated = dynamic_component_geometry - .iter() - .flatten() - .any(|c| c.animated); if live_roots || animated { let component_draw_range_indices = root_shadow_draw_range_indices .into_iter() |
