Save material variation in gltf

Hello,

We are looking for a way to store material variations in one single gltf file, the goal is to be able to change the material of the model after the model has been loaded. From what we have read, it seems to be possible to do that in the gltf format but unfortunately, no gltf exporter includes that feature (We are working with Maya).

The one solution we found would be to use an extra mesh, like a basic plane, which will use the other material so that it is indeed exported in the gltf and then we will be able to play with it in BabylonJS.
We can’t manage the solution manually by creating a server folder with the different material variations in it as it won’t fit with our clients need and will take too much time to set up.

Has any of you already had this issue? What would you advise us to do?

Thanks a lot for your help, Pichou

Hi PichouPichou,

I’m no expert, but I believe I’ve heard @PatrickRyan talk about this as a common issue artists deal with when trying to have multiple materials for the same model. If it’s the same issue, I believe the “standard” solution is exactly what you described: create a tiny primitive hidden somewhere in the model that effectively serves as a “swatch” preserving the desired material so it can be programmatically applied to real geometry when the model is being used. If there are other nuances or options to be considered, Patrick will know them; but in general, as I understand it, the solution you’ve already found is the best and most common way to work around this issue.

1 Like

@PichouPichou, I apologize for the delay in my response. With the fast approaching release of 4.2, I have been heads down in preparations. @syntheticmagus is correct in that there aren’t many options to pack multiple materials into a glTF until the KHR_materials_variants extension is ratified and then the exporters update to support it. It is very close, but I am unsure of the lag in development time for the exporters to start supporting it. You have two options to do this in the mean time:

  • Go with the suggestion of @syntheticmagus that you place a quad in your scene named in such a way that you can search the scene meshes to set visibility of these helper meshes to false. You will need one helper mesh per extra material you want to bring in. The tax that needs to be paid here is that you will end up with extra meshes that need to be managed by the viewer which would be unique to your models. You could combat this by making the meshes small (though not a near 0 scale so that they don’t get culled in export) and possible hide them within your mesh like in between walls of an opaque mesh. Obviously this is a very hacky way to include the materials in your glTF.
  • You could also manually edit your glTF file to include the extension for material variants following the schema of the new extension. This will allow you to add your materials into your glTF file and any viewer that is staying current with in-development extension should be able to handle the import of the extra materials. You can see below that Babylon has an experimental implementation of the new extension, but we have not finished the inspector integration due to the need for ratification which should be very soon. We will import the materials into the scene and then they can be applied to your mesh through code.

Right now, those are the only two ways to get material variants into a glTF file until the extension is ratified and the exporters catch up with the new extension. I wish there were an easier answer to this issue, but you are on the bleeding edge of new features right now. Let me know if you have other questions.

2 Likes

Hi @PatrickRyan and @syntheticmagus

Thanks for your explanations. It feels a bit weird that a feature like that which seems essential is only getting ratified now. But I know how it goes and why it can be hard to achieve this kind critical change for an amazing format like gltf.
It will be awesome to see that going forward and have the feature implemented in exporters! So useful for a lot of applications and for our software Naker!

Thanks for the GitHub link @PatrickRyan, do you know what is the best way to follow the progress of this feature? Both in BabylonJS and in tools like Maya for the exporter.

At Naker we are speaking with the Substance team and this is definitely something we will introduce them if they don’t already know about it!

@PichouPichou, the time it takes for a feature like this is due to the number of contributors that all work on the specification to ensure that they are covering the needs of everyone involved and be somewhat future proof. This can take a lot of trying out ideas to make sure it’s easy to implement and does not cause edge case issues. Going too quickly here can result in the need for breaking changes which can cause big headaches for companies needing to go back and change assets to bring them past breaking changes so it’s always best to be slow and deliberate here.

As far as tracking, if you look at the github page, you can see that the status is complete, and not draft, so the ratification vote may have already taken place. With Adobe being one of the contributors, I am sure that Allegorithmic is aware of the extension and probably have it on their roadmap somewhere since they are now owned by Adobe. As far as following the process of development, I would say to look into the communities surrounding the tools you use and ask those questions on forums or in github repos.

For the Babylon exporters, we are getting through the next release and then are going to look at what updates need to be made as more glTF features come online. I can’t make any announcements yet as we need to have conversations following the 4.2 release about where we will focus our resources for the next release. But if you follow our github repo, we are always very transparent about what we are working on and I doubt it will be long before we are making updates. Hope this helps.

1 Like

I totally agree with you @PatrickRyan, it is way better to make sure te solution will be strong and answer the need in the best way possible. I am more surprised that this specific issue has not been adressed long ago!
But it is coming and this is already awesome! :wink:
By te way the next step would be to manage geometry variation but it will be a huge subject too.

Thanks for all the information,
I wish you a good 4.2 release week :muscle: :raised_hands:

1 Like

@PichouPichou, this will help you for now until there are native exporters. Khronos just released this blog post about material variants. In it is a link to a tool facebook is using to generate material variant glTF files by comparing the meshes of several files. If the meshes are exactly the same with different materials, it will create a glTF file with material variants for you. Hope this helps in the mean time.

3 Likes

Hi Ryan ,i have installed the KHR-variants material addon in blender . I have added variants and tried exporting glb but there are no variants installed before ? How to export glb file with variants material ? thank you
image

@Tran_Son, welcome to the community! Can you share a little more information about your issue? First of all, can you link to the add-on that you are currently using because there is no official material variant add-on that I can find from Blender. The only add-on I could find for material variants on github looks to be in a very early state and lists a version of 0.0.1. If that is the add-on you are using, I don’t know that we can be a lot of help with any difficulties in exporting with this tool as we have no connection to it.

If it’s simply a matter of the file not rendering correctly, it would also be helpful if you can show us what is happening when you try to render in Babylon (even in the sandbox) with some screen captures. It’s very helpful if you can also share with us a sample glTF file that is not rendering correctly so we can debug from there.

1 Like