Home > Back-end >  Create mesh of even thickness around path with unordered points in Unity
Create mesh of even thickness around path with unordered points in Unity

Time:08-11

I have an array of points in no particular order, representing the paths visible along one cross-section of a 3D mesh. I would like to display this cross-section to the user, but obviously with just the points, it is infinitely thin and therefore invisible. I have attempted to extrude this collection of paths, but the only method that has worked so far resulted in a mesh of uneven thickness, at some points even disappearing entirely. Is there a way that I can extrude this path with even thickness despite the points being in a seemingly random order?

CodePudding user response:

Meshing depends on linking vertices together in a particular order. What you're doing is making triangles. If I have a set of vertices like

1   2   3   4   5

6   7   8   9  10

Then linking <1,2,3> doesn't really show anything because they're all in a line. Similarly, <5,6,7> makes a triangle, but it's not making it with adjacent vertices, so the mesh would come out looking off.

You could do <1,6,2> and <2,6,7> to make a square face and keep working down that way, or you could skip interior points and go <1,6,5> and <5,6,10>, but you lose the interior resolution and just wind up with a plane.

All this is to say you need to know something about the order of the points. Imagine what a connect-the-dots picture would look like if you picked successor points at random.

Now you can try to make sense of the points by trying to order them, but you'll get into trouble if they don't form a closed polygon because you'll chain all the points together.

What you could do is to start with the first point, then iterate through all the points and compare distances between them and the first point. Keep the one with the smallest distance as your nearest neighbor, then iterate through the remaining points, etc., keeping nearest neighbors until you've gone through all the points.

At the end, you can link the last point to the first and have a closed loop, but whether or not that looks right will depend on the points and how you calculated their order.

This just leaves you with the same problem you describe in your original post, but you've got the meshing order now.

Now you could average all the points to get the centroid, then you can shoot "spoke" rays from the centroid to all the points, and then you can pad that width out to whatever you want.

Vector geometry here, you can subtract two points to their vector difference in position. The distance between the two is the magnitude of the vector difference and the direction between the two is the norm.

If the arbitrary point is called "spoke" and the centroid is the "hub" then

var direction = (spoke - hub).normalized;
var tire = spoke   tireWidth*direction;

Then you have your trail edge that is radially outward from the hub. Now you can mesh that surface by getting a vertex in the mesh (spoke), comparing that to the centroid (hub), and then calculating the outer trail edge (tire), and going through all the points and building the mesh by indexing <spoke[n], tire[n], spoke[n 1]>, etc., and again looping it like you do (end->0) above.

  • Related