c# - 检测一个点是否没有被其他点包围
问题描述
我为我的游戏制作了一个块加载系统,我正在为渲染距离的截断效果制作一个很酷的效果,它需要一个着色器来获取场景深度,为了实现这一点,需要有一个玩家可以看到的隐形墙通过。我知道如何在运行时生成网格,但我需要知道哪些点没有被包围(边缘点),所以我可以让它们成为我的网格的一部分。问题是我不知道如何检测它们是否被包围。我的代码:
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
蓝色轮廓的点(所有点都存储在变量中)
干杯,谢谢!
解决方案
为什么不首先绘制边缘点?下面我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 值计算为下限整数,确保它们在半径内。
推荐阅读
- linux - 如何知道 Libreoffice 安装在 Docker 映像中的什么位置?
- kerberos - linux docker容器中的Windows身份验证
- c# - 带有 xamarin 的免费地图(商业用途)
- sql-server - 将 id 从一个表复制到另一个
- c# - 触发器不适用于自定义控件图像按钮 wpf
- html - 为什么在这里使用(本地文件)chrome不播放音频?
- c# - WPF - 在异步任务的回调中更改 UI 文化
- ios - (Swift)即使没有文本,如何在 searchBar 上显示取消按钮?
- kubernetes - 重置 kubeadm 后无法删除 etcd
- django - 如何优化 Django admin 中针对来自另一个模型的自定义字段的查询计数?