Search Unity

Unity simplifying my convex mesh collider.

Discussion in 'Physics' started by Fu11English, Oct 31, 2016.

  1. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    So I make a mesh collider in Maya of 200 triangles. I know for a fact it's convex.

    Then i import to Unity, bring it into the scene and click 'convex'.

    Unity proceeds to simplify my mesh to a shape that is unwanted. It basically cuts the triangle count to about one third, even though I am already well within the 255 limit. Is there a way to stop it doing this?

    I have tried all the options in the import menu. Mesh compression, optimize mesh etc.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I've never seen any case where Unity altered a convex mesh collider, if it actually is convex. I assume you're mistaken and it's concave (being 100% convex is a lot more limiting than you might think), but without the mesh there's no way to know.

    --Eric
     
  3. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    It's possible I suppose. It's basically two cones butted together made into one object, delete the bottom faces then merge the vertices. Looks convex to me.

    The shape remains the same in Unity, it just becomes a lot more faceted. Or should I say, less faceted.

    Would you say a Maya primitive cone is convex?
     
    Last edited: Oct 31, 2016
  4. guy_lateur

    guy_lateur

    Joined:
    Oct 4, 2015
    Posts:
    9
    I'd say a cone is convex, unless it extends along both sides of the 'top point', ie looks like this: |><| Some modeling applications support these sort of 'extended' primitives.

    If I understand 'butting together' 2 cones correctly, it would look like <>, which would still be convex, assuming both sides are perfectly aligned/mirrored. It probably also helps if your geometry is closed, so you can unambiguously determine whether a given point is inside or outside of the mesh.

    Just to have it out there: here's 2 ways to (theoretically) check whether your (closed) geometry is convex:
    1. For any pair of points both inside the mesh, all points on the line segment between those points are also inside the mesh.
    2. For any point outside the mesh, there's only 1 point inside/on the mesh closest to this given point (the 'projection').

    If you keep having troubles with this, you may want to consider modeling the collision mesh yourself and use that instead.
     
  5. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    Yes it's like <> with the butted up faces deleted. Then I combine the objects into one and merge the vertices which are all on top of each other. Finally delete history yada yada and import to unity.

    It seems Unity then just guestimates the shape and makes it's own convex collider from it. Again, significantly lowering the detail. Mine has 100 facets around the circle and Unity drops it to 32 I think.

    I'm not sure how else I could make one that's 'more' convex than what I have. And if I had to guess it's because the angle difference between each facet is very low and Unity doesn't like being that precise :)
     
    Last edited: Oct 31, 2016
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The requirement is for <256 vertices, not triangles. Also, Unity splits vertices based on normals etc. so check the actual number in Unity, not Maya. It's possible the collider display you see is simplified rather than the actual mesh, but since there's no way to get the convex mesh, there's no way to know. In any case there are no options for convex colliders aside from the checkbox to turn it on or not.

    --Eric
     
  7. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    From the documentation - 'Convex mesh colliders are limited to 255 triangles'

    When I try and use a mesh collider as convex with over 255 tris, Unity throws an error. When I try with less than 255, it creates the collider but lowers the detail.

    So who's right, you or the Unity docs?

    Also, I already thought maybe the scene is just displaying a lower poly version but the actual version is intact. Nope. With my shape being circular I tried rolling it down a box collider. I could see all the small bumps made by the low poly detail.
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's not about me...the docs may say that, but Unity itself complains about >255 vertices. ;) From a tech standpoint, having a limit of 255 vertices makes more sense than 255 triangles, so I'd believe Unity over the docs. (In the same way that regular meshes have a limit of ~65K vertices, but no particular limit on triangles.)

    Screen Shot 2016-10-31 at 8.24.38 PM.png

    Anyway, since the collider is made from polygons, there will always be small bumps when rolling, unless perhaps you had a very large number of verts (which is of course not possible in this case).

    --Eric
     
  9. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    Well I don't get that error when I click 'convex' or 'play'. Hows that?

    And my convex hull should have 202 vertices anyway. It does indeed have more as you say, probably due to it splitting them for normals etc. But a collider does not need those, no?

    And why are the docs wrong for something so simple that's been available for years?
     
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It wouldn't be the first time, and in this case it wouldn't actually make a big difference for most shapes. But just to resolve it 100% for sure, I made a sphere with 242 verts and 480 tris. This worked fine as a convex mesh. I then changed it to 258 verts and 512 tris. This failed. So yeah, Unity isn't lying: it's verts. But you are correct that colliders don't need verts split because of normals etc., so I expect they get merged for collision shapes.

    --Eric
     
  11. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Well, here the limit seems to be on triangles, not vertices. When Unity receives a large mesh, the importer splits it among several parts (appending MeshPart0, MeshPart1, etc to the name) where each part contains exactly 65534 triangles:

    upload_2016-11-1_19-55-17.png
     
  12. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    With regards to the convex colliders, Unity always creates its own convex collider out of the provided mesh. It doesn't use the provided mesh directly.

    You may pass it a regular complex mesh, mark it as convex, and then the importer takes the vertex cloud and builds a convex hull out of it. I've passed meshes with thousands of vertices and getting a clean convex mesh collider.

    Only in the case of the resulting convex hull being larger than 255 polygons then it results in error:

    upload_2016-11-1_20-4-6.png

    Having a convex hull larger than 255 polygons depends on the complexity of the resulting convex shape only, not on the number of vertices of the original mesh. As said, you may use a high-density mesh as convex collider, and Unity may success on getting a convex hull out of it smaller than 255 polygons.
     
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, the limit on a mesh is 65,534 vertices, because the vertex indexing is done with ushorts (16 bit...and why 65,534 instead of 65,535, I don't know, plus having Mesh.triangles be int[] instead of ushort[] is not a great API decision and does make things less efficient than they could be). If the mesh importer is splitting on triangles instead, it's doing it wrong. Which, again, would not be the first time; there was a bug in older versions of Unity where the importer prevented meshes with >65K triangles from importing even if the number of verts was <65K. It's trivial to verify that the 65K limit on meshes is verts rather than tris by using the Mesh class, and noting that >65K vertices results in an error, while there is no limit on the triangles array. Anything else is a mistake on the part of the importer programmer.

    --Eric
     
  14. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Oh, ok. I guess that they decided to split on triangles because this way the partial mesh is ensured to have < 64k vertices. Splitting on vertices may have been more complicated than triangles.