Skip to content

Implement EditorTextureImportPlugin to extend texture importer #105342

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

beicause
Copy link
Contributor

@beicause beicause commented Apr 13, 2025

Partially implements godotengine/godot-proposals#1943
This PR intends to address the situation that many proposals need options to customize texture importer:

Related PRs:

EditorTextureImportPlugin can be used to extend ResourceImporterTexture and modify the process of importing CompressedTexture2D, allowing to load custom file format, add importer options, and change the image when processing.

The design mainly refers to the EditorScenePostImportPlugin.

Example:

@tool
extends EditorTextureImportPlugin

func _load_image(source_file: String) -> Image:
    var image := Image.load_from_file(source_file)
    return image
    
func _get_import_options(path: String, preset_index: int) -> void:
    add_import_option("process/resize", Vector2i(0, 0))
    add_import_option("process/trim_alpha", false)
    add_import_option("process/invert_color", false)

func _pre_process(image: Image) -> Image:
    var resize: Vector2i = get_option_value("process/resize")
    var trim_alpha: bool = get_option_value("process/trim_alpha")
    var invert_color: bool = get_option_value("process/invert_color")

    if resize.x > 0 and resize.y > 0:
        image.resize(resize.x, resize.y, Image.INTERPOLATE_CUBIC)
    
    if trim_alpha:
        var rect := image.get_used_rect();
        var img := image.get_region(rect);
        image.set_data(rect.size.x, rect.size.y, image.has_mipmaps(), image.get_format(), img.get_data());

    if invert_color:
        for i in range(image.get_width()):
            for j in range(image.get_height()):
                var color := image.get_pixel(i, j);
                var inverted := Color(1 - color.r, 1 - color.g, 1 - color.b, color.a);
                image.set_pixel(i, j, inverted);
            
    return image

@beicause beicause force-pushed the plugin-extends-tex-import branch from cf0fbe8 to 7a800a9 Compare April 13, 2025 08:26
@beicause beicause changed the title Implement EditorTextureImportPlugin to extends texture importer Implement EditorTextureImportPlugin to extend texture importer Apr 13, 2025
@beicause beicause force-pushed the plugin-extends-tex-import branch from 7a800a9 to bae898a Compare April 13, 2025 08:27
@BlueCube3310 BlueCube3310 added this to the 4.x milestone Apr 13, 2025
@beicause beicause force-pushed the plugin-extends-tex-import branch from bae898a to e2a90fa Compare April 13, 2025 09:26
@beicause beicause force-pushed the plugin-extends-tex-import branch from e2a90fa to a4ab645 Compare April 13, 2025 10:16
@beicause
Copy link
Contributor Author

The naming of "add_import_option_advanced" is misleading. It made me think that it adds an option to advanced import.

However, for compatibility and consistency, I follow this naming as it's also used in EditorSceneFormatImporter and EditorScenePostImportPlugin.

@beicause
Copy link
Contributor Author

Renamed EditorTextureImportPlugin to EditorTexturePostImportPlugin. For me it seems more similar to PostImportPlugin rather than EditorImportPlugin.

This PR has limitations. If you want to import custom resource that contains ctex, it is still only possible to use EditorImportPlugin and PortableCompressedTexture2D.

Let me know if this design is reasonable, or if there is a better way to extend texture importer.

@beicause beicause marked this pull request as ready for review April 29, 2025 06:57
@beicause beicause requested review from a team as code owners April 29, 2025 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Work in progress
Development

Successfully merging this pull request may close these issues.

2 participants