Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Power07RM · Sep 04, 2015 at 05:20 PM · unity 5meshcutting

How to get common part of two meshes?

Hi, I am working on cutting meshes. What am I exactly trying to achieve is that:

Input - two meshes(procedurally generated of course)

Output - three meshes(or more, depends on the given meshes)

A - which is a meshA substract mesh B,

B - which is a common part of meshes A and B,

C - which is a mesh B substract mesh A. alt text

I am using the Triangulator that for the given set of vertices creates triangles for me. All I have to do is to give him those vertices, and here goes the question. Is there any algorithm that would help me to do this? Maybe your brilliant idea that came into your mind at the time you saw this picture?

1stack.png (7.2 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by rageingnonsense · Sep 04, 2015 at 07:38 PM

This is a fairly difficult things to do. I just spent about a month getting my mesh bisector to work. It is far too verbose to go into here, but, here are a few things to remember that may help you:

  1. when you bisect a mesh, you will get two new meshes. Each mesh has triangles from the original that are kept whole, and some that are bisected. In reality, you are bisecting individual triangles.

  2. Each triangle will be bisected into at least 2 new triangles, and at most 3. usually it will be 3, but if the bisection happens on or close to a vertex of a triangle; it effectly splits it into two.

  3. adjacent triangles share edges, but the edges should always be in opposite order to the triangle next to it.

  4. when a triangle is bisected, one mesh will get one triangle, and the other will get one or two (depending on if the triangle was split into 2 or 3).

  5. you WILL have floating point nightmares. keep this in mind for the cases where your bisector cuts along the edge of a triangle almost exactly.

Knowing that, the best way to wind the triangles is to take a known good triangle from the original, and use that as the basis for winding the rest of them.

Here is as method I use to rewind triangles after a bisection:

 public static int[] RewindTriangles(int[] triangles) {        
         int[] processed = new int[triangles.Length];        
 
         // the first triangle is considered to be correct for the
         // purposes of this routine        
         processed[0] = triangles[0];
         processed[1] = triangles[1];
         processed[2] = triangles[2];
                
         // initialize the reference edges using the first triangle
         uint re0 = BuildEdge(triangles[0],triangles[1]);
         uint re1 = BuildEdge(triangles[1],triangles[2]);
         uint re2 = BuildEdge(triangles[2],triangles[0]);
 
         for (int t = 3; t < triangles.Length; t += 3) {
             int i0 = triangles[t + 0];
             int i1 = triangles[t + 1];
             int i2 = triangles[t + 2];
 
             uint e0 = BuildEdge(i0, i1);
             uint e1 = BuildEdge(i1, i2);
             uint e2 = BuildEdge(i2, i0);
 
             if (e0 == re0 || e0 == re1 || e0 == re2 ||
                 e1 == re0 || e1 == re1 || e1 == re2 ||
                 e2 == re0 || e2 == re1 || e2 == re2) {
                 // this was wound incorrectly (because neighboring edges are the same; no bueno), correct it
                 i0 = triangles[t + 2];
                 i1 = triangles[t + 1];
                 i2 = triangles[t + 0];              
             }
 
             processed[t + 0] = i0;
             processed[t + 1] = i1;
             processed[t + 2] = i2;
 
             // rebuild reference edges for the next iteration. Remember, we always check
             // the edges from the previous iteration against the edges of the next one
             re0 = BuildEdge(i0, i1);
             re1 = BuildEdge(i1, i2);
             re2 = BuildEdge(i2, i0);
         }       
         
         return processed;
     }

 public static uint BuildEdge(int i0, int i1) {
     return ((uint)i0 << 16) | (uint)i1;
 }

Something to note about this is that I keep track of edges using the fact that triangle indices are 16 bit, but ints can be 32 bit. Using some bitshifting, you can tell if adjacent edges are wound correctly. If the edges are the same int value, then one of them is wound incorrectly.

It is worth noting that I have had trouble getting my mesh bisection to work on spheres. I don't think this has anything to do with the winding of the triangles, but it could be, so take this all as a starting point.

Now, if you want to try a different approach, look up Deluaney Triangulation. I am almost definitely spelling that incorrectly. There are several algorithms you can use for this, like Sweep Line. Since you are bisecting complete meshes already though you already have a basis for triangulation, so the method I posted may be faster.

EDIT: I re-read your question. I realize I made the assumption you are bisecting using a plane, but you are bisecting with another mesh.

I think this means that your triangles could potentially be split into more than 3 triangles. The re-winding of triangles should still be the same though.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to optimize mesh cutting and put load on GPU? 0 Answers

Does the animations done using slice modifier used in 3Ds max show the same animations in Unity ? 0 Answers

Cutting simple plane with a line (~2D) 2 Answers

Unity 5 - Hex Tile change color on mouseover 0 Answers

How to combine several Materials into one? 0 Answers


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