- Home /
Assigning UVs to mesh
According to the docs and error messages I've seen, Unity's Mesh.uv
should be the same length as Mesh.vertices
. This seems wrong to me. UV maps usually have more vertices than the actual mesh due to duplication of vertices at seams.
Going through a collada file for example, there's a UV co-ordinate (Vector2) for each vertex in each triangle. That gives a total of 3 times the number of mesh triangles. This makes more sense to me.
So, how exactly should one go about assigning UVs to a mesh in unity? All the examples I've been able to find on google are of simple planes with no seams, hence no vertex duplication. Could someone please provide an example with a more complex mesh? A cube perhaps.
Oh another source of confusion for me is the fact that unity's normals are assigned per-vertex not per-face. Is there a way to convert from per-face to per-vertex?
In Unity, each vertex is assigned position, UV, normal, tangent, colour. A shared vertex is only imagined, it may have the same position as another vertex, but different UV, normal and tangent, therefore is it's own entry in the mesh data array. A vertex is split into multiple ones if any of the other data needs to be unique.
Perhaps this video will explain better : https://www.youtube.com/watch?v=o9R$$anonymous$$6O2kO$$anonymous$$o&t=4m12s
As in the above video, imagine a cube. It has 8 corners, but 24 vertices (UVs, normals, tangents, colours). So really it is 6 planes.
As for converting per-face to per-vertex normals, simply apply the face normal to each vertex in that triangle.
Wow! my $$anonymous$$d just got blown! That's very helpful indeed. Ah, so that's why unity advises us to keep seams and hard edges at the $$anonymous$$imum to squeeze out more performance!
So this implies that not all shared verts are duplicated, right? Only the ones at seams and hard edges are duplicated.
If that's the case, this makes my task infinitely more complicated, trying to read a collada file at runtime.
The per-face to per-vertex method you provided will only work on verts at hard edges and seams, right? What about those at smooth edges? Will averaging the surrounding face normals work?
Finally, what if we have a vert that's split by a seam, but is not part of a hard edge? Duplicating the vert will automatically make its edges hard, won't it?
Have never exa$$anonymous$$ed a collada mesh data before, so not sure if I can be much help.
1/ yes getting an average value for a normal from 2 faces that share a vertex should work.
2/ duplicating the vertex, then having different normals on each vert would create a hard seam. Again, averaging the normals of the faces using that vertex position should help it only appear as a soft seam (same vert position and normal).
Good Luck!
Answer by AlucardJay · Apr 26, 2016 at 03:03 PM
Information given in comments helped explain how a Unity Mesh is defined:
In Unity, each vertex is assigned position, UV, normal, tangent, colour. A shared vertex is only imagined, it may have the same position as another vertex, but different UV, normal and tangent, therefore is it's own entry in the mesh data array. A vertex is split into multiple ones if any of the other data needs to be unique.
Perhaps this video will explain better : https://www.youtube.com/watch?v=o9RK6O2kOKo&t=4m12s
Your answer
Follow this Question
Related Questions
Remove Distortion on custom UV mesh 1 Answer
Trouble with UV Mapping 1 Answer
Building Uvs in Editor for Combined Meshes 0 Answers
How to fill out a procedurally generated mesh with quads of varying sizes? 0 Answers
Finding stitched uvs 1 Answer