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

Obj2Gltf doesn't always convert materials #27

Open
naikrovek opened this issue Apr 12, 2018 · 12 comments
Open

Obj2Gltf doesn't always convert materials #27

naikrovek opened this issue Apr 12, 2018 · 12 comments

Comments

@naikrovek
Copy link

I've spent some time trying to get this library to use the proper materials in the glTF files it creates, without success. I decided to do a sanity check and attempt to use Obj2Gltf to convert a .obj created with this library to glTF and see if materials were imported. To my surprise, they were not.

Every OBJ viewer I have tried loads the materials properly. The materials, in this case, are just some colors, and not any textures. The materials are stored in a .mtl file with the same basename and path as the .obj file.

If you would like the files I am using, please let me know.

@javagl
Copy link
Owner

javagl commented Apr 12, 2018

Sorry for the hassle.

The OBJ-to-glTF-conversion currently only transfers the texture information. The point is: MTL files may contain really complex material definitions. There is a branch of the OBJ library where the support for MTL has been extended to cover (nearly?) everything that is supported by MTL.

Translating this information into the "most appropriate" glTF PBR material is far from trivial.

I agree that as a starter, I could just dump the diffuse colors (Kd in MTL) into the baseColorFactor, but this would only be a first step.

@naikrovek
Copy link
Author

I did what you described in your last paragraph and that got me rolling. I'll try the materials branch soon. I am generating the .obj and .mtl files as Strings in RAM as I walk a file of an entirely different type. The material definitions I am creating are simple, using only the classic non-PBR material definitions.

So, I'm good, now. Once I try the materials branch, maybe I'll be even better.

Thank you for the quick response.

@javagl
Copy link
Owner

javagl commented Apr 12, 2018

Admittedly, I'm a bit surprised to hear that this is "resolved" now. Did you add some sort of translation logic near https://github.com/javagl/JglTF/blob/master/jgltf-obj/src/main/java/de/javagl/jgltf/obj/v2/MtlMaterialHandlerV2.java#L68 ? I think that's the point where the MTL could be examined further: If it has a Kd (diffuse color), then its R,G,B components could be passed to the createMaterialWithColor method there. But of course, this is only a tiny part of the possible material definitions.

I'm not sure how well an MTL could map to a PBR at all. Some contortions might be justifiable, e.g. mapping the "shininess" to "metallicFactor" or so. But all this would have to be verified with a glTF PBR renderer that is much more powerful and closer to the intended behavior than what JglTF currently supports (the renderer is, indeed, only a very basic one, and it's a bit unfortunate that I did not continue the development there for a while now...). And after all, the translation will become a real challenge when the different possible textures that may be contained in an MTL come into play.

Suggestions (or pull requests) are welcome, of course!

@naikrovek
Copy link
Author

Well I closed it, since it isn't an issue for me anymore.

There is a glTF extension you can declare that tells renderers that you've defined classic material values, rather than PBR materials. I don't know the extension name off-hand, nor do I know how to tell this API to use that extension.

My need is not photorealistic, so I closed it, and with my comment now, it will reopen, and I'll leave it open.

@naikrovek naikrovek reopened this Apr 12, 2018
@javagl
Copy link
Owner

javagl commented Apr 12, 2018

There's the KHR_materials_common extension. It was originally only defined for glTF 1.0. I thought there are also efforts for creating something similar for glTF 2.0, but couldn't find it in the meeting agendas right now. In any case, there are currently some other, related extensions (mainly KHR_lights) that are currently being sorted out.

Leaving the issue open until the MTL-to-glTF-PBR translation is extended to at least a "more reasonable" state is fine. I hope that I can allocate some more time for this soon...

@naikrovek
Copy link
Author

Related question - I'm not using textures at all. Is there a straightforward way to use diffuse/specular colors in a basecolor/metallic system like glTF uses? I just want to convert the colors & specular data into a PBR workflow. I understand that textures require a lot of work, and I'm hoping that plain colors do not.

Thanks.

@javagl
Copy link
Owner

javagl commented Apr 13, 2018

As already mentioned above: I think that it should be possible to obtain the Kd (diffuse color) from the MTL, and pass it to the createMaterialWithColor method at https://github.com/javagl/JglTF/blob/master/jgltf-obj/src/main/java/de/javagl/jgltf/obj/v2/MtlMaterialHandlerV2.java#L68

Currently, it exports materials that do not have a diffuse texture as uniformly gray. Passing in the Kd values there could already do the job: When there are no textures, then the baseColorFactor (i.e. the material.pbrMetallicRoughness.baseColorFactor) should basically serve as "the color of the material", as shown in the example at https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#metallic-roughness-material

If this doesn't work for you, I could try to do some basic test this weekend (something similar would have to be a small building block of a more sophisticated MTL-to-PBR-translation anyhow...)

@naikrovek
Copy link
Author

OK, that's what I thought. I was hoping that the "far from trivial" stuff you mentioned earlier only applied to textures, and that simple colors were more straightforward.

You don't need to work on that or anything else for me, the change you suggested did work. Of course the colors are "different" in glTF viewers because of PBR, but that doesn't matter in my circumstances.

This library does everything I need of it. Thank you for your work on it, and thanks again for your help.

@naikrovek
Copy link
Author

Regarding conversion of materials, I found this, which may be of interest to you. I can't find it again, now, but the link to this was presented alongside a viewer which showed the old material format compared alongside the converted, and they looked identical, as far as my eyes could tell. Maybe this will help you in your material conversion efforts.

https://github.com/bghgary/glTF/blob/gh-pages/convert-between-workflows-bjs/js/babylon.pbrUtilities.js

@javagl
Copy link
Owner

javagl commented May 29, 2018

Thanks @naikrovek . Some snippets from that might be helpful, but the particular functions there are mainly intended for the conversion between the "default" glTF PBR mode (Metallic-Roughness) and the Specular-Glossiness model from the extension at https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness . The models are quite similar (they basically contain the same information, in a slightly different representation), so the conversion is a bit simpler than that of creating a PBR model from OBJ.

@naikrovek
Copy link
Author

Ah. I thought the Specular-Glossiness model was the "old" way that materials have been defined in the past before PBR came around. I have more to learn.

@javagl
Copy link
Owner

javagl commented Jul 14, 2018

A very basic extension of the translation from MTL to PBR was done in 2470c78 . It will have to be extended after the extended MTL support is part of a released version of the OBJ library.

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

No branches or pull requests

2 participants