Galactica/crates/render/shaders/include/animate.wgsl

43 lines
864 B
Plaintext
Raw Normal View History

// 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[idx].frame_count;
let rep = sprites[idx].repeatmode;
let fps = sprites[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) {
frame = u32(min(
2024-01-05 12:09:59 -08:00
age / fps,
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);
}
return frame + sprites[idx].first_frame;
}