microsoft / UVAtlas

UVAtlas isochart texture atlas
https://walbourn.github.io/uvatlas-return-of-the-isochart/
MIT License
846 stars 148 forks source link

vertex correspondences saved in _map.txt look incorrect #85

Open gyxiaochina opened 2 years ago

gyxiaochina commented 2 years ago

I used "-t -c -m -wf" switches on a per vertex color OBJ input. I got the _map.txt which contains the vertex correspondences. However, the correspondences do not look correct. I compared the 3D coordinates of some vertices in the input mesh with those of the corresponding vertices in the output mesh. The coordinates are quite different.

Did I misinterpret the content of _map.txt? Thank you very much!

walbourn commented 2 years ago

The map file is intended to contain:

newIndex,oldIndex

This means that vertex newIndex in the new mesh came originally from oldIndex in the original mesh. This is supposed to account for both duplicates from the clean process and the new parameterization.

gyxiaochina commented 2 years ago

Thank you for your reply. That is actually what I thought the _map.txt indicates, but what I found out later in my experimental results somewhat baffled me. For example, I still used this per-vertex-color OBJ as input original mesh original mesh, then I applied "-t -c -m -wf" switches on this input mesh to generate a new mesh together with a resulting _map.txt file. This _map.txt looks like below:

0,0 1,1 2,2 ...

This pattern of same newIndex vs. oldIndex goes all the way down till line#67151 before the newIndex and oldIndex are different. I assume this means the new indices 0, 1, and 2 in the new mesh corresponds to old indices 0, 1, and 2 in the original mesh, respectively. However, the 3D coordinates of vertices 0, 1, and 2 in the original mesh are:

v 0.236947 -1.026314 0.613356 v -1.028812 -0.994679 0.613356 v 0.490099 -1.011664 0.613356

while the 3D coordinates of vertices 0, 1, and 2 in the new mesh are:

v -0.522509 -1.03974 0.676644 v -0.522509 -1.03742 0.657444 v -0.556232 -1.03742 0.676644

These two set of 3D coordinates look quite different. I previously thought they should be similar if not exactly the same, since no large scale spatial transformation is required for the mesh partition. Then what did I do wrong?

Thank you very much!

walbourn commented 2 years ago

What if you try without the -t -c switches?

gyxiaochina commented 2 years ago

I just tried "-m -wf" as you advised. The results still show mismatches between the vertices in the original mesh and the vertices in the new mesh.

Raildex commented 1 year ago

How am I supposed to read the map.txt file in the first place? I also noticed the vertex positions to be shuffled to some degree.

I am using uvatlastool.exe from C# and I am not sure how to transfer the UVs of the new mesh to the old mesh.

My function looks like this:

private void ApplyLightmapUVs(ref RoomMesh mesh, StreamReader resultMeshReader, StreamReader mappingReader)
{
    List<Vector2> newUVs = new List<Vector2>();
    using (resultMeshReader)
    {
        string line = resultMeshReader.ReadLine();
        while (line != null)
        {
            if (line.StartsWith("vt"))
            {
                var splits = line.Split(" ");
                var u = float.Parse(splits[1]);
                var v = float.Parse(splits[2]);
                newUVs.Add(new Vector2(u, v));
            }
            line = resultMeshReader.ReadLine();
        }

    }
    using (mappingReader)
    {
        string line = mappingReader.ReadLine();
        while (line != null)
        {
            var splits = line.Split(",");
            var oldIdx = int.Parse(splits[1]);
            var newIdx = int.Parse(splits[0]);
            RoomVertex oldVertex = mesh.Vertices[oldIdx];
            RoomVertex newVertex = oldVertex;
            newVertex.LightmapUV = newUVs[newIdx]; // apply new UVs
            mesh.Vertices[oldIdx] = newVertex;
            line = mappingReader.ReadLine();
        }
    }
    ExportRoomToOBJ(mesh, "Temp/test.obj",true); // sanity check. shows messed up UVs
}

resultMeshReader is the obj file of the result mesh after uvatlastool.exe. mappingReader is the mapping file. However, the obj I export for a sanity check has messed up UVs. I have attached all meshes Temp.zip

Raildex commented 1 year ago

I came back to Uvatlastool because other alternatives (namely xatlas) did not produce satisfying results either.

I still have the issue with messed up UVs after uvatlas tool.

In the following Zip I have 3 files: The original object (which is exported in my toolchain and that is used as input for uvatlastool), the object from uvatlastool itself (-o parameter) and a file exported again after applying uvs in the following way (C#):

Dictionary<int,int> mapping = new Dictionary<int,int>();
foreach(var s in File.ReadAllLines(roomMeshOutMapFile)) {
    var splitted = s.Split(",");
    mapping.Add(int.Parse(splitted[0]), int.Parse(splitted[1]));
}
var uvList = new List<Vector2>();
string[] outputMesh = File.ReadAllLines(roomMeshOutFile);
foreach(var l in outputMesh) {
    if (l.StartsWith("vt")){
        string[] splitted = l.Split(" ");
        float u = float.Parse(splitted[1]);
        float v = float.Parse(splitted[2]);
        uvList.Add(new Vector2(u, v));
    }
}

foreach( var kv in mapping)
{
    mesh.Vertices[kv.Value].LightmapUV = uvList[kv.Key];
}

Room 2 (split from Room 1)_remapped.zip grafik

Raildex commented 1 year ago

Are there any news on this? I also noticed that the vertex positions are swapped (the x-shaped outputs are supposed to be quads with only two shared vertices)

Raildex commented 6 months ago

This time, I have a very simple mesh (28 triangles) and used VBO as both output and input. Room 0 3tjojlbf.qna.zip _unwrapped.vbo is the output of uvatlastool. The obj file is the output after this mapping process:

void ApplyLightmeshUV(RoomMesh mesh, BinaryReader unwrappedVBO, StreamReader vertexMapping)
{
    var numVertices = unwrappedVBO.ReadInt32();
    var numIndices = unwrappedVBO.ReadInt32();
    var newIndices = new List<int>();
    List<Vector2> newUVs = new List<Vector2>();
    {
        for(int i =0; i < numVertices; ++i)
        {
            //position, ignore
            unwrappedVBO.ReadSingle();
            unwrappedVBO.ReadSingle();
            unwrappedVBO.ReadSingle();
            //normal, ignore
            unwrappedVBO.ReadSingle();
            unwrappedVBO.ReadSingle();
            unwrappedVBO.ReadSingle();
            //uvs
            float u = unwrappedVBO.ReadSingle();
            float v = unwrappedVBO.ReadSingle();
            newUVs.Add(new Vector2(u, v));
        }
        for(int i =0; i < numIndices; ++i)
        {
            var idx = unwrappedVBO.ReadInt16();
            newIndices.Add(idx);
        }
    }

    {
        // make a copy of the old vertices.
        var oldVertices = mesh.Vertices.ToArray();
        string? line = vertexMapping.ReadLine();
        int i = 0;
        while (line is not null)
        {
            var splits = line.Split(",");
            Debug.WriteLine($"{splits[0]},{splits[1]}");
            var oldIdx = int.Parse(splits[1]);
            var newIdx = int.Parse(splits[0]);
            RoomVertex oldVertex = oldVertices[oldIdx];
            RoomVertex newVertex = oldVertex;
            newVertex.LightmapUV = newUVs[newIdx]; // apply new UVs
            mesh.Vertices[oldIdx] = newVertex;
            line = vertexMapping.ReadLine();
            i++;
        }

    }
}

grafik

I would like to atleast have some feedback. I don't know whether the mapping is incorrect on my side or whether _map.txt is just wrong.