From f17a18619f8b3b33ca2ceb7a62d9ad38adce6355 Mon Sep 17 00:00:00 2001 From: Variable Date: Fri, 27 Sep 2024 23:29:46 +0500 Subject: [PATCH] Allow clipping to selection during export --- src/Autoload/Export.gd | 9 +++++++++ src/Shaders/SelectionClip.gdshader | 12 ++++++++++++ src/UI/Dialogs/ExportDialog.gd | 6 ++++++ src/UI/Dialogs/ExportDialog.tscn | 7 +++++++ 4 files changed, 34 insertions(+) create mode 100644 src/Shaders/SelectionClip.gdshader diff --git a/src/Autoload/Export.gd b/src/Autoload/Export.gd index f20c95ab2..0b2c5f8db 100644 --- a/src/Autoload/Export.gd +++ b/src/Autoload/Export.gd @@ -52,6 +52,7 @@ var blended_frames := {} var export_json := false var split_layers := false var trim_images := false +var erase_unselected_area := false # Spritesheet options var orientation := Orientation.COLUMNS @@ -288,6 +289,14 @@ func process_animation(project := Global.current_project) -> void: else: var image := Image.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8) image.copy_from(blended_frames[frame]) + if erase_unselected_area and project.has_selection: + var selection_image = project.selection_map.return_cropped_copy(project.size) + var selection_tex = ImageTexture.create_from_image(selection_image) + var clipper = ShaderImageEffect.new() + var clip_shader = preload("res://src/Shaders/SelectionClip.gdshader") + clipper.generate_image( + image, clip_shader, {"selection": selection_tex}, project.size + ) if trim_images: image = image.get_region(image.get_used_rect()) var duration := frame.duration * (1.0 / project.fps) diff --git a/src/Shaders/SelectionClip.gdshader b/src/Shaders/SelectionClip.gdshader new file mode 100644 index 000000000..d1f8cbd87 --- /dev/null +++ b/src/Shaders/SelectionClip.gdshader @@ -0,0 +1,12 @@ +shader_type canvas_item; +render_mode unshaded; + +uniform sampler2D selection : filter_nearest; + +void fragment() { + vec4 selection_color = texture(selection, UV); + // Not using equality here because this alternative was recommended in Godot warning instead. + if (abs(selection_color.a) < 0.00001){ + COLOR = vec4(0.0); + } +} \ No newline at end of file diff --git a/src/UI/Dialogs/ExportDialog.gd b/src/UI/Dialogs/ExportDialog.gd index 9db80b27d..7f952fee7 100644 --- a/src/UI/Dialogs/ExportDialog.gd +++ b/src/UI/Dialogs/ExportDialog.gd @@ -464,6 +464,12 @@ func _on_trim_images_toggled(toggled_on: bool) -> void: set_preview() +func _on_clip_images_selection_toggled(toggled_on: bool) -> void: + Export.erase_unselected_area = toggled_on + Export.process_data() + set_preview() + + func _on_frames_item_selected(id: int) -> void: Export.frame_current_tag = id Export.process_data() diff --git a/src/UI/Dialogs/ExportDialog.tscn b/src/UI/Dialogs/ExportDialog.tscn index e98b6de23..2e2457cf3 100644 --- a/src/UI/Dialogs/ExportDialog.tscn +++ b/src/UI/Dialogs/ExportDialog.tscn @@ -324,6 +324,12 @@ tooltip_text = "Trims the exported images to their visible portion, considering mouse_default_cursor_shape = 2 text = "Trim images" +[node name="ClipSelection" type="CheckBox" parent="VBoxContainer/VSplitContainer/VBoxContainer/AdvancedOptions/GridContainer" groups=["ExportImageOptions"]] +layout_mode = 2 +tooltip_text = "Only shows content that is within the bounds of a selected area." +mouse_default_cursor_shape = 2 +text = "Clip image content to selection" + [node name="PathDialog" type="FileDialog" parent="." groups=["FileDialogs"]] mode = 2 title = "Open a Directory" @@ -379,6 +385,7 @@ size_flags_horizontal = 3 [connection signal="toggled" from="VBoxContainer/VSplitContainer/VBoxContainer/AdvancedOptions/GridContainer/IncludeTagsInFilename" to="." method="_on_include_tags_in_filename_toggled"] [connection signal="toggled" from="VBoxContainer/VSplitContainer/VBoxContainer/AdvancedOptions/GridContainer/MultipleAnimationsDirectories" to="." method="_on_multiple_animations_directories_toggled"] [connection signal="toggled" from="VBoxContainer/VSplitContainer/VBoxContainer/AdvancedOptions/GridContainer/TrimImages" to="." method="_on_trim_images_toggled"] +[connection signal="toggled" from="VBoxContainer/VSplitContainer/VBoxContainer/AdvancedOptions/GridContainer/ClipSelection" to="." method="_on_clip_images_selection_toggled"] [connection signal="canceled" from="PathDialog" to="." method="_on_path_dialog_canceled"] [connection signal="dir_selected" from="PathDialog" to="." method="_on_path_dialog_dir_selected"] [connection signal="confirmed" from="FileExistsAlert" to="." method="_on_file_exists_alert_confirmed"]