From fbaf44e97a413e1705b1dfb011849e19d4b21fa5 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 26 Nov 2026 06:17:14 +0400 Subject: fix(movement): drive tread material cycles from chassis travel Bind animated tread batches to their individual hull displacements. Preserve the authored phase sequence and derive nominal cycle length from the Control speed, scale and MAT0 duration. Checked stock tracked models in the normal AutoDemo window with Vulkan validation clean. Signed travel, stop and respawn are covered by a unit test. Per-track turning and physical cycle length are approximate. --- apps/fparkan-game/src/chassis_animation.rs | 103 ++++++++++++++++++++++++++++- apps/fparkan-game/src/main.rs | 14 ++++ 2 files changed, 114 insertions(+), 3 deletions(-) (limited to 'apps/fparkan-game/src') diff --git a/apps/fparkan-game/src/chassis_animation.rs b/apps/fparkan-game/src/chassis_animation.rs index 77019b2c..3691c181 100644 --- a/apps/fparkan-game/src/chassis_animation.rs +++ b/apps/fparkan-game/src/chassis_animation.rs @@ -1,9 +1,106 @@ //! Binds locomotion playback to the existing mesh pose update. -use super::{live_root_pose, ControlAnimationInstance, MissionRenderRoot, StaticPreviewScene}; -use fparkan_runtime::{loaded_mission_assets, loaded_walk_chassis, Engine, WalkAnimation}; +use super::{ + live_root_pose, ControlAnimationInstance, MaterialAnimationBinding, MissionRenderRoot, + StaticPreviewScene, +}; +use fparkan_material::MaterialAnimationMode; +use fparkan_runtime::{ + loaded_mission_assets, loaded_mission_control_device_tables, loaded_mission_object_drafts, + loaded_tracked_chassis, loaded_walk_chassis, Engine, TreadAnimation, WalkAnimation, +}; use fparkan_world::WorldSnapshot; -use std::collections::HashMap; +use std::collections::{BTreeMap, HashMap}; +use std::num::NonZeroU16; + +pub(super) struct TreadMaterials { + object_index: usize, + animation: TreadAnimation, + bindings: Vec<(usize, NonZeroU16)>, +} + +pub(super) fn connect_treads(engine: &Engine, scene: &mut StaticPreviewScene) { + let Some(drafts) = loaded_mission_object_drafts(engine) else { + return; + }; + let Some(tables) = loaded_mission_control_device_tables(engine) else { + return; + }; + let mut owners = BTreeMap::::new(); + for (index, binding) in scene.material_animations.iter().enumerate() { + let mut ranges = binding.variants.iter().flat_map(|variant| { + variant + .range_indices + .iter() + .filter_map(|&i| scene.mesh.draw_ranges.get(i)) + }); + let Some(first) = ranges.next() else { continue }; + if first.owner_scope == 0 + || first.batch_flags & 4 == 0 + || ranges + .any(|range| range.owner_scope != first.owner_scope || range.batch_flags & 4 == 0) + { + continue; + } + let object_index = (first.owner_scope - 1) as usize; + if !loaded_tracked_chassis(engine, object_index) + || binding.base.frozen_phase_index.is_some() + { + continue; + } + let Some(period) = binding + .base + .document + .animation_blocks + .get(binding.base.animation_block_index) + .filter(|block| block.mode() == MaterialAnimationMode::Loop && block.keys.len() > 1) + .and_then(|block| NonZeroU16::new(block.keys.last()?.k1)) + else { + continue; + }; + let Some(id) = drafts.get(object_index).and_then(|draft| draft.original_id) else { + continue; + }; + let Some(animation) = tables + .iter() + .find(|table| table.original_id == Some(id)) + .and_then(|table| table.owner_profile.as_ref()) + .and_then(|profile| TreadAnimation::new(f32::from_bits(profile.original_words[7]))) + else { + continue; + }; + owners + .entry(object_index) + .or_insert_with(|| TreadMaterials { + object_index, + animation, + bindings: Vec::new(), + }) + .bindings + .push((index, period)); + } + scene.tread_animations = owners.into_values().collect(); +} + +pub(super) fn update_treads( + owners: &mut [TreadMaterials], + materials: &mut [MaterialAnimationBinding], + roots: &[Option], + snapshot: &WorldSnapshot, +) { + for owner in owners { + let Some(root) = roots.get(owner.object_index).and_then(Option::as_ref) else { + continue; + }; + let Some(pose) = live_root_pose(root.handle, snapshot) else { + continue; + }; + owner.animation.sample(root.handle, pose); + for &(binding, period) in &owner.bindings { + materials[binding].normalized_phase_override = Some(owner.animation.phase(period)); + } + } +} pub(super) fn connect_walkers(engine: &Engine, scene: &mut StaticPreviewScene) { let Some(assets) = loaded_mission_assets(engine) else { diff --git a/apps/fparkan-game/src/main.rs b/apps/fparkan-game/src/main.rs index d0d6d2b8..ac63ed41 100644 --- a/apps/fparkan-game/src/main.rs +++ b/apps/fparkan-game/src/main.rs @@ -295,6 +295,7 @@ fn run(args: &[String]) -> Result { )?; if !args.viewer { chassis_animation::connect_walkers(&engine, &mut preview); + chassis_animation::connect_treads(&engine, &mut preview); } let terrain_materials_duration = terrain_materials_duration_started.elapsed(); // Atmosphere files are mission-local assets. Resolving them from the @@ -4736,6 +4737,7 @@ struct StaticPreviewScene { type16_present_ranges: HashSet, materials: Vec, material_animations: Vec, + tread_animations: Vec, ambient_effect_slots: Vec, control_point_light_slots: Vec, control_sound_slots: Vec, @@ -8829,6 +8831,7 @@ fn static_preview_mesh_and_materials( type16_present_ranges, materials, material_animations, + tread_animations: Vec::new(), ambient_effect_slots, control_point_light_slots, control_sound_slots, @@ -9963,6 +9966,7 @@ struct StaticVulkanApp { type16_present_ranges: HashSet, materials: Vec, material_animations: Vec, + tread_animations: Vec, ambient_effect_slots: Vec, control_point_light_slots: Vec, control_sound_slots: Vec, @@ -10104,6 +10108,7 @@ impl StaticVulkanApp { type16_present_ranges: preview.type16_present_ranges, materials: preview.materials, material_animations: preview.material_animations, + tread_animations: preview.tread_animations, ambient_effect_slots: preview.ambient_effect_slots, control_point_light_slots: preview.control_point_light_slots, control_sound_slots: preview.control_sound_slots, @@ -11552,6 +11557,14 @@ impl StaticVulkanApp { &self.ambient_effect_slots, self.view_camera.position, )?; + if let Some(frame) = &self.mission_frame { + chassis_animation::update_treads( + &mut self.tread_animations, + &mut self.material_animations, + &self.mission_render_roots, + &frame.snapshot, + ); + } let material_update = sample_material_animations( &mut self.material_animations, &self.materials, @@ -16198,6 +16211,7 @@ mod tests { type16_present_ranges: HashSet::new(), materials, material_animations: vec![material_animation], + tread_animations: Vec::new(), ambient_effect_slots: Vec::new(), control_point_light_slots: vec![control_slot], control_sound_slots: Vec::new(), -- cgit v1.2.3