diff options
Diffstat (limited to 'apps/fparkan-game/src/designer_geometry.rs')
| -rw-r--r-- | apps/fparkan-game/src/designer_geometry.rs | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/apps/fparkan-game/src/designer_geometry.rs b/apps/fparkan-game/src/designer_geometry.rs new file mode 100644 index 00000000..db754a41 --- /dev/null +++ b/apps/fparkan-game/src/designer_geometry.rs @@ -0,0 +1,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, + }) + } +} |
