Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minecraft speed for loading chunks

I am working on a voxel terrain generator. Everything is fine, I have biomes, blocks, etc.

What tickles me is the speed of my project in unity. If I run everything on the main thread I can only load and render 1 to 2 chunks without dropping below 70fps. That's mainly because every block in a chunk has to check their neighbours to define their block side visibility. A block has 6 neighbours and a chunk has 16 blocks. That makes a lot of checks very quickly.

I've read that minecraft is single threaded but I have a hard time believing that since its chunk loading speed is quite fast and without fps drops.

My solution would be to run the checkings of a chunk's blocks's neighbours on a another thread. It would greatly improve my fps and my chunk loading speed. Is it the correct way though? I don't want to have to use threads because my code is not optimized. It would be like pushing the dust under the carpet.

Thanks for reading

EDIT : Code that checks for neighbours

//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;
}

Therefor, every chunk which is 16x16x16 blocks has 4096 blocks to run this function on.

The code that creates the blocks is simply a triple for loop containing this :

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());
    }
}

After I have filled a chunk, I render the mesh with this function :

//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;
}

So to resume, I'm checking the 16x16x16 blocks of a chunk to know how to render the chunk mesh based on neighbours. Once I am done with that function, I can choose to render the chunk. I'm doing that, let's say for a 16x16x16 chunks around the player. (Even if I do one chunk a frame, I get pretty bad fps drops.)

EDIT 2 :

For the chunk.SetBlock() and chunk.GetBlock() from the chunk script :

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;
}

The isSolid in the block script (is not really important if game only have cubes

//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;
}

And the image from the profiler (not sure if that is what was asked)

Profiler

like image 978
Pouissante Avatar asked Nov 07 '22 18:11

Pouissante


1 Answers

I am not an expert but as far as I know unity 3d uses polygons and is not a voxel engine. Voxel engines are different.

A direct consequence of this difference is that polygons can efficiently represent simple 3D structures with lots of empty or homogeneously filled space, while voxels excel at representing regularly sampled spaces that are non-homogeneously filled.

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

For technical details see:

  • https://0fps.net/2012/01/14/an-analysis-of-minecraft-like-engines/
  • http://advsys.net/ken/voxlap.htm

What some voxel engines do is use big arrays then use them to determine what is in the field of view and what is not. This is very different from the classic 3d polygon way of doing things which started with Quake. Famous voxel games include Comanche series, Outcast ... and now Minecraft.

like image 198
Christophe Roussy Avatar answered Nov 15 '22 00:11

Christophe Roussy