Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by mathmos_ · Jan 14, 2013 at 10:15 AM · collisionphysicsmeshmesh collider

Generating a convex hull

Hello wonderful unity community,

I'm looking something that would generate a convex mesh with less than 255 triangles for an arbitrary high ploy concave mesh like this in Unity. The convex mesh would be used for a mesh collider.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by mathmos_ · Feb 05, 2013 at 03:03 PM

Hi there. So I finally found a solution. Basically I just take 64 random polygons from the original mesh, feed those to the mesh collider and enable 'convex'. It doesn't have to be 64, but that's what works for me.

 using UnityEngine;
 using System.Collections;
 
 public class SimpleConvex {
     Mesh mesh;
     
     // Use this for initialization
     public SimpleConvex(Mesh mesh) {
         this.mesh = mesh;
     }
     
     public Mesh BuildSimplifiedConvexMesh()
     {
         Debug.Log(mesh.triangles.Length/3 + " tris");
         
         SplitMeshBuilder builder = new SplitMeshBuilder();
         
         for (int i = 0; i < 64; i++)
         {
             int index = Random.Range(0, mesh.triangles.Length/3) * 3;
             
             Vector3[] triangle = new Vector3[]{mesh.vertices[mesh.triangles[index]], mesh.vertices[mesh.triangles[index + 1]], mesh.vertices[mesh.triangles[index + 2]]};
             Vector2[] uvs = new Vector2[]{mesh.uv[mesh.triangles[index]], mesh.uv[mesh.triangles[index + 1]], mesh.uv[mesh.triangles[index + 2]]};
             
             builder.AddTriangleToMesh(triangle, uvs);
         }
         
         Mesh polygonSoup = builder.Build();
         Debug.Log(polygonSoup.triangles.Length/3 + " tris");
         
         return polygonSoup;
     }
 }

The MeshBuilder something I made to ease mesh construction. You can make your own or I can post mine if you guys want it.

Hope it helps.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DemianT · Jul 27, 2013 at 04:07 PM 1
Share

Genius! :)

avatar image Comet612 · Sep 05, 2013 at 10:08 AM 1
Share

Hello,

I'm new to unity and trying to do exactly the same thing. Could you post the "Split$$anonymous$$eshBuilder" class code please? I'll be really thankful !

avatar image mathmos_ · Sep 06, 2013 at 07:28 AM 0
Share

Sure. $$anonymous$$eshBuilder.cs

Looks like I altered the mesh builder a little since I posted this code, so you also need to pass the normals now. Their not really important, unless you need to render the mesh, so just an array of three dummy vectors, like Vector3.Up.

Let me know how it works out.

avatar image Thierry_J · Apr 05, 2016 at 02:26 AM 0
Share

Hi, I think this is what I need to, could you explain what to to do with this script. I have a complex object with too many polygons and I want to add a convex mesh collider to use the On$$anonymous$$ouseDown function on it. Thanks!

avatar image mathmos_ · Apr 05, 2016 at 09:16 AM 0
Share

Honestly, I would not use this code now. The randomness makes it too unreliable, but if you want to use it, just generate a mesh by feeding the constructor your original mesh, then use BuildSimplifiedConvex$$anonymous$$esh() to get your new mesh and assign that to meshfilter of an gameobject with a meshfilter and a meshcollider. But you should investigate other solutions, this one is too hacky imo.

avatar image
3

Answer by Wolfram · Jan 14, 2013 at 10:27 AM

Not sure what you're trying to do, but Unity creates such a collider automatically if you tick "Convex" in the MeshCollider.

EDIT:

As Unity (version dependently?) apparently sometimes has problems finding such a convex hull with

This configurable algorithm generates fast and accurate approximations for the convex hull.

For example, using "`-t -n.01`", I was able to construct a 236 triangles convex hull for a 69451 triangles Stanford bunny in 3 seconds. (Note: uses Wavefront .obj, a trivial 3D file format. The only caveat: face indices start at 1, not 0).

Comment
Add comment · Show 16 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jan 14, 2013 at 10:47 AM 0
Share

Right, if you need the mesh only for the meshcollider just set convex to true after you assigned a mesh.

avatar image mathmos_ · Jan 14, 2013 at 11:04 AM 0
Share

This only works if the original mesh has 255 triangles or less. I'm looking for something that would generate a low poly convex hull for any concave or convex high poly mesh.

avatar image Wolfram · Jan 14, 2013 at 11:06 AM 0
Share

I believe that was the case some time in the past, but now it should work for any mesh size. Tested with 3.5.7.

avatar image mathmos_ · Jan 14, 2013 at 11:32 AM 0
Share

I just tested it with a 4968 tri Stanford bunny in Unity version 4.0.0f7 and I got the following error message: "The hull has more than 255 polygons. This is invalid. UnityEditor.DockArea:OnGUI()"

avatar image Wolfram · Jan 14, 2013 at 11:40 AM 0
Share

Hm, sounds like a regression bug to me, tried various models with 5000-55000 tris. Tried collisions with Rigidbodies as well.

When/what exactly prints that error, for what are you using the $$anonymous$$eshCollider?

Show more comments
avatar image
1

Answer by ivarboms · Jan 17, 2013 at 02:49 PM

Unity will automatically create a convex hull of the mesh when you tick 'Convex' in a MeshCollider. Unity is able to create convex hulls from meshes with more than 255 triangles, but with certain kinds of meshes (i.e. highly tessellated spheres), the convex hull it generates contains more than 255 polygons. This is the reason you get error messages.

Unfortunately, Unity gives you no control over convex hull generation, so the only option is using external tools to generate a simplified mesh (or even a convex hull if you have tools which support that) with simpler shapes, and using this as the MeshCollider's mesh.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Normal vector from collision? 1 Answer

Colliding to mesh seams when shouldn't 0 Answers

Rigidbody w/ sphere collider falls or pushes through mesh collider. 0 Answers

Mesh Collider Bounciness working only in a few areas 1 Answer

Mesh Collider Changes in Unity 5.6 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges