diff --git a/editor/plugins/sprite_frames_editor_plugin.cpp b/editor/plugins/sprite_frames_editor_plugin.cpp index 48087e3166aa..00a4dbc0f1f0 100644 --- a/editor/plugins/sprite_frames_editor_plugin.cpp +++ b/editor/plugins/sprite_frames_editor_plugin.cpp @@ -503,6 +503,53 @@ void SpriteFramesEditor::_update_show_settings() { toggle_settings_button->set_icon(get_editor_theme_icon(split_sheet_settings_vb->is_visible() ? SNAME("Forward") : SNAME("Back"))); } } +bool SpriteFramesEditor::_matches_background_color(const Color &background_color, const Color &pixel_color) { + if ((background_color.a == 0 && pixel_color.a == 0) || background_color.is_equal_approx(pixel_color)) { + return true; + } + + return pow(background_color.r - pixel_color.r, 2) + pow(background_color.g - pixel_color.g, 2) + pow(background_color.b - pixel_color.b, 2) + pow(background_color.a - pixel_color.a, 2) < 0.04; +} + +Size2i SpriteFramesEditor::_estimate_sprite_sheet_size(const Ref texture) { + Ref image = texture->get_image(); + Size2i size = texture->get_size(); + + Color assumed_background_color = image->get_pixel(0, 0); + Size2i sheet_size = Size2i(0, 0); + + bool previous_line_background = true; + for (int x = 0; x < size.x; x++) { + int y = 0; + while (y < size.y && _matches_background_color(assumed_background_color, image->get_pixel(x, y))) { + y++; + } + bool current_line_background = (y == size.y); + if (previous_line_background && !current_line_background) { + sheet_size.x++; + } + previous_line_background = current_line_background; + } + + previous_line_background = true; + for (int y = 0; y < size.y; y++) { + int x = 0; + while (x < size.x && _matches_background_color(assumed_background_color, image->get_pixel(x, y))) { + x++; + } + bool current_line_background = (x == size.x); + if (previous_line_background && !current_line_background) { + sheet_size.y++; + } + previous_line_background = current_line_background; + } + + if (sheet_size == Size2i(0, 0) || sheet_size == Size2i(1, 1)) { + sheet_size = Size2i(4, 4); + } + + return sheet_size; +} void SpriteFramesEditor::_prepare_sprite_sheet(const String &p_file) { Ref texture = ResourceLoader::load(p_file); @@ -530,10 +577,11 @@ void SpriteFramesEditor::_prepare_sprite_sheet(const String &p_file) { // Different texture, reset to 4x4. dominant_param = PARAM_FRAME_COUNT; updating_split_settings = true; - split_sheet_h->set_value(4); - split_sheet_v->set_value(4); - split_sheet_size_x->set_value(size.x / 4); - split_sheet_size_y->set_value(size.y / 4); + const Size2i split_sheet = _estimate_sprite_sheet_size(texture); + split_sheet_h->set_value(split_sheet.x); + split_sheet_v->set_value(split_sheet.y); + split_sheet_size_x->set_value(size.x / split_sheet.x); + split_sheet_size_y->set_value(size.y / split_sheet.y); split_sheet_sep_x->set_value(0); split_sheet_sep_y->set_value(0); split_sheet_offset_x->set_value(0); diff --git a/editor/plugins/sprite_frames_editor_plugin.h b/editor/plugins/sprite_frames_editor_plugin.h index 9b6aaf98fe0c..aa9d4e4b2512 100644 --- a/editor/plugins/sprite_frames_editor_plugin.h +++ b/editor/plugins/sprite_frames_editor_plugin.h @@ -234,6 +234,8 @@ class SpriteFramesEditor : public HSplitContainer { void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from); void _open_sprite_sheet(); + bool _matches_background_color(const Color &background_color, const Color &pixel_color); + Size2i _estimate_sprite_sheet_size(const Ref texture); void _prepare_sprite_sheet(const String &p_file); int _sheet_preview_position_to_frame_index(const Vector2 &p_position); void _sheet_preview_draw();