Im working on something where Im extracting UV points from a mesh and trying to draw them onto a Texture2D and reverse the UV mapping process. Like, I want to get the UVs from a mesh and recreate the UV Wireframe map.
As we all know UVs are store in percentage, but for the life of me (maybe just a brain fart kind of day) what formula can I use to convert that to an absolute pixel point. Assuming that Im drawing on a 512x512 Texture2D…
Thanks!
The simplest case:
int texelX = Mathf.FloorToInt(uv.x * textureWidth);
int texelY = Mathf.FloorToInt(uv.y * textureHeight);
With a few caveats:
- This does not take into account wrapping/clamping for coordinates outside the [0, 1] range. You can get this with modulo (
%
) or Mathf.Clamp
, respectively.
- The y direction may be flipped relative to what you expect.
TextureHeight * (1 - uv.y)
may be what you want instead, depending on your setup.
- I don’t think DirectX 9’s half-pixel offset applies in this case, but if you find your answers are one pixel off you may need to add
+0.5f
inside each of the floors above.