- Home /
Manually loading .obj file, issue understanding UVs
Hello!
I'm trying to convert a .obj to a mesh via a script but I've become confused as to how to map from what the obj has to the fields in the Mesh object. So the mesh object has
vertices normals uvs triangles
I'm moving all of the vertices from my .obj (indicated with v prefix) into an Vector3 array and pass that to vertices. Then I'll get the triangles, normals and uvs from the v/vt/vn lines. I've managed to get a pink object showing up that looks correct. I tried applying a texture and turns out my uvs aren't correct. My assumption was to take the vt from v/vt/vn lines and set up an array such that my vertex array has an accompaning vt array that would associate a vertex with a texture coord(vert[i] is paired with texCoord[i]. This sounds right to me, but I've noticed in my .obj that sometimes a vertex has different texture coordinates ie...
f 12/2/3 10/2/3 11/2/3
f 13/1/3 12/1/3 11/1/3
In the above example on the first line vertex 11 has a texture coordinate of coord 2. In the second like vertex 11 has coord 1 though. Would this not mean that a single vertex has two different coordinates? I'm a little confused as to what I'm supposed to do here. Any clarification on uvs and vertices would be great.
Edit: As a more concrete example, I have a 1x2x1 voxel, where I set one side of it to red and another side to blue(to break down where the problem is occuring). The 8 vertices that lie on the seam between the two colors are getting 2 texture coordinates each, which makes sense is thats where the colors meet. I'm not sure how this gets passed into the mesh object though as you need to have the same number of UVs as you do vertices.
Edit2: So I went and checked the vertex count between what the file has vs what unity does on its own. It turns out the unity one has 8 more vertices, so I'm guessing it checks to see that a vertice has another texture coord and adds a new vertex to that position with its texture coordinate. I'm going to have to figure out a way to handle seeing new texture coordinates for vertices and adding them to the vertex list.