【发布时间】:2019-04-15 00:11:49
【问题描述】:
我正在开发体素地形生成器。一切都很好,我有生物群落、方块等。
让我发痒的是我的项目的统一速度。如果我在主线程上运行所有内容,我只能加载和渲染 1 到 2 个块而不会低于 70fps。这主要是因为块中的每个块都必须检查它们的邻居来定义它们的块侧可见性。一个块有 6 个邻居,一个块有 16 个块。这可以非常快速地进行大量检查。
我听说过我的世界是单线程的,但我很难相信,因为它的块加载速度非常快并且没有 fps 下降。
我的解决方案是在另一个线程上检查一个块的块的邻居。这将大大提高我的 fps 和我的块加载速度。这是正确的方法吗?我不想使用线程,因为我的代码没有优化。这就像把灰尘推到地毯下面。
感谢阅读
编辑:检查邻居的代码
//Block provides its mesh information
//Check for solidity of adjacent blocks
public virtual MeshData CreateBlockData(Chunk chunk, int x, int y, int z, MeshData meshData)
{
//Set this to true to turn on collider creation shaped like the chunks
meshData.useRenderDataForCol = true;
if (!chunk.GetBlock(x, y + 1, z).IsSolid(Direction.down))
{
meshData = FaceDataUp(chunk, x, y, z, meshData);
}
if (!chunk.GetBlock(x, y - 1, z).IsSolid(Direction.up))
{
meshData = FaceDataDown(chunk, x, y, z, meshData);
}
if (!chunk.GetBlock(x, y, z + 1).IsSolid(Direction.south))
{
meshData = FaceDataNorth(chunk, x, y, z, meshData);
}
if (!chunk.GetBlock(x, y, z - 1).IsSolid(Direction.north))
{
meshData = FaceDataSouth(chunk, x, y, z, meshData);
}
if (!chunk.GetBlock(x + 1, y, z).IsSolid(Direction.west))
{
meshData = FaceDataEast(chunk, x, y, z, meshData);
}
if (!chunk.GetBlock(x - 1, y, z).IsSolid(Direction.east))
{
meshData = FaceDataWest(chunk, x, y, z, meshData);
}
return meshData;
}
//The center of block is the origin
protected virtual MeshData FaceDataUp(Chunk chunk, int x, int y, int z, MeshData meshData)
{
meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z + 0.5f));
meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f));
meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z - 0.5f));
meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z - 0.5f));
meshData.AddQuadTriangles();
//Adds UVs range (0 to 3) to uv list
meshData.uv.AddRange(FaceUVs(Direction.up));
return meshData;
}
因此,每个 16x16x16 块的块有 4096 个块来运行此功能。
创建块的代码只是一个包含以下内容的三重 for 循环:
static void GeneratePlainBiome(Chunk chunk, int x, int y, int z, FastNoise noise)
{
int stoneHeight = GetNoise2D(noise, x, z, 0, 50);
int chunkX = (int)chunk.transform.position.x;
int chunkY = (int)chunk.transform.position.y;
int chunkZ = (int)chunk.transform.position.z;
if(y == 0)
{
chunk.SetBlock(x - chunkX, y - chunkY, z - chunkZ, new BlockSnow());
}
else if(stoneHeight > y)
{
chunk.SetBlock(x - chunkX, y - chunkY, z - chunkZ, new BlockEarth());
}
else if(stoneHeight == y)
{
chunk.SetBlock(x - chunkX, y - chunkY, z - chunkZ, new BlockGrass());
}
else
{
chunk.SetBlock(x - chunkX, y - chunkY, z - chunkZ, new BlockAir());
}
}
填充一个块后,我用这个函数渲染网格:
//Sends the calculated mesh information to the mesh and collision components
void RenderMesh(MeshData meshData)
{
//Mesh construction
filter.mesh.Clear();
filter.mesh.vertices = meshData.vertices.ToArray();
filter.mesh.triangles = meshData.triangles.ToArray();
//Uv mapping
filter.mesh.uv = meshData.uv.ToArray();
filter.mesh.RecalculateNormals();
//Collision component creation
coll.sharedMesh = null;
Mesh meshColl = new Mesh();
meshColl.vertices = meshData.colVertices.ToArray();
meshColl.triangles = meshData.colTriangles.ToArray();
meshColl.RecalculateNormals();
coll.sharedMesh = meshColl;
}
所以要继续,我正在检查块的 16x16x16 块,以了解如何根据邻居渲染块网格。完成该功能后,我可以选择渲染块。我正在这样做,假设玩家周围有一个 16x16x16 的块。 (即使我每帧做一个块,我的 fps 下降也很糟糕。)
编辑 2:
对于chunk脚本中的chunk.SetBlock()和chunk.GetBlock():
public void SetBlock(int x, int y, int z, Block block)
{
if (InRange(x) && InRange(y) && InRange(z))
{
blocks[x, y, z] = block;
}
else
{
LoadBiomes.SetBlock((int)transform.position.x + x, (int)transform.position.y + y, (int)transform.position.z + z, block);
}
}
public Block GetBlock(int x, int y, int z)
{
if(InRange(x) && InRange(y) && InRange(z))
{
Block block = blocks[x, y, z];
return block;
}
else
{
//return new BlockAir();
int xPos = (int)transform.position.x + x;
int yPos = (int)transform.position.y + y;
int zPos = (int)transform.position.z + z;
Block blockToReturn = LoadBiomes.GetBlock(xPos,yPos,zPos);
return blockToReturn;
}
}
//This work since the values passed to the function are block position - chunk position
public static bool InRange(int index)
{
if (index < 0 || index >= CHUNK_SIZE)
return false;
return true;
}
块脚本中的 isSolid(如果游戏只有立方体,则不是很重要
//Every face is solid for a cube
public virtual bool IsSolid(Direction direction)
{
switch (direction)
{
case Direction.north:
return true;
case Direction.east:
return true;
case Direction.south:
return true;
case Direction.west:
return true;
case Direction.up:
return true;
case Direction.down:
return true;
}
return false;
}
以及来自分析器的图像(不确定这是否是所要求的)
【问题讨论】:
-
你是在计算所有~加载的方块~还是只计算当前视野中的方块的可见性?
-
我在玩家周围有一排区块(比如说 8x8x8),我在根据区块的可见性渲染区块网格之前检查每个区块的每个区块@FiringSquadWitness
-
您是否在调试或发布版本中测量过您的性能?
-
我没有进行构建以使统计信息和分析器选项卡可用。 @user743414
-
如果我有一个发布版本,我认为这不会有任何帮助。也许我会掌握一些 fps,但这并不是重点。
标签: c# multithreading unity3d minecraft procedural-generation