Is it possible to get the 2D mesh generated by PolygonCollider2D?

When you pass points to a PolygonCollider2D, it triangulates a nice mesh out of those points. Is there any way to access that mesh, or is the function that does that mesh generation exposed anywhere? I feel like I must be missing something because it seems crazy that there’s no way to access it?

Technically speaking its not triangulation, because the end result is not triangles, but convex polygons (3 or more indices). This is the format that Box2D prefers. Knowing this, would you still have use-case for the mesh?

There is a triangulator here: http://wiki.unity3d.com/index.php?title=Triangulator

It’s not super fast, but I used something pretty close to it for RageSpline

Ah yeah that’s a good point, I hadn’t looked closely enough at the output to notice that it was creating polygons, not triangles. I actually do have my own triangulator that works great, but it seemed like a waste to essentially triangulate the same path twice (once by PolygonCollider2D and once by my own triangulator).

That said, I think having some kind of utility method exposing Unity’s polygon decomposer would be really handy. I do already have some code that breaks concave polygons into complex polygons (that I used to use when creating games using Unity’s 3D physics), but I assume the Unity one would be faster and more reliable.

Also, doesn’t Unity still use a triangulator in the code for the 2D rendering stuff somewhere? Some of the stuff seems like it might need one.

Yes we do, but unfortunately not currently exposed to userspace. We’ll think about it.

You can access the rendered sprite mesh from userspace, but currently only in the editor, not runtime:

http://docs.unity3d.com/ScriptReference/Sprites.DataUtility.GetSpriteIndices.html
http://docs.unity3d.com/ScriptReference/Sprites.DataUtility.GetSpriteMesh.html
http://docs.unity3d.com/ScriptReference/Sprites.DataUtility.GetSpriteUVs.html

NOTE: Documentation claims its runtime (UnityEngine.Sprites.DataUtility), but actually these are editor (UnityEditor.Sprites.DataUtility). It’s just a bug in the documentation. We are thinking about exposing these in the runtime, too.

i just found that there are limitations in procedural polygoncollider2d creation and then i found this thread so i have question

can i somehow cheat this
“The collider did not create a collision shape as it failed verification. This could be because it was deemed too small or its vertices were too close. Vertices can also become close under certain rotations”

??

Was reading this with interest as I’d like to visualise a polygon 2d collider area and this data would be really useful to use rather than, as Matt mentioned, use another one. Share the joy… …OR a Polygon2DRenderer component?!! And if you do, can you make a line renderer work if attached too! Am having to write both now and I’m sure others would find it useful!

You can try var mesh = polygonCollider.CreateMesh(false, false). Or true, true if you want to apply transform pos and rot

polygonCollider.CreateMesh creates a mesh that sadly has really terrible triangulation, I’m looking longingly at the gizmo mesh with the nice even polygons right now… gonna have to figure out how to re-triangulate a concave hull on my own because of this.

It’s the same code that’s used to decompose the PolygonCollider2D itself however for CreateMesh, it is limited to producing trangles for the mesh, it cannot create convex polygons which can be used by physics.

It’s also not terrible triangulation. If you have a lot of dense outline points and are asking to produce triangles from it, you’ll get a dense set of triangles. You should consider making a simpler outline in the collider and checking how many polygon shapes are being produced too.

That said, to combat this, in 2023.1, there’s an additional argument added that allows you to create a Delaunay mesh which produces much better results: https://docs.unity3d.com/2023.1/Documentation/ScriptReference/Collider2D.CreateMesh.html

You can also find this option on the PolygonCollider2D component itself even though it’s producing polygons.

NOTE: Please try to ask you own questions in your own thread and don’t necro 9 year old threads. These features didn’t even exist back then.

Thanks.

1 Like