//! Initial AniMesh attachment transforms shared by placed and previewed units. use super::{ compose_unit_pose, node38_pose, node38_pose_from_hierarchy, node38_pose_relative_to_root, node38_pose_relative_to_root_from_hierarchy, unit_component_is_visible, NodePoseBuffer, PreparedModel, PreparedVisual, VulkanNodePose, }; use fparkan_prototype::{UnitComponentRecord, UnitComponentTreeNode}; pub(super) fn initial_mount( index: usize, records: &[UnitComponentRecord], tree: &[UnitComponentTreeNode], components: &[(&PreparedVisual, Option<&PreparedModel>)], poses: &[Option], mounts: &[Option], animation_frame: Option, ) -> Result, String> { let Some(parent) = tree[index].parent_index else { return Ok(None); }; let Some((visual, model)) = components.get(parent) else { return Ok(None); }; if !unit_component_is_visible(parent, visual.source_type) { return Ok(None); } let model = model.ok_or_else(|| { format!("unit component {index} parent {parent} has no model for mount socket") })?; let link = records[index].parent_or_link; let socket = usize::try_from(link) .map_err(|_| format!("unit component {index} has negative mount socket {link}"))?; let parent_mount = mounts[parent]; let parent_socket = match (poses[parent].as_ref(), parent_mount.is_some()) { (Some(hierarchy), true) => node38_pose_relative_to_root_from_hierarchy(hierarchy, socket), (Some(hierarchy), false) => node38_pose_from_hierarchy(hierarchy, socket), (None, true) => node38_pose_relative_to_root(&model.validated, animation_frame, socket), (None, false) => node38_pose(&model.validated, animation_frame, socket), }.ok_or_else(|| { format!( "unit component {index} mount socket {socket} is absent from parent {parent} (parent source={:?}, node_count={}, slots={}, animation={})", model.source, model.validated.node_count, model.validated.slots.len(), model.validated.animation.is_some() ) })?; Ok(Some(compose_unit_pose( parent_mount.unwrap_or(VulkanNodePose { translation: [0.0; 3], rotation: [0.0, 0.0, 0.0, 1.0], }), parent_socket, ))) }