Implementing Marching Cubes


Overview

I started the week off by implementing the Marching Cubes Algorithm. Once that was up and running, I started refactoring the code, so that I can now have chunks (small parts of a bigger volume). Each chunk creates its mesh with the Marching Cube. Then I went ahead and refactored to code once again, now structuring it, so that the chunks represent an Octree-like data structure. This allows each chunk to be split into 8 smaller, child-chunks with higher resolution. After that, I wrote a little camera-script that now dynamically changes the resolution of the object by making smaller chunks when getting closer to them. The different chunk sizes lead to cracks inside the mesh, when a neighbouring chunk has a different size. This problem I will have to tackle next week.

Marching Cubes Implementation

The implementation of the Marching Cube Algorithm was pretty straight forward. I created a class called MC_Vertex. This represents each Vertex of the object. It holds the Vector3 position and a boolean whether the vertex is inside the mesh or not. The second class needed is the MC_Octree. This is the class holding all the logic. It represents one chunk, holding its position, its size and resolution along side with other Unity specific things like material, mesh components etc.

When creating an instance of the MC_Octree, a Game-object is being added to the unity scene holding the mesh renderer etc. It also creates all the vertices needed. With those vertices, the cube can begin to march its way through the chunk. This is actually much simpler than one might think. We simply take 8 vertices that for a cube, then, adding up an index in the following order:

With this index, we can now look up how the mesh will have to look like, or rather how the triangulation has to look like. Lookup tables for this can be found on the internet, I use this one. With that, we can set the vertices and triangles for the mesh and let it generate.

Dynamic LOD / Octree

By further expanding the MC_Octree class with an array on child-chunks, which also are MC_Octree instances, we can dynamically grow the octree. Once the camera gets close enough to the current chunk, a divide-function is being called. This divide-function splits up the current chunk into its children, thus doubling its resolution.

Far cam

Closer cam (1 step closer)

!Those screenshots are just small test results, the final product will be ,much bigger, with much higher resolution!

Even tho the dynamic chunk loading seems to work pretty good, there still is the crack-problem. Cracks occur when different chunk sizes are next to each other.I already have a possible solution for this in mind, but I will have to try it out next week. So stay tuned for that!

Get TerraNovaOdyssey - Planet Generation

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.