- Home /
Hexagon mesh subdivided into triangles
Edit: after some editing I have fixed the bug of the incomplete hexagons.
Hi guys,
I am trying to create a hexagon mesh where each hexagon is subdivided into 6 triangles. I want to be able to texture each triangle with an individual texture, however have run into a snag creating the mesh. At the moment I have written some code that creates hexagons however the results are pretty far off from what I want to achieve.
After some messing around I have managed to fix a bug causing the hexagons to appear incomplete. Now I need to solve how to correctly place the hexagons in a grid formation. The code for the offset of each hexagon is:
float height = _radius * 2;
float width = Mathf.Sqrt(3)/2 * height;
float horizon = height*3/4;
float vertical = width;
Vector2 sO = new Vector2(x*horizon, z*vertical);
Vector2 sOffset = new Vector2(_startCenter.x + (sO.x + (z%2==0 ? 0 : (sO.x/2))), (_startCenter.z + sO.y));
Complete code below:
private void GenerateGrid()
{
//Constants for hexagon calculations
float height = _radius * 2;
float width = Mathf.Sqrt(3)/2 * height;
float horizon = height*3/4;
float vertical = width;
//Arrays for hexagon vertices (tG), nomalization and uv
Vector3[] tG = new Vector3[_totalHex*18];
Vector3[] norm = new Vector3[_totalHex*18];
Vector2[] uv = new Vector2[_totalHex*18];
for(int z = 0; z < _size.y; z++)
{
for(int x = 0; x < _size.x; x++)
{
//Sets variables for hexagon creation
int y = 0;
Vector2 sO = new Vector2(x*horizon, z*vertical);
Vector2 sOffset = new Vector2(_startCenter.x + (sO.x + (z%2==0 ? 0 : (sO.x/2))), (_startCenter.z + sO.y));
for(int i = 0; i < 18; i++)
{
//Iterates y every third iteration of i
y += ((i+1)%3 == 0 ? 1 : 0);
//sets y to 0 to close the hexagon and the end
if((i+1)%18 == 0)
{
y = 0;
}
////
//Every third (including 0) should be the center vertex (c), all others should be the vertexes of the outer ring (n)
//The iteration on hexagon one should be [c, n0, n1, c, n1, n2, c, n2, n3, c, n3, n4, c, n4, n5, c, n5, n0]
tG[((z*_size.x+x)*18)+i] = (i%3 == 0 ? new Vector3(sOffset.x, _startCenter.y, sOffset.y)
: new Vector3(sOffset.x + (_radius * Mathf.Cos(2*Mathf.PI * (y+0.5f)/6)),
_startCenter.y,
sOffset.y + (_radius * Mathf.Sin(2*Mathf.PI * (y+0.5f)/6))));
//uv[((z*_size.x+x)*18)+i] = new Vector2(x*i, y*i);
norm[((z*_size.x+x)*18)+i] = Vector3.up;
}
}
}
//sets arrays for mesh (_key = triangles)
_worldVertices = tG;
_normals = norm;
_key = Enumerable.Range(0, _totalHex*18).ToArray();
}
I hope I have made the question easily understandable and someone is able to tell me what I'm doing wrong and how I might fix it. Possibly tell me a less contrived way of doing this. Finally, I apologize if this question is cluttered.
Bonus question: How would I UV map these hexagons?
Answer by Mithosbluefish · Jan 29, 2015 at 12:53 AM
Solved the issues, turns out my offset calculations were wrong. I was calculating distance for a pointed top grid while I was generating a flat top grid.
Correct calculations are:
float width = _radius * 2;
float height = Mathf.Sqrt(3)/2 * width;
float horizon = width*3/4;
float vertical = height;
Vector2 sO = new Vector2(x*horizon, z*vertical+(x%2 == 0 ?(vertical/2) : 0));
Vector2 sOffset = new Vector2(_startCenter.x + sO.x, _startCenter.z + sO.y);