【问题标题】:Minecraft speed for loading chunks加载区块的 Minecraft 速度
【发布时间】: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


【解决方案1】:

我不是专家,但据我所知,unity 3d 使用多边形而不是体素引擎。体素引擎是不同的。

这种差异的直接后果是多边形可以 有效地表示具有大量空的简单 3D 结构或 均匀填充的空间,而体素擅长表示 定期采样的非均匀填充的空间。

https://en.wikipedia.org/wiki/Voxel

技术细节见:

一些体素引擎所做的是使用大数组,然后使用它们来确定什么在视野范围内,什么不在视野范围内。这与从 Quake 开始的经典 3d 多边形做事方式非常不同。著名的体素游戏包括 Comanche 系列、Outcast ... 以及现在的 Minecraft。

【讨论】:

  • 虽然,Minecraft 是最知名的体素游戏,但它的表现非常糟糕,如果不是最糟糕的话。 Unity3d 为 Minecraft 克隆提供了足够的功能(实际上有人在 YouTube 上使用 Unity3d 编写了我的世界)。我只是在看其他一些多人体素项目,看看可以用更少的资源消耗做些什么。
  • 我不知道关于 Minecraft 是如何工作的任何细节,我想在 2019 年你可以通过硬件加速来强制使用多边形体素引擎。
  • 谢谢解答,我觉得不是unity相关的问题。正如您在我所做的编辑中看到的那样,我认为我有太多的迭代。此外,我正在渲染播放器旁边的内容。不是我一次渲染太多,只是我有很多迭代,我不知道如何减少它们。
  • 很有趣,我听说大家都说我的世界性能很差,但我找不到一个做得更好或接近其性能的教程@eocron
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多