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
|
use super::{
append_node_fallback_batch, compose_pose, draw_batches, node38_fallback_hierarchy,
node38_sampled_hierarchy, selected_slot, strip_node38_root_pose, DamageStageRanges, Group,
LegacyIron3dEulerTransform, Lod, ModelAsset, NodeId, NodePoseBuffer, Pose,
VulkanAssetMeshError, VulkanNodePose, VulkanStaticMesh,
};
/// Source state for projecting a world component at an authored detail group.
#[derive(Clone, Copy)]
pub struct WorldLodPose<'a> {
/// Outer world placement of the component's root object.
pub transform: LegacyIron3dEulerTransform,
/// Scale in the root object's axes.
pub scale: [f32; 3],
/// Current evaluated hierarchy, if supplied by the scene.
pub hierarchy: Option<&'a NodePoseBuffer>,
/// Source frame used by the static viewer when no hierarchy is supplied.
pub frame: Option<u16>,
/// Pose of the parent socket replacing the child model's root.
pub mount: Option<VulkanNodePose>,
}
/// Draw ranges for one authored node and damage state at one or more groups.
pub struct WorldLodDraw {
/// Bit `n` selects group `n` of the source node table.
pub groups: u8,
/// Node, state and corresponding draw ranges.
pub state: DamageStageRanges,
}
/// Adds each distinct authored slot once per node and state. The initial group-0
/// geometry remains in place for consumers that address its original ranges.
pub fn append_world_msh_lods(
mesh: &mut VulkanStaticMesh,
model: &ModelAsset,
pose: WorldLodPose<'_>,
include_damage: bool,
base_stages: &[DamageStageRanges],
) -> Result<Vec<WorldLodDraw>, VulkanAssetMeshError> {
if model.node_stride != 38 || model.animation.is_none() {
return Ok(Vec::new());
}
let hierarchy = pose.hierarchy.cloned().or_else(|| {
pose.frame.map_or_else(
|| node38_fallback_hierarchy(model),
|frame| node38_sampled_hierarchy(model, frame),
)
});
let hierarchy = if pose.mount.is_some() {
hierarchy.and_then(strip_node38_root_pose)
} else {
hierarchy
};
let Some(hierarchy) = hierarchy else {
return Ok(Vec::new());
};
let mount = pose.mount.map(|mount| Pose {
translation: mount.translation,
rotation: mount.rotation,
});
let base_range_count = mesh.draw_ranges.len();
let mut base_cursor = 0;
let mut draws = Vec::new();
for node in usize::from(mount.is_some())..model.node_count {
let id = NodeId(u32::try_from(node).map_err(|_| VulkanAssetMeshError::IndexOutOfRange)?);
let node_pose = hierarchy.poses[node];
let node_pose = mount.map_or(node_pose, |mount| compose_pose(mount, node_pose));
for stage in 0..if include_damage { 3 } else { 1 } {
let slots =
[0, 1, 2, 3].map(|group| selected_slot(model, id, Lod(stage), Group(group)));
let existing = if stage == 0 {
let from = base_cursor;
if let Some(slot) = slots[0] {
base_cursor += draw_batches(model, slot)
.map_err(|_| VulkanAssetMeshError::IndexOutOfRange)?
.iter()
.filter(|batch| batch.index_count != 0)
.count();
}
if base_cursor > base_range_count {
return Err(VulkanAssetMeshError::IndexOutOfRange);
}
Some(from..base_cursor)
} else {
base_stages
.iter()
.find(|entry| entry.node == node && entry.stage == stage)
.map(|entry| entry.ranges.clone())
};
for (group, &slot) in slots.iter().enumerate() {
let Some(slot) = slot else { continue };
if slots[..group].contains(&Some(slot)) {
continue;
}
let groups = slots.iter().enumerate().fold(0, |mask, (index, &entry)| {
mask | (u8::from(entry == Some(slot)) << index)
});
let ranges = if let Some(ranges) = existing.as_ref().filter(|_| group == 0) {
ranges.clone()
} else {
let start = mesh.draw_ranges.len();
for batch in draw_batches(model, slot)
.map_err(|_| VulkanAssetMeshError::IndexOutOfRange)?
{
append_node_fallback_batch(
model,
batch,
node_pose.translation,
node_pose.rotation,
pose.transform,
pose.scale,
&mut mesh.vertices,
&mut mesh.indices,
&mut mesh.draw_ranges,
)?;
}
start..mesh.draw_ranges.len()
};
if !ranges.is_empty() {
draws.push(WorldLodDraw {
groups,
state: DamageStageRanges {
node,
stage,
ranges,
},
});
}
}
}
}
Ok(draws)
}
|