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

43 lines
884 B
Plaintext

// 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);
// Once
if rep == u32(1) {
frame = u32(min(
age / fps,
f32(len) - 1.0
));
// 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;
}