From baf7ee9d15cc4a2a6a1018a1b5e75f474bc3c138 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Mon, 10 Jun 2024 20:33:17 +0200 Subject: [PATCH] Adjust StyleBoxFlat antialiasing to account for 2D stretch scale This prevents the antialiasing feather from becoming too wide at viewport sizes higher than the default, which can lead to blurry visuals. This is adjusted to account for the 2D scale factor returned on the root Window, so it takes both the `canvas_items` scaling and `content_scale_factor` into account. --- scene/resources/style_box_flat.cpp | 37 +++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/scene/resources/style_box_flat.cpp b/scene/resources/style_box_flat.cpp index 52d02e92cb45..30c8973db0fe 100644 --- a/scene/resources/style_box_flat.cpp +++ b/scene/resources/style_box_flat.cpp @@ -30,6 +30,8 @@ #include "style_box_flat.h" +#include "scene/main/scene_tree.h" +#include "scene/main/window.h" #include "servers/rendering_server.h" float StyleBoxFlat::get_style_margin(Side p_side) const { @@ -415,10 +417,28 @@ void StyleBoxFlat::draw(RID p_canvas_item, const Rect2 &p_rect) const { Rect2 infill_rect = style_rect.grow_individual(-adapted_border[SIDE_LEFT], -adapted_border[SIDE_TOP], -adapted_border[SIDE_RIGHT], -adapted_border[SIDE_BOTTOM]); Rect2 border_style_rect = style_rect; + + float aa_size_scaled = 1.0f; + if (aa_on) { + float scale_factor = 1.0f; + const SceneTree *tree = Object::cast_to(OS::get_singleton()->get_main_loop()); + if (tree) { + const Window *window = tree->get_root(); + const Vector2 stretch_scale = window->get_stretch_transform().get_scale(); + scale_factor = MIN(stretch_scale.x, stretch_scale.y); + } + + // Adjust AA feather size to account for the 2D scale factor, so that + // antialiasing doesn't become blurry at viewport resolutions higher + // than the default when using the `canvas_items` stretch mode + // (or when using `content_scale_factor` values different than `1.0`). + aa_size_scaled = aa_size / scale_factor; + } + if (aa_on) { for (int i = 0; i < 4; i++) { if (border_width[i] > 0) { - border_style_rect = border_style_rect.grow_side((Side)i, -aa_size); + border_style_rect = border_style_rect.grow_side((Side)i, -aa_size_scaled); } } } @@ -464,26 +484,27 @@ void StyleBoxFlat::draw(RID p_canvas_item, const Rect2 &p_rect) const { real_t aa_border_width_half[4]; real_t aa_fill_width[4]; real_t aa_fill_width_half[4]; + if (draw_border) { for (int i = 0; i < 4; i++) { if (border_width[i] > 0) { - aa_border_width[i] = aa_size; - aa_border_width_half[i] = aa_size / 2; + aa_border_width[i] = aa_size_scaled; + aa_border_width_half[i] = aa_size_scaled * 0.5; aa_fill_width[i] = 0; aa_fill_width_half[i] = 0; } else { aa_border_width[i] = 0; aa_border_width_half[i] = 0; - aa_fill_width[i] = aa_size; - aa_fill_width_half[i] = aa_size / 2; + aa_fill_width[i] = aa_size_scaled; + aa_fill_width_half[i] = aa_size_scaled * 0.5; } } } else { for (int i = 0; i < 4; i++) { aa_border_width[i] = 0; aa_border_width_half[i] = 0; - aa_fill_width[i] = aa_size; - aa_fill_width_half[i] = aa_size / 2; + aa_fill_width[i] = aa_size_scaled; + aa_fill_width_half[i] = aa_size_scaled * 0.5; } } @@ -536,7 +557,7 @@ void StyleBoxFlat::draw(RID p_canvas_item, const Rect2 &p_rect) const { } // Compute UV coordinates. - Rect2 uv_rect = style_rect.grow(aa_on ? aa_size : 0); + Rect2 uv_rect = style_rect.grow(aa_on ? aa_size_scaled : 0); uvs.resize(verts.size()); for (int i = 0; i < verts.size(); i++) { uvs.write[i].x = (verts[i].x - uv_rect.position.x) / uv_rect.size.width;