Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 myama · Mar 18, 2016 at 12:20 PM · meshunity5uvstexture atlas

Alter mesh UVs for Texture Atlas

I have a game with playing cards. My playing card model has 2 sub meshes, one for the front and one for the back of the card. I have my app working where when I deal different cards, I load a different texture onto the front of the card’s material. So now I am looking to optimize my code by combining all 52 card faces into a single Texture Atlas, so the face’s material would point to that. Then I was going to change the uvs for each card to display the correct card face. In this way I was hoping that I would reduce the draw calls.

To experiment, I tried getting the triangles for just the front submesh, then I tried to alter the uvs for just those triangles and put it back into the mesh. When looking at a single card, I thought I’d test it out by leaving the single texture and altering the uv’s to take the top left 1/4 of the card by lerping from to 0 - 0.5 for both x and y. But it does not seem to work that way. I get a extremely zoomed in upper right corner of the card when I do this.

When I spin the model around, the card back is not affected, so I think I am altering the correct uv’s…

 MeshFilter filter = this.GetComponent<MeshFilter>();
 if (filter != null) {
    
     int[] frontFaceTris = filter.mesh.GetTriangles(1);
    
     List<Vector2> uvs0 = new List<Vector2>();
    
     filter.mesh.GetUVs(0, uvs0);
    
     Vector2 uv = Vector2.zero;
    
     float xout = 0;
    
     float yout = 0;
    
     foreach (int tri in frontFaceTris![alt text][1]) {
    
         uv = uvs0[tri];
    
         xout = Mathf.Lerp(0f, 0.5f, uv.x);
    
         yout = Mathf.Lerp(0f, 0.5f, uv.y);
    
         uvs0[tri] = new Vector2(xout, yout);
    
     }
    
     filter.mesh.SetUVs(0, uvs0); 
 }


Attached is a before and after image of my model. Can anyone help?

[1]: /storage/temp/66000-before-after-alter-uvs.jpg

before-after-alter-uvs.jpg (28.4 kB)
Comment
Add comment · Show 1
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 myama · Mar 16, 2016 at 07:17 PM 0
Share

Ok I figured out my problem. Since many of the vertices are shared between triangles, I was scaling the uv's for those verts multiple times which was causing distortion. To solve this I created an array of bools the size vertexCount. If I scale the uv for a vert, I set that vert's flag to true, so I would not scale it again, and I would check that flag before scaling a uv.

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to change mesh UV's permanent in Unity (texture atlas) 1 Answer

Creating a mesh programatically for a tile-based 2D game 1 Answer

Rounding Error in Texture Creation? 0 Answers

Lighting Issues with custom generated mesh 0 Answers

creating a mesh and generating it's specific uvs 2 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