public static Mesh GenerateContiguousTerrainFromHM(HeightMap heightMap, ContiguousTerrainSettings terrainSettings)
    {
        switch (terrainSettings.terrainMeshType)
        {
        case GridType.Square:
            return(ContiguousMeshGenerator.GenerateTerrainMesh(heightMap, terrainSettings));

        case GridType.PointyHex:
            return(ContiguousPointyHexMeshGenerator.GenerateTerrainMesh(heightMap, terrainSettings));

        default:
            Debug.LogError("Terrain underlying grid type hasn't set up.");
            break;
        }
        return(new Mesh());
    }
    public static HeightMap GenerateContiguousTerrainHeightMap(ContiguousTerrainSettings terrainSettings)
    {
        HeightMap heightMap;

        switch (terrainSettings.terrainMeshType)
        {
        case GridType.Square:
            return(HeightMapGenerator.GenerateHeightMap(terrainSettings.heightMapSettings, new Vector2(0, 0), terrainSettings.tileMapSettings.mapWidth, terrainSettings.tileMapSettings.mapHeight));

        case GridType.PointyHex:
            return(HeightMapGenerator.GenerateHeightMap(terrainSettings.heightMapSettings, new Vector2(0, 0), terrainSettings.tileMapSettings.mapWidth * 2 + (terrainSettings.tileMapSettings.mapHeight > 1 ? 2 : 1), terrainSettings.tileMapSettings.mapHeight * 3 + 2));

        default:
            Debug.LogError("Terrain underlying grid type hasn't set up.");
            break;
        }
        return(new HeightMap());
    }
Example #3
0
    public static Mesh GenerateTerrainMesh(HeightMap heightMap, ContiguousTerrainSettings settings)
    {
        Mesh mesh = new Mesh();

        Vector2 bottomLeft;

        /// FIX CENTERED (vertexOffest breaks centre)
        if (settings.tileMapSettings.isCentered)
        {
            bottomLeft = new Vector2(-settings.tileMapSettings.mapWidth * settings.tileMapSettings.cellScale * (PointyHexTileData.innerRadius * 2f) * 0.5f,
                                     -((settings.tileMapSettings.mapHeight - 1) * (PointyHexTileData.outerRadius * 1.5f) + 0.5f * PointyHexTileData.outerRadius) * settings.tileMapSettings.cellScale * 0.5f)
                         + new Vector2(settings.tileMapSettings.mapOffsetX, -settings.tileMapSettings.mapOffsetZ);
        }
        else
        {
            bottomLeft = new Vector2(settings.tileMapSettings.mapOffsetX, -settings.tileMapSettings.mapOffsetZ);
        }

        Debug.DrawRay(new Vector3(bottomLeft.x, 0.0f, bottomLeft.y), Vector3.up * 5.0f, Color.red, 0.5f);

        Vector3[] vertices = new Vector3[2 * settings.tileMapSettings.mapWidth + settings.tileMapSettings.mapHeight * (settings.tileMapSettings.mapWidth * 3 + 2)];
        int[,] cellCenters = new int[settings.tileMapSettings.mapWidth, settings.tileMapSettings.mapHeight];
        Vector2[] uv        = new Vector2[vertices.Length];
        Color[]   colors    = new Color[vertices.Length];
        int[]     triangles = new int[settings.tileMapSettings.mapWidth * settings.tileMapSettings.mapHeight * 18]; //18 = 6 triangles * 3 vertices

        Vector3 vertexOffset = new Vector3(PointyHexTileData.innerRadius, 0.0f, PointyHexTileData.outerRadius) * settings.tileMapSettings.cellScale;

        Color cellColor = Color.blue;

        int vert = 0;
        int tri  = 0;

        // Assign vertices
        for (int z = 0; z < settings.tileMapSettings.mapHeight; z++)
        {
            for (int x = 0; x < settings.tileMapSettings.mapWidth; x++)
            {
                Vector3 cellOffset = new Vector3((x + z * 0.5f - z / 2) * (PointyHexTileData.innerRadius * 2f) * settings.tileMapSettings.cellScale,
                                                 0.0f, z * (PointyHexTileData.outerRadius * 1.5f) * settings.tileMapSettings.cellScale) + new Vector3(bottomLeft.x, 0.0f, bottomLeft.y);

                Vector3 cellSpawnPoint = cellOffset + vertexOffset;

                Debug.DrawRay(cellSpawnPoint, Vector3.up * 3.0f, Color.green, 0.5f);

                cellCenters[x, z] = vert;
                CreateCenter(ref vertices, ref uv, ref colors, ref vert, ref cellSpawnPoint, ref settings.tileMapSettings.cellScale, ref cellColor, ref x, ref z, ref heightMap);

                CreateVertices(0, 2, ref vertices, ref uv, ref colors, ref vert, ref cellSpawnPoint, ref settings.tileMapSettings.cellScale, ref cellColor, ref x, ref z, ref heightMap);

                if (z == 0)
                {
                    CreateVertices(2, 2, ref vertices, ref uv, ref colors, ref vert, ref cellSpawnPoint, ref settings.tileMapSettings.cellScale, ref cellColor, ref x, ref z, ref heightMap);
                    if (x == 0)
                    {
                        CreateVertices(4, 2, ref vertices, ref uv, ref colors, ref vert, ref cellSpawnPoint, ref settings.tileMapSettings.cellScale, ref cellColor, ref x, ref z, ref heightMap);
                    }
                }
                else if (z % 2 == 0)
                {
                    if (x == 0)
                    {
                        CreateVertices(4, 2, ref vertices, ref uv, ref colors, ref vert, ref cellSpawnPoint, ref settings.tileMapSettings.cellScale, ref cellColor, ref x, ref z, ref heightMap);
                    }
                }
                else
                {
                    if (x == 0)
                    {
                        CreateVertices(5, 1, ref vertices, ref uv, ref colors, ref vert, ref cellSpawnPoint, ref settings.tileMapSettings.cellScale, ref cellColor, ref x, ref z, ref heightMap);
                    }
                    if (x == settings.tileMapSettings.mapWidth - 1)
                    {
                        CreateVertices(2, 1, ref vertices, ref uv, ref colors, ref vert, ref cellSpawnPoint, ref settings.tileMapSettings.cellScale, ref cellColor, ref x, ref z, ref heightMap);
                    }
                }

                // Triangles
                CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, 0), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, 1), ref triangles, ref tri);

                if (z == 0)
                {
                    for (int i = 1; i < 3; i++)
                    {
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, i), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, i + 1), ref triangles, ref tri);
                    }
                    if (x == 0)
                    {
                        for (int i = 3; i < 6; i++)
                        {
                            CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, i), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, i + 1), ref triangles, ref tri);
                        }
                    }
                    else
                    {
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, 3), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x - 1, z, 2), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x - 1, z, 2), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x - 1, z, 1), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x - 1, z, 1), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, 0), ref triangles, ref tri);
                    }
                }
                else if (z % 2 == 0)
                {
                    CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, 1), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z - 1, 0), ref triangles, ref tri);
                    if (x == 0)
                    {
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z - 1, 0), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z - 1, 5), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z - 1, 5), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, 4), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, 4), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, 5), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, 5), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, 0), ref triangles, ref tri);
                    }
                    else
                    {
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z - 1, 0), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x - 1, z - 1, 1), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x - 1, z - 1, 1), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x - 1, z - 1, 0), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x - 1, z - 1, 0), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x - 1, z, 1), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x - 1, z, 1), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, 0), ref triangles, ref tri);
                    }
                }
                else
                {
                    if (x == settings.tileMapSettings.mapWidth - 1)
                    {
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, 1), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, 2), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, 2), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z - 1, 1), ref triangles, ref tri);
                    }
                    else
                    {
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, 1), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x + 1, z - 1, 0), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x + 1, z - 1, 0), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z - 1, 1), ref triangles, ref tri);
                    }

                    CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z - 1, 1), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z - 1, 0), ref triangles, ref tri);

                    if (x == 0)
                    {
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z - 1, 0), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, 5), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, 5), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, 0), ref triangles, ref tri);
                    }
                    else
                    {
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z - 1, 0), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x - 1, z, 1), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x - 1, z, 1), GetVertexInHex(ref cellCenters, ref settings.tileMapSettings.mapWidth, x, z, 0), ref triangles, ref tri);
                    }
                }
            }
        }

        mesh.name      = "Contiguous Pointy Hex Terrain Mesh";
        mesh.vertices  = vertices;
        mesh.uv        = uv;
        mesh.triangles = triangles;
        mesh.colors    = colors;
        mesh.RecalculateNormals();

        return(mesh);
    }
    public static Mesh GenerateTerrainMesh(HeightMap map, ContiguousTerrainSettings settings)
    {
        Mesh mesh = new Mesh();

        Vector2 bottomLeft;

        if (settings.tileMapSettings.isCentered)
        {
            bottomLeft = new Vector2(-map.width, -map.height) * settings.tileMapSettings.cellScale * 0.5f + new Vector2(settings.tileMapSettings.mapOffsetX, -settings.tileMapSettings.mapOffsetZ);
        }
        else
        {
            bottomLeft = new Vector2(settings.tileMapSettings.mapOffsetX, -settings.tileMapSettings.mapOffsetZ);
        }

        Debug.DrawRay(new Vector3(bottomLeft.x, 0.0f, bottomLeft.y), Vector3.up * 5.0f, Color.red, 0.5f);

        Vector3[] vertices  = new Vector3[map.width * map.height];
        Vector2[] uv        = new Vector2[vertices.Length];
        int[]     triangles = new int[(map.width - 1) * (map.height - 1) * 6]; //6 = 2 triangles * 3 vertices
        Color[]   colors    = new Color[vertices.Length];

        float vertexOffset = settings.tileMapSettings.cellScale;

        int vert = 0;
        int tri  = 0;

        for (int y = 0; y < map.height; y++)
        {
            for (int x = 0; x < map.width; x++)
            {
                Vector3 cellOffset = new Vector3(x * settings.tileMapSettings.cellScale, 0.0f, y * settings.tileMapSettings.cellScale) + new Vector3(bottomLeft.x, 0.0f, bottomLeft.y);
                //cellOffset.y += map.values[x, y] * settings.tileMapSettings.cellScale;
                cellOffset.y += map.values[x, y];

                vertices[vert] = cellOffset;
                uv[vert]       = new Vector2((float)x / map.width, (float)y / map.height);
                //colors[vert] = map.values[x == 0 ? x : x - 1, y == 0 ? y : y - 1].cellMapColor;
                colors[vert] = Color.blue;

                Debug.DrawRay(vertices[vert], Vector3.up * 3.0f, Color.green, 0.5f);

                vert++;
            }
        }

        vert = 0;
        for (int y = 0; y < map.height - 1; y++) //careful, <, not <=  Because triangles use i and i+1 column inside the loop
        {
            for (int x = 0; x < map.width - 1; x++)
            {
                triangles[tri]     = triangles[tri + 3] = vert;
                triangles[tri + 1] = vert + (map.width);
                triangles[tri + 2] = triangles[tri + 4] = vert + (map.width) + 1;
                triangles[tri + 5] = vert + 1;

                vert++;
                tri += 6;
            }

            vert++;
        }

        mesh.name      = "Contiguous Terrain Mesh";
        mesh.vertices  = vertices;
        mesh.uv        = uv;
        mesh.triangles = triangles;
        mesh.colors    = colors;
        mesh.RecalculateNormals();

        return(mesh);
    }