【问题标题】:Detect if a point is not surrounded by other points or not检测一个点是否没有被其他点包围
【发布时间】:2020-09-02 01:25:59
【问题描述】:

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

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 变量中)

干杯,谢谢!

【问题讨论】:

  • 包围是什么意思?你创建了很多半径为 0.1f 的球体,你能详细说明一下在不同的位置吗?
  • 我建议您使用“格雷厄姆扫描”算法来查找“凸包”。 en.wikipedia.org/wiki/Graham_scan
  • 嗨@Frenchy,我的意思是边缘点没有完全被其他点“包围”,因此被视为“边缘点”

标签: c# algorithm unity3d


【解决方案1】:

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

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 值计算为下限整数,确保它们在半径内。

【讨论】:

    猜你喜欢
    • 2013-02-21
    • 2014-03-01
    • 2011-10-21
    • 1970-01-01
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    相关资源
    最近更新 更多