Example #1
0
    /// <summary>
    /// Moore-Neighbor Tracing algorithm.
    /// </summary>
    /// <param name="body">The landmass.</param>
    /// <param name="i">The row.</param>
    /// <param name="j">The column.</param>
    private void TraceEdge(TerrainBody body, int i, int j)
    {
        Vector2Int start = new Vector2Int(i, j);

        body.AddVertex(ArrayToPosition(i, j));
        Vector2Int boundary    = start;
        Vector2Int enteredFrom = new Vector2Int(i, j - 1);
        //Vector2Int neighbor = new Vector2Int(i - 1, j);
        int        neighborIndex = 6; // North in MooreNeighbor array
        Vector2Int neighbor      = MooreNeighbor[neighborIndex];

        while (neighbor != start)
        {
            int row = neighbor.x;
            int col = neighbor.y;
            if (heights[row, col] > 0)
            {
                body.AddVertex(ArrayToPosition(row, col));
                enteredFrom = new Vector2Int(row, col);
                boundary    = neighbor;
                if (++neighborIndex > 7)
                {
                    neighborIndex = 0;
                }
                neighbor = MooreNeighbor[neighborIndex];
                //neighbor = NextMooreNeighbor(enteredFrom, neighbor);
            }
            else
            {
                if (++neighborIndex > 7)
                {
                    neighborIndex = 0;
                }
                neighbor = MooreNeighbor[neighborIndex];
                //neighbor = NextMooreNeighbor(enteredFrom, neighbor);
            }
        }
        if (!body.IsValid)
        {
            throw new System.Exception("Landmass was not closed!");
        }
    }
Example #2
0
    // It broke. For some reason every heights entry is the same and idk por quois
    private TerrainBody[] LoadBodies()
    {
        Terrain terrain = GetComponent <Terrain>();

        terrainData = terrain.terrainData;
        TerrainBody[] bodies = new TerrainBody[20];

        heights = terrainData.GetHeights(0, 0, numSamples, numSamples);//terrainData.alphamapWidth, terrainData.alphamapHeight);
        //heights = new float[terrainData.size, terrainData.size];
        //for (int i = 0; i < terrainData.size; ++i) {
        //    for (int j = 0; j < terrainData.size; ++j) {
        //        heights[i, j] = terrainData.GetHeight(i, j);
        //    }
        //}
        for (int i = 1; i < heights.GetLength(0) - 1; ++i)
        {
            for (int j = 1; j < heights.GetLength(1) - 1; ++j)
            {
                if (heights[i, j] <= 0.01F)
                {
                    continue;
                }
                if (heights[i, j] == Mathf.Infinity)
                {
                    while (j < terrainData.alphamapWidth - 1 && heights[i, j++] != Mathf.Infinity)
                    {
                    }
                    continue;
                }

                bodies[size] = new TerrainBody(terrain);
                TraceEdge(bodies[size], i, j);   // Traces edge of body and stores vertices in bodies[bodiesIndex]
                ++size;
            }
        }

        return(bodies);
    }