# Calculate UV Scale reliably based on mesh scaling

**URL:** <https://discourse.threejs.org/t/calculate-uv-scale-reliably-based-on-mesh-scaling/67784>\
**Category:** Questions\
**Created:** [July 6, 2024, 6:00pm UTC](https://discourse.threejs.org/t/calculate-uv-scale-reliably-based-on-mesh-scaling/67784 "2024-07-06T18:00:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DukeVen](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/dukeven/32/49574_2.png) [@DukeVen](https://discourse.threejs.org/u/DukeVen)\
**Post date:** [July 6, 2024, 6:00pm UTC](https://discourse.threejs.org/t/calculate-uv-scale-reliably-based-on-mesh-scaling/67784/1 "2024-07-06T18:00:10Z")

</div>

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:

[![](https://img.youtube.com/vi/mwET1bzJ6xg/maxresdefault.jpg "Correct Face Attributes in Blender 2 9") ](https://www.youtube.com/watch?v=mwET1bzJ6xg&t=30)

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

But sadly, this does not work reliably for every case:  
[https://codepen.io/vendduke/pen/jOjNgEb](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](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/f/b/fbcdadf9b76325278a9e5fbc193992b7faf7b3a6.png)

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.

---

<div class="post-metadata">

**Author:** ![PavelBoytchev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pavelboytchev/32/38639_2.png) [@PavelBoytchev](https://discourse.threejs.org/u/PavelBoytchev)\
**Post date:** [July 6, 2024, 6:39pm UTC](https://discourse.threejs.org/t/calculate-uv-scale-reliably-based-on-mesh-scaling/67784/2 "2024-07-06T18:39:46Z")

</div>

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.

```javascript
obj2.geometry.computeVertexNormals();

```

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/5/a/5ac0cac04aeabf6d448c9c6dc9ce45500e580292.jpeg)

---

<div class="post-metadata">

**Author:** ![manthrax](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/manthrax/32/2596_2.png) [@manthrax](https://discourse.threejs.org/u/manthrax)\
**Post date:** [July 7, 2024, 4:08am UTC](https://discourse.threejs.org/t/calculate-uv-scale-reliably-based-on-mesh-scaling/67784/3 "2024-07-07T04:08:05Z")

</div>

> **[Glitch Code Editor ･ﾟ✧](https://glitch.com/edit/#!/orthocamera-fit-triplanar-mapping)**
>
> Simple, powerful, free tools to create and use millions of apps.

---

<div class="post-metadata">

**Author:** ![DukeVen](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/dukeven/32/49574_2.png) [@DukeVen](https://discourse.threejs.org/u/DukeVen)\
**Post date:** [July 7, 2024, 10:57am UTC](https://discourse.threejs.org/t/calculate-uv-scale-reliably-based-on-mesh-scaling/67784/4 "2024-07-07T10:57:33Z")

</div>

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.
