Chapter 6: Textures (without ModelI/O)

Hey, I’m back with my thoughts on this

Firstly, I hope you’re all coping well with this pandemic. For me, it’s a good opportunity to play with Metal.

So I have my own home-baked mesh which consists of inter-locking hexagons (which themselves each comprise 6 triangles). I want to decorate each hexagon with one of eight possible textures (terrain pictures: desert, mountain, etc.) which will be determined somewhat randomly. Accordingly, I have structured my mesh as eight sub-meshes, each of which is associated with one unique terrain. Each sub-mesh may contain adjacent or non-adjacent hexagons.
hexes
The picture shows a small region when the sub-meshes are consolidated. I have addressed a few of the vertices and fragments just to illustrate my problem. Vertex 1 happens to be always associated with deserts but vertex 2’s association can only be determined at the level of the sub-mesh since it abuts three different terrain types. Accordingly it is hard to provide property information at the vertex level. For instance, to colour different terrains differently is not possible in the vertex shader. However, my sub-mesh structure makes it possible to colour the hexagons a uniform colour by passing different colours to the fragment shader when processing each sub-mesh. I thought I was making progress …

Now I want to go a bit further. I want to paint a texture onto each hexagon. Look at the picture to see the problem, first at the level of vertex and then at the level of fragment. Take even vertex 1 which is always desert: in fragment A, its uv is near the bottom right of the desert bitmap where for fragment B it is near the top right. In fact it’s in six different locations in six different fragments! (Don’t even ask about vertex 2 which spans sub-meshes too!!).

As a solution, I’ve considered producing even more sub-meshes - 6 times as many with one for each of the 6 different triangles in each terrain. Then I’d send six uv pairs with each vertex but I’d still need some way of deciding which of the pairs to select for each fragment. I can’t see how. And just look at the amount of tables and indices I need to produce for what is really a very structured mesh!!

Now I know people are able to do far more sophisticated work than I’m trying to do so the above discussion just illustrates that I don’t understand something. I’m sure Metal is capable of meeting my requirements and I need to reorganise my mesh and/or write more sophisticated shaders. Can anyone tell what I’m missing?

[NB The solution of going with, say, Blender is not a solution. Blender will produce a torus divided concentric circles which in turn are divided into triangles, it is true. However, my conventric circles are not equidistant (as in Blender) nor are my triangles right-angled. Maybe that could be achieved with lots of customisation, but my model produces a torus for any number of hexes as demanded. In any case, I’m doing this to understand Metal without relying on a ModelIO black-box.]

PS. I guess I shouldn’t overlook the obvious. If I set up eight sub-meshes each with a full set of appropriate vertices and their related UVs, clearly I should be able do this in a straightforward way. Each hex is, after all, almost planar. I will end up with every vertex reproduced 6 times (each is in three hexagons and used twice in each of these hexagons - 6 triangles = 18 vertices, 6 exterior vertices twice each + central vertex 6 times) so that, far from reaping the benefits of indexing, I’ll be multiplying the data footprint. Is this what is happening under the hood with the Blender file?

It’s exceedingly inelegant and I’m shocked the developers of Metal didn’t give us access to faces. Maybe they did?