- Home /

# Is there any way to find the mesh triangles which are colliding with another mesh?

I'm working on a task which requires me to highlight the perimeter of collision and the perimeter will change according the collision depth. For that I need to find the mesh triangles which are colliding with another mesh. Keeping in mind that both colliders are mesh colliders with **convex** property on. Can someone suggest me how to approach this problem?

I'm attaching some reference screenshots of a particular shader from this site. Initially this seemed to work fine but I had two issues. First is that I'm using orthographic camera and this shader works for perspective camera. Secondly even in perspective camera mode if I apply it to all models they tend fail at rendering the intersection contour. I don't know why because I don't have any experience with shader writing.

So any suggestion?

**Answer** by Bunny83
·
Oct 04, 2018 at 10:25 AM

No, Unity doesn't have anything build in to determine this through the physics system. A convex mesh collider usually does no longer represent the actual mesh. It represents the (or a) convex hull of the original mesh. However you could work it out yourself. Though this is not trivial, especially for non convex meshes like yours.

Every triangle can be seen as plane (mathematically infinite plane). You can calculate the intersection points of another triangle that is cut by that plane. However after you have the intersection points you have to do an in-bounds check since we only want to cut a triangle by a triangle and not the whole plane. Since we would need to do that with every triangle from mesh one and testing against every triangle from mesh two this gets very expensive if the meshes are quite complex. It's most likely worth to first generate an octree where you sort in all the triangles. That way it's very easy to determine which triangles potentially overlap.

What is mainly required is the Line-Plane intersection. If you are not familiar with geometry you could also use Unity's Plane struct for the intersection part.

To determine if an intersection ponit is actually inside the triangle that formed the plane you can use barycentric coordinates. For this you can use this helper struct.

Apart from determining the intersection points for complex meshes it's also quite difficult to keep track of the order of those points. In the end the main question is what you actually want to do. Note that the shader you're referring to does not reallly have to do anything with the intersection but it just shades the area of the two objects which are close together on the z axis. So if two triangles are right on top of each other the shader will shade it red if it's drawn slightly behind the top one. The further away in the z direction they are, the less red it will be drawn. Typically an intersection involves an area where one object is on top of another and quite close. However it will also shade anything red that is overlapping and close enought together.

So what do you actually need? Do you just want a visual representation of the intersection seam? So you want to split / carve / intersect the meshes at that seam?

If you're looking for a CSG (constructive solid geometry) solution you may have a look at some of the AssetStore solutions like this or this or maybe this free solution. I haven't used any of those but all claim to be able to perform boolean operations on the meshes. Though as i said we don't know what you're after.

To answer your question, I just need visual representation of the intersection seam. I don't actually want to split, carve or deform the mesh at intersection. To give you a little more insight. The above meshes are models of human teeth. If I want to move a tooth, I want to give visual representation to the user that how much tooth A is intersecting tooth B through the seam.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.