Flipbook
A simple and common way to animate a texture. Great for particle effects, smoke, fire, etc. Try it out with the image below. The animation plays from top left to bottom right, so make sure your texture follows that layout.
grid_size sets the number of columns and rows in the sprite sheet. frame is the frame number. You can use TIME multiplied by a speed value to animate automatically.
vec2 flipbook(vec2 uv, vec2 grid_size, float frame) {
float frame_index = mod(floor(frame), grid_size.x * grid_size.y);
vec2 frame_position = vec2(
mod(frame_index, grid_size.x),
floor(frame_index / grid_size.x)
);
return (fract(uv) + frame_position) / grid_size;
}
void fragment() {
vec2 flip_uv = flipbook(UV, vec2(4.0, 4.0), TIME * 10.0);
COLOR = texture(TEXTURE, flip_uv);
}

