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!
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.
Compute the barycentric coordinates of the point in the triangle. Find the smallest component to get the nearest edge.
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