The Unity Engine itself has a set of three dimensional shapes/meshes to choose from. Unfortunately a ‘hexagon prism mesh’ is not one of them. That is why this project’s sole purpose is to generate a ‘hexagon prism mesh’, using only code and putting it algorithmically in a grid.
Wayne programmed both of those things in the language ‘C#’. He uses the angles of a hexagon to determine the placment of vertices.
In the example below (HexagonMesh.cs) is shown that the vertices are divided into three-point-connectivity. That is because the mesh is built using polygons. The vertices need to contain a pattern of connectivity from which a texture can be seen. That determines whether or not the side the user is looking at is actually the outside of a mesh and not the inside. The normals of the vertices determine the way light is reflected off of the mesh. As stated in the algorithm below, the angles of a hexagon is also used to determine the placement of the grid. With each hexagon mesh being placed in an exact pattern next to eachother.
Wayne has yet to apply game-related features in this project to fully bring out the usefulness of an algorithm like this. It will contain variations in height placement of meshes to really bring out the cells in the grid. A player will also be able to walk among the cells. The amount of cells in the grid is adjustable right now, but not by any user of the build.
protected void DrawMesh()
{
vertices = new Vector3[Constants.VertexCount.HEXAGON_MESH];
for (int i = 0; i < 6; i++) {
vertices[i] = new Vector3(Mathf.Cos((30 + (i * 60)) *
Constants.Math.DEGREE_TO_RADIAN), 0.5f,
Mathf.Sin((30 + (i * 60)) * Constants.Math.DEGREE_TO_RADIAN));
}
for (int i = 6; i < vertices.Length; i++) {
vertices[i] = new Vector3(Mathf.Cos((30 + ((i – 6) * 60)) *
Constants.Math.DEGREE_TO_RADIAN), -0.5f,
Mathf.Sin((30 + ((i – 6) * 60)) * Constants.Math.DEGREE_TO_RADIAN));
}
normals = new Vector3[Constants.VertexCount.HEXAGON_MESH]
{
-Vector3.forward, -Vector3.forward, -Vector3.forward, Vector3.forward,
Vector3.forward, -Vector3.forward, -Vector3.forward, -Vector3.forward,
Vector3.forward, Vector3.forward, -Vector3.forward, -Vector3.forward
};
uvCoords = new Vector2[Constants.VertexCount.HEXAGON_MESH]
{
new Vector2(0, 0), new Vector2(0, 0), new Vector2(1, 0),
new Vector2(1, 1), new Vector2(1, 1), new Vector2(0, 1),
new Vector2(0, 0), new Vector2(0, 0), new Vector2(1, 0),
new Vector2(1, 1), new Vector2(1, 1), new Vector2(0, 1)
};
tris = new int[Constants.PolygonCount.HEXAGON_MESH]
{
0, 5, 4, 0, 4, 3, 0, 3, 1, 1, 3, 2, //Upper Hexagon
0, 1, 7, 0, 7, 6, 1, 2, 8, 1, 8, 7, //Sides
2, 3, 9, 2, 9, 8, 3, 4, 10, 3, 10, 9,
4, 5, 11, 4, 11, 10, 5, 0, 6, 5, 6, 11,
6, 10, 11, 6, 9, 10, 6, 7, 9, 7, 8, 9 //Lower Hexagon
};
}
void InitGrid()
{
hexAngle = Mathf.Sin(60 * Constants.Math.DEGREE_TO_RADIAN);
gridPosition[0] = new Vector3(transform.position.x – (hexAngle *
(_gridLength.x – 0.5f)), transform.position.y, transform.position.z +
(Mathf.Pow(hexAngle, 2) * (_gridLength.y – 1)));
offset = Mathf.Cos(60 * Constants.Math.DEGREE_TO_RADIAN) * hexAngle * 2;
for (int i = 0; i < (_gridLength.x * _gridLength.y); i++)
{
gridPosition[1] = new Vector3(gridPosition[0].x + ((i % _gridLength.x) *
2 * hexAngle), transform.position.y,
gridPosition[0].z – (Mathf.FloorToInt(i / _gridLength.x) *
Mathf.Pow(Mathf.Sin(60 * Constants.Math.DEGREE_TO_RADIAN), 2) * 2));
if ((i % (_gridLength.x * 2)) >= _gridLength.x &&
(i % (_gridLength.x * 2)) < (_gridLength.x * 2)) {
gridPosition[1].x += offset;
}
Instantiate(_mesh, gridPosition[1], transform.rotation, transform);
}
}