首页 > 解决方案 > 检测一个点是否没有被其他点包围

问题描述

我为我的游戏制作了一个块加载系统,我正在为渲染距离的截断效果制作一个很酷的效果,它需要一个着色器来获取场景深度,为了实现这一点,需要有一个玩家可以看到的隐形墙通过。我知道如何在运行时生成网格,但我需要知道哪些点没有被包围(边缘点),所以我可以让它们成为我的网格的一部分。问题是我不知道如何检测它们是否被包围。我的代码:

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
        }
    }
}

为了澄清起见,我想自动找到ChunkCords蓝色轮廓的点(所有点都存储在变量中)在此处输入图像描述

干杯,谢谢!

标签: c#algorithmunity3d

解决方案


为什么不首先绘制边缘点?下面我Vector2用 a 替换了你的类,Point因为我没有你的参考资料,但它们的作用相同:

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
        }

    }
}

这将从半径上的负 Y 值开始,并在整数 Y 值上迭代到半径上的正值。对于每个 Y 值,正负 X 值计算为下限整数,确保它们在半径内。


推荐阅读