Heyhey, I try to create a mesh out of a bunch of vertices stored in an 3D array. Let’s say the array of vertices forms a sphere for example. Now I need to connect all the vertices with triangles. But how?
I hope that’s not a too stupid question, but the longer I think about it, the more confused I am >.>
I see, the HeightmapGenerator builds the triangels in this piece of code:
// Build triangle indices: 3 indices into vertex array for each triangle
var triangles = new int[(height - 1) * (width - 1) * 6];
var index = 0;
for (y=0;y<height-1;y++)
{
for (x=0;x<width-1;x++)
{
// For each grid cell output two triangles
triangles[index++] = (y * width) + x;
triangles[index++] = ((y+1) * width) + x;
triangles[index++] = (y * width) + x + 1;
triangles[index++] = ((y+1) * width) + x;
triangles[index++] = ((y+1) * width) + x + 1;
triangles[index++] = (y * width) + x + 1;
}
}
This is 2 dimensional and runs linearly through the array. I’m afraid that this wont so easily in 3D…you don’t know which vertices is the next, it could be the one above you, under or in front of you, forming a different mesh every time. Do I see this wrong or could it get in fact a bit nasty, especially with complex objects?
Basically, you either know how the vertices are arranged, in which case figuring out the indices is just a matter of some bookkeeping and arithmetic, or you don’t, in which case you have a more complicated problem on your hands.
Can you describe the problem in more detail? Where do the vertices come from? How are they generated?
Indeed, there is no “correct” way to make a mesh from an arbitrary set of vertices. A pretty good solution that is used in many cases is the convex hull. PhysX’ solid mesh colliders use this solution. Here is a paper describing an algorithm that generates a convex hull for a set of points in three dimensions.
Of course, I don’t know how the vertices are arranged, they are generated by code and form geometric objects like a torus or something like that. It’s for a math research =)
Thanks for pointing me in the direction of these convex hulls! Has anyone tried to implement something similar in Unity or do I have to do some pioneering?
If they’re generated by code, you need to alter the code to generate the triangles at the same time. For a torus, certainly - as it is created, the triangles can be mathematically determined.
That’s not an ‘of course’. In fact, it’s the opposite; if you’re generating vertices for objects such as tori procedurally, then you should know how the vertices are arranged, and generating the triangle indices should be relatively straightforward (as previously noted).
But I didn’t determined who is actually generating the vertices. It could be an input from someone else, without any information about the generation process (and it could be something more fancy then a torus). But I think I will drop that feature, because this would mean that I really would need a convex hull algorithm, right?
Ok, let’s see how the straightforward solution with the vertices created by my own works out =)
For now, thanks for the help!
A convex hull algorithm isn’t going to correctly construct a triangulation for a non-convex shape such as a torus. If you have a set of points that represents (for example) a regular tessellation of a torus, but you don’t know how those points are organized, you could probably write code to ‘deduce’ the organization and triangulate accordingly. However, that would be pretty nonsensical, and it wouldn’t help as far as arbitrary shapes go.
There are other ways to generate a triangulation from an arbitrary point cloud, but they’re not exactly trivial. If that’s something you want to pursue though, I think more information about the problem you’re trying to solve (such as where the data is coming from and why you have vertices but no connectivity information) would be helpful.
The face data is perhaps the most important part of it. You would have to ask yourself, how is it being created, is it imported, created on the fly. You can fly an airplane around and generate trajectory information, but if you only create the vertices, you will never know what it was.
Still, in the case of a Taurus, you can still get the information if it is exact, 4 closest points make a quad type of stuff… but if anyone can throw anything at it, it becomes complicated real quick.
At work, we tested the feasibility of rendering a point-cloud in Unity. Because of the issues described above regarding meshing up an arbitrary, unsorted point cloud, we figured the easiest/simplest way was just to render it as points. We tried a few methods (with particles, by creating small triangles for each point, and a couple others) each with their own pros/cons, but for the most part the cloud would coalesce into a solid when far enough away from it. So if your point cloud is dense enough, you could try getting that way.
Is it unfeasible to simple disallow vertex input, and only allow vertex+triangle input?
If the vertices must have triangles according to THE RULES as put forth by you, then whatever generates the vertices should be forced to also create triangles to accompany them.
I can’t think of a situation myself in which I might have an incoming set of vertices that satisfy both conditions, that
I need to form them into a mesh, and
Whatever created the vertices can’t have formed them into a mesh for me.
Perhaps if you specified how the vertices are generated or what they’re being used for, someone might have a solution or an alternative?