- Home /
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?"
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;
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);
}
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;
}
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();
...
Your answer

Follow this Question
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