Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to flatten a partitioned 3D mesh onto 2D UV map? #83

Closed
gyxiaochina opened this issue Nov 4, 2022 · 17 comments
Closed

How to flatten a partitioned 3D mesh onto 2D UV map? #83

gyxiaochina opened this issue Nov 4, 2022 · 17 comments
Labels

Comments

@gyxiaochina
Copy link

I am pretty new to UVAtlas and have limited experiences with 3D graphics. I tested using "UVAtlasCreate" to partition an input 3D mesh (.obj) and got the result in .sdkmesh file. I could use "directxtkmodelviewer" (https://github.com/walbourn/directxtkmodelviewer) to see the partitioned 3D mesh. However, what I would like to have ultimately is a 2D UV map, such as the figures in Zhou, et al, 2004 and Sander, et al, 2002. I suspect (not sure) that the 2D UV map is in the form of a .mtl file and a corresponding .png file, both of which can be used together with a texture-less .obj mesh to visualize the textured 3D mesh in a mesh viewer (e.g., MeshLab). I have read the UVAtlas Wiki and the source code, but I have not found out how to flatten the partitioned 3D mesh onto a 2D UV map. Can anyone please kindly show me how to do that? Many thanks in advance!

@walbourn
Copy link
Member

walbourn commented Nov 4, 2022

The workflow for the tool is generally to output the UV mapping as replacement UV values in the model output. You can use the -wf switch to get a simple WaveFront OBJ output and the texture coordinates in that output are the ones generated by UVAtlas.

There are a number of command-line options for the uvatlastool that provide different visualizations of the result. This includes -c which modifies the output materials to help you see the way the tool partitioned the original model, and -t which outputs a 2D model where the generated UVs are set as positions.

@gyxiaochina
Copy link
Author

@walbourn Thank you for your quick reply! Sorry I did not explain my problem clearly. The WaveFront OBJ file I used as input for UVAtlas is a per vertex color OBJ file, so it does not have corresponding .mtl or .png file. What I would like to have as a result is a new .mtl file and a new .png file. Moreover, I will also hope to be able to put different weights to different 3D vertices, so that I could place more details to certain regions of the 3D mesh, such as that shown in Fig 1 in Sander et al, 2002 (https://hhoppe.com/ssp.pdf).

I followed your advice to generate OBJ output using -wf switch. I can see the 2D texture coordinate (vt in .obj file) is generated for each vertex. The 2D texture coordinates range from 0 to 1. I suppose they are the coordinates in a texture image (e.g., a .png file). However, I do not know how/where to get this texture image.

I once thought maybe I could use the color at each 3D vertex (in the input OBJ file) and its corresponding 2D texture coordinate (in the output OBJ file) to construct a 2D texture image myself. However, I later noticed that the number of 3D vertices in the output OBJ file is different from the number of 3D vertices in the input OBJ file, so that I do not have the color info for each vertex in the output OBJ file.

Please excuse me if my question sounds naive, as I am pretty new to UVAtlas. Thank you very much!

@walbourn
Copy link
Member

walbourn commented Nov 5, 2022

I'm fairly sure the 2D image on the right of this is just a simple screenshot of a render of the output of -t where the vertex colors are the normals.

@walbourn walbourn reopened this Nov 5, 2022
@gyxiaochina
Copy link
Author

Thank you so much for replying my question so fast! I am afraid that I am a bit lost because of my poor background knowledge. When you said "output of -t", did you mean using both "-t" and "-wf" switches when runnng uvatlas? The resulting OBJ file looks like a 3D model (it contains 3D coordinates for all vertices). I tried to visualize it using MeshLab, it first popped up an error message "Some materials definitions were not found, a default white material is used where no material was available", then displayed a white 3D model.

FYI, the format of my input OBJ file is like this:

vn 1.22 6.10 -0.78
v -0.03 0.12 0.00 0.49 0.49 0.49
vn ...
v ...
...
...
f 212//212 216//216 400//400
f ...
...

Can you please kindly let me know how I could generate the 2D model as shown in the right figure? Thank you very much!

@walbourn
Copy link
Member

walbourn commented Nov 5, 2022

The -t output is called *_texture.* which shows the UV parameters as vertex positions. I just added a -vn option to visualize normals, but it doesn't look exactly like the figure.

It's also possible that figure was generated with the "gutter helper" ResampleTex feature which I don't yet have implemented for this open source version of UVAtlas.

@walbourn
Copy link
Member

walbourn commented Nov 5, 2022

acf451e

@walbourn walbourn closed this as completed Nov 5, 2022
@gyxiaochina
Copy link
Author

gyxiaochina commented Nov 5, 2022

@walbourn Thank you for your further explanation! My problem is that when I used "-t" and "-wf" switches, I got ONLY one ouput OBJ file. I did not see any _texture.obj file. Yet, when I used only "-t" switch (which should by default generate .sdkmesh output file), I had two output .sdkmesh files (.sdkmesh and _texture.sdkmesh). Using your "directxtkmodelviewer" (https://github.com/walbourn/directxtkmodelviewer), I could visualize the partitioned 3D mesh saved in .sdkmesh. But when I tried to visualize _texture.sdkmesh, all I got was a blank window.

Did you mean that I should have another *_texture.obj file in my result when using "-t" and "-wf" switches? Then what did I go wrong?

Thank you very much!

@gyxiaochina
Copy link
Author

Just now I did some further tests. I noticed that when I used "-t" and "-wf" switches, there was a warning message at the end of the process, which I overlooked previously. The warning message is as below:

WARNING: WaveFront Object (.obj) not supported for UV visualization (requires Vertex Colors)
):-> 'bun_zipper_texture.obj'80004001: Not implemented

I guess that is why I failed to get the *_texture.obj in the result. However, as we can see in the format of my input .obj file, which I showed above, that .obj file does contains vertex colors. (I assume the 6 values following flag "v" indicate 3D coordinate (x,y,z) and color (R,G,B)).

FYI, the input .obj file I used in my experiment is the Standford bunny (http://graphics.stanford.edu/data/3Dscanrep/). Since the original file is in .ply format, I first had to use MeshLab to export it to .obj format.

Maybe the vertex color should be set in a different way?

Thank you very much!

@walbourn
Copy link
Member

walbourn commented Nov 6, 2022

I realized when doing the normal visualization work that the block on writing _texture with WaveFront OBJ was wrong. Per-vertex colors are only needed for the new normals visualization or when using -uv2, but not for basic -t or -c -t usage. That's fixed in the same commit. I also made it a fatal error if the requested file wasn't generated.

Support for 'per-vertex-color' in WaveFrontOBJ isn't widely supported, but I have a note to look at supporting at least importing it in the future here.

There's also a PR I need to work through for adding direct support for PLY to this tool. #70

@gyxiaochina
Copy link
Author

Thank you for your further explanation! Using your latest commit, now I can generate the _texture.obj which contains the 2D model, although the color info is lost. Does this mean that right now, for per vertex color OBJ input, it is still not possible to get the 2D color UV map like shown in uvatlas2.jpg and uvatlas4.jpg? If so, in order to have 2D UV map like that, what type (format) of input OBJ file must I prepare?

BTW, I am really impressed by your quick reply and commit even during weekend. Thank you very much!

@walbourn
Copy link
Member

walbourn commented Nov 7, 2022

The specific images you see in those are both outputs of the -c option which sets the material colors to indicate the partition. The first image appears to have a texture on it as well, which I can't do in the tool but you can do with editing the resulting mtl.

Per vertex colors are only needed for the new normals visualization, but I have a work item to capture support for this in WaveFront OBJ: microsoft/DirectXMesh#75

If you can attach the Wavefront OBJ you are using that has per vertex color, I can use it for testing. Thanks!

@gyxiaochina
Copy link
Author

gyxiaochina commented Nov 8, 2022

I see that I misinterpreted these two images. I previously thought they show an example of unwrapping a 3D colored mesh onto 2D UV map. Sorry about that.

I have attached a per vertex color OBJ file (archived into a zip as I am not allowed to upload an obj file directly). This is how I prepared this file: I first downloaded pittsburghbridge.zip from pittsburghbridge.zip, then chose processed.ply from it. Next, I used Meshlab to convert the .ply to .obj (File -> Export Mesh As...). After that, I still used Meshlab to add normals to the exported .obj file (Filters -> Normals Curvatures and Orientation -> Compute normals for point sets). Finally, this point cloud was turned into mesh (Filters -> Remeshing Simplification and Reconstruction -> Surface Reconstruction: Screened Poisson).
processed_meshlab_normal_mesh.zip

I tested using "-t -c -wf" switch on this OBJ file. I got the _texture.obj result. It shows the partitioned mesh on the uv plane, but the color is not there. I wonder if there is any way I could get the visualization similar to the "Color Map Results" in Fig 8 of Zhou et al 2004?

Thank you very much!

@walbourn
Copy link
Member

walbourn commented Nov 8, 2022

I must have broken something. It's not writing out the mtl file for the -c case which has materials like:

Unable to find material Chart01 (missing .MTL file?)
Unable to find material Chart02 (missing .MTL file?)
Unable to find material Chart03 (missing .MTL file?)
Unable to find material Chart04 (missing .MTL file?)
Unable to find material Chart05 (missing .MTL file?)
Unable to find material Chart06 (missing .MTL file?)
Unable to find material Chart07 (missing .MTL file?)
Unable to find material Chart08 (missing .MTL file?)

@walbourn
Copy link
Member

walbourn commented Nov 8, 2022

Doh! I see the bug. I didn't actually WRITE the mtl file for WaveFront export when I do the -c option :(

@walbourn walbourn reopened this Nov 8, 2022
@walbourn
Copy link
Member

walbourn commented Nov 8, 2022

Try again :)

48ddb5c

@walbourn walbourn closed this as completed Nov 8, 2022
@walbourn
Copy link
Member

walbourn commented Nov 8, 2022

Note that with VS 2015 or later, you can load WaveFront OBJ files directly into the IDE.

@gyxiaochina
Copy link
Author

I see now I can generate _texture.obj, _map.txt and _charts.mtl if I turn on "-t -c -m" switches. I guess with the vertex correspondences in _map.txt, I should be able to map the per vertex color from the 3D mesh to the 2D UV map. Thank you very much for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants