How to make the atlas two-dimensional
If you have a 3x3 grid of textures in your atlas, you have 9 possible textures, so you update Number of Textures
to 9
. So now the multiply -> floor nodes will produce a number in {0, 1, 2, 3, 4, 5, 6, 7, 8}. If you write down a table on how they correspond to the atlas, you will get this:
| y / x | x:0 | x:1 | x:2 |
|-------|-----|-----|-----|
|y:0| 0 | 1 | 2 |
|y:1| 3 | 4 | 5 |
|y:2| 6 | 7 | 8 |
A standard way to convert index to row index is to make an integer division of the index by the number of columns. A standard way to convert index to column index is to calculate the integer division remainder (so-called modulo operation)
- 0 // 3 = 0 (remainder: 0)
- 1 // 3 = 0 (remainder: 1)
- 2 // 3 = 0 (remainder: 2)
- 3 // 3 = 1 (remainder: 0)
- 4 // 3 = 1 (remainder: 1)
- 5 // 3 = 1 (remainder: 2)
- 6 // 3 = 2 (remainder: 0)
- 7 // 3 = 2 (remainder: 1)
- 8 // 3 = 2 (remainder: 2)
(first bold number above is Y coordinate, second is X coordinate)
Now when we change the texture atlas and update the UV:
We can use this node setup:
It's quite ugly, we can simplify this part:
Other than that, the setup would be much simpler if it just randomized X and Y separately - but that would assume the last row is full, as opposed to having e.g. 8 elements in a 3x3 grid where the bottom-right corner is empty.