C# 如何高效地返回基于heightmap的平铺?
我正在按程序生成一个世界,对于Tilemap中的每个位置,我调用此函数以获得与高度匹配的瓷砖类型:C# 如何高效地返回基于heightmap的平铺?,c#,unity3d,optimization,procedural-generation,C#,Unity3d,Optimization,Procedural Generation,我正在按程序生成一个世界,对于Tilemap中的每个位置,我调用此函数以获得与高度匹配的瓷砖类型: public static NaturalTile GetNaturalTileByHeight(float height) { SortedDictionary<float, NaturalTile> tilesByHeight = new SortedDictionary<float, NaturalTile>(); foreach (var tile
public static NaturalTile GetNaturalTileByHeight(float height)
{
SortedDictionary<float, NaturalTile> tilesByHeight = new SortedDictionary<float, NaturalTile>();
foreach (var tile in NaturalTileTypes)
{
if (tile.MaxGenHeight > 0) tilesByHeight.Add(tile.MaxGenHeight, tile);
}
foreach (var tile in tilesByHeight)
{
if (height <= tile.Key) return tile.Value;
}
return null;
}
public static NaturalTile GetNaturalTileByHeight(浮动高度)
{
SortedDictionary tilesByHeight=新的SortedDictionary();
foreach(自然多类型中的变量tile)
{
如果(tile.MaxGenHeight>0)tilesByHeight.Add(tile.MaxGenHeight,tile);
}
foreach(瓷砖高度中的var瓷砖)
{
如果(height,则不需要对它们进行单独排序。只需在一个循环中找到参数height
上方的最佳平铺(MaxGenHeight
,但离它最近)
public static NaturalTile GetNaturalTileByHeight(float height)
{
NaturalTile bestTile = null;
foreach (var tile in NaturalTileTypes)
{
// Is this tile type valid for the given height?
if (tile.MaxGenHeight > 0 && tile.MaxGenHeight >= height)
{
// Is it better than the current best tile?
if (bestTile == null || tile.MaxGenHeight < bestTile.MaxGenHeight)
{
bestTile = tile;
}
}
}
return bestTile;
}
public static NaturalTile GetNaturalTileByHeight(浮动高度)
{
NaturalTile bestTile=null;
foreach(自然多类型中的变量tile)
{
//此平铺类型对给定高度有效吗?
如果(tile.MaxGenHeight>0&&tile.MaxGenHeight>=高度)
{
//它比目前最好的瓷砖好吗?
if(bestTile==null | | tile.MaxGenHeight
使用字典在这里不是非常有用,因为你没有离散值,所以你不能从字典查找的效率中获益。相反,你可以在O(n)时间内使用它(迭代所有项目,直到找到你想要的)。你可以定义某种树结构,允许更快的查找(如果正确实施)。