2024-01-04 21:30:12 -08:00
|
|
|
// Pick frame of animation from an instance.
|
|
|
|
//
|
|
|
|
// This function assumes that the uniform header has been loaded,
|
|
|
|
// and that `InstanceInput` contains a field `texture_index`
|
|
|
|
fn animate(instance: InstanceInput, age: f32) -> u32 {
|
|
|
|
|
|
|
|
let idx = instance.texture_index;
|
|
|
|
let len = sprites.data[idx].frame_count;
|
|
|
|
let rep = sprites.data[idx].repeatmode;
|
|
|
|
let fps = sprites.data[idx].fps;
|
|
|
|
var frame: u32 = u32(0);
|
|
|
|
|
2024-01-04 21:41:15 -08:00
|
|
|
|
2024-01-05 12:09:59 -08:00
|
|
|
// Once
|
2024-01-04 21:41:15 -08:00
|
|
|
if rep == u32(1) {
|
|
|
|
|
2024-01-04 21:30:12 -08:00
|
|
|
frame = u32(min(
|
2024-01-05 12:09:59 -08:00
|
|
|
age / fps,
|
2024-01-04 21:30:12 -08:00
|
|
|
f32(len) - 1.0
|
|
|
|
));
|
2024-01-04 21:41:15 -08:00
|
|
|
|
|
|
|
// Reverse
|
|
|
|
} else if rep == u32(2) {
|
|
|
|
let x = age / fps;
|
|
|
|
let m = f32(len) * 2.0 - 1.0;
|
|
|
|
// x fmod m
|
|
|
|
frame = u32(x - floor(x / m) * m);
|
|
|
|
|
|
|
|
if frame >= len {
|
|
|
|
frame = len + len - frame - u32(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repeat (default)
|
|
|
|
} else {
|
|
|
|
let x = age / fps;
|
|
|
|
let m = f32(len);
|
|
|
|
// x fmod m
|
|
|
|
frame = u32(x - floor(x / m) * m);
|
|
|
|
|
2024-01-04 21:30:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return frame + sprites.data[idx].first_frame;
|
|
|
|
}
|