Hi guys.
I’m working in a voxel based terrain and finally got it working.
Now, I wanto to smooth it so it doesn’t look so “cubic”. I found that marching cubes may be the answer to this problem, and I started to search more about this subject.
After searching I found this website:
wich explains it pretty well I think.
But I still have no idea how to do it. Based on the explanation given on the previous website. It says that if a vertice is outside our desired model we shoud draw a triangle in the middle of the 3 edges that make that vertice. Ok pretty easy to understand, what is hard as sh*t to understand, at least to me, is, how do I know if the vertice is inside or outside the “model”, if I don’t even know how the model is?I mean I could check if some block has neighbours, and after that implement that process to the vertices where there are no neighbours, but I fear that this will lead to some bugs/glitches because if, let’s say a block has 3 blocks of empty sapace around it, (WestNeighbour, NorthNeighbour and EastNeighbour) it will calculate the middle of the middle center of the front edges face and draw the triangles there, right? So can anyone here explain me how to implement marching cubes using the generated vertices of the blocks, please ?
Thank in advanced guys.
Respects.
Marching cubes it’s not the correct algorithm to chose because your scalar field is binary and the result mesh is also not smooth. You should look for smoothing algorithms for meshes.
The marching cubes algorithm produces a mesh from a scalar field (also known as voxels). If you already have a mesh generated then this may not be what your looking for. You can try and apply the algorithm to what data you originally created the ‘blocky meshes’ from.
I have a working Unity project of the marching cubes and the marching tetrahedron algorithm found here. You might find it easier to figure out whats going on by examining how the code works.
At first I was actually trying to avoid marching cubes, and I thought about smoothing the mesh by just creating points between cubes to smooth the mesh, but I am afraid that in a near future this will become a nightmare because the possibility of creating tunnels, caves and so on, I mean it may be possible but my approach needs to be rethought because it seems to me that complexity will grow with the mesh/geometry complexity.
And as far as I know it is actually possible to create marching cubes from those binary fields, because as you should know jcongote.unity I’m creating the mesh vertices just like you would create them for a set of common cubes, all belonging to a chunck (generating only one mesh, of course), and the marching cubes algorithm takes exactly this. It takes vertices from cubes to create the mesh, that’s why I thought about using it as I already had those vertices calculated. But I found it to be quite hard to understand it…
On the other hand… I love to learn about complicated stuff and after posting this doubt I found the “metaballs” project in the wiki which uses marching cubes and now I am actually starting to understand how marching cubes algorithm works… not everything still
Yeap scrawk, I was thinking to apply the algorithm to the data that I originally had before creating the mesh, even because it would be pretty wasteful to generate a mesh then clear it up to generate another one. That part I got it
Thank you very much for the link, be sure that I will read it very carefully and I hope I can understand how does this actually works once and for all.
Still, I’m divided… I don’t know which one of this two approaches would produce the best results because by one hand I want a good quality smooth mesh and by the other I need it to render as fast as possible with the minimum memory usage… I think I will take a closer look at both approaches and see what could fit me better. Still, I’m more inclined to marching cubes, it seems pretty good both in performance as in quality.
THANK YOU VERY MUCH both, guys, you were just awesome with your advices, it was even better to have two different approaches to the same problem.
Thanks for the help.
about you algorithm there’s one thing I didn’t understand.
Inside your CreateMesh method:
for(int x = 0; x < voxels.GetLength(0)-1; x++)
{
for(int y = 0; y < voxels.GetLength(1)-1; y++)
{
for(int z = 0; z < voxels.GetLength(2)-1; z++)
{
x<voxels.GetLength(0)-1 means that you never get to the last element in voxels array. Is this what you intended to do?
If you want to go till n-1 index in any array you use x<n, now I don’t know if there is any need to go till the last index of this array.
Could you help with this, please?
Yes it is meant to be like that. You will see the function FillCube that takes a subset of the voxel data to process. This function gathers all the neighbours of a voxel and so will access out of bounds data if the -1 is not there.
Very cool stuff you guys! I am trying to do something similar, but with discrete meshes to be used for each, instead of a voxel calculation. Is anyone still messing with this stuff? How did this go in the end?