# How To Do UV Indexing Like This?

how to do uv indexing like this procedurally?

i wanna make some shader that can pixelate image but with hexagonal.

• "pixelate" is unclear, do you just need a pattern with a-rows and b-columns? I would recommend starting with a gradient that adds the red channel on the u-axis and green channel on the v-axis. The colors for the hexagons are solid and can be based on their center points. If the last hexagon row/column wraps around (and it does), there are 10 hexagons. So the sample points (centers) will be u = i/10, v = j/10. Of course that isn't the hard part. I'll keep an eye on this question, but I think you need to increment i and j for inequalities in a union of if-statements representing boundaries – hatinacat2000 Dec 23 '19 at 20:12
• This isn't really an "answer" but I started dissecting this free shader and it is interesting. It won't provide exactly the same effect though. Needs some tweaking. It is interesting to examine how modular arithmetic is used to create a repeating pattern. gumroad.com/l/yOBIH – hatinacat2000 Dec 24 '19 at 2:05
• @hatinacat2000 that's cool. imgur.com/o1iUZg5 still not there yet but it getting closer. any thoughts on this? – Baybaypras Dec 24 '19 at 2:39
• Here's one I made earlier. It's got a few subgroups in it that can be replaced now, since Vector Math nodes have been improved in 2.81. It has cell index, cell UV, cell distance-from-edge, and cell radial-angle. Christmas is making things a bit busy.. will expand to an explained answer when I get a chance. – Robin Betts Dec 24 '19 at 9:37
• @RobinBetts you sunuvabitch...you've done it! Looks great – hatinacat2000 Dec 24 '19 at 11:56

Hexagonal Tiling

Updated. See Edit at bottom of this answer..

You can't map the centers of tiled hexagons onto one rectangular grid, but you can map them onto two overlapping rectangular grids, and use a Voronoi-like distance-to-closest-point test to decide which grid a shading point belongs to.

Preparation

Unfortunately, Blender's node implementation of modulo goes into the negative for negative bases, and we would like a plain regular sawtooth, not jumping as input crosses 0. So we can make a group implementing an alternative mod(): x - y * floor(x/y):

which, for convenience, we can use in a group 'Vector Modulus', applying it element-wise to a vector:

Rectangular Grids

We can use the Vector Modulus group to divide a given UV space into rectangular cells, width 1, height sqrt(3). The centers of these rectangular cells are at the centers of successive tessellated hexagons in a column, and adjacent tessellated hexagons in a row.

We move (0,0) to the centers of the cells by subtracting (0.5, sqrt(3)/2) from (Cell U, Cell V):

.. and call it 'Grid A'.

We create another grid, 'Grid B', by offsetting Grid A by half its dimensions in both directions, so the corners of A at the centers of B, and vice-versa.

To Hexagons:

Now, by selecting the shorter of the Grid A UV and the Grid B UV vectors, i.e. the the UV in the space of the closest cell from either grid, the result falls into tessellated hexagons. (Surprising, until you think about it)

We haven't scaled or distorted the original Input UV in this conversion, just renumbered chunks of it. The rate of change in X and Y is the same, so if we subtract the Cell UV from the Input UV, the result will be constant in any cell, and different for each cell, so can be used to index the cells:

• Grid ID, a 2D unique ID for each cell
• The cell's internal UV, (-.5 to .5)
• The 2 distances of the shading point: from the center and the edges of its cell.

Thanks to The Art of Code, aka BigWings on ShaderToy, for this method.

Edit:

Since answering, and with newer nodes, Wannes Malfait has pointed out a much snappier way of constructing the offset grids, with just 9 nodes:

• Victory. And a merry Xmas to you too Mr B. – batFINGER Dec 24 '19 at 18:16

Adjacent hexagons will be translated by (+-1,+-SQRT(3)/2). I did not put in a scaling function (though I did attempt to tile it).

Here's the file I was working on. It's not very useful so far but you can move the hexagon by changing the center coordinates:

This has been especially challenging for me because hexagons do not tesselate on a square unless you are willing to crop 7 of them at once. I assumed making the unit based upon multiple hexagons would make it impossible (or more difficult) to link the color data to every hexagon's center. At this stage though, I have not even succeeded at that yet.

# EDIT1

Got the color working:

Share your file with me and maybe I'll be inspired.

• @Baybaypras and you... Commenting here, because running out of room in OP.. check out the superb teaching of the Art of Code here.. wish I could credit by name.. I've learned a lot from him... it's where my implementation comes from. Maybe you can improve it. – Robin Betts Dec 24 '19 at 11:06
• I started watching this guy this week, he does very cool stuff. I am looking forward to getting into Shadertoy. Also, @Baybaypras, it seems that the guy who made that free hexagonal shader is using the paid version to make the same pattern you cited: youtu.be/OENT4l4PFNE – hatinacat2000 Dec 24 '19 at 11:49