tirbofish/dropbear
main / crates / dropbear-engine / src / animation.rs · 20397 bytes
crates/dropbear-engine/src/animation.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
use crate::buffer::{DynamicBuffer, UniformBuffer, WritableBuffer};
use crate::graphics::SharedGraphicsContext;
use crate::model::{AnimationInterpolation, ChannelValues, Model, NodeTransform};
use dropbear_utils::Dirty;
use glam::Mat4;
use std::collections::HashMap;
use std::sync::Arc;
pub const MAX_MORPH_WEIGHTS: usize = 4096;
pub const MAX_SKINNING_MATRICES: usize = 256;
#[repr(C)]
#[derive(Copy, Clone, Default, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct MorphTargetInfo {
pub num_vertices: u32,
pub num_targets: u32,
pub base_offset: u32,
pub weight_offset: u32,
pub uses_morph: u32, // 0,1 bool
pub _padding: [u32; 3],
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct AnimationComponent {
#[serde(default)]
pub active_animation_index: Option<usize>,
#[serde(default)]
pub time: f32,
#[serde(default)]
pub speed: f32,
#[serde(default)]
pub looping: bool,
#[serde(default)]
pub is_playing: bool,
#[serde(default)]
pub animation_settings: HashMap<usize, AnimationSettings>,
#[serde(skip)]
pub local_pose: HashMap<usize, NodeTransform>,
#[serde(skip)]
pub skinning_matrices: Dirty<Vec<Mat4>>,
#[serde(skip)]
pub skinning_buffer: Option<DynamicBuffer<Mat4>>,
#[serde(skip)]
pub morph_deltas_buffer: Option<DynamicBuffer<f32>>,
#[serde(skip)]
pub morph_weights_buffer: Option<DynamicBuffer<f32>>,
#[serde(skip)]
pub morph_info_buffer: Option<UniformBuffer<MorphTargetInfo>>,
#[serde(skip)]
pub available_animations: Vec<String>,
#[serde(skip)]
pub last_animation_index: Option<usize>,
#[serde(skip)]
pub morph_weights: Dirty<HashMap<usize, Vec<f32>>>,
#[serde(skip)]
pub morph_weight_count: u32,
}
impl Clone for AnimationComponent {
fn clone(&self) -> Self {
Self {
active_animation_index: self.active_animation_index,
time: self.time,
speed: self.speed,
looping: self.looping,
is_playing: self.is_playing,
animation_settings: self.animation_settings.clone(),
local_pose: HashMap::new(),
skinning_matrices: Dirty::new(Vec::new()),
skinning_buffer: None,
morph_deltas_buffer: None,
morph_weights_buffer: None,
morph_info_buffer: None,
available_animations: Vec::new(),
last_animation_index: None,
morph_weights: Dirty::new(HashMap::new()),
morph_weight_count: 0,
}
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct AnimationSettings {
#[serde(default)]
pub time: f32,
#[serde(default)]
pub speed: f32,
#[serde(default)]
pub looping: bool,
#[serde(default)]
pub is_playing: bool,
}
impl Default for AnimationSettings {
fn default() -> Self {
Self {
time: 0.0,
speed: 1.0,
looping: true,
is_playing: true,
}
}
}
impl Default for AnimationComponent {
fn default() -> Self {
Self {
active_animation_index: None,
time: 0.0,
speed: 1.0,
looping: true,
is_playing: true,
animation_settings: HashMap::new(),
local_pose: HashMap::new(),
skinning_matrices: Dirty::new(Vec::new()),
available_animations: vec![],
last_animation_index: None,
morph_weights: Dirty::new(HashMap::new()),
morph_weight_count: 0,
skinning_buffer: None,
morph_deltas_buffer: None,
morph_weights_buffer: None,
morph_info_buffer: None,
}
}
}
impl AnimationComponent {
pub fn new() -> Self {
Self::default()
}
pub fn update(&mut self, dt: f32, model: &Model) {
puffin::profile_function!(&model.label);
self.available_animations = model
.animations
.iter()
.map(|v| v.name.clone())
.collect::<Vec<_>>();
if self.active_animation_index != self.last_animation_index {
self.local_pose.clear();
self.morph_weights.clear();
self.last_animation_index = self.active_animation_index;
}
let Some(anim_idx) = self.active_animation_index else {
self.reset_to_bind_pose(model);
return;
};
if anim_idx >= model.animations.len() {
self.reset_to_bind_pose(model);
return;
}
let settings =
self.animation_settings
.entry(anim_idx)
.or_insert_with(|| AnimationSettings {
time: self.time,
speed: self.speed,
looping: self.looping,
is_playing: self.is_playing,
});
let animation = &model.animations[anim_idx];
self.morph_weights.clear();
self.morph_weight_count = 0;
if settings.is_playing {
settings.time += dt * settings.speed;
if settings.looping {
if animation.duration > 0.0 {
settings.time %= animation.duration;
}
} else {
settings.time = settings.time.clamp(0.0, animation.duration);
if settings.time >= animation.duration {
settings.is_playing = false;
}
}
}
self.time = settings.time;
self.speed = settings.speed;
self.looping = settings.looping;
self.is_playing = settings.is_playing;
for channel in &animation.channels {
let count = channel.times.len();
if count == 0 {
continue;
}
if count == 1 || settings.time <= channel.times[0] {
Self::apply_single_keyframe(
channel,
0,
&mut self.local_pose,
&mut self.morph_weights,
model,
);
continue;
}
if settings.time >= channel.times[count - 1] {
Self::apply_single_keyframe(
channel,
count - 1,
&mut self.local_pose,
&mut self.morph_weights,
model,
);
continue;
}
let next_idx = channel.times.partition_point(|&t| t <= settings.time);
let prev_idx = next_idx.saturating_sub(1);
let start_time = channel.times[prev_idx];
let end_time = channel.times[next_idx];
let duration = end_time - start_time;
let factor = if duration > 0.0 {
(settings.time - start_time) / duration
} else {
0.0
};
let transform = self
.local_pose
.entry(channel.target_node)
.or_insert_with(|| {
model
.nodes
.get(channel.target_node)
.map(|n| n.transform.clone())
.unwrap_or_else(NodeTransform::identity)
});
let dt = end_time - start_time;
match &channel.values {
ChannelValues::Translations(values) => {
transform.translation = match channel.interpolation {
AnimationInterpolation::Step => values[prev_idx],
AnimationInterpolation::Linear => {
let start = values[prev_idx];
let end = values[next_idx];
start.lerp(end, factor)
}
AnimationInterpolation::CubicSpline => {
let t = factor;
let t2 = t * t;
let t3 = t2 * t;
let idx0 = prev_idx * 3;
let idx1 = next_idx * 3;
if idx1 + 1 >= values.len() {
values[idx0 + 1]
} else {
let p0 = values[idx0 + 1];
let m0 = values[idx0 + 2] * dt;
let m1 = values[idx1 + 0] * dt;
let p1 = values[idx1 + 1];
let h00 = 2.0 * t3 - 3.0 * t2 + 1.0;
let h10 = t3 - 2.0 * t2 + t;
let h01 = -2.0 * t3 + 3.0 * t2;
let h11 = t3 - t2;
p0 * h00 + m0 * h10 + p1 * h01 + m1 * h11
}
}
};
}
ChannelValues::Rotations(values) => {
transform.rotation = match channel.interpolation {
AnimationInterpolation::Step => values[prev_idx],
AnimationInterpolation::Linear => {
let start = values[prev_idx];
let end = values[next_idx];
start.slerp(end, factor).normalize()
}
AnimationInterpolation::CubicSpline => {
let t = factor;
let t2 = t * t;
let t3 = t2 * t;
let idx0 = prev_idx * 3;
let idx1 = next_idx * 3;
if idx1 + 1 >= values.len() {
values[idx0 + 1]
} else {
let p0 = values[idx0 + 1];
let m0 = values[idx0 + 2] * dt;
let m1 = values[idx1 + 0] * dt;
let p1 = values[idx1 + 1];
let h00 = 2.0 * t3 - 3.0 * t2 + 1.0;
let h10 = t3 - 2.0 * t2 + t;
let h01 = -2.0 * t3 + 3.0 * t2;
let h11 = t3 - t2;
let res = p0 * h00 + m0 * h10 + p1 * h01 + m1 * h11;
res.normalize()
}
}
};
}
ChannelValues::Scales(values) => {
transform.scale = match channel.interpolation {
AnimationInterpolation::Step => values[prev_idx],
AnimationInterpolation::Linear => {
let start = values[prev_idx];
let end = values[next_idx];
start.lerp(end, factor)
}
AnimationInterpolation::CubicSpline => {
let t = factor;
let t2 = t * t;
let t3 = t2 * t;
let idx0 = prev_idx * 3;
let idx1 = next_idx * 3;
if idx1 + 1 >= values.len() {
values[idx0 + 1]
} else {
let p0 = values[idx0 + 1];
let m0 = values[idx0 + 2] * dt;
let m1 = values[idx1 + 0] * dt;
let p1 = values[idx1 + 1];
let h00 = 2.0 * t3 - 3.0 * t2 + 1.0;
let h10 = t3 - 2.0 * t2 + t;
let h01 = -2.0 * t3 + 3.0 * t2;
let h11 = t3 - t2;
p0 * h00 + m0 * h10 + p1 * h01 + m1 * h11
}
}
};
}
ChannelValues::MorphWeights(values) => {
let weights = self
.morph_weights
.entry(channel.target_node)
.or_insert_with(|| vec![0.0; values[0].len()]);
*weights = match channel.interpolation {
AnimationInterpolation::Step => values[prev_idx].clone(),
AnimationInterpolation::Linear => {
let a = &values[prev_idx];
let b = &values[next_idx];
a.iter()
.zip(b.iter())
.map(|(a, b)| a + (b - a) * factor)
.collect()
}
AnimationInterpolation::CubicSpline => {
// stored as [in_tangent, value, out_tangent] per keyframe
let p0 = &values[prev_idx * 3 + 1];
let m0 = &values[prev_idx * 3 + 2];
let m1 = &values[next_idx * 3 + 0];
let p1 = &values[next_idx * 3 + 1];
let t = factor;
let t2 = t * t;
let t3 = t2 * t;
let h00 = 2.0 * t3 - 3.0 * t2 + 1.0;
let h10 = t3 - 2.0 * t2 + t;
let h01 = -2.0 * t3 + 3.0 * t2;
let h11 = t3 - t2;
p0.iter()
.enumerate()
.map(|(i, p0i)| {
p0i * h00 + m0[i] * dt * h10 + p1[i] * h01 + m1[i] * dt * h11
})
.collect()
}
};
}
}
}
self.update_matrices(model);
}
fn reset_to_bind_pose(&mut self, model: &Model) {
self.local_pose.clear();
self.morph_weights.clear();
self.morph_weight_count = 0;
self.update_matrices(model);
}
fn apply_single_keyframe(
channel: &crate::model::AnimationChannel,
index: usize,
pose: &mut HashMap<usize, NodeTransform>,
morph_weights: &mut HashMap<usize, Vec<f32>>,
model: &Model,
) {
let transform = pose.entry(channel.target_node).or_insert_with(|| {
model
.nodes
.get(channel.target_node)
.map(|n| n.transform.clone())
.unwrap_or_else(NodeTransform::identity)
});
match &channel.values {
ChannelValues::Translations(v) => {
if let Some(val) = v.get(index) {
transform.translation = *val;
}
}
ChannelValues::Rotations(v) => {
if let Some(val) = v.get(index) {
transform.rotation = *val;
}
}
ChannelValues::Scales(v) => {
if let Some(val) = v.get(index) {
transform.scale = *val;
}
}
ChannelValues::MorphWeights(v) => {
let actual_index = match channel.interpolation {
AnimationInterpolation::CubicSpline => index * 3 + 1,
_ => index,
};
if let Some(frame) = v.get(actual_index) {
morph_weights.insert(channel.target_node, frame.clone());
}
}
}
}
fn update_matrices(&mut self, model: &Model) {
if let Some(skin) = model.skins.first() {
if self.skinning_matrices.len() != skin.joints.len() {
self.skinning_matrices
.resize(skin.joints.len(), Mat4::IDENTITY);
}
let mut global_transforms = HashMap::new();
for &joint_idx in &skin.joints {
self.resolve_global_transform(joint_idx, model, &mut global_transforms);
}
for (i, &joint_node_idx) in skin.joints.iter().enumerate() {
if let Some(global_transform) = global_transforms.get(&joint_node_idx) {
let inverse_bind = skin.inverse_bind_matrices[i];
self.skinning_matrices[i] = *global_transform * inverse_bind;
}
}
}
}
fn resolve_global_transform(
&self,
node_idx: usize,
model: &Model,
cache: &mut HashMap<usize, Mat4>,
) -> Mat4 {
if let Some(&matrix) = cache.get(&node_idx) {
return matrix;
}
let node = &model.nodes[node_idx];
let local_matrix = self
.local_pose
.get(&node_idx)
.map(|transform| transform.to_matrix())
.unwrap_or_else(|| node.transform.to_matrix());
let global_matrix = if let Some(parent_idx) = node.parent {
let parent_global = self.resolve_global_transform(parent_idx, model, cache);
parent_global * local_matrix
} else {
local_matrix
};
cache.insert(node_idx, global_matrix);
global_matrix
}
pub fn prepare_gpu_resources(&mut self, graphics: Arc<SharedGraphicsContext>) {
let has_skinning = !self.skinning_matrices.is_empty();
let has_morph_weights = !self.morph_weights.is_empty();
if !has_skinning && !has_morph_weights {
return;
}
if has_skinning {
let buffer = self.skinning_buffer.get_or_insert_with(|| {
DynamicBuffer::new(
&graphics.device,
self.skinning_matrices.len(),
wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
"skinning buffer",
)
});
if self.skinning_matrices.is_dirty() {
buffer.write(&graphics.device, &graphics.queue, &self.skinning_matrices);
self.skinning_matrices.mark_clean();
}
}
if has_skinning || has_morph_weights {
let mut flat: Vec<f32> = Vec::new();
let mut num_targets: usize = 0;
let mut sorted_nodes: Vec<usize> = self.morph_weights.keys().cloned().collect();
sorted_nodes.sort();
for weights in self.morph_weights.values() {
num_targets = num_targets.max(weights.len());
}
if let Some(node_idx) = sorted_nodes.first() {
let weights = &self.morph_weights[node_idx];
flat.extend_from_slice(weights);
}
if flat.len() < num_targets {
flat.resize(num_targets, 0.0);
}
if flat.is_empty() {
flat.push(0.0);
}
let weights_buffer = self.morph_weights_buffer.get_or_insert_with(|| {
DynamicBuffer::new(
&graphics.device,
flat.len().max(1),
wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
"morph weights buffer",
)
});
weights_buffer.write(&graphics.device, &graphics.queue, &flat);
self.morph_weight_count = num_targets as u32;
// todo: this is extremely inefficient
let info = MorphTargetInfo {
num_vertices: 0,
num_targets: num_targets as u32,
base_offset: 0,
weight_offset: 0,
uses_morph: has_morph_weights as u32,
_padding: Default::default(),
};
let info_buffer = self
.morph_info_buffer
.get_or_insert_with(|| UniformBuffer::new(&graphics.device, "morph info buffer"));
info_buffer.write(&graphics.queue, &info);
self.morph_deltas_buffer = None;
}
}
}