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 zardini123 · May 20, 2016 at 01:13 AM · meshuv mappingtriangles

How do I use the output of Unwrapping.GeneratePerTriangleUV?

I am trying to use Unwrapping.GeneratePerTriangleUV to unwrap my mesh created in the editor. In the docs of the function, it says "You'll need to merge [uvs] yourself." What does it mean by "merging uvs" and how would I do said "merging?"

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Max-Bot · Sep 08, 2016 at 05:46 PM

Here is how I assume it should bu used. But it doesn't work on my PC and each function call there is crash report from some external program UnwrapCL.exe :( Maybe somebody helps code bellow:

 Vector2[] uvs = new Vector2[newVerts.Count];
         UnwrapParam paramteters = new UnwrapParam();
 
         int t = 0;
         foreach(Vector2 uvPerTri in Unwrapping.GeneratePerTriangleUV(mesh, paramteters)) {
 
             uvs[tris[t]] = uvPerTri;
             ++t;
         }
 
 mesh.uv = uvs;

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
0

Answer by CarpeFunSoftware · May 21, 2016 at 05:40 AM

See documentation on the Mesh object (http://docs.unity3d.com/ScriptReference/Mesh.html) it shows you the vertex array, the triangles array and the UV array.

The triangles use the vertex indices in the vertex array. The UVs array has the x,y positions in the texture for each vertex in the vertex array.

So if your mesh was a simple triangle (consisting of only 3 vertices) you would have a vertex array equal to the 3 points of the triangle, a triangles array consisting of index 0, 1, 2, into the vertex array, which is one triangle, and (this is the important part for your question) you would have UV mappings for each of those vertices onto the texture. Note there's several UV sets (4 in all .uv[], .uv2[], .uv3[], .uv4) so you could even keep the "old" uv mapping and assign the new one to a different UV set and/or swap em around (old in uv2, new in uv1).

Short Answer-->> Basically, the function you asked about above returns a result array to you, but doesn't assign it to the mesh. That array returned result is just "info floating in memory". You have to decide what to do with it. Try assigning it to a UV set in the mesh and see what happens. It may or may not map as you wish it to. Experimentation is in order here. The function does a "best guess" on how to map the UVs given the mesh vertex data.

The mesh UV documentation creates a new UV set in the example out of x and z vertex values and that's just one example of "merging your own uv's in" programmatically: http://docs.unity3d.com/ScriptReference/Mesh-uv.html

They assigned it to uv set 1 after the for-loop was finished. That would trash what was there before, if anything. Or, like I said, you could assign it to uv2 or 3 or 4.

Shortest Answer -> Warning: The manual says this is a preliminary interface in UnityEditor namespace.

 using UnityEditor;
 ...
 void SomewhereOverTheRainbow() {
     myMesh.uv = Unwrapping.GeneratePerTriangleUV(myMesh);
 }



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
0

Answer by parasiteEvie · Jan 03, 2018 at 11:07 PM

So the output is in relation to the index array (or mesh.triangles) rather than the vertex array. I miught have over complicated it, but this works for me. Bonus points for anyone that submits a faster than n^2 solution.

Here is my solution:

 public void UpdateUVs()
     {
         SkinnedMeshRenderer meshRenderer = GetComponent<SkinnedMeshRenderer>();
         Vector2[] uvs = new Vector2[meshRenderer.sharedMesh.vertices.Length];
         
         Vector2[] all = Unwrapping.GeneratePerTriangleUV(meshRenderer.sharedMesh);
         int[] triangles = meshRenderer.sharedMesh.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++;
                 }
             }
         }
 
         meshRenderer.sharedMesh.uv = uvs;
     }


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
0

Answer by Smaughbeer · Oct 11, 2019 at 06:37 AM

 Mesh m = new Mesh();
 ...
 Vector2[] uvPerTriangle = Unwrapping.GeneratePerTriangleUV(m, param);
 
 Vector2[] uvs = new Vector2[m.vertices.Length];
 for (int i = 0; i < m.triangles.Length; i++)
     // Triangle contents reference to vertex # 
     uvs[m.triangles[i]] = uvPerTriangle[i];
 
 m.uv = uvs;
 m.RecalculateTangents();
 ...
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

48 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 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 avatar image avatar image avatar image avatar image

Related Questions

Making Different vertices,triangle,uv maps in the same mesh 1 Answer

Problem drawing a mesh with Graphics.DrawMeshNow 1 Answer

contains two different ids for the same vertex 0 Answers

Object odd light problem with mesh? 1 Answer

How to set UV (Map) for Mesh made by script without duplicating vertices? 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