- Home /
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
alucardj, thats not the same kind of marching cubes implementation. The one Paul Bourke presents interpolates the edges to create smooth hulls while the one I posted in that answer is just "$$anonymous$$ecrafty" blocky voxels.
Yes, Statement is correct but thank you for contributing, alucardj.
Based on Paul Bourke's, possible point of reference with the information in Statements answer :
http://wiki.unity3d.com/index.php?title=$$anonymous$$etaBalls
more theory : http://www.exaflop.org/docs/marchcubes/
Edit : no worries, I shall go away now =]
I have seen metaballs before but I am not sure how to adapt it to function like the game I linked to in my question. Thanks again!
Answer by Statement · Mar 08, 2013 at 03:08 PM
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:
Generate triangles for a given chunk
Create a new Mesh object and assign verts, uvs, normals, indices etc
Create a new GameObject to hold this chunk
Add a MeshFilter to the GameObject
Assign the Mesh to the MeshFilter
Add a MeshRenderer to the GameObject
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.
Unfortunately I rarely save source code for pet projects, otherwise I could have shown the messy code I had.
Thank you for the answer! This is what I meant by grid (quoted from first paragraph of solution here):
The fundamental problem is to form a facet approximation to an isosurface through a scalar field sampled on a rectangular 3D grid. Given one grid cell defined by its vertices and scalar values at each vertex, it is necessary to create planar facets that best represent the isosurface through that grid cell.
However, I think your description of a chunk is the same as this and what I want.
Yes, I do have a function for generating triangles. I have the whole of Paul Bourke's Polygonise source code in C#.
This clayful? If so, you know how to make what I want. I just want a box/sphere object to do the cutting/drawing of the terrain like in this game.
Thanks very much. I will try this and get back to you. I appreciate the help.
$$anonymous$$
Yeah okay if you can generate triangles based on a float[,,] scalarField = new scalarField[16, 16, 16]; then all you need to do is to paint into that (add/subtract) and then regenerate the mesh
Would I use this code to generate a chunk of triangles:
using UnityEngine;
using System.Collections;
public class example : $$anonymous$$onoBehaviour {
public Vector3[] newVertices;
public Vector2[] newUV;
public int[] newTriangles;
void Start() {
$$anonymous$$esh mesh = new $$anonymous$$esh();
GetComponent<$$anonymous$$eshFilter>().mesh = mesh;
mesh.vertices = newVertices;
mesh.uv = newUV;
mesh.triangles = newTriangles;
}
}
Then add the 8 vertices of the cube/chunk. I'm not sure what to use the triangles array for though. Thanks again!
Yeah okay if you can generate triangles based on a float[,,] scalarField = new scalarField[16, 16, 16]; then all you need to do is to paint into that (add/subtract) and then regenerate the mesh
Could you explain this in more detail, please? What steps in your list would that method be in place of?
Answer by Sammael · Sep 26, 2013 at 07:03 PM
Take a look into this link: Link removed due to phishing site (2017.01.06)
Hope it helps
the link above is a phishing site. DO NOT CLIC$$anonymous$$ IT!
I guess the blog has been hijacked. I remove the link from the answer to avoid people clicking it.
Thanks for the info.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Fading To New Scene Problem? 1 Answer
I know C#, but what Unity methods should I learn? 2 Answers
UDP implementation in Unity, unable to send to two different machines 0 Answers