Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GLTF: Preserve the original bytes when extracting a texture while importing #79533

Merged
merged 1 commit into from
Aug 3, 2023

Conversation

aaronfranke
Copy link
Member

Before this PR, the GLTF importer will import images into an Image resource, and then use this to save a PNG file to disk. This means that JPG, WebP, etc images would be converted to PNG, using more disk space than necesssary, and being slower than necessary since it needs to re-encode the images.

As of this PR, when the importer wants to save the images to separate files, the image data in the GLTF will just be directly written to disk, for PNG and JPG always, and for other image formats it will save it into a file when the GLTFDocumentExtension defines get_image_file_extension to indicate what the file extension should be.

To be clear, preserving the original bytes for new image formats is optional (for the extension programmer). If a GLTFDocumentExtension class wants to import images and have Godot save them as PNG, just don't define get_image_file_extension. When this method is not defined, Godot will save an Image resource as a PNG like before.

Some test files I used with JPG/PNG/WebP all are extracted correctly:

Screenshot 2023-07-16 at 12 20 53 AM

@aaronfranke aaronfranke added this to the 4.2 milestone Jul 16, 2023
@aaronfranke aaronfranke requested a review from a team as a code owner July 16, 2023 05:53
@aaronfranke aaronfranke force-pushed the gltf-image-keep-bytes branch from 18ead49 to 4fa0f6d Compare August 1, 2023 20:42
Copy link
Contributor

@lyuma lyuma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems pretty reasonable. I understand a lot of the complexity is because embedded images may have a mime type but do not contain their file extension (such as .jpg, .png, .webp) so it has to ask an extension (or use .jpg / .png built in)

@@ -3078,15 +3078,18 @@ Ref<Image> GLTFDocument::_parse_image_bytes_into_image(Ref<GLTFState> p_state, c
Error err = ext->parse_image_data(p_state, p_bytes, p_mime_type, r_image);
ERR_CONTINUE_MSG(err != OK, "GLTF: Encountered error " + itos(err) + " when parsing image " + itos(p_index) + " in file " + p_state->filename + ". Continuing.");
if (!r_image->is_empty()) {
r_file_extension = ext->get_image_file_extension();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't this need to pass in p_index ? Or is it expected that a given extension only handles exactly one image format.

Copy link
Member Author

@aaronfranke aaronfranke Aug 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code expects that one GLTFDocumentExtension handles importing one kind of image format (can be "multiple formats" with one file extension like Lossy WebP and Lossless WebP). If multiple file extensions are needed, it's not a big deal for a plugin/add-on to supply multiple GLTFDocumentExtension classes. In fact that's probably for the best since it would keep the code of multiple image formats separated. Note that the import code checks each image individually so it may be the case that multiple GLTFDocumentExtensions are used to import one glTF file.

@aaronfranke aaronfranke force-pushed the gltf-image-keep-bytes branch from 4fa0f6d to 2d13a96 Compare August 3, 2023 15:31
@akien-mga akien-mga merged commit 2e59878 into godotengine:master Aug 3, 2023
@akien-mga
Copy link
Member

Thanks!

@aaronfranke aaronfranke deleted the gltf-image-keep-bytes branch August 3, 2023 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants