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

43 lines
888 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.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
// Repeat
if rep == u32(1) {
frame = u32(min(
(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.data[idx].first_frame;
}