Tile atlas shader

Hi,

I’m trying to create tiled atlas shader but I’m pretty much clueless how it should be done.

System works like this: I’ve 3 by 3 (total of 9 tiles) atlas having 64 by 64 pixels tiles.

I can pick correct tile by changing texture tiling and offset x/y values by C# code. Setting correct tile in C#-code is not a big problem but I would appreciate that I could see correct tile just by assigning correct material.

I tried to google something for basis but I couldn’t find anything that works like that.

So I would be very happy if someone can point me to right direction. Fully working shader would be fine but I would rather learn how to do such a simple (at least to me it seems rather simple) shader from the scratch.

Why 3x3 64x64 tiles? That doesn’t make a power of two texture…

Well does that matter for the principle?

Let’s say then that for the testing purposes tileset is 4 by 4 with 64 by 64 pixel tiles (that should build up 256 by 256 pixels “power of two perfect”) texture if that makes someone to feel better.

Well if you want it set per-material, you would have to set up a material for each tile of your texture.

And since you can store your texture offsets per material - the values you’re changing via script are right there in the inspector for the material - there isn’t an issue.

You’re correct, I can calc them manually (or automatically on the fly) and create materials for them. But I would like to (if nothing else but educational purposes) to just say that my atlas does have N by M tiles and select tile X from there and let the simple shader to take care of actual drawing.