aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/object_lod.rs
blob: 249121245d363a5eb57aa1d087d16f051c94b122 (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
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 })
}