Drastically shrink GLTFExported models with jpeg texture option?

We’re using the GLTFExporter to export models to GLB and one of the things we noticed is that even if the textures are loaded as jpegs, the exporter is hardcoded to convert them to PNGs, which can really bloat the output size.

It seems like with a couple small changes to the GLTF Exporter, we can make the texture mime-type an option that could be passed into GLBAsync() like so:

GLTF2Export.GLBAsync(scene, queryParams.filename, {mimetype: 'image/jpeg'}).then((glb) => {...}

stash that in the Exporter like so
this._textureMimetype = options && options.mimetype ? options.mimetype : 'image/png';

then when calling convertMaterial, use the specified mimetype.

line 1508 or so:

return this._glTFMaterialExporter._convertMaterialsToGLTFAsync(exportMaterials, this._textureMimetype, true).then(function () {

This results in a pretty sizeable reduction in texture sizes, and resulting GLB. What’s the best way to help get this merged in?

Adding @bghgary as I guess it needs to be done carefully to prevent some channels requiring alpha to go through this path.

1 Like

That was my assumption as well, but in our use-case we don’t need alpha channels at all and could really do with the size reduction. Would be great if that was a decision up to the caller. I’ve tested the code changes in those two places and it works great.

We’ve talked about this in the past. Like @sebavan says, we just need to be careful about the alpha channel since JPG has no alpha channel. I expect if we add a setting to export JPG, it will only do it if the texture has no alpha channel. We will be happy to accept contributions in this area. :slight_smile:

1 Like

Cool, I’ll try to pull a PR together for this. Massive glb file savings if your texture has no alphas.

1 Like

Maybe consider looking at the following glTF extensions also.

The loader supports both and the exporter doesn’t support either, but I’ll mention them just in case.


Pulled a PR together over here [GLTF Serializer] Allow GLTF Exporter to export supported mimetypes by christaggart · Pull Request #13062 · BabylonJS/Babylon.js · GitHub.

I couldn’t find any tests for the GLTF Serializer to update… do those… exist yet?


Yes. Here are some: Babylon.js/config.json at master · BabylonJS/Babylon.js · GitHub