Texture.toJSON not working with CompressedTexture

Looks like the serializeImage function cannot handle CompressedTexture instances.

Is there any solution for this issue?


How are you creating this texture? It’s worth noting that compressed textures created from KTX2 and Basis files have been transcoded to a format compatible with the current device’s GPU. That is not reversible, so serializing it to JSON will give you a file that will only work on certain devices and GPUs.

If you are OK with all of that, but CompressedTexture serialization is not working as expected under those requirements, it may be worth filing an issue on GitHub. Also see: Support of Compressed Textures in core · Issue #13911 · mrdoob/three.js · GitHub

I see.

Is there an option to fallback to PNG or something?


The toJSON() method is not capable of rendering a compressed texture to a PNG, that’s not a trivial conversion. It would probably be better to do that conversion first (by rendering the image to a new WebGL canvas, then saving to PNG) before trying to serialize the scene.

That might be a solution.

Do you happen to know if there’s a way to utilize the createImageBitmap function on the mipmaps[0] data or something like that?

There is no built-in JavaScript API that understands GPU texture formats — you’ll have to rely on hardware (via a WebGL context) for that. If you happen to know what GPU format this texture is using, you might be able to find an external JS library that can decode it. E.g. for DDS textures, try decode-dxt - npm.

Got it,

Appreciate your help @donmccurdy.