Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

  • 0
Rhodos32

Voxel Chunk Creation

Question

Hello Guys!

 

I tried to create a voxel based world for learning and idk i just cant get the Chunks Good -.-

 

My Blocks are Script Created and they are Planes!

 

So my Question is is there any way to do it better with Cubes or something!


Right now after a Chunk is Created 10x5x10 i make a Call to the blocks that they should check if there is any other block on top,bottom,left,right and and and but this eats a lot of Performance!

 

 

Please login or register to see this attachment.

Please login or register to see this attachment.

Please login or register to see this attachment.

Share this post


Link to post
Share on other sites

6 answers to this question

Recommended Posts

  • 0

What's eating performance is not checking directions, it's that each chunk is instantiating/containing hundreds of objects/meshes. Each chunk is supposed to be 1 gameobject containing 1 mesh and you just alter the mesh data anytime that chunk has changed

 

edit: as you currently have it, each face is a different gameobject/mesh, which is even more performance intensive then just instantiating cubes. But ideally it all needs to be one gameobject and one mesh

Share this post


Link to post
Share on other sites
  • 0

Ok that could be the problem :)

But now i must figure out how to do it^^ can u give me a little help with it?

 

 

Edit: Please login or register to see this link. ...Creating Meshes by script and edit it and than put it into one big mesh... im right?

 

Share this post


Link to post
Share on other sites
  • 0

I'm on my phone right now and not near a computer but you can either check out a tutorial or check out the mesh building part in one of my projects: 

 

the basic concept is to instead of instant a new plane, just add to the mesh data the verts, uv and normals. You can check out the isTransparent function in project that checks if a face should be built, and if so, it auto generates mesh data to be added. Once all the mesh data is added, you assign it: meshFilter.mesh = customMesh; if you still need help, I can type something up sometime when around a computer and have time but not sure how long that would be

 

edit: or on my lunch break in a couple hours I can show you how to build the vertices. I won't be able to do the uvs and blahty blah tho bc there is is only so much I can type up on a phone without going crazy

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

Thanks i will check out and try multiple things^^ :)

Share this post


Link to post
Share on other sites
  • 0

Ok, I'm on my lunch break so I'll try to write up some quick pseudo code for an intro to voxel vert generation on my phone. Just remember it's pseudo code:

 

//loop thru the chunk

for(x)

{

for(y)

{

for(z)

{

//if this space is empty, skip it

if(chunk[x][y][z] == 0) continue;

 

//if the block above is empty

v3 up = (x, y+1,z);

if(!isEmpty(up);

{

//build the face

//params (start vert, up the plane if you were looking directly at it, right of the plane if you were looking directly at it

BuildFace(up, v3.forward, v3.right);

}

 

//if the block behind is empty

v3 back = (x,y,z);

if(!isEmpty(x,y,z-1))

{

//same comment as before

BuildFace(back, v3.up, v3.right);

}

 

//if the block to the right is empty

v3 right = (x+1,y,z);

if(!isEmpty(right))

{

BuildFace(right, v3.up, v3.forward);

}

 

//and so on for the other faces

}

}

}

newMesh.vertices = verts.toArray();

meshFilter.mesh = newMesh;

 

 

function isEmpty(v3 p)

{

//return false if v3 is out of bounds

if(p.x < 0 || p.y < 0 || p.z < 0 || p.x >= chunkSize.x || p.y >= chunkSize.y || p.z >= chunkSize.z) return false;

 

//else return the value of the block

return chunk[p.x][p.y][p.z] == 0;

}

 

function BuildFace(start, up, right)

{

//Add verts for the 1st triangle

verts.add(start);

verts.add(start + up);

verts.add(start + up + right);

 

//Add verts for the 2nd triangle

verts.add(start + up + right);

verts.add(start);

verts.add(start + right);

}

Share this post


Link to post
Share on other sites
  • 0

You don't actually need that many verts because you can reuse the 1st and third vert in a face when calculating the triangle array but for the sake of simplicity so I don't go crazy typing it on my phone and have to add in tris as well as verts, it'll work to get a basic understanding of how to do it. It also doesn't account for more than one chunk or the chunk not started on (0,0,0). You would just have to add those calculations in when building the verts. For instance, instead of verts.add(start); it would be verts.add(chunkPosition + start); and instead of continuing when checking is a block is out of bounds, compare it to the neighboring chunk

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×