aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/object_lod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'apps/fparkan-game/src/object_lod.rs')
-rw-r--r--apps/fparkan-game/src/object_lod.rs127
1 files changed, 127 insertions, 0 deletions
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 })
+}