From ec513e13e598964e8139bf3518bb35ebee176b7a Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 26 Nov 2026 11:15:34 +0400 Subject: fix(render): size warbot shield pulses from mounted models --- crates/fparkan-runtime/src/lib.rs | 2 +- crates/fparkan-runtime/src/projectile_shield.rs | 94 +++++++++++++++++++++++-- crates/fparkan-runtime/src/weapon_pose.rs | 43 +++++++++++ 3 files changed, 133 insertions(+), 6 deletions(-) (limited to 'crates') diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index be2005b1..f11614fb 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -1417,7 +1417,7 @@ pub struct Engine { autodemo_reinforcements: Option, production: Option, destruction_effects: Option>, - shield_impacts: Option>, + shield_impacts: Option, } struct LoadedMissionState { diff --git a/crates/fparkan-runtime/src/projectile_shield.rs b/crates/fparkan-runtime/src/projectile_shield.rs index 0d47a5e4..d41ea0bd 100644 --- a/crates/fparkan-runtime/src/projectile_shield.rs +++ b/crates/fparkan-runtime/src/projectile_shield.rs @@ -2,7 +2,8 @@ use crate::{ control_device_budget_coefficient, control_device_selector_zero, fixed_words_ascii_c_string, - projectile::unit_sphere, Engine, MissionControlDeviceTableStatus, ProjectileContact, + projectile::unit_sphere, weapon_pose::WeaponPoses, Engine, MissionControlDeviceTableStatus, + ProjectileContact, }; use fparkan_render::LegacyIron3dEulerTransform; use fparkan_world::transform_state; @@ -17,9 +18,41 @@ pub struct ShieldImpact { pub world_from_effect: [[f32; 4]; 4], } +pub(super) struct ShieldImpacts { + pulses: Vec, + local_spheres: Vec>, +} + /// Enables collection of transient shield pulses for the presentation layer. pub fn collect_shield_impacts(engine: &mut Engine) { - engine.shield_impacts = Some(Vec::new()); + let local_spheres = engine.loaded.as_ref().map_or_else(Vec::new, |mission| { + let assets = &mission.mission_assets; + mission + .object_drafts + .iter() + .enumerate() + .map(|(index, draft)| { + if draft.unit_dat_header_flags != Some(crate::boarding::WARBOT_CLASS) { + return None; + } + let controls = assets.object_component_controls.get(index)?; + if !controls + .iter() + .flatten() + .flat_map(|control| &control.typed_records) + .any(|record| record.tag == 9) + { + return None; + } + let poses = WeaponPoses::new(draft, assets, index); + enclosing_sphere(&poses.initial_model_spheres(assets, index)) + }) + .collect() + }); + engine.shield_impacts = Some(ShieldImpacts { + pulses: Vec::new(), + local_spheres, + }); } /// Drains shield pulses accumulated since the last presentation update. @@ -27,7 +60,7 @@ pub fn take_shield_impacts(engine: &mut Engine) -> Vec { engine .shield_impacts .as_mut() - .map_or_else(Vec::new, std::mem::take) + .map_or_else(Vec::new, |impacts| std::mem::take(&mut impacts.pulses)) } /// Finds authored shield effects for placed units and prepared spawn slots. @@ -131,16 +164,57 @@ pub(super) fn absorb(engine: &mut Engine, hit: ProjectileContact, volume: f32) - deltas[sector] = (f32::from_bits(deltas[sector]) + (updated - fraction)).to_bits(); if let Some(impacts) = engine.shield_impacts.as_mut() { if let Some(source) = shield_effect_source(&shield.descriptor.fixed_words) { - impacts.push(ShieldImpact { + let display_sphere = || { + let (center, radius) = *impacts.local_spheres.get(index)?.as_ref()?; + let scale = pose.scale.map(f32::from_bits); + let transform = LegacyIron3dEulerTransform { + translation: pose.position.map(f32::from_bits), + orientation_radians: pose.orientation.map(f32::from_bits), + }; + Some(( + transform.try_transform_scaled_point(center, scale)?, + radius * scale.into_iter().map(f32::abs).fold(0.0, f32::max), + )) + }; + let (display_center, display_radius) = display_sphere().unwrap_or((center, radius)); + // Keep the direction of the actual contact on the collision + // sphere until the field has its own collision surface. + impacts.pulses.push(ShieldImpact { object_index: index, source, - world_from_effect: effect_transform(center, offset, radius), + world_from_effect: effect_transform(display_center, offset, display_radius), }); } } remaining } +fn enclosing_sphere(parts: &[([f32; 3], f32)]) -> Option<([f32; 3], f32)> { + // AniMesh+0x9510 weights model centers by the authored radii. A second + // pass then puts even the most distant model sphere inside the result. + let weight: f32 = parts.iter().map(|(_, radius)| radius).sum(); + if weight <= 0.0 { + return None; + } + let center = std::array::from_fn(|axis| { + parts + .iter() + .map(|(point, radius)| point[axis] * radius) + .sum::() + / weight + }); + let radius = parts + .iter() + .map(|(point, radius)| { + let distance_sq: f32 = (0..3) + .map(|axis| (center[axis] - point[axis]).powi(2)) + .sum(); + distance_sq.sqrt() + radius + }) + .fold(0.0, f32::max); + Some((center, radius)) +} + fn effect_transform(center: [f32; 3], [x, y, z]: [f32; 3], radius: f32) -> [[f32; 4]; 4] { // Control+0x2c844 builds X from the incident side of the sphere, Y in the // horizontal plane, and Z as their cross product. @@ -181,6 +255,13 @@ fn intercept(volume: f32, capacity: f32) -> (f32, f32) { mod tests { use super::*; + #[test] + fn enclosing_sphere_uses_radii_to_weight_the_center() { + let parts = [([0.0; 3], 2.0), ([6.0, 0.0, 0.0], 1.0)]; + assert_eq!(enclosing_sphere(&parts), Some(([2.0, 0.0, 0.0], 5.0))); + assert_eq!(enclosing_sphere(&[]), None); + } + #[test] fn shield_spends_strength_before_dealing_hull_damage() { assert_eq!(intercept(899.0, 3040.0), (0.0, 2141.0)); @@ -253,6 +334,9 @@ mod tests { }) .expect("stock shield with a charged front sector"); collect_shield_impacts(&mut engine); + let (center, radius) = engine.shield_impacts.as_ref().unwrap().local_spheres[0] + .expect("stock AutoDemo warbot enclosure"); + assert!(center[2] > 0.0 && radius > 6.0); assert!( apply_projectile_contact(&mut engine, contact, capacity * 0.5) .unwrap() diff --git a/crates/fparkan-runtime/src/weapon_pose.rs b/crates/fparkan-runtime/src/weapon_pose.rs index 2df14f27..f5f7758e 100644 --- a/crates/fparkan-runtime/src/weapon_pose.rs +++ b/crates/fparkan-runtime/src/weapon_pose.rs @@ -165,6 +165,49 @@ impl WeaponPoses { self.aims.get(component).copied().flatten() } + pub(super) fn initial_model_spheres( + &self, + assets: &MissionAssets, + index: usize, + ) -> Vec<([f32; 3], f32)> { + let models: Vec<_> = assets + .visuals_for_object(index) + .iter() + .map(|&id| assets.model_by_id(assets.visual_by_id(id)?.model_id?)) + .collect(); + let controls = assets.object_component_controls.get(index); + let hierarchies: Vec<_> = models + .iter() + .enumerate() + .map(|(component, model)| { + let model = &model.as_ref()?.validated; + let bindings = controls + .and_then(|controls| controls.get(component)?.as_ref()) + .map_or(&[][..], |control| control.bindings.as_slice()); + initial_component_node_times(model.node_count, bindings) + .ok() + .and_then(|times| node38_sampled_hierarchy_at_times(model, ×)) + .or_else(|| node38_fallback_hierarchy(model)) + }) + .collect(); + models + .into_iter() + .zip(&self.mounts) + .filter_map(|(model, mount)| { + let model = &model?.validated; + let center = model.bounding_sphere_center; + let center = match *mount { + Mount::Root => center, + Mount::Socket(parent, socket) => { + mounted_point(&hierarchies, &self.mounts, parent, socket, center)? + } + Mount::Unavailable => return None, + }; + Some((center, model.bounding_sphere_radius)) + }) + .collect() + } + #[cfg(test)] pub(super) fn has_aim(&self) -> bool { self.aims.iter().any(Option::is_some) -- cgit v1.2.3