From 52aee9c325ab44203a3ec3840301143576d6ba2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gilles=20Roudi=C3=A8re?= Date: Mon, 28 Apr 2025 13:34:39 +0200 Subject: [PATCH] Fix rotated/flipped tiles rendering origin --- editor/plugins/tiles/tile_map_layer_editor.cpp | 9 +++++++-- scene/2d/tile_map_layer.cpp | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/editor/plugins/tiles/tile_map_layer_editor.cpp b/editor/plugins/tiles/tile_map_layer_editor.cpp index d823e5caff..64e72ba552 100644 --- a/editor/plugins/tiles/tile_map_layer_editor.cpp +++ b/editor/plugins/tiles/tile_map_layer_editor.cpp @@ -988,19 +988,24 @@ void TileMapLayerEditorTilesPlugin::forward_canvas_draw_over_viewport(Control *p bool transpose = tile_data->get_transpose() ^ bool(E.value.alternative_tile & TileSetAtlasSource::TRANSFORM_TRANSPOSE); if (transpose) { - dest_rect.position = (tile_set->map_to_local(E.key) - Vector2(dest_rect.size.y, dest_rect.size.x) / 2 - tile_offset); + dest_rect.position = (tile_set->map_to_local(E.key) - Vector2(dest_rect.size.y, dest_rect.size.x) / 2); + SWAP(tile_offset.x, tile_offset.y); } else { - dest_rect.position = (tile_set->map_to_local(E.key) - dest_rect.size / 2 - tile_offset); + dest_rect.position = (tile_set->map_to_local(E.key) - dest_rect.size / 2); } if (tile_data->get_flip_h() ^ bool(E.value.alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_H)) { dest_rect.size.x = -dest_rect.size.x; + tile_offset.x = -tile_offset.x; } if (tile_data->get_flip_v() ^ bool(E.value.alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_V)) { dest_rect.size.y = -dest_rect.size.y; + tile_offset.y = -tile_offset.y; } + dest_rect.position -= tile_offset; + // Get the tile modulation. Color modulate = tile_data->get_modulate() * edited_layer->get_modulate_in_tree() * edited_layer->get_self_modulate(); diff --git a/scene/2d/tile_map_layer.cpp b/scene/2d/tile_map_layer.cpp index c5e19916b5..5e2177cd9e 100644 --- a/scene/2d/tile_map_layer.cpp +++ b/scene/2d/tile_map_layer.cpp @@ -2609,19 +2609,24 @@ void TileMapLayer::draw_tile(RID p_canvas_item, const Vector2 &p_position, const bool transpose = tile_data->get_transpose() ^ bool(p_alternative_tile & TileSetAtlasSource::TRANSFORM_TRANSPOSE); if (transpose) { - dest_rect.position = (p_position - Vector2(dest_rect.size.y, dest_rect.size.x) / 2 - tile_offset); + dest_rect.position = (p_position - Vector2(dest_rect.size.y, dest_rect.size.x) / 2); + SWAP(tile_offset.x, tile_offset.y); } else { - dest_rect.position = (p_position - dest_rect.size / 2 - tile_offset); + dest_rect.position = (p_position - dest_rect.size / 2); } if (tile_data->get_flip_h() ^ bool(p_alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_H)) { dest_rect.size.x = -dest_rect.size.x; + tile_offset.x = -tile_offset.x; } if (tile_data->get_flip_v() ^ bool(p_alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_V)) { dest_rect.size.y = -dest_rect.size.y; + tile_offset.y = -tile_offset.y; } + dest_rect.position -= tile_offset; + // Draw the tile. if (p_frame >= 0) { Rect2i source_rect = atlas_source->get_runtime_tile_texture_region(p_atlas_coords, p_frame);