Hey all,
I’m currently working on a voxel engine like minecraft in Unity.
Right now i have a chunk that contains creates a mesh based on the cubes inside.
The problem is that a mesh cannot contain more than 65000 vertices, this disallows the chunk to have more than somewhere like 12x12x12 cubes.
My question is if there is a way around this or if I have to take a different approach than having the entire chunk be one mesh (Mobs etc. will not be included in the chunk mesh, it’s just the cubes.).
The mesh creating part of the chunk looks as follows:
///
/// Creates the mesh.
///
public void CreateMesh()
{
int x;
int y;
int z;
for (x = 0; x < CHUNK_SIZE; ++x)
{
for (y = 0; y < CHUNK_SIZE; ++y)
{
for (z = 0; z < CHUNK_SIZE; ++z)
{
// If the block is inactive it should not be drawn.
if (blocks[x,y,z].IsActive == false)
{
continue;
}
CreateCube(x, y, z);
}
}
}
mesh.vertices = positions.ToArray();
mesh.triangles = indices.ToArray();
mesh.uv = uvs.ToArray();
mesh.RecalculateNormals();
Color[] colors = new Color[mesh.vertexCount];
for (int i = 0; i < colors.Length; i++)
{
colors *= new Color(1.0f, 1.0f, 1.0f);*
-
}*
mesh.colors = colors;
-
}*
-
///*
-
/// Updates the mesh.*
-
///*
-
public void UpdateMesh()*
-
{*
-
mesh.Clear();*
-
CreateMesh();*
-
}*
-
///*
-
/// Creates a cube.*
-
///*
-
/// <param>*
-
/// X.*
-
///*
-
/// <param>*
-
/// Y.*
-
///*
-
/// <param>*
-
/// Z.*
-
///*
-
public void CreateCube(int x, int y, int z)*
-
{*
x = (x * BLOCK_RENDER_SIZE) + chunkX * CHUNK_RENDER_SIZE;
y = (y * BLOCK_RENDER_SIZE) + chunkY * CHUNK_RENDER_SIZE;
z = (z * BLOCK_RENDER_SIZE) + chunkZ * CHUNK_RENDER_SIZE;
-
Vector3 p1 = new Vector3(x - (BLOCK_RENDER_SIZE / 2), y - (BLOCK_RENDER_SIZE / 2), z + (BLOCK_RENDER_SIZE / 2));*
-
Vector3 p2 = new Vector3(x + (BLOCK_RENDER_SIZE / 2), y - (BLOCK_RENDER_SIZE / 2), z + (BLOCK_RENDER_SIZE / 2));*
-
Vector3 p3 = new Vector3(x + (BLOCK_RENDER_SIZE / 2), y + (BLOCK_RENDER_SIZE / 2), z + (BLOCK_RENDER_SIZE / 2));*
-
Vector3 p4 = new Vector3(x - (BLOCK_RENDER_SIZE / 2), y + (BLOCK_RENDER_SIZE / 2), z + (BLOCK_RENDER_SIZE / 2));*
-
Vector3 p5 = new Vector3(x + (BLOCK_RENDER_SIZE / 2), y - (BLOCK_RENDER_SIZE / 2), z - (BLOCK_RENDER_SIZE / 2));*
-
Vector3 p6 = new Vector3(x - (BLOCK_RENDER_SIZE / 2), y - (BLOCK_RENDER_SIZE / 2), z - (BLOCK_RENDER_SIZE / 2));*
-
Vector3 p7 = new Vector3(x - (BLOCK_RENDER_SIZE / 2), y + (BLOCK_RENDER_SIZE / 2), z - (BLOCK_RENDER_SIZE / 2));*
-
Vector3 p8 = new Vector3(x + (BLOCK_RENDER_SIZE / 2), y + (BLOCK_RENDER_SIZE / 2), z - (BLOCK_RENDER_SIZE / 2));*
-
int v1;*
-
int v2;*
-
int v3;*
-
int v4;*
-
int v5;*
-
int v6;*
-
int v7;*
-
int v8;*
-
// Front.*
-
v1 = addVertex(p1);*
-
v2 = addVertex(p2);*
-
v3 = addVertex(p3);*
-
v4 = addVertex(p4);*
-
indices.Add(v1);*
-
indices.Add(v2);*
-
indices.Add(v3);*
-
indices.Add(v1);*
-
indices.Add(v3);*
-
indices.Add(v4);*
-
// Back.*
-
v5 = addVertex(p5);*
-
v6 = addVertex(p6);*
-
v7 = addVertex(p7);*
-
v8 = addVertex(p8);*
-
indices.Add(v5);*
-
indices.Add(v6);*
-
indices.Add(v7);*
-
indices.Add(v5);*
-
indices.Add(v7);*
-
indices.Add(v8);*
-
// Right.*
-
v2 = addVertex(p2);*
-
v5 = addVertex(p5);*
-
v8 = addVertex(p8);*
-
v3 = addVertex(p3);*
-
indices.Add(v2);*
-
indices.Add(v5);*
-
indices.Add(v8);*
-
indices.Add(v2);*
-
indices.Add(v8);*
-
indices.Add(v3);*
-
// Left.*
-
v6 = addVertex(p6);*
-
v1 = addVertex(p1);*
-
v4 = addVertex(p4);*
-
v7 = addVertex(p7);*
-
indices.Add(v6);*
-
indices.Add(v1);*
-
indices.Add(v4);*
-
indices.Add(v6);*
-
indices.Add(v4);*
-
indices.Add(v7);*
-
// Top.*
-
v4 = addVertex(p4);*
-
v3 = addVertex(p3);*
-
v8 = addVertex(p8);*
-
v7 = addVertex(p7);*
-
indices.Add(v4);*
-
indices.Add(v3);*
-
indices.Add(v8);*
-
indices.Add(v4);*
-
indices.Add(v8);*
-
indices.Add(v7);*
-
// Bottom.*
-
v6 = addVertex(p6);*
-
v5 = addVertex(p5);*
-
v2 = addVertex(p2);*
-
v1 = addVertex(p1);*
-
indices.Add(v6);*
-
indices.Add(v5);*
-
indices.Add(v2);*
-
indices.Add(v6);*
-
indices.Add(v2);*
-
indices.Add(v1);*
-
}*