unbanished_formats/types/animation.rs
1//! Animation groups: clips, events and blends.
2
3use super::*;
4
5/// An animation clip: the bones' transforms for each frame, and its events.
6#[derive(Debug, Clone)]
7pub struct AnimationClip {
8 /// The clip's name, for example `Walk`.
9 pub name: String,
10 /// The mesh it was made for.
11 pub mesh_name: String,
12 /// The original animation file.
13 pub source_name: String,
14 /// Whether the clip loops.
15 pub looping: bool,
16 /// The frame rate.
17 pub sample_rate: f32,
18 // +0x30/+0x34
19 /// Animation: source frame range for loop-point conversion.
20 pub start_frame: i32,
21 /// The last frame of the source range.
22 pub end_frame: i32,
23 // +0x38 / +0x48
24 /// Declared rate and stored sample interval.
25 pub declared_rate: f32,
26 /// The time between stored samples.
27 pub sample_interval: f32,
28 // +0x40
29 /// Source frame at which a looping animation resumes.
30 pub loop_start_frame: i32,
31 // +0x28 / +0x2c
32 /// Shared animation-instance configuration.
33 pub instance_count: i32,
34 /// Whether all entities share one instance of the clip.
35 pub instanced: bool,
36 /// The frame at which a looping clip starts again.
37 pub loop_start: usize,
38 /// The number of bones in each frame.
39 pub bone_count: usize,
40 /// Frame-major quaternion (xyzw) + translation (xyz) samples.
41 pub frames: Vec<Vec<crate::skeleton::Transform>>,
42 // +0x58: a serialised System::Time (microseconds, Runtime Timer::Startup 0x1800cd030).
43 /// The clip's length, in microseconds.
44 pub duration_ticks: i64,
45 /// The events of the clip.
46 pub events: Vec<AnimationEvent>,
47}
48
49/// An event in a clip (for example a footstep sound), at a time.
50#[derive(Debug, Clone)]
51pub struct AnimationEvent {
52 /// The frame at which it happens.
53 pub frame: i32,
54 /// The resource it plays (for example a sound effect).
55 pub resource: Option<String>,
56}
57
58/// How the game blends from one clip to another.
59#[derive(Debug, Clone)]
60pub struct AnimationBlend {
61 /// The clip that ends.
62 pub source: String,
63 /// The clip that begins.
64 pub target: String,
65 /// The blend time.
66 pub seconds: f32,
67}
68
69/// A group of animation clips for one skeleton.
70#[derive(Debug, Clone)]
71pub struct AnimationGroup {
72 /// The clips.
73 pub clips: Vec<AnimationClip>,
74 // +0x30; +0x38/+0x40
75 /// The blend time between clips that have no blend of their own, in seconds.
76 pub default_blend: f32,
77 /// The blends between named clips.
78 pub blends: Vec<AnimationBlend>,
79 // +0x48/+0x50
80 /// Precompiled [new clip][previous clip] transition matrix.
81 pub blend_matrix: Vec<f32>,
82 /// The resource it was read from, normalised, as its loader sets it (empty when unknown). The
83 /// original loads one object per resource (`ExternalFactory::FindOrLoad`), so this names the
84 /// group the way the original's pointer does.
85 pub name: String,
86}
87
88impl AnimationGroup {
89 /// The resource class name.
90 pub const CLASS: &'static str = "AnimationGroup";
91
92 /// `Graphics::AnimationGroup::Serialize` and `Graphics::Animation::Serialize` from the Ghidra
93 /// Runtime export. Animation samples are 4 quaternion floats and 3 translation floats;
94 /// UpdateNodes interpolates adjacent samples per mesh node.
95 ///
96 /// Original: Runtime `0x1800fdf30`, Runtime `0x1800fd910`.
97 pub fn read(r: &mut Reader) -> Result<Self> {
98 r.block(|r, _| {
99 let clip_count = r.count()?;
100 let mut clips = Vec::with_capacity(clip_count);
101 for _ in 0..clip_count {
102 let name = r.string()?;
103 let mesh_name = r.string()?;
104 let source_name = r.string()?;
105 let start_frame = r.i32()?;
106 let end_frame = r.i32()?;
107 let declared_rate = r.f32()?;
108 let looping = r.bool()?;
109 let loop_start_frame = r.i32()?;
110 let instance_count = r.i32()?;
111 let instanced = r.bool()?;
112 let event_count = r.count()?;
113 let events = (0..event_count).map(|_| Ok(AnimationEvent {
114 frame: r.i32()?, resource: r.external_ref()?,
115 })).collect::<Result<Vec<_>>>()?;
116 let frame_count = r.count()?;
117 let sample_rate = r.f32()?;
118 let sample_interval = r.f32()?;
119 let bone_count = r.count()?;
120 let sample_count = r.count()?;
121 if sample_count != frame_count.saturating_mul(bone_count) {
122 return Err(crate::error::Error::Format(format!(
123 "animation {name}: {sample_count} samples != {frame_count} frames x {bone_count} bones"
124 )));
125 }
126 let loop_start = if loop_start_frame == 0 || end_frame == start_frame {
127 0
128 } else {
129 ((((loop_start_frame - start_frame) as f32
130 / (end_frame - start_frame) as f32)
131 * frame_count as f32) as usize)
132 .min(frame_count.saturating_sub(1))
133 };
134 let mut frames = Vec::with_capacity(frame_count);
135 for _ in 0..frame_count {
136 let mut frame = Vec::with_capacity(bone_count);
137 for _ in 0..bone_count {
138 frame.push(crate::skeleton::Transform {
139 // Engine quaternions use the inverse convention to our column-vector
140 // transform math, as do the serialized mesh bind rotations.
141 rotation: {
142 let mut q = r.f32s()?;
143 q[..3].iter_mut().for_each(|v| *v = -*v);
144 q
145 },
146 translation: r.f32s()?,
147 });
148 }
149 frames.push(frame);
150 }
151 let duration_ticks = r.i64()?;
152 clips.push(AnimationClip {
153 name,
154 mesh_name,
155 source_name,
156 looping,
157 sample_rate,
158 start_frame,
159 end_frame,
160 declared_rate,
161 sample_interval,
162 loop_start_frame,
163 instance_count,
164 instanced,
165 loop_start,
166 bone_count,
167 frames,
168 duration_ticks,
169 events,
170 });
171 }
172 let blend_count = r.count()?;
173 let blends = (0..blend_count).map(|_| Ok(AnimationBlend {
174 source: r.string()?, target: r.string()?, seconds: r.f32()?,
175 })).collect::<Result<Vec<_>>>()?;
176 let default_blend = r.f32()?;
177 let blend_matrix_count = r.count()?;
178 let blend_matrix = (0..blend_matrix_count).map(|_| r.f32()).collect::<Result<Vec<_>>>()?;
179 Ok(AnimationGroup { clips, default_blend, blends, blend_matrix, name: String::new() })
180 })
181 }
182}