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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
//! Standalone Unit DAT assembly for the constructor's model viewport.
use super::{
add3, aggregate_component_sphere, append_static_preview_component_with_lightmaps,
configure_world_material_categories, length3, model_mounts::initial_mount,
mounted_shadow_sphere, native_msh_cull_mode, native_msh_handedness,
node38_pose_relative_to_root, node38_pose_relative_to_root_from_hierarchy,
project_msh_to_static_mesh_in_world_space_with_node_fallback_poses,
project_msh_to_static_mesh_in_world_space_with_node_fallback_poses_and_mount,
project_msh_to_static_mesh_in_world_space_with_node_pose_buffer,
project_msh_to_static_mesh_in_world_space_with_node_pose_buffer_and_mount, scale3,
static_preview_component_materials, static_preview_component_pose_buffer,
static_preview_lightmap_textures, sub3, unit_component_is_visible, LegacyIron3dEulerTransform,
MissionAssets, VulkanStaticMaterial, VulkanStaticMesh,
};
use fparkan_prototype::{unit_component_tree, UnitDat};
pub(super) struct DesignerGeometry {
pub(super) mesh: VulkanStaticMesh,
pub(super) materials: Vec<VulkanStaticMaterial>,
pub(super) center: [f32; 3],
pub(super) radius: f32,
}
impl DesignerGeometry {
pub(super) fn prepare(project: &UnitDat, assets: &MissionAssets) -> Result<Self, String> {
let tree = unit_component_tree(&project.records).map_err(|error| error.to_string())?;
let components = assets
.visuals_for_object(0)
.iter()
.map(|id| {
let visual = assets.visual_by_id(*id).ok_or("missing designer visual")?;
let model = visual.model_id.and_then(|id| assets.model_by_id(id));
Ok((visual, model))
})
.collect::<Result<Vec<_>, &str>>()?;
if components.len() != tree.len() {
return Err("designer visuals do not match project components".into());
}
let mut poses = vec![None; components.len()];
let mut mounts = vec![None; components.len()];
let mut mesh = VulkanStaticMesh {
vertices: Vec::new(),
indices: Vec::new(),
draw_ranges: Vec::new(),
};
let mut materials = Vec::new();
let mut animations = Vec::new();
let mut spheres = Vec::new();
let transform = LegacyIron3dEulerTransform {
translation: [0.0; 3],
orientation_radians: [0.0; 3],
};
let scale = [1.0; 3];
for (index, &(visual, model)) in components.iter().enumerate() {
if !unit_component_is_visible(index, visual.source_type) {
continue;
}
let model = &model.ok_or("missing external designer model")?.validated;
let control = assets
.object_component_controls
.first()
.and_then(|controls| controls.get(index))
.and_then(Option::as_ref);
let bindings = control.map_or(&[][..], |control| control.bindings.as_slice());
poses[index] = static_preview_component_pose_buffer(model, bindings, None)?;
mounts[index] = initial_mount(
index,
&project.records,
&tree,
&components,
&poses,
&mounts,
None,
)?;
let mounted_root = mounts[index].and_then(|_| match poses[index].as_ref() {
Some(poses) => node38_pose_relative_to_root_from_hierarchy(poses, 1),
None => node38_pose_relative_to_root(model, None, 1),
});
spheres.extend(mounted_shadow_sphere(
model.bounding_sphere_center,
model.bounding_sphere_radius,
mounts[index],
mounted_root,
));
let mut component = match (poses[index].as_ref(), mounts[index]) {
(Some(poses), Some(mount)) => {
project_msh_to_static_mesh_in_world_space_with_node_pose_buffer_and_mount(
model, transform, scale, poses, mount,
)
}
(Some(poses), None) => {
project_msh_to_static_mesh_in_world_space_with_node_pose_buffer(
model, transform, scale, poses,
)
}
(None, Some(mount)) => {
project_msh_to_static_mesh_in_world_space_with_node_fallback_poses_and_mount(
model, transform, scale, mount,
)
}
(None, None) => project_msh_to_static_mesh_in_world_space_with_node_fallback_poses(
model, transform, scale,
),
}
.map_err(|error| error.to_string())?;
let handedness = native_msh_handedness(transform, scale);
for range in &mut component.draw_ranges {
range.pipeline_state.cull = native_msh_cull_mode(range.batch_flags, handedness);
}
let selectors = static_preview_component_materials(
assets,
visual,
&component,
Some(0),
&mut materials,
&mut animations,
)?;
let lightmaps = static_preview_lightmap_textures(assets, visual)?;
append_static_preview_component_with_lightmaps(
&mut mesh,
component,
&selectors,
Some(&mut materials),
&lightmaps,
Some(&mut animations),
)?;
}
let ranges = (0..mesh.draw_ranges.len()).collect::<Vec<_>>();
configure_world_material_categories(&mut mesh, &materials, &ranges)?;
if let Some(sphere) = aggregate_component_sphere(&spheres) {
return Ok(Self {
mesh,
materials,
center: sphere.center,
radius: sphere.radius,
});
}
let first = mesh
.vertices
.first()
.ok_or("project has no visible geometry")?
.position;
let (lo, hi) = mesh
.vertices
.iter()
.fold((first, first), |(lo, hi), vertex| {
(
std::array::from_fn(|axis| lo[axis].min(vertex.position[axis])),
std::array::from_fn(|axis| hi[axis].max(vertex.position[axis])),
)
});
let center = scale3(add3(lo, hi), 0.5);
let radius = length3(sub3(hi, lo)).ok_or("project geometry has no finite bounds")? * 0.5;
Ok(Self {
mesh,
materials,
center,
radius,
})
}
}
|