- Home /
Find closest edge of mesh renderer from raycasthit
I've been working on making my decal system a little smarter, and resizing or cutting off decals that are too close to the edge of the renderer. But I've been having a lot of trouble trying to get the distance of the raycasthit to the edge of the renderer. I've looked at collider.bounds, but that doesn't give me where the raycast hit, or the distance. Here's an image to illustrate better.
Any help is very appreciated!
Answer by MakeCodeNow · Jul 12, 2014 at 05:48 PM
It's been a while since I've done this, but I can think of two options. In both cases, you need to know which triangle of the mesh you are on.
1) Compute the barycentric coordinates of the point in the triangle. Find the smallest component to get the nearest edge.
2) Project the point onto each edge. Check the distance between the original point and projected point. Smallest distance gives you the closest edge.
However, for a decal system specifically, most edges are fine. You need to find the closest edge and then determine if it's a "visible" edge, which usually means doing a dot product of the face normal for the two tris that share the edge and seeing if they are greater than some number.
Note that there are a lot of good articles and books that talk about good decal systems, clipping, and mesh acceleration structures. I definitely recommend doing some research if you want to make a really great decal system. Or just buy one of the ones from the asset store :)
Thanks, this is helping a lot, now I'm trying to figure out the Vector3.Dot. I'm not sure what to feed it for the two vector3's. One would be my hit.transform I would imagine, and that gets the rotation of the object I hit, how do I get the rotation of the face next to it?
Dot the face normal of each tri, which is what I said in my original answer :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613153905im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get closest point on collider on at a certain z depth 0 Answers
Checking if ground is 'level' 1 Answer
how to find the width of a parent gameobject without using bounds? 1 Answer
Can I use a raycast to retrieve information from the collider? 2 Answers
Simple example bound box or get the top position gameobject 1 Answer