• 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
Question by hassanyawar · Nov 23, 2018 at 02:22 PM · meshintersection

Triangle Intersection using Geometry3Sharp library.

I'm using Geometry3Sharp and want some help regarding triangle intersection. I'm using Unity to test the triangle intersection of two Unity sphere meshes. I converted the Unity mesh data to built DMesh3 using Build method of DMesh3Builder and provided vertices and triangles from Unity's Mesh data structure. I intend to generate spheres on the intersection points. Here is a chink of my code to give you and idea.

 DMesh3 meshA = DMesh3Builder.Build(verticesA, trianglesA, normalsA);
 DMesh3 meshB = DMesh3Builder.Build(verticesB, trianglesB, normalsB);
 
 DMeshAABBTree3 spatialA = new DMeshAABBTree3(meshA, true);
 DMeshAABBTree3 spatialB = new DMeshAABBTree3(meshB, true);
 
 DMeshAABBTree3.IntersectionsQueryResult iqr = spatialA.FindAllIntersections(spatialB);
 List<DMeshAABBTree3.PointIntersection> pointIntersection = iqr.Points;
 
 for (int i = 0; i < pointIntersection.Count; i++)
 {
     GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
     sphere.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
     sphere.transform.position = (Vector3) pointIntersection[i].point;
 }

In the screenshot there is no apparent intersection but spheres are still being generated as if there is intersection. Need some help.

alt text

48947381-d1bd4900-ef52-11e8-8b64-092cc0b80973.png (108.9 kB)
Comment

People who like this

0 Show 0
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
Best Answer

Answer by hassanyawar · Nov 28, 2018 at 09:39 AM

I found the solution. I contacted the author of this library and he was kind enough to guide me. This is the link my thread in case someone wants to get complete idea.

To answer my own question. First I needed to convert Unity's Mesh vertices and normals to world space and then assign to DMesh3. Then after using FindAllIntersections, instead of using Points intersections query result I used Segments intersection query result. I got all the intersecting triangle pairs. The Point intersections are where two triangles touch exactly at an edge or vertex. This hardly ever happens so segment intersections is the answer.

Here is my code to give an idea:

 using System.Collections.Generic;
 using UnityEngine;
 using g3;
 
 public class TriTriIntersection : MonoBehaviour
 {
     public MeshFilter mf1;
     public MeshFilter mf2;
 
     private Transform t1, t2;
     private Mesh mesh1, mesh2;
 
     private Vector3[] oldVerticesA, oldVerticesB;
     private Vector3[] oldNormalsA, oldNormalsB;
     private int[] trianglesA, trianglesB;
 
     private Vector3f[] newVerticesA, newVerticesB;
     private Vector3f[] newNormalsA, newNormalsB;
 
     void Start ()
     {
         // Caching meshes
         mesh1 = mf1.sharedMesh;
         mesh2 = mf2.sharedMesh;
 
         int totalVertex1 = mesh1.vertexCount;
         int totalVertex2 = mesh2.vertexCount;
 
         // Caching transforms
         t1 = mf1.transform;
         t2 = mf2.transform;
 
         // Getting vertices, triangles and normals for mesh 1
         oldVerticesA = mesh1.vertices;
         oldNormalsA = mesh1.normals;
         trianglesA = mesh1.triangles;
 
         // Getting vertices, triangles and normals for mesh 2
         oldVerticesB = mesh2.vertices;
         oldNormalsB = mesh2.normals;
         trianglesB = mesh2.triangles;
 
         // Creating Vector3f lists for converted data
         newVerticesA = new Vector3f[totalVertex1];
         newNormalsA = new Vector3f[totalVertex1];
         newVerticesB = new Vector3f[totalVertex2];
         newNormalsB = new Vector3f[totalVertex2];
     }
 
     void Update ()
     {
         if (Input.GetKeyDown(KeyCode.Space))
         {
             FindIntersectionTriangles();
         }
     }
 
     private void FindIntersectionTriangles ()
     {
         // Converting Vector3 to Vector3f and local positions/directions to world positions/directions for mesh 1
         for (int i = 0; i < oldVerticesA.Length; i++)
         {
             newVerticesA[i] = t1.TransformPoint(oldVerticesA[i]);
             newNormalsA[i] = t1.TransformDirection(oldNormalsA[i]);
         }
 
         // Converting Vector3 to Vector3f and local positions/directions to world positions/directions for mesh 2
         for (int i = 0; i < oldVerticesB.Length; i++)
         {
             newVerticesB[i] = t2.TransformPoint(oldVerticesB[i]);
             newNormalsB[i] = t2.TransformDirection(oldNormalsB[i]);
         }
 
         // Building g3 meshes        
         DMesh3 meshA = DMesh3Builder.Build(newVerticesA, trianglesA, newNormalsA);
         DMesh3 meshB = DMesh3Builder.Build(newVerticesB, trianglesB, newNormalsB);
         
 
         // Building AABB trees
         DMeshAABBTree3 treeA = new DMeshAABBTree3(meshA, true);
         DMeshAABBTree3 treeB = new DMeshAABBTree3(meshB, true);
 
         // Getting all intersection points
         DMeshAABBTree3.IntersectionsQueryResult iqr = treeA.FindAllIntersections(treeB);
         List<DMeshAABBTree3.SegmentIntersection> segmentIntersection = iqr.Segments;
 
         for (int i = 0; i < segmentIntersection.Count; i++)
         {
             GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
             sphere.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
             sphere.transform.position = (Vector3) segmentIntersection[i].point0;
         }
     }
 }


Comment
x4637x
Silvarilon
Mile88

People who like this

3 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

104 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Intersection of two game object 0 Answers

How to show only the intersection between two meshes at runtime? 0 Answers

Detecting if two meshes intersect, precisely 1 Answer

Rendering objects without mesh intersections 3 Answers

Get mesh intersection points with arbitrary plane/disc 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