GLTF: Allow specifying export image format including from extensions #79314
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds support for specifying the image format when exporting a GLTF file from Godot. This is similar to #76895, but for export instead of import. The base GLTFDocument code supports "None", "PNG", and "JPEG" image formats. GLTFDocumentExtension classes can add support for new image formats, and the WebP extension class adds support for "Lossless WebP" and "Lossy WebP" formats (friendly names intended to be exposed to the UI later).
Four new methods have been added to GLTFDocumentExtension, a very quick summary:
_get_saveable_image_formats
: An extension can return an array of user-friendly image formats to use for saving/exporting. If the image format is set to one of these, this extension will be used for image export._serialize_image_to_bytes
: When exporting with embedded textures, write to a buffer._save_image_at_path
: When exporting with separate textures, write to a file._serialize_texture_json
: For setting the texture JSON data that will be present in the GLTF textures array.Also, this PR means that the export code is no longer hard-coded for PNG.
Q&A:
Q: Why are you adding variables to GLTFDocument? Isn't it supposed to be stateless?
A: GLTFDocument is stateless in terms of not containing any scene data. However, the image format settings used for export is not in the Godot scene, nor in the GLTF file, the settings just change what the Godot scene data is translated into. The export settings are separate from the scene data in GLTFState in the same way that the
.import
file is separate from the data in the.gltf
. From a practical perspective, this also makes it possible to use one GLTFDocument to process multiple scenes with the same settings.Q: What about exporting multiple texture formats, like WebP + PNG/JPG fallback?
A: The whole point of using WebP is to have a reduced file size, so in order for a fallback to make any sense, the combination would need to be smaller in file size than just storing the full quality as PNG/JPG. This means that the PNG/JPG would need to be reduced in resolution or quality. Doing this automatically feels like too much magic, and would be a lot of code, but we can easily add this in the future if we have a good idea of how it will work.
Q: What about preserving the original file bytes if available? Like a
.jpg
file when exporting.jpg
keeps its exact data instead of being recompressed, losing quality and gaining artifacts.A: Lyuma suggested this, it would be a good improvement to make, after this PR is merged.
Q: What about an export settings menu to make adjusting this setting user-friendly?
A: Yes, I want that. This PR came as a result of me working on an export settings menu, and splitting off the code that allows configuring the export image setting in GLTFDocument itself.