Пример #1
0
    public static Mesh GenerateDiscreteTerrainMesh(DiscreteTerrainSettings terrainSettings)
    {
        EmptyGrid cellMap   = GridGenerator.GenerateEmptyGrid(terrainSettings.tileMapSettings);
        HeightMap heightMap = HeightMapGenerator.GenerateHeightMap(terrainSettings.heightMapSettings, new Vector2(0, 0), terrainSettings.tileMapSettings.mapWidth, terrainSettings.tileMapSettings.mapHeight);

        if (terrainSettings.useHeightLayers)
        {
            TerrainHeightLayersGenerator.SeparateTerrainOnLayers(ref cellMap, ref terrainSettings.tileMapSettings, ref heightMap, ref terrainSettings.heightLayersSettings);
        }

        switch (terrainSettings.terrainMeshType)
        {
        case GridType.Square:
            return(DiscreteVoxelMeshGenerator.GenerateTerrainMesh(cellMap, terrainSettings.tileMapSettings, heightMap, terrainSettings.heightLayersSettings));

        case GridType.PointyHex:
            return(DiscreteSolidPointyHexMeshGenerator.GenerateTerrainMesh(cellMap, terrainSettings.tileMapSettings, heightMap, terrainSettings.heightLayersSettings));

        default:
            Debug.LogError("Terrain underlying grid type hasn't set up.");
            break;
        }

        return(new Mesh());
    }
    public static Mesh GenerateTerrainMesh(EmptyGrid map, TileMapSettings settings, HeightMap heightMap, TerrainHeightLayers heightLayersSettings = null)
    {
        mesh      = new Mesh();
        vertices  = new List <Vector3>();
        triangles = new List <int>();
        colors    = new List <Color>();

        Vector2 bottomLeft;

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

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

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

        if (heightLayersSettings != null)
        {
            TerrainHeightLayersGenerator.SeparateTerrainOnLayers(map, settings, heightMap, heightLayersSettings);
        }

        // * 0.5f because it's cube
        float   cellScale    = settings.cellScale * 2;
        Vector3 vertexOffset = Vector3.one * settings.cellScale;

        for (int y = 0; y < map.height; y++)
        {
            for (int x = 0; x < map.width; x++)
            {
                Vector3 cellOffset = new Vector3(x * cellScale, 0.0f, y * cellScale) + new Vector3(bottomLeft.x, 0.0f, bottomLeft.y);
                Vector3 spawnPoint = cellOffset + vertexOffset;

                spawnPoint.y += heightMap.values[x, y];

                CreateCube(spawnPoint, settings.cellScale, x, y, map, heightMap);
                Debug.DrawRay(spawnPoint, Vector3.up * 3.0f, Color.green, 0.5f);
            }
        }

        mesh.name      = "Discrete Voxel Terrain Mesh";
        mesh.vertices  = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
        mesh.colors    = colors.ToArray();

        mesh.RecalculateNormals();

        return(mesh);
    }
    public static Mesh GenerateTerrainMesh(EmptyGrid map, TileMapSettings settings, HeightMap heightMap, TerrainHeightLayers heightLayersSettings = null)
    {
        mesh      = new Mesh();
        vertices  = new List <Vector3>();
        triangles = new List <int>();
        colors    = new List <Color>();

        Vector2 bottomLeft;

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

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

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

        if (heightLayersSettings != null)
        {
            TerrainHeightLayersGenerator.SeparateTerrainOnLayers(map, settings, heightMap, heightLayersSettings);
        }

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

                Vector3 spawnPos = cellOffset + vertexOffset;

                spawnPos.y += heightMap.values[x, z];

                CreateSolidHex(spawnPos, settings.cellScale, x, z, map, heightMap);
                Debug.DrawRay(spawnPos, Vector3.up * 3.0f, Color.green, 0.5f);
            }
        }

        mesh.name      = "Discrete Solid PointyHex Terrain Mesh";
        mesh.vertices  = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
        mesh.colors    = colors.ToArray();
        mesh.RecalculateNormals();

        return(mesh);
    }