Search Unity

Access Polygon2D's sub-shapes

Discussion in '2D' started by Estecka, Oct 12, 2017.

  1. Estecka

    Estecka

    Joined:
    Oct 11, 2013
    Posts:
    62
    I'm makinh a script that programatically generate a Mesh out of a PolygonCollider2D for rendering.
    So far it works well with convex colliders, but when it comes to concave ones, I havent figured a way to check the triangles are contained into the collider.

    When a polygon collider is concave, it already gets separated into smaller convex shapes, so I thought about using this code once per shape to generate my triangles; I'd just need to figure out which points are these sub-shapes made of.
    I found of `PolygonCollider2D.shapeCount` (visible in the inspector) that tells how many shapes to make, but absolutely nothing else.
    Surely there gotta be a way to get more data on these, right?
     
  2. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    You could use http://wiki.unity3d.com/index.php?title=Triangulator instead of creating your own algorythm (or maybe look at the way they do it to find your solution as it works with concave poly). It works directly using a set of points, you could just grab the points from you collider.

    You can also use "Triangle.NET", this one works also with polygons having holes in it. It also has some fancy features to it to lower the number of triangles for low quality settings. Googling something like "Unity use triangle.net", you will find a youtube video showing how to import it on your unity project.
    If you use the experimental .NET 4.6 (build settings => player settings => Scripting runtime version), you can just grab the library and put it in your unity project without having to modify it at all.
     
    Estecka likes this.
  3. Estecka

    Estecka

    Joined:
    Oct 11, 2013
    Posts:
    62
    Thanks for the tools !
    I'm going with the triangulator. Seems simpler to use, and I don't need to make holes in my meshes for now. (Unless there's a way to make holes in a PolygonCollider2D...)
     
  4. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    Well actually i'm not sure about 2D poly. But, using editor scripting you can create the mesh directly just the way you'd modify a polygoncollider. As Triangle.net use a polygon to shape the mesh, and others polygons to create the holes, you can create the polygonCollider using the outter Polygon.
    Here is an example of editor scripting showing how to create curves and splines :
    http://catlikecoding.com/unity/tutorials/curves-and-splines/ .
    (actually with some tweaking, you can use that to create curved polygons and meshes, it can get a little complexe though).
    I'm not sure exactly what are your needs, but using all that you can basically create anything.

    Unity is also going to realease a feature called SmartSprite soon ( not sure about exactly when). It basically does everything (meshes, with curves, with an edge collider all around it. Not sure about holes) .