private static void AddTriangle(HexMeshData meshData, Vector3 center, Vector3 uv, float neighborElevation, float previousElevation, float nextElevation, HexDirection direction, Color color) { var v1 = center; var v2 = center + meshData.GetFirstSolidCorner(direction); var v3 = center + meshData.GetSecondSolidCorner(direction); //Add inner solid triangle AddTriangle(meshData, v1, v2, v3, color, uv); //Add Quad To Fill Border Gap var v4 = v2 + meshData.GetBridge(direction); v4.y = neighborElevation; var v5 = v3 + meshData.GetBridge(direction); v5.y = neighborElevation; AddQuad(meshData, v2, v3, v4, v5, color, uv); //Add Triangles to fill gap on sides of quad var v6 = center + meshData.GetFirstCorner(direction); v6.y = Mathf.Min(neighborElevation, previousElevation); AddTriangle(meshData, v2, v6, v4, color, uv); var v7 = center + meshData.GetSecondCorner(direction); v7.y = Mathf.Min(neighborElevation, nextElevation); AddTriangle(meshData, v3, v5, v7, color, uv); }
private static void AddQuad(HexMeshData meshData, Vector3 v1, Vector3 v2, Vector3 v3, Vector3 v4, Color color, Vector3 uv) { var vertexIndex = meshData.Vertices.Count; meshData.Vertices.Add(v1); meshData.Vertices.Add(v2); meshData.Vertices.Add(v3); meshData.Vertices.Add(v4); meshData.Colors.Add(color); meshData.Colors.Add(color); meshData.Colors.Add(color); meshData.Colors.Add(color); meshData.Uv.Add(uv); meshData.Uv.Add(uv); meshData.Uv.Add(uv); meshData.Uv.Add(uv); meshData.Triangles.Add(vertexIndex); meshData.Triangles.Add(vertexIndex + 2); meshData.Triangles.Add(vertexIndex + 1); meshData.Triangles.Add(vertexIndex + 1); meshData.Triangles.Add(vertexIndex + 2); meshData.Triangles.Add(vertexIndex + 3); }
public static Mesh GenerateHexagonMesh(float radius, float border, int startX, int startY, int chunkWidth, int chunkHeight, int mapWidth, int mapHeight, float[] heightMap, Func <float, Color> colorFunction, Func <float, float> elevationFunction) { var meshData = new HexMeshData(radius, border); for (int dy = 0; dy < chunkHeight && (startY + dy) < mapHeight; dy++) { for (int dx = 0; dx < chunkWidth && (startX + dx) < mapWidth; dx++) { int x = startX + dx; int y = startY + dy; var index = (y * mapWidth) + x; var xOffset = x + y * 0.5f - (int)(y / 2); var center = new Vector3(xOffset * meshData.InnerRadius * 2, 0, y * meshData.OuterRadius * 1.5f); for (var direction = 0; direction < 6; direction++) { var elevation = elevationFunction?.Invoke(heightMap[index]) ?? 0; var previousNeighborElevation = elevation; var neighborElevation = elevation; var nextNeighborElevation = elevation; var neighbor = GetNeighbor(index, (HexDirection)direction, mapWidth, mapHeight); if (neighbor != -1) { neighborElevation = Mathf.Min(elevation, elevationFunction?.Invoke(heightMap[neighbor]) ?? 0); } neighbor = GetNeighbor(index, ((HexDirection)direction).Previous(), mapWidth, mapHeight); if (neighbor != -1) { previousNeighborElevation = Mathf.Min(elevation, elevationFunction?.Invoke(heightMap[neighbor]) ?? 0); } neighbor = GetNeighbor(index, ((HexDirection)direction).Next(), mapWidth, mapHeight); if (neighbor != -1) { nextNeighborElevation = Mathf.Min(elevation, elevationFunction?.Invoke(heightMap[neighbor]) ?? 0); } center.y = elevation; var color = colorFunction?.Invoke(heightMap[index]) ?? Color.white; var uv = new Vector2(x / (float)mapWidth, y / (float)mapHeight); AddTriangle(meshData, center, uv, neighborElevation, previousNeighborElevation, nextNeighborElevation, (HexDirection)direction, color); } } } return(meshData.CreateMesh()); }