Generating 2D Mesh Given An Outline?

Hi, I am working on a tool where I am generating a Bézier curve to define the borders of the ground in our game, and then I want to programmatically create the ground mesh.

I have the portion of the tool generating a Bézier curve given a list of control points working, and am onto the step of making the ground mesh.

As you can see here, I have generated the curve and a mesh along it, which will be the ‘border’ on our ground. The green box is our ‘level bounds’, which defines the furthest position our player and camera can move.

Now, given that curve and the level bounds, I want to generate a mesh that will fill that area entirely, as shown here…

Could anyone point me in the right direction to figuring this out? I was thinking maybe something along the lines of how Unity’s built-in PolygonCollider2D builds a mesh given a sprite (which I assume would be a very similar process), but wasn’t sure if that’s something I can leverage or not.

Thanks!

Edit: Using the Triangulator script solved this for me perfectly! I just had to pass in the outer borders, and everything worked as expected.

Sounds like a good job for a BSP tree. The basic idea is you want to take an area and subdivide it in half at a point along the line (these are the actual point in the geometry, not the bezier points). You keep subdividing until every line segment is in it’s own quad. The main catch is that you can’t divide at a point/along an axis if the line crosses back over the axis you are trying to divide, so you would have to search through the line to see if it doubles back. If you want loops and circles, you would need to break them up and do portions separately.

It’s possible you could do this backwards too, where you just make the line segment quads and fill the empty space with quads. If you throw in random points, it’s also possible to use Delaunay triangulation. There are bound to be dozens of ways to do it, but what methods produce decent meshes is a bit of a gamble. If you just want a flat mesh, then anything is viable, but if you intend to add height, then things will get messy.

Have a look at the Ear Clipping algorithm. It generates a triangulation of a set of points that defines the boundary of a non-convex polygon without holes.

http://wiki.unity3d.com/index.php?title=Triangulator

–Eric

2 Likes

Thanks, Eric! Actually found that script thanks to a friend of mine not too long ago, implemented it, and it works perfectly!