diff options
Diffstat (limited to 'adapters/fparkan-render-vulkan')
| -rw-r--r-- | adapters/fparkan-render-vulkan/src/asset_mesh.rs | 134 | ||||
| -rw-r--r-- | adapters/fparkan-render-vulkan/src/ffi.rs | 8 |
2 files changed, 134 insertions, 8 deletions
diff --git a/adapters/fparkan-render-vulkan/src/asset_mesh.rs b/adapters/fparkan-render-vulkan/src/asset_mesh.rs index dffbe710..bbe538fe 100644 --- a/adapters/fparkan-render-vulkan/src/asset_mesh.rs +++ b/adapters/fparkan-render-vulkan/src/asset_mesh.rs @@ -795,6 +795,107 @@ fn strip_node38_root_pose( Some(hierarchy) } +/// Draw ranges that represent one visual stage of a damageable mesh node. +#[derive(Clone)] +pub struct DamageStageRanges { + pub node: usize, + pub stage: u8, + pub ranges: std::ops::Range<usize>, +} + +/// Append authored damage meshes after the intact component's geometry. +/// The intact ranges keep their original indices and can still cast the base shadow. +pub fn append_msh_damage_stages( + mesh: &mut VulkanStaticMesh, + model: &ModelAsset, + transform: LegacyIron3dEulerTransform, + scale: [f32; 3], + node_poses: Option<&NodePoseBuffer>, + mount_pose: Option<VulkanNodePose>, +) -> Result<Vec<DamageStageRanges>, VulkanAssetMeshError> { + if model.node_stride != 38 || model.animation.is_none() { + return Ok(Vec::new()); + } + let hierarchy = node_poses + .cloned() + .or_else(|| node38_fallback_hierarchy(model)); + let hierarchy = if mount_pose.is_some() { + hierarchy.and_then(strip_node38_root_pose) + } else { + hierarchy + }; + let Some(hierarchy) = hierarchy else { + return Ok(Vec::new()); + }; + let base_range_count = mesh.draw_ranges.len(); + let mut base_range_start = 0; + let mut stages = Vec::new(); + for node in usize::from(mount_pose.is_some())..model.node_count { + let id = NodeId(u32::try_from(node).map_err(|_| VulkanAssetMeshError::IndexOutOfRange)?); + let Some(base) = selected_slot(model, id, Lod(0), Group(0)) else { + continue; + }; + let base_batches = + draw_batches(model, base).map_err(|_| VulkanAssetMeshError::IndexOutOfRange)?; + let base_range_end = base_range_start + + base_batches + .iter() + .filter(|batch| batch.index_count != 0) + .count(); + if selected_slot(model, id, Lod(1), Group(0)).is_some() { + stages.push(DamageStageRanges { + node, + stage: 0, + ranges: base_range_start..base_range_end, + }); + let pose = *hierarchy + .poses + .get(node) + .ok_or(VulkanAssetMeshError::IndexOutOfRange)?; + let pose = mount_pose.map_or(pose, |mount| { + compose_pose( + Pose { + translation: mount.translation, + rotation: mount.rotation, + }, + pose, + ) + }); + for stage in 1..3 { + let Some(slot) = selected_slot(model, id, Lod(stage), Group(0)) else { + break; + }; + let start = mesh.draw_ranges.len(); + for batch in + draw_batches(model, slot).map_err(|_| VulkanAssetMeshError::IndexOutOfRange)? + { + append_node_fallback_batch( + model, + batch, + pose.translation, + pose.rotation, + transform, + scale, + &mut mesh.vertices, + &mut mesh.indices, + &mut mesh.draw_ranges, + )?; + } + stages.push(DamageStageRanges { + node, + stage, + ranges: start..mesh.draw_ranges.len(), + }); + } + } + base_range_start = base_range_end; + } + if base_range_start != base_range_count { + return Err(VulkanAssetMeshError::IndexOutOfRange); + } + Ok(stages) +} + fn inverse_pose(pose: Pose) -> Pose { let [x, y, z, w] = pose.rotation; let length_squared = x * x + y * y + z * z + w * w; @@ -1532,16 +1633,20 @@ mod tests { } #[test] - fn cockpit_projection_does_not_include_the_exterior_slot() { + fn cockpit_and_damage_stage_use_distinct_node_slots() { let mut source = model( vec![[0.0; 3], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]], - vec![0, 1, 2, 2, 1, 0], + vec![0, 1, 2, 2, 1, 0, 1, 2, 0], vec![ batch(0, 3, 0), Batch { material_index: 7, ..batch(3, 3, 0) }, + Batch { + material_index: 9, + ..batch(6, 3, 0) + }, ], ); source.node_stride = 38; @@ -1551,7 +1656,8 @@ mod tests { source.nodes_raw[2..4].copy_from_slice(&u16::MAX.to_le_bytes()); source.nodes_raw[8..10].copy_from_slice(&0_u16.to_le_bytes()); source.nodes_raw[16..18].copy_from_slice(&1_u16.to_le_bytes()); - source.slots = [0, 1] + source.nodes_raw[18..20].copy_from_slice(&2_u16.to_le_bytes()); + source.slots = [0, 1, 2] .map(|batch_start| Slot { tri_start: batch_start, tri_count: 1, @@ -1577,7 +1683,7 @@ mod tests { translation: [10.0, 20.0, 30.0], orientation_radians: [0.0; 3], }; - let exterior = project_msh_to_static_mesh_in_world_space_with_node_pose_buffer( + let mut exterior = project_msh_to_static_mesh_in_world_space_with_node_pose_buffer( &source, transform, [1.0; 3], &poses, ) .expect("exterior"); @@ -1587,6 +1693,26 @@ mod tests { assert_eq!(exterior.draw_ranges[0].material_index, 0); assert_eq!(cockpit.draw_ranges.len(), 1); assert_eq!(cockpit.draw_ranges[0].material_index, 7); + let stages = append_msh_damage_stages( + &mut exterior, + &source, + transform, + [1.0; 3], + Some(&poses), + None, + ) + .expect("damage stage"); + assert_eq!(stages.len(), 2); + assert_eq!( + (stages[0].node, stages[0].stage, stages[0].ranges.clone()), + (0, 0, 0..1) + ); + assert_eq!( + (stages[1].node, stages[1].stage, stages[1].ranges.clone()), + (0, 1, 1..2) + ); + assert_eq!(exterior.draw_ranges[1].material_index, 9); + assert_eq!(exterior.vertices[3].position, [11.0, 20.0, 30.0]); } #[test] diff --git a/adapters/fparkan-render-vulkan/src/ffi.rs b/adapters/fparkan-render-vulkan/src/ffi.rs index 92e4291e..72df48e2 100644 --- a/adapters/fparkan-render-vulkan/src/ffi.rs +++ b/adapters/fparkan-render-vulkan/src/ffi.rs @@ -25,8 +25,8 @@ mod validation; const FRAME_VIEWS_PER_SLOT: usize = 3; pub use self::asset_mesh::{ - node38_pose, node38_pose_from_hierarchy, node38_pose_relative_to_root, - node38_pose_relative_to_root_from_hierarchy, project_cockpit_msh, + append_msh_damage_stages, node38_pose, node38_pose_from_hierarchy, + node38_pose_relative_to_root, node38_pose_relative_to_root_from_hierarchy, project_cockpit_msh, project_land_msh_lod_to_static_mesh_in_world_space, project_land_msh_to_static_mesh, project_land_msh_to_static_mesh_in_world_space, project_land_msh_to_static_mesh_in_xy_frame, project_msh_to_static_mesh, project_msh_to_static_mesh_in_world_space, @@ -39,8 +39,8 @@ pub use self::asset_mesh::{ project_msh_to_static_mesh_in_world_space_with_transform, project_msh_to_static_mesh_in_xy_frame, project_msh_to_static_mesh_in_xy_frame_with_node_fallback_poses, - project_msh_to_static_mesh_in_xy_frame_with_node_sampled_poses, VulkanAssetMeshError, - VulkanNodePose, VulkanStaticXyFrame, + project_msh_to_static_mesh_in_xy_frame_with_node_sampled_poses, DamageStageRanges, + VulkanAssetMeshError, VulkanNodePose, VulkanStaticXyFrame, }; pub use self::capabilities::{ probe_vulkan_runtime_capabilities, probe_vulkan_runtime_capabilities_for_request, |
