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);
    }
示例#3
0
 public static TileMapSettings PreprocessMap(TileMapSettings mapSettings, DiffusionLimitedAggregationSettings settings)
 {
     if (mapSettings.mapWidth < 10)
     {
         mapSettings.mapWidth = 10;
     }
     if (mapSettings.mapHeight < 10)
     {
         mapSettings.mapHeight = 10;
     }
     return(mapSettings);
 }
示例#4
0
    public static TileMapSettings PreprocessMap(TileMapSettings mapSettings, RoomGridSettings settings)
    {
        int maxRoomsCount = settings.gridWidth * settings.gridHeight;

        if (settings.roomCount > maxRoomsCount)
        {
            settings.roomCount = maxRoomsCount;
        }
        else if (settings.roomCount <= 0)
        {
            settings.roomCount = 1;
        }

        if (settings.roomWidth < 3)
        {
            settings.roomWidth = 3;
        }
        if (settings.roomHeight < 3)
        {
            settings.roomHeight = 3;
        }

        if (settings.isSquareShaped)
        {
            settings.roomWidth  = settings.roomWidth > settings.roomHeight ? settings.roomWidth : settings.roomHeight;
            settings.roomHeight = settings.roomWidth;
        }

        settings.roomWidthCenter  = Mathf.FloorToInt((settings.roomWidth - 1) * 0.5f);
        settings.roomHeightCenter = Mathf.FloorToInt((settings.roomHeight - 1) * 0.5f);

        bool isSpawnPointOutsideGrid = (settings.initialRoomSpawnPoint.x < 0) || (settings.initialRoomSpawnPoint.z < 0) ||
                                       (settings.initialRoomSpawnPoint.x >= settings.gridWidth) || (settings.initialRoomSpawnPoint.z >= settings.gridHeight);

        if (isSpawnPointOutsideGrid || settings.isSpawnPointCentered)
        {
            settings.initialRoomSpawnPoint.x = Mathf.FloorToInt((settings.gridWidth - 1) * 0.5f);
            settings.initialRoomSpawnPoint.z = Mathf.FloorToInt((settings.gridHeight - 1) * 0.5f);
        }

        int mapWidth  = settings.roomWidth * settings.gridWidth;
        int mapHeight = settings.roomHeight * settings.gridHeight;

        mapSettings.mapWidth  = mapWidth;
        mapSettings.mapHeight = mapHeight;

        return(mapSettings);
    }
示例#5
0
    public static TileMapSettings PreprocessMap(TileMapSettings mapSettings, RandomWormsSettings settings)
    {
        // Random Generator
        Random.State initialState = Random.state;
        if (settings.useFixedSeed)
        {
            Random.InitState(settings.seed.GetHashCode());
        }
        else
        {
            Random.InitState(Time.time.ToString().GetHashCode());
        }

        RandomWorms.settings         = settings;
        RandomWorms.worms            = new List <RandomWorm>();
        RandomWorms.cellsCoordinates = new List <Coordinate>(settings.cellsToCreateCount)
        {
            new Coordinate(0, 0) // Initial spawn cell
        };

        RandomWorms.maxX = RandomWorms.maxZ = RandomWorms.minX = RandomWorms.minZ = 0;

        RandomWorms.worms.Add(new RandomWorm(settings.chanceToTurnAround, settings.chanceToTurn, settings.allowTurnAround));

        for (int i = 1; i < settings.wormsCount; i++)
        {
            RandomWorms.worms.Add(new RandomWorm(RandomWorms.worms[0].GetCurrentCoord()));
        }
        RandomWorm.SetChanceToDie(settings.initialChanceToDie, settings.increaseDieChanceBy);

        RandomWorm.createRoomEvent += CreateRectangularRoom;

        MoveWorms();

        RandomWorm.createRoomEvent -= CreateRectangularRoom;

        RandomWorm.GetMaxCoord(ref RandomWorms.minX, ref RandomWorms.maxX, ref RandomWorms.minZ, ref RandomWorms.maxZ);
        mapSettings.mapWidth  = RandomWorms.width = maxX - minX + 1;  //+1 for zero tile
        mapSettings.mapHeight = RandomWorms.height = maxZ - minZ + 1; //+1 for zero tile

        Random.state = initialState;
        return(mapSettings);
    }
    public static TileMapSettings PreprocessMap(TileMapSettings mapSettings, RandomRoomWithCorridorSettings settings)
    {
        if (settings.roomsMinWidth < 3)
        {
            settings.roomsMinWidth = 3;
        }
        if (settings.roomsMinHeight < 3)
        {
            settings.roomsMinHeight = 3;
        }
        if (settings.roomsMaxWidth < settings.roomsMinWidth)
        {
            settings.roomsMaxWidth = settings.roomsMinWidth;
        }
        if (settings.roomsMaxHeight < settings.roomsMinHeight)
        {
            settings.roomsMaxHeight = settings.roomsMinHeight;
        }
        if (settings.minCorridorLength < 0)
        {
            settings.minCorridorLength = 0;
        }
        if (settings.maxCorridorLength < settings.minCorridorLength)
        {
            settings.maxCorridorLength = settings.minCorridorLength;
        }
        if (mapSettings.mapWidth < settings.roomsMaxWidth + settings.maxCorridorLength)
        {
            mapSettings.mapWidth = settings.roomsMaxWidth + settings.maxCorridorLength;
        }
        if (mapSettings.mapHeight < settings.roomsMaxHeight + settings.maxCorridorLength)
        {
            mapSettings.mapHeight = settings.roomsMaxHeight + settings.maxCorridorLength;
        }

        return(mapSettings);
    }
    public static Mesh GenerateGridMesh(EmptyGrid map, TileMapSettings settings)
    {
        mesh = new Mesh();
        //Mesh 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 * 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);

        //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];

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

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

        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.cellScale, 0.0f, y * settings.cellScale) + new Vector3(bottomLeft.x, 0.0f, bottomLeft.y);

                if (!settings.showWallsOnly)
                {
                    if (map.values[x, y].cellType == CellType.Empty || map.values[x, y].cellType == CellType.Floor)
                    {
                        CreateFace(Direction.Up, vertexOffset, cellOffset + (Vector3.forward + Vector3.right) * vertexOffset + Vector3.down * settings.cellScale);
                        ColorizeFace(map.values[x, y].cellMapColor);
                        continue;
                    }
                }
                else
                {
                    if (map.values[x, y].cellType == CellType.Empty || map.values[x, y].cellType == CellType.Floor)
                    {
                        continue;
                    }
                }

                CreateCube(cellOffset + (Vector3.forward + Vector3.right) * vertexOffset, vertexOffset, x, y, map);
                Debug.DrawRay(cellOffset + (Vector3.forward + Vector3.right) * vertexOffset, Vector3.up * 3.0f, Color.green, 0.5f);
            }
        }

        mesh.name      = "Discrete Voxel Mesh";
        mesh.vertices  = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
        mesh.colors    = colors.ToArray();
        //mesh.vertices = vertices;
        //mesh.uv = uv;
        //mesh.triangles = triangles;
        //mesh.colors = colors;
        mesh.RecalculateNormals();

        return(mesh);
    }
    public static Mesh GenerateGridMesh(EmptyGrid map, TileMapSettings settings)
    {
        mesh = new Mesh();
        //Mesh 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;

        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;

                if (!settings.showWallsOnly)
                {
                    if (map.values[x, z].cellType == CellType.Empty || map.values[x, z].cellType == CellType.Floor)
                    {
                        int verticesCount;
                        CreateFace(Direction.Up, settings.cellScale, spawnPos + 2 * Vector3.down * settings.cellScale, out verticesCount);
                        ColorizeFace(map.values[x, z].cellMapColor, verticesCount);
                        continue;
                    }
                }
                else
                {
                    if (map.values[x, z].cellType == CellType.Empty || map.values[x, z].cellType == CellType.Floor)
                    {
                        continue;
                    }
                }

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

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

        return(mesh);
    }
    public static Mesh GenerateGridMesh(EmptyGrid map, TileMapSettings settings)
    {
        Mesh mesh = new Mesh();

        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[] vertices  = new Vector3[map.width * map.height * 18]; //6 triangles, 3 vertices in each
        Vector2[] uv        = new Vector2[vertices.Length];
        int[]     triangles = new int[vertices.Length];                 //6 = 2 triangles * 3 vertices
        Color[]   colors    = new Color[vertices.Length];

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

        int vert = 0;

        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);

                Debug.DrawRay(cellOffset + vertexOffset, Vector3.up * 3.0f, Color.green, 0.5f);

                for (int i = 0; i < 6; i++)
                {
                    vertices[vert]  = cellOffset + vertexOffset;
                    uv[vert]        = new Vector2(0.5f, 0.5f);
                    colors[vert]    = map.values[x, z].cellMapColor;
                    triangles[vert] = vert++;

                    vertices[vert]  = cellOffset + vertexOffset + PointyHexTileData.vertices[i] * settings.cellScale;
                    uv[vert]        = PointyHexTileData.uv[i];
                    colors[vert]    = map.values[x, z].cellMapColor;
                    triangles[vert] = vert++;

                    vertices[vert]  = cellOffset + vertexOffset + PointyHexTileData.vertices[i + 1] * settings.cellScale;
                    uv[vert]        = PointyHexTileData.uv[i + 1];
                    colors[vert]    = map.values[x, z].cellMapColor;
                    triangles[vert] = vert++;
                }
            }
        }

        mesh.name      = "Discrete PointyHex Mesh";
        mesh.vertices  = vertices;
        mesh.uv        = uv;
        mesh.triangles = triangles;
        mesh.colors    = colors;
        mesh.RecalculateNormals();

        return(mesh);
    }
示例#10
0
    public static Mesh GenerateGridMesh(EmptyGrid map, TileMapSettings settings)
    {
        Mesh mesh = new Mesh();

        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[] vertices = new Vector3[2 * map.width + map.height * (map.width * 3 + 2)];
        int[,] cellCenters = new int[map.width, map.height];
        Vector2[] uv        = new Vector2[vertices.Length];
        Color[]   colors    = new Color[vertices.Length];
        int[]     triangles = new int[map.width * map.height * 18]; //6 = 6 triangles * 3 vertices

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

        int vert = 0;
        int tri  = 0;

        // Assign vertices
        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 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.cellScale, ref map.values[x, z].cellMapColor);

                CreateVertices(0, 2, ref vertices, ref uv, ref colors, ref vert, ref cellSpawnPoint, ref settings.cellScale, ref map.values[x, z].cellMapColor);

                if (z == 0)
                {
                    CreateVertices(2, 2, ref vertices, ref uv, ref colors, ref vert, ref cellSpawnPoint, ref settings.cellScale, ref map.values[x, z].cellMapColor);
                    if (x == 0)
                    {
                        CreateVertices(4, 2, ref vertices, ref uv, ref colors, ref vert, ref cellSpawnPoint, ref settings.cellScale, ref map.values[x, z].cellMapColor);
                    }
                }
                else if (z % 2 == 0)
                {
                    if (x == 0)
                    {
                        CreateVertices(4, 2, ref vertices, ref uv, ref colors, ref vert, ref cellSpawnPoint, ref settings.cellScale, ref map.values[x, z].cellMapColor);
                    }
                }
                else
                {
                    if (x == 0)
                    {
                        CreateVertices(5, 1, ref vertices, ref uv, ref colors, ref vert, ref cellSpawnPoint, ref settings.cellScale, ref map.values[x, z].cellMapColor);
                    }
                    if (x == map.width - 1)
                    {
                        CreateVertices(2, 1, ref vertices, ref uv, ref colors, ref vert, ref cellSpawnPoint, ref settings.cellScale, ref map.values[x, z].cellMapColor);
                    }
                }

                // Triangles
                CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref map.width, x, z, 0), GetVertexInHex(ref cellCenters, ref map.width, 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 map.width, x, z, i), GetVertexInHex(ref cellCenters, ref map.width, 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 map.width, x, z, i), GetVertexInHex(ref cellCenters, ref map.width, x, z, i + 1), ref triangles, ref tri);
                        }
                    }
                    else
                    {
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref map.width, x, z, 3), GetVertexInHex(ref cellCenters, ref map.width, x - 1, z, 2), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref map.width, x - 1, z, 2), GetVertexInHex(ref cellCenters, ref map.width, x - 1, z, 1), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref map.width, x - 1, z, 1), GetVertexInHex(ref cellCenters, ref map.width, x, z, 0), ref triangles, ref tri);
                    }
                }
                else if (z % 2 == 0)
                {
                    CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref map.width, x, z, 1), GetVertexInHex(ref cellCenters, ref map.width, x, z - 1, 0), ref triangles, ref tri);
                    if (x == 0)
                    {
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref map.width, x, z - 1, 0), GetVertexInHex(ref cellCenters, ref map.width, x, z - 1, 5), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref map.width, x, z - 1, 5), GetVertexInHex(ref cellCenters, ref map.width, x, z, 4), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref map.width, x, z, 4), GetVertexInHex(ref cellCenters, ref map.width, x, z, 5), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref map.width, x, z, 5), GetVertexInHex(ref cellCenters, ref map.width, x, z, 0), ref triangles, ref tri);
                    }
                    else
                    {
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref map.width, x, z - 1, 0), GetVertexInHex(ref cellCenters, ref map.width, x - 1, z - 1, 1), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref map.width, x - 1, z - 1, 1), GetVertexInHex(ref cellCenters, ref map.width, x - 1, z - 1, 0), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref map.width, x - 1, z - 1, 0), GetVertexInHex(ref cellCenters, ref map.width, x - 1, z, 1), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref map.width, x - 1, z, 1), GetVertexInHex(ref cellCenters, ref map.width, x, z, 0), ref triangles, ref tri);
                    }
                }
                else
                {
                    if (x == map.width - 1)
                    {
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref map.width, x, z, 1), GetVertexInHex(ref cellCenters, ref map.width, x, z, 2), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref map.width, x, z, 2), GetVertexInHex(ref cellCenters, ref map.width, x, z - 1, 1), ref triangles, ref tri);
                    }
                    else
                    {
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref map.width, x, z, 1), GetVertexInHex(ref cellCenters, ref map.width, x + 1, z - 1, 0), ref triangles, ref tri);
                        CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref map.width, x + 1, z - 1, 0), GetVertexInHex(ref cellCenters, ref map.width, x, z - 1, 1), ref triangles, ref tri);
                    }

                    CreateTriangle(ref cellCenters[x, z], GetVertexInHex(ref cellCenters, ref map.width, x, z - 1, 1), GetVertexInHex(ref cellCenters, ref map.width, x, z - 1, 0), ref triangles, ref tri);

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

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

        return(mesh);
    }
示例#11
0
    public static Mesh GenerateGridMesh(EmptyGrid map, TileMapSettings settings)
    {
        Mesh mesh = new Mesh();

        Vector2 bottomLeft;

        if (settings.isCentered)
        {
            bottomLeft = new Vector2(-map.width, -map.height) * settings.cellScale * 0.5f + new Vector2(settings.mapOffsetX, -settings.mapOffsetZ);
            //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);

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

        float vertexOffset = settings.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.cellScale, 0.0f, y * settings.cellScale) + new Vector3(bottomLeft.x, 0.0f, bottomLeft.y);

                vertices[vert]     = cellOffset;                                                    //bottom left
                vertices[vert + 1] = new Vector3(vertexOffset, 0.0f, 0.0f) + cellOffset;            //bottom right
                vertices[vert + 2] = new Vector3(0.0f, 0.0f, vertexOffset) + cellOffset;            //top left
                vertices[vert + 3] = new Vector3(vertexOffset, 0.0f, vertexOffset) + cellOffset;    //top right

                uv[vert]     = new Vector2((float)x / map.width, (float)y / map.height);
                uv[vert + 1] = new Vector2((float)(x + 1) / map.width, (float)y / map.height);
                uv[vert + 2] = new Vector2((float)x / map.width, (float)(y + 1) / map.height);
                uv[vert + 3] = new Vector2((float)(x + 1) / map.width, (float)(y + 1) / map.height);

                colors[vert] = colors[vert + 1] = colors[vert + 2] = colors[vert + 3] = map.values[x, y].cellMapColor;

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

                triangles[tri + 2] = vert++;                        //_ _ 1 | _ _ _
                triangles[tri + 1] = triangles[tri + 3] = vert++;   //_ 2 1 | 2 _ _
                triangles[tri]     = triangles[tri + 4] = vert++;   //3 2 1 | 2 3 _
                triangles[tri + 5] = vert++;                        //3 2 1 | 2 3 4
                tri += 6;
            }
        }

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

        return(mesh);
    }
    public static TileMapSettings PreprocessMap(TileMapSettings mapSettings, BinarySpacePartitioningSettings settings)
    {
        if (settings.roomsMinHeight < 3)
        {
            settings.roomsMinHeight = 3;
        }
        if (settings.roomsMinWidth < 3)
        {
            settings.roomsMinWidth = 3;
        }

        if (settings.useRoomsMaxSizeValues)
        {
            if (settings.roomsMaxHeight < settings.roomsMinHeight)
            {
                settings.roomsMaxHeight = settings.roomsMinHeight;
            }
            if (settings.roomsMaxWidth < settings.roomsMinWidth)
            {
                settings.roomsMaxWidth = settings.roomsMinWidth;
            }

            settings.roomsMinHeight = settings.roomsMaxHeight;
            settings.roomsMinWidth  = settings.roomsMaxWidth;

            if (settings.minSplitSizeHorizontal < settings.roomsMaxHeight)
            {
                settings.minSplitSizeHorizontal = settings.roomsMaxHeight;
            }
            if (settings.minSplitSizeVertical < settings.roomsMaxWidth)
            {
                settings.minSplitSizeVertical = settings.roomsMaxWidth;
            }

            if (settings.minSpaceSizeHorizontal < settings.minSplitSizeHorizontal * 2 - 1)
            {
                settings.minSpaceSizeHorizontal = settings.minSplitSizeHorizontal * 2 - 1;
            }
            if (settings.minSpaceSizeVertical < settings.minSplitSizeVertical * 2 - 1)
            {
                settings.minSpaceSizeVertical = settings.minSplitSizeVertical * 2 - 1;
            }

            if (mapSettings.mapHeight < settings.minSpaceSizeHorizontal)
            {
                mapSettings.mapHeight = settings.minSpaceSizeHorizontal;
            }
            if (mapSettings.mapWidth < settings.minSpaceSizeVertical)
            {
                mapSettings.mapWidth = settings.minSpaceSizeVertical;
            }
        }
        else
        {
            if (settings.minSplitSizeHorizontal < settings.roomsMinHeight)
            {
                settings.minSplitSizeHorizontal = settings.roomsMinHeight;
            }
            if (settings.minSplitSizeVertical < settings.roomsMinWidth)
            {
                settings.minSplitSizeVertical = settings.roomsMinWidth;
            }

            if (settings.minSpaceSizeHorizontal < settings.minSplitSizeHorizontal * 2 - 1)
            {
                settings.minSpaceSizeHorizontal = settings.minSplitSizeHorizontal * 2 - 1;
            }
            if (settings.minSpaceSizeVertical < settings.minSplitSizeVertical * 2 - 1)
            {
                settings.minSpaceSizeVertical = settings.minSplitSizeVertical * 2 - 1;
            }

            if (mapSettings.mapHeight < settings.minSpaceSizeHorizontal)
            {
                mapSettings.mapHeight = settings.minSpaceSizeHorizontal;
            }
            if (mapSettings.mapWidth < settings.minSpaceSizeVertical)
            {
                mapSettings.mapWidth = settings.minSpaceSizeVertical;
            }
        }

        return(mapSettings);
    }
    public static Mesh GenerateGridMesh(EmptyGrid map, TileMapSettings settings)
    {
        Mesh mesh = new Mesh();

        Vector2 bottomLeft;

        if (settings.isCentered)
        {
            bottomLeft = new Vector2(-map.width, -map.height) * 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[] vertices  = new Vector3[(map.width + 1) * (map.height + 1)];
        Vector2[] uv        = new Vector2[vertices.Length];
        int[]     triangles = new int[map.width * map.height * 6]; //6 = 2 triangles * 3 vertices
        Color[]   colors    = new Color[vertices.Length];

        float vertexOffset = settings.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.cellScale, 0.0f, y * settings.cellScale) + new Vector3(bottomLeft.x, 0.0f, bottomLeft.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;

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

                vert++;
            }
        }

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

                vert++;
                tri += 6;
            }

            vert++;
        }

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

        return(mesh);
    }