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 fparkan_render_vulkan::{DamageStageRanges, VulkanStaticMesh}; use std::collections::{HashMap, HashSet}; #[derive(Clone)] pub(super) struct WorldLodGeometry { pub(super) extra_vertices: usize, pub(super) include_damage: bool, pub(super) stages: Vec, } pub(super) struct ObjectLodBounds { kind: ModelKind, sphere: ShadowSphere, initial: LivePlacement, animated: bool, sources: Vec, } impl ObjectLodBounds { pub(super) fn new( native_kind: u32, placement: LivePlacement, animated: bool, spheres: &[ShadowComponentSphere], sources: Vec, ) -> Option { 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, pose_buffers: &[Vec>], mount_poses: &[Vec>], placements: &HashMap, ) { 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 selected_groups( bounds: &HashMap, quality: ModelDetail, camera: ModelLodCamera, ) -> HashMap { bounds .iter() .filter_map(|(&index, bound)| { let size = camera.projected_radius(bound.sphere.center, bound.sphere.radius)?; Some((index, quality.select(bound.kind, size))) }) .collect() } pub(super) fn inactive_ranges( mesh: &VulkanStaticMesh, world_ranges: &[usize], draw_groups: &HashMap, selected: &HashMap, mut detached: impl FnMut(usize) -> bool, hidden: &mut HashSet, ) { for &range_index in world_ranges { let range = &mesh.draw_ranges[range_index]; let Some(owner) = range.owner_scope.checked_sub(1) else { continue; }; let selection = selected .get(&(owner as usize)) .copied() .unwrap_or(ModelLod::Group(0)); let hide = match selection { ModelLod::Culled => true, ModelLod::Group(group) => draw_groups .get(&range_index) .is_some_and(|mask| mask & (1 << group) == 0), }; if hide && !detached(range_index) { hidden.insert(range_index); } } } pub(super) fn sample_component_spheres( sources: &[DynamicShadowSphereSource], pose_buffers: Option<&[Option]>, mount_poses: Option<&[Option]>, ) -> Vec { 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 { 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 }) }