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

41 lines
900 B
Plaintext

// Pick a frame of a sprite animation from an instance.
fn animate(sprite_index: u32, age: f32, offset: f32) -> f32 {
let len = global_sprites[sprite_index].frame_count;
let rep = global_sprites[sprite_index].repeatmode;
let fps = global_sprites[sprite_index].fps;
var frame: f32 = 0.0;
// Once
if rep == u32(1) {
frame = min(
age / fps + offset,
f32(len) - 1.0
);
// Reverse
} else if rep == u32(2) {
let x = age / fps + offset;
let m = f32(len) * 2.0 - 1.0;
// x fmod m
frame = x - floor(x / m) * m;
if frame >= f32(len) {
frame = (
f32(len) + f32(len) - 1.0
// Split integer and fractional part so tweening works properly
- floor(frame)
+ fract(frame)
);
}
// Repeat (default)
} else {
let x = age / fps + offset;
let m = f32(len);
frame = x - floor(x / m) * m;
}
return frame + f32(global_sprites[sprite_index].first_frame);
}