Detect if a point is not surrounded by other points or not

Viewed 57

Im making a chunk loading system for my game and I am making a cool effect for the cut off effect for the render distance and it requires to have a shader get scene depth and to accomplish this there needs to be a invisible wall that the player can pass through. I know how to generate the mesh at runtime but I need to know what points are not surrounded (edge points) so I can make them part of my mesh. The problem is that I do not know how to detect if they are surrounded or not. My code:

using UnityEngine;


public class TerrainGeneration : MonoBehaviour
{
    //create render/generation distance
    public int GenRadius = 5;
    int xSize, ySize;
    Vector2[] ChunkCords;

    void OnDrawGizmos()
    {
        xSize = GenRadius * 2;
        ySize = GenRadius * 2;
        ChunkCords = new Vector2[xSize * ySize];
        Gizmos.color = Color.blue;
        for (int x = -xSize/2, i = 0; x <= xSize / 2; x++)
        {
            for (int y = -ySize/2; y <= ySize/2; y++)
            {
                //if x,y is within GenRadius of 0,0
                if ((x * x) + (y * y) <= GenRadius * GenRadius)
                {
                    // draw points
                    Gizmos.DrawSphere(new Vector3(x, 0, y), 0.1f);
                    //store all points in a vector2 array
                    ChunkCords[i] = new Vector2(x, y);
                    i++;
                }
            }
        }
        for(int i = 0; i <= ChunkCords.Length; i++)
        {
            // edge detect code here
        }
    }
}

For clarification I want to automatically to find the points (all points are stored in the ChunkCords variable) outlined in blue enter image description here

Cheers and thank you!

1 Answers

Why not ONLY draw the edge points in the first place? Below I've replaced your Vector2 class with a Point since I do not have your references, but they act the same:

public class TerrainGeneration
{
    //create render/generation distance
    public int GenRadius = 5;
    int yMin, yMax;
    List<Point> ChunkCords;

    public void OnDrawGizmos()
    {
        // Assuming GenRadius is always int
        yMin = 0 - GenRadius;
        yMax = 0 + GenRadius;
        ChunkCords = new List<Point>();
        // Add the points on the X0 line
        ChunkCords.Add(new Point(0, yMin));
        ChunkCords.Add(new Point(0, yMax));

        int xValue;
        for (int y = yMin + 1; y < yMax; y++)
        {
            // For this value of Y on the circle, calculate X as SQRT(R2 - Y2)
            // Use Floor to ensure we take the closest integer INSIDE the radius
            xValue = (int)Math.Floor(Math.Pow(Math.Pow(GenRadius, 2) - Math.Pow(y, 2),0.5));
            // Add Both the postive and negative x varients to the list
            ChunkCords.Add(new Point(xValue, y));
            ChunkCords.Add(new Point(-xValue, y));
        }

        foreach(Point point in ChunkCords)
        {
            // Draw your point as you like
        }

    }
}

This will start with a negative Y value on the radius and iterate up on integer Y values to a positive value on the radius. For each Y value, the positive and negative X values are calculated to the floored integer, ensuring they are inside the radius.

Related