Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JohnRossitter · May 20, 2014 at 09:26 PM · meshtexture2duv

How do I convert a Vector2 from a UV coordinate to an absolute pixel position?

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!

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JohnRossitter · May 21, 2014 at 12:31 AM 0
Share

Well, I was able to extract a UV wireframe sort of... But it was not what I was expecting to say the least. I took the UV coordinates from my mesh, and drew lines from one to the next, but I guess what I did't account for is that I really have no idea when one starts and another ends. Any advice on recreating a UV wireframe from the $$anonymous$$esh UVs?

alt text

blastdoor1_uv.png (96.8 kB)
avatar image Bunny83 · May 21, 2014 at 02:45 AM 0
Share

Well, you don't simply draw lines from one to the next ;) You have to iterate through your triangles array. 3 indices define one triangle. You get 3 UV coordinates with those 3 indices. Just draw each line of the triangle.

 //C# - pseudocode
 int[] triangles = mesh.triangles;
 Vector2[] uvs = mesh.uv;
 
 for(int i = 0; i < triangles.Length; i+=3)
 {
     var p1 = uvs[triangles[i + 0]];
     var p2 = uvs[triangles[i + 1]];
     var p3 = uvs[triangles[i + 2]];
     DrawLine(p1,p2);
     DrawLine(p2,p3);
     DrawLine(p3,p1);
 }
avatar image Bunny83 · May 21, 2014 at 02:58 AM 0
Share

btw: For UV viewers it's more common to repeat the unterlying texture and just draw the UVs as they are (of course multiplied by the texture size but not wrapped "manually"). I wrote an EditorWindow some time ago that did this, but can't find it at the moment ;)

avatar image JohnRossitter · May 21, 2014 at 01:51 PM 0
Share

Ahhhhhh, that makes much more sense now that you think about it. I suppose this will work as long as your mesh does not share UV space, but that's a problem for another day :)

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by DMGregory · May 20, 2014 at 10:03 PM

The simplest case:

 int texelX = Mathf.FloorToInt(uv.x * textureWidth);
 int texelY = Mathf.FloorToInt(uv.y * textureHeight);

With a few caveats:

  1. 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.

  2. the y direction may be flipped relative to what you expect. TextureHeight - texelY - 1 may be what you want instead, depending on your setup.

  3. 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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JohnRossitter · May 21, 2014 at 12:21 AM 0
Share

thats looking like what I needed!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How should I convert Vector2 Coordinates to UV Coordinates? 1 Answer

Understanding verts and triangles in Unity 1 Answer

Mesh UV min/max Y position 2 Answers

Texturing procedurally generated geometry 0 Answers

uv sub meshes in cube c# 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges