Exemple #1
0
    private void _findAreas(TerrainData data)
    {
        HashSet<Block> visited = new HashSet<Block>();

        int width = data.Width;
        int height = data.Height;

        LinkedList<Area> areas = new LinkedList<Area>();

        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                Block block = data.GetBlock(new Point(x, y));
                if (block.Value > 0 && !visited.Contains(block))
                {
                    Area newArea = _floodFillArea(block, visited);
                    if (newArea != null)
                        areas.AddLast(newArea);
                }
            }
        }

        foreach (Area area in areas)
        {
            data.AddArea(area);
        }
    }
Exemple #2
0
    private void _generateMesh(TerrainData data)
    {
        Vector3 startPosition = transform.position - new Vector3(m_data.Width, 0, m_data.Height) * m_boxSize * 0.5f;
        Vector3 step = new Vector3(m_boxSize, 0, m_boxSize);

        int width = data.Width;
        int height = data.Height;
        
        ControlNode[,] nodes = new ControlNode[width,height];

        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                Vector3 offset = startPosition + new Vector3(step.x * x, 0.0f, step.z * y);
                nodes[x, y] = new ControlNode(offset, data.GetBlock(new Point(x, y)), m_boxSize);
            }
        }

        LinkedList<Square> squares = new LinkedList<Square>();

        for (int y = 0; y < height - 1; y++)
        {
            for (int x = 0; x < width - 1; x++)
            {
                ControlNode bottomLeft = nodes[x, y];
                ControlNode bottomRight = nodes[x + 1, y];
                ControlNode topLeft = nodes[x, y + 1];
                ControlNode topRight = nodes[x + 1, y + 1];

                Square square = new Square(topLeft, topRight, bottomLeft, bottomRight);
                squares.AddLast(square);
            }
        }

        LinkedList<Vector3> verticies = new LinkedList<Vector3>();
        LinkedList<int> indicies = new LinkedList<int>();

        foreach (Square square in squares)
        {
            _buildSquare(square, verticies, indicies);
        } 

        Mesh mesh = new Mesh();
        GetComponent<MeshFilter>().mesh = mesh;

        mesh.vertices = verticies.ToArray();
        mesh.triangles = indicies.ToArray();
        mesh.RecalculateNormals();
    }