How to implement marching cubes in Unity with C#

Hi

I am wanting to create a game with similar use of marching cubes as this
game for my final year project in university. The differences being mine will be a horror game, you cannot change the distance of the ‘terrain-editing box’ from the player and it will be played from a first-person viewpoint.

So far, I have translated the C Source code of Paul Bourke’s tutorial to C#. I’m not sure how to actually use this code to create something like the game I linked to. I think I will need to create a grid and a custom, modifiable mesh. I don’t know how to create the grid. I have seen this for how to create a mesh, but I’m not sure how to use marching cubes to form and change this mesh.

I have seen that there are many other questions about marching cubes but none are the same as what I am asking, as far as I am aware. I have seen this question and it has a lot of source code (more further down the page) but I can’t get my head around it.

If anyone could just give me a list of the steps I would need to take to move from the Paul Bourke source code to the linked game, that would be great. Any code or tips would also be a great help.

Thank you so much for any help with this,

Daniel

I am not sure I know what you mean by “a grid”. I’ll assume you mean a chunk, which means that you kind of slice the space up in boxes. These boxes contains typically one mesh, the voxel datas generated triangles for that particular chunk.

So your input is some scalar field, the voxel input. You probably have a function (or consider making one) to generate triangles for a given box in space. That is like saying, “from 0, 0, 0 to 16, 16, 16, give me all of the triangles for this area”. These triangles are what goes into your chunk or “grid” if I understand you correctly. Each chunk could be a game object with a MeshRenderer and MeshFilter on it, and the output of your voxel scalar field to polygon should be assigned to a Mesh. As you can see, you can assign vertices and indices on a new Mesh object and this is how I did it when I wrote Clayful - which also was derived from Paul Bourkes code.

So I guess you’d do this in an outlined form:

  1. Generate triangles for a given chunk
  2. Create a new Mesh object and assign verts, uvs, normals, indices etc
  3. Create a new GameObject to hold this chunk
  4. Add a MeshFilter to the GameObject
  5. Assign the Mesh to the MeshFilter
  6. Add a MeshRenderer to the GameObject
  7. Assign an appropriate Material to the Renderer

This would represent one chunk in a very simple form. To make another chunk, just triangulate another region of space.

I solved texturing using triplanar texturing - this is basically just projecting 3 textures on the mesh, from 3 axes (x, y and z projection) and interpolating between them based on the normal.

To make the model editable, I used a 3d float (or byte) array that represented the scalar field. When you modify (carve/build) you just modify the floats (add/subtract). So your polygonizer should sample that 3d array. This is a naive implementation and I am sure there could be ways to make it faster, better, smaller, smarter and so on, but I was happy to just get it working in my run.

Take a look into this link: Link removed due to phishing site (2017.01.06)

Hope it helps