Calculate UV Scale reliably based on mesh scaling

Hello,
I’ve been looking into ways of adjusting the UVs of a mesh based on it’s scaling, to avoid the texture looking all stretchy.

Basically something like this:

I’ve found a good solution to this problem by @PavelBoytchev : https://codepen.io/boytchev/pen/rNZxLLK?editors=0010

But sadly, this does not work reliably for every case:
https://codepen.io/vendduke/pen/jOjNgEb

Notice how for (non-box-shaped) meshes with a smaller y scaling, the UVs of angled faces do not match with the UVs of the rest of the mesh.
image

Is there a solution for correctly calculating the UVs of a mesh, no matter the scaling?
Any help is appreciated, thanks!

Also tagging @PavelBoytchev since he shared the code in the first place.

For a robust solution you need much more complex algorithm (like triplanar mapping, 3D textures, etc).

For this specific case, you may need to recalculate the normal vectors – they were not recalculated in the original demo, because it was not needed that much.

obj2.geometry.computeVertexNormals();

2 Likes
2 Likes

Thank you! Recalculating normals was enough for my use case, as I’m not working with super complex meshes, where I do would have to resort to triplanar mapping.

1 Like