[Closed] Get the right UV per-vertex
Hi!
I’m having troubles getting the UVs correctly, please tell me where is my mistake:
1- I take the faces that are in a face (meshOp.getFacesUsingVert), then I get the first face
2- I search in the face where is the vertex I’m getting the UV
3- Than I get “getMapFace” with the same face I used before (the first of all the faces using my vert)
4- Using the index finded in point 2, I finally get the mapping vertex.
Well, it seems that it is not that simple: I can’t always get the first face.
But how can I choose what face from the list should I choose?
Bye and thanks!
Are you trying to get the uvs for the selected face, or are you trying to get the uvs for the selected vertex?
To clarify: UVs are stored in the vertices. The faces are the “bridge” to translate from mesh space to UV space. Each vertex in the mesh can have one or more corresponding texture vertices – up to as many as there are faces that reference that mesh vertex.
So if you have a vertex and you want to know its UV coordinates, you have to assume there could be more than one!
- Initialize an empty array. Knowing the vertex index, get ALL faces it is referenced by.
- Loop through these faces, and then FOR EACH FACE,
- Find where in the mesh face the vertex is (1st, 2nd or 3rd).
- Get the texture face with the same index
- Get the index of the vertex referenced by the texture face at the same position found in 3. (1st, 2nd or 3rd, i.e. .x, .y or .z component)
- Append this value to the array initialized in 1.
- REPEAT from 3 to 7 until you have found all possible texture vertices.
Now you should have an array with 1 or more values, ALL of which are valid texture coordinates. For example, the corner of the box has to have 3 different texture coordinates to accomodate the mapping on each side of the box it is used by…
But… how can I export a mesh in a way I can use in an engine realtime?
I should create a new triangle for every MapVertex I find? If I do so, the triangles number will completely explode.
Is there a way to fix this or I have to do this (nonsense) task?
Hi.
This is the process I follow:
-
Build a list of vertices (that could be a struct with all attributes you want for your vertex. e.g. vertex coordinates, UVs, color, normal, etc). This list defines the mesh triangles (the vertices are added consecutively based on face indices, texture face indices, color face indices, normal face indices, etc).
-
Now declare a list for final vertices (this could be a hash table, see below) and another one for final indices.
-
Loop through the list from point #1. For each vertex, see if it already exists in your final vertices list (you can use a hash table for this to accelerate the process. Recomended!). If it exists, just insert the index that points to the current vertex in your (final) vertices list into the final indices list. Else, if it’s not in the vertices list, insert the current vertex in the vertices list and insert the new index (that could be the final vertices list size, that is, the number of vertices already stored in the list) for this vertex into de indices list.
The result of this will be a vertex list that contains no duplicated vertices and an indices list that defines the mesh triangles. Just what you need to render your mesh.
Besides that, I think exists libraries that help you optimize your mesh topology for better render performance (better cache use).
Hope that helps.