GLTF Serializer - is there a way to remove unused dynamic textures on export

When we export a scene via GLTF2Export.GLTFAsync, dynamic textures that are not in use by any exported nodes show up in the exported files (referenced in the GLTF and listed as png’s). Note: they might have been referenced by nodes that were filtered out using the shouldExportNode options filter.

Is there a way to filter unneeded dynamic textures out of the exported GLTF?

Hi @jeffb, welcome to the Forum!

Currently, the exporter exports all materials in scene, pulling all referenced textures with them, then links the material to each exported mesh, but it should be fairly straightforward to add a method that allows the user to exclude materials from the scene similar to: IExportOptions - Babylon.js Documentation where we export the scene contents by default, and let the user manually define which meshes should be ignored.

Otherwise the alternative is to dispose of the materials with textures that you don’t need exported, which isn’t super ideal if you want to use them later.

Would this solution work for you?

It would be awesome if it also accepted an array of meshes, if you don’t want the entire scene,

[myMeshA, myMeshB]

Or if you want all meshes, but not lights, cameras, etc

scene.meshes

And then only serialize child tree and connected materials, textures etc

It would be awesome if it also accepted an array of meshes, if you don’t want the entire scene,
[myMeshA, myMeshB]

That sounds already pretty doable with the current function pointer based implementation:

let meshesToExport = [myMeshA, myMeshB]
BABYLON.GLTF2Export.GLBAsync(scene, assetName, {shouldExportNode: (node) =>{
     return meshesToExport.indexOf(node) != -1;
});

Or if you want all meshes, but not lights, cameras, etc

Ah! now thats something to consider. Perhaps adding a collection of shouldExportX functions for each type of element in scene?

And then only serialize child tree and connected materials, textures etc

I’m a bit on the fence about this one as well, one can modify the sample to do something like this:

let meshesToExport = [myMeshA, myMeshB]
BABYLON.GLTF2Export.GLBAsync(scene, assetName, {shouldExportNode: (node) =>{
     return meshesToExport.some( (mesh) => { mesh === node || node.isDescendantOf(mesh)}});
});

But that’s not super efficient…

Having a need of exporting select meshes, i found myself hacking away as i’ve had no time to look at the source yet.

final result was this process;
Export to babylon data string
Create new empty scene & import babylon data string
Export new scene to gltf or glb :joy:

so for me at least, the ability to export an array of meshes & their connected materials & textures would be great :sweat_smile:
Either way, it’s no problem if you guys don’t feel it’s worth including in source, i’ll get around to a local fix eventually
Sorry for hi-jacking the thread :slight_smile:

those snippets should definitely give you what you need regarding mesh selection at export, no need to go through the scene serializer if you can avoid it. The materals/texture removal will need some additional feature work, but I don’t quite have the time to take that on right now.

But I definitely agree a couple switches like “IncludeChildren” and “IgnoreUnused” to add to the export options would be really useful