Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Attapi0 · Jul 07, 2015 at 12:11 AM · meshtexture2duv

How should I convert Vector2 Coordinates to UV Coordinates?

I created a function to slice texture atlases as long as each texture is the same size (Think like Minecraft). This function returns the upper-right hand corner of each texture on the atlas in a Vector2 coordinate. I can use this to figure out the 3 other points I need to texture a quad.

Here's my question. How do I convert these points on the image to UV coordinates? Should I be going about this a different way?

Comment
Add comment
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

1 Reply

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

Answer by Dave-Carlile · Jul 07, 2015 at 01:22 AM

Texture coordinates range between 0 and 1. In the horizontal or u direction 0 is on the left and 1 is on the right. In the vertical or v direction 0 is on the bottom and 1 is on the top. Another way to look at this is the lower left corner is at uv=0,0 and the upper right corner is at uv=1,1.

So you need to figure out where in texture space your sprite texture is within your atlas. You can figure this out given the texture atlas size and the pixel coordinates of your sprite corners.

To map between the pixels and textures you just divide the pixel position by the width of the texture. This basically gives you a percentage. For example, if your atlas is 256 wide and the corner pixel is at x=128, you calculate the u texture coordinate like this:

 u = 128 / 256

Doing the math gives you 0.5, which makes sense because the pixel at x=128 lies directly in the center of the atlas, and 0.5 is directly in the center between 0 and 1.

Do the same math with the y pixel coordinate and the texture atlas height.

All that said, Unity already has import functionality that can split the sprites for you and generate sprite objects without you doing all the work. Of course, if you're doing a Minecraft-like project and want moddable textures that doesn't work so well.

In that case you might look at Texture2D.PackTextures.

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 Attapi0 · Jul 07, 2015 at 01:30 AM 0
Share

Oh my god I'm so angry with myself. I thought of this yesterday but I convinced myself it wouldn't work before I even tried it. Thanks man!

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 do I convert a Vector2 from a UV coordinate to an absolute pixel position? 1 Answer

Mesh breaks apart when using vertex displacement with shadergraph. 3 Answers

How to set up UV for lightmaps for generated meshes? 4 Answers

texturing a procedurally created mesh 0 Answers

Same material renders differently on a mesh generated from script and normal cubes. 1 Answer


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