I've been using a projected decal system (very similar to the one described in Game Programming Gems 2), where the decal is projected along a vector onto triangles that intersect the projected 3D box. I'm able to find and clip the triangles without much issue. But I'm having some problems generating the UV coordinates in a way that doesn't stretch significantly across surfaces that run parallel to the projection vector.
This makes sense - I'm doing a 2D planar map onto 3D geometry, so the 3D parts are not going to come out looking all that great. But I'm trying to figure out a way to improve the situation.
To compute the UV coordinates, I find the local/relative position of each vertex between the clipping planes. For example, if a vertex is very close to the right clipping plane, it will get horizontal UV coordinate very close to 1.0. The problem is that the front and back clipping planes have no influence on the UV coordinates, allowing the decal to span whatever distance along that axis without the UV coordinates changing in any way. Here are some images. The top one is just a simple projection onto a flat surface (mostly). The bottom two show the stretching problem:
I've been trying to come up with something that at least improves this, like trying to use the depth (Z +/-) of the vertices to move the UV (x/y) coordinates farther outward in whatever direction they were already closest to. It helped a little with the stretching, but caused some pretty bad distortion. The idea still seams sound in my mind (the UV coordinates become closer to the UV edge as the vertices extend into the Z axis) - but if it is so, I haven't been able to apply it correctly.
One other possibilty is to (some how) compute the average normal of all triangle surfaces and project the decal using the average. Then the stretching is somewhat averaged throughout the whole decal. But I have no idea how to compute an average normal that uses each triangle's surface size to increase how much its normal influences the average (smaller triangles should have less influence on the average than larger ones).
Does anyone have any ideas on how to solve this issue? Some way to UV-map vertices that allows the Z axis to influence the coordinates? Or just any idea to help prevent the stretching?
Thanks for any advice