5
$\begingroup$

enter image description here

how to do uv indexing like this procedurally?

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

$\endgroup$
  • $\begingroup$ "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 $\endgroup$ – hatinacat2000 Dec 23 '19 at 20:12
  • $\begingroup$ 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 $\endgroup$ – hatinacat2000 Dec 24 '19 at 2:05
  • $\begingroup$ @hatinacat2000 that's cool. imgur.com/o1iUZg5 still not there yet but it getting closer. any thoughts on this? $\endgroup$ – Baybaypras Dec 24 '19 at 2:39
  • 1
    $\begingroup$ 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. $\endgroup$ – Robin Betts Dec 24 '19 at 9:37
  • 2
    $\begingroup$ @RobinBetts you sunuvabitch...you've done it! Looks great $\endgroup$ – hatinacat2000 Dec 24 '19 at 11:56
6
$\begingroup$

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):

enter image description here

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

enter image description here

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):

enter image description here

.. 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:

enter image description here

The 'Hexagonal Grid' group in the download has the outputs:

  • 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.

| improve this answer | |
$\endgroup$
  • 2
    $\begingroup$ Victory. And a merry Xmas to you too Mr B. $\endgroup$ – batFINGER Dec 24 '19 at 18:16
3
$\begingroup$

enter image description here

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: enter image description here

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

| improve this answer | |
$\endgroup$
  • $\begingroup$ @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. $\endgroup$ – Robin Betts Dec 24 '19 at 11:06
  • 1
    $\begingroup$ 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 $\endgroup$ – hatinacat2000 Dec 24 '19 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.