Texture.serialize, hardcoded image/png for all mime types

Version: 7.51.1 and git master
OS: any
Browser: any
Engine: any

Desc

In texture.serialize, if the texture contains _buffer as Uint8Array, commonly if loaded from a glb model, serializing texture will result in base64 url in image/png type.

But in gltf spec, texture can be in many types, including png, jpeg, webp, and possibily avif.

Texture has a prop named mime type that can be directly loaded from model, and since image formats supported by browser is limited, it can be detected from first few bytes of the buffer.

1 Like

I think it’s a known issue with the glTF serializer, cc @alexchuber and @srzerbetto.

1 Like

Thanks for the report! Yes, this is a known issue that I plan to investigate soon. I recall seeing your previous thread about this :slight_smile:

(Sidenote, our serialize methods like the one in Texture are for our .babylon exporter, not .gltf)

Hmmm, my previous threads are about gltf exporting, it changes name and content of the texture

1 Like

Here’s a fix to address this case:

Texture roundtripping needs more work beyond the scope of this PR. So far, we don’t have a reliable way to preserve the original texture format-- Texture.mimeType is only a hint. While it’s true that we could read the first few bytes of the image data to get the format, there’s some extra complexity to consider. For example, if we parsed the data at load time, we’d incur extra computation for every loaded texture in preparation for something (an export) that may never happen. We also can’t parse the buffer at export time, as we don’t always have it.

2 Likes

What is currently the best way to load a GLB with webp textures and export it with webp textures?

We’d need to add EXT_textures_webp support to the glTF exporter to do it correctly, or the best way.

We already preserve imported webp images on export (except in Safari and maybe Node). However, because we don’t add the extension to the glTF, it’s technically an invalid glTF. :confused: That said, I’m still able to load the invalid glTF in Babylon and Blender. They must be forgiving about image/webp mime types used without EXT_texture_webp.

Let me add this to the exporter, as it’d be good to have while we work on KHR_texture_basisu encoding support.

1 Like

Good news!
Will it be possible in this case to load a GLB with PNG textures and export it with WEBP textures?

Not yet-- at least, not without some manual manipulation-- but I’m certainly open to the idea. That’d make a good export optimization option :slight_smile:

1 Like

Here is how it done in GLTF-Transform - glTF-Transform/packages/functions/src/texture-compress.ts at main Ā· donmccurdy/glTF-Transform Ā· GitHub
My Sandbox implementation - https://glb.babylonpress.org/

1 Like