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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Ren the Unclean · May 23, 2013 at 11:02 PM · meshmaterialuv

How to merge per triangle UVs

I am trying to generate correct UVs for meshes I have combined with CombineMeshes().

UnityEditor.Unwrapping.GeneratePerTriangleUV() seems to do what I want, but the documentation just says "You'll need to merge them yourself." This function seems to be giving me back one UV pair per triangle, but I don't know how to merge/map those onto the vertex UV pairs that the mesh seems to have (the UV array is the same size as the vertex array, while the generated UV array is the same size as the triangle array).

Here is my function:

 public GameObject MeshCombination(Material material, CombineInstance[] combines)
     {
                     
         GameObject newMeshObj = new GameObject(material.name);
         
         MeshFilter targetFilter = null;
         targetFilter = newMeshObj.AddComponent<MeshFilter>();
         
         newMeshObj.AddComponent<MeshRenderer>();
         targetFilter.sharedMesh = new Mesh();
         targetFilter.sharedMesh.CombineMeshes(combines);
         
         Vector2[] triangleUV = UnityEditor.Unwrapping.GeneratePerTriangleUV(targetFilter.sharedMesh);
         // How do I merge the per triangle UVs and assign them to the mesh?
 
         targetFilter.renderer.material = material;        
         
         return newMeshObj;
     }

Thanks in advance for whatever help you guys can give me!

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
0

Answer by virgilcwyile · Mar 05, 2018 at 08:38 AM

I have a code which works for me. However I cannot use that in my project as UnityEditor code cannot be used in the Builds. Below is the code:

  private static Vector2[] GenerateUvs(Mesh mesh)
         {
 
             Vector2[] uvs = new Vector2[mesh.vertices.Length];
             Vector2[] all = UnityEditor.Unwrapping.GeneratePerTriangleUV(mesh);
             int[] triangles = mesh.triangles;
 
             int count = 0;
             while (count < uvs.Length)
             {
                 for (int i = 0; i < triangles.Length; i++)
                 {
                     if (triangles[i] == count)
                     {
                         uvs[count] = all[i];
                         count++;
                     }
                 }
             }
 
             return uvs;
 
            // return null;
         }

@Ren

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 Bunny83 · Mar 05, 2018 at 12:25 PM 0
Share

Uhm, you just turned an O(n) operation into an O(n²). If you look closely at the body of your inner loop you may notice that this:

 if (triangles[i] == count)
 {
     uvs[count] = all[i];
     count++;
 }

actually results in this:

 uvs[triangles[i]] = all[i];

Therefore you only need a single loop that looks like this:

 for (int i = 0; i < triangles.Length; i++)
 {
     uvs[triangles[i]] = all[i];
 }


However keep in $$anonymous$$d that the method might generate seperate UVs for two adjacent triangles. If the mesh is a shared mesh the result would be wrong as you would assign two different UVs to the same vertex so only the last assigned value would take effect. The result would be wrong in this case. In that case you would have to split the vertex which is quite a bit more complicated. However if you created the vertices the way you need them it should work just fine.

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

15 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

Related Questions

2 Textures on a single mesh 1 Answer

3D Procedural Hex UV Map 0 Answers

can't put texture materials on imported mesh. 1 Answer

UV Mapping for 2D Meshes with 3 Vertices Only 1 Answer

Manual UV-Mapping of a Primitive Cube 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