Tiling images from a spritesheet

This post is for fairly advanced users.

I wanted to put my whole scene into a single mesh, but it had several images, so I put them into a single spritesheet. But then I needed to tile (ie repeat several times) some of those images, eg my floor is a very large rectangle with a repeated gravel texture, and that texture is only a small part of the spritesheet.

I figured out a way to do it with a shader. It requires one line to be changed from the default shader, along with special texture and colour coordinates, but it works great, and is simple to configure.

I’ve written it up here, with demo code.

Awesome! I must admit I’d briefly tried using a sprite sheet with a tiler shader, then decided it couldn’t be made to work and gave up.

Instead of mod(vTexCoord.x, 1.0), I use fract(vTexCoord.x) (returns the fractional amount). I probably should profile them to see which, if any, is faster.