Procedural Mesh UVs Edge Case?

I’m working on setting UV’s for a procedurally generated mesh, and I’m running into an issue with specific triangles of the mesh (as seen in the picture below). The way I’m currently setting the UV’s is based off the distances between vertex coordinates – so I compare the x distance between vertices to the y distance and z distance, and then the y distance to the z distance and determine which coordinate has the smallest distance. Then I use that to determine which plane I should base the UV’s off of.

However, I get a very small number of triangles that aren’t properly UV’ed. Would someone know how to fix this issue?

It will be easier if you show some part of your script. Or you can find some Triplanar Shader in the internet so that you don’t need to care about UV