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
4
Question by Ian 1 · Jun 21, 2010 at 02:41 PM · texture2dtexture-atlas

Texture-Atlas Modifying Mesh UVs

I am trying to combine multiple textures into a texture atlas using Texture2D.PackTextures() which works fine. I create a new material for all the materials used on my mesh and sub-meshes. I end up with a new Packed Texture with 4 images.

This new material using the texture atlas is then applied to all my mesh and submeshes.

I then modify the UVs of my mesh and submeshes to point to the new image within the texture atlas. But what I seem to get is the complete texture atlas being used as the single texture, rather than the image within the atlas.

This is what I am expecting (apologies for the dark colours, there's no lights in my test scene!):

alt text

And this is what I get:

alt text

I've checked the UVs and they look to be getting updated correctly.

Any ideas as to what I'm missing?

This is my code snippet, its nothing fancy.

Material atlasMaterial = new Material(Shader.Find("Diffuse Fast")); Component[] filters = GetComponentsInChildren(typeof(MeshFilter));

Texture2D[] textures = new Texture2D[filters.Length];

for (int i=0 ; i<filters.Length ; i++) { textures[i] = (Texture2D)filters[i].gameObject.renderer.material.mainTexture; }

textureAtlas=new Texture2D(maxSquareTextureSize,maxSquareTextureSize); Rect[] uvs = textureAtlas.PackTextures(textures,0,maxSquareTextureSize);

atlasMaterial.mainTexture = textureAtlas;

Vector2[] oldUV,newUV; for (int i=0 ; i<filters.Length ; i++) {
filters[i].gameObject.renderer.material=atlasMaterial; oldUV = (Vector2[])(((MeshFilter)filters[i]).mesh.uv); newUV = new Vector2[oldUV.Length]; for (int j=0 ; j<oldUV.Length ; j++) { newUV[j]=new Vector2((oldUV[j].x*uvs[i].width)+uvs[i].x, (oldUV[j].y*uvs[i].height)+uvs[i].y); }

 ((MeshFilter)filters[i]).mesh.uv=newUV; 

}

Comment
Add comment · Show 2
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 Rblain · Jun 23, 2010 at 05:47 PM 0
Share

I am guessing here but worked many years with unwrapping UVs. It appears that the UV's are still going from 0-1 for each submesh from the dark pics above. If the width is 0-1 I don't see where you have percentiled or scaled and offset the sub UV sets. I see them iterated over and multiplied but not percentiled according to the new rect. I may be barking up a wrong tree here as the script looks fine but that is where I would head next if I was trying to crack this nut. Am doing this scheduled next week for a bunch of stuff.

avatar image Ian 1 · Jun 30, 2010 at 07:15 PM 0
Share

hmmm... that should be accounted for from the uvs[] array, and I'm sure I saw the UVs co$$anonymous$$g out as 0,0.5,1,1.5 etc. But I will certainly look into that and make sure!

Thanks.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Max Kaufmann · Dec 13, 2010 at 03:12 AM

It might be this line:

((MeshFilter)filters[i]).mesh.uv=newUV; 

In general, you should use .sharedMesh wherever you're using .mesh right now; the latter acts like a value-type and returns a copy, which is probably not what you want.

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

Answer by d3coy · Jan 02, 2011 at 06:45 AM

My bet is that the textures array being packed wasn't full of single occurrence items (like in a Set). This code has the fundamental assumption that there is a 1:1 correspondence between texture, rectangle, and meshfilter, when this may not be the case at all.

First a verified Set of Texture2D should be used as the input textures. Then when choosing a rectangle for any particular Mesh, make sure to get the proper index into the Rectangles array using the Mesh's texture, with Array.IndexOf.

This also means that the various game objects should not be set to the new material until the UV's have been adjusted, since the mainTexture is needed to do the rectangle lookup.

I realize this is an old question, but figured someone might be wondering the same thing.

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

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

No one has followed this question yet.

Related Questions

image stretched when packing textures in large size 1 Answer

Texture2D.PackTextures Only Packing First Texture 0 Answers

Coding my own auto-slicer, getting "islands" of pixels at runtime? 1 Answer

Best way to atlas texture2Ds used on quads? 0 Answers

Texture2D.PackTextures - attempt to determine if texture was scaled 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