Skip to content

Commit

Permalink
Merge pull request #74814 from kleonc/tilemap-fix-rendering-odd-sized…
Browse files Browse the repository at this point in the history
…-tiles

`TileMap` Fix rendering odd-sized tiles
  • Loading branch information
YuriSizov authored Apr 18, 2023
2 parents 9882af4 + c49a7fe commit 9e6b680
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions editor/plugins/tiles/tile_atlas_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ void TileAtlasView::_draw_base_tiles() {
for (int frame = 0; frame < tile_set_atlas_source->get_tile_animation_frames_count(atlas_coords); frame++) {
// Update the y to max value.
Rect2i base_frame_rect = tile_set_atlas_source->get_tile_texture_region(atlas_coords, frame);
Vector2i offset_pos = base_frame_rect.get_center() + tile_set_atlas_source->get_tile_data(atlas_coords, 0)->get_texture_origin();
Vector2 offset_pos = Rect2(base_frame_rect).get_center() + Vector2(tile_set_atlas_source->get_tile_data(atlas_coords, 0)->get_texture_origin());

// Draw the tile.
TileMap::draw_tile(base_tiles_draw->get_canvas_item(), offset_pos, tile_set, source_id, atlas_coords, 0, frame);
Expand Down Expand Up @@ -331,7 +331,7 @@ void TileAtlasView::_draw_base_tiles_shape_grid() {
}
Rect2i texture_region = tile_set_atlas_source->get_tile_texture_region(tile_id, frame);
Transform2D tile_xform;
tile_xform.set_origin(texture_region.get_center() + in_tile_base_offset);
tile_xform.set_origin(Rect2(texture_region).get_center() + in_tile_base_offset);
tile_xform.set_scale(tile_shape_size);
tile_set->draw_tile_shape(base_tiles_shape_grid, tile_xform, color);
}
Expand Down
16 changes: 8 additions & 8 deletions scene/2d/tile_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ void TileMap::_update_dirty_quadrants() {
q->self()->local_to_map.clear();
for (const Vector2i &E : q->self()->cells) {
Vector2i pk = E;
Vector2i pk_local_coords = map_to_local(pk);
Vector2 pk_local_coords = map_to_local(pk);
q->self()->map_to_local[pk] = pk_local_coords;
q->self()->local_to_map[pk_local_coords] = pk;
}
Expand Down Expand Up @@ -1092,7 +1092,7 @@ void TileMap::_rendering_notification(int p_what) {
TileMapQuadrant &q = E_quadrant.value;

// Update occluders transform.
for (const KeyValue<Vector2i, Vector2i> &E_cell : q.local_to_map) {
for (const KeyValue<Vector2, Vector2i> &E_cell : q.local_to_map) {
Transform2D xform;
xform.set_origin(E_cell.key);
for (const KeyValue<Vector2i, RID> &kv : q.occluders) {
Expand Down Expand Up @@ -1250,7 +1250,7 @@ void TileMap::_rendering_update_dirty_quadrants(SelfList<TileMapQuadrant>::List
RID prev_ci;

// Iterate over the cells of the quadrant.
for (const KeyValue<Vector2i, Vector2i> &E_cell : q.local_to_map) {
for (const KeyValue<Vector2, Vector2i> &E_cell : q.local_to_map) {
TileMapCell c = get_cell(q.layer, E_cell.value, true);

TileSetSource *source;
Expand Down Expand Up @@ -1348,13 +1348,13 @@ void TileMap::_rendering_update_dirty_quadrants(SelfList<TileMapQuadrant>::List

for (TileMapLayer &layer : layers) {
// Sort the quadrants coords per local coordinates.
RBMap<Vector2i, Vector2i, TileMapQuadrant::CoordsWorldComparator> local_to_map;
RBMap<Vector2, Vector2i, TileMapQuadrant::CoordsWorldComparator> local_to_map;
for (const KeyValue<Vector2i, TileMapQuadrant> &E : layer.quadrant_map) {
local_to_map[map_to_local(E.key)] = E.key;
}

// Sort the quadrants.
for (const KeyValue<Vector2i, Vector2i> &E : local_to_map) {
for (const KeyValue<Vector2, Vector2i> &E : local_to_map) {
TileMapQuadrant &q = layer.quadrant_map[E.value];
for (const RID &ci : q.canvas_items) {
RS::get_singleton()->canvas_item_set_draw_index(ci, index++);
Expand Down Expand Up @@ -1436,7 +1436,7 @@ void TileMap::_rendering_draw_quadrant_debug(TileMapQuadrant *p_quadrant) {
}
}

void TileMap::draw_tile(RID p_canvas_item, const Vector2i &p_position, const Ref<TileSet> p_tile_set, int p_atlas_source_id, const Vector2i &p_atlas_coords, int p_alternative_tile, int p_frame, Color p_modulation, const TileData *p_tile_data_override) {
void TileMap::draw_tile(RID p_canvas_item, const Vector2 &p_position, const Ref<TileSet> p_tile_set, int p_atlas_source_id, const Vector2i &p_atlas_coords, int p_alternative_tile, int p_frame, Color p_modulation, const TileData *p_tile_data_override) {
ERR_FAIL_COND(!p_tile_set.is_valid());
ERR_FAIL_COND(!p_tile_set->has_source(p_atlas_source_id));
ERR_FAIL_COND(!p_tile_set->get_source(p_atlas_source_id)->has_tile(p_atlas_coords));
Expand Down Expand Up @@ -1468,7 +1468,7 @@ void TileMap::draw_tile(RID p_canvas_item, const Vector2i &p_position, const Ref
Color modulate = tile_data->get_modulate() * p_modulation;

// Compute the offset.
Vector2i tile_offset = tile_data->get_texture_origin();
Vector2 tile_offset = tile_data->get_texture_origin();

// Get destination rect.
Rect2 dest_rect;
Expand Down Expand Up @@ -3059,7 +3059,7 @@ void TileMap::_build_runtime_update_tile_data(SelfList<TileMapQuadrant>::List &r
while (q_list_element) {
TileMapQuadrant &q = *q_list_element->self();
// Iterate over the cells of the quadrant.
for (const KeyValue<Vector2i, Vector2i> &E_cell : q.local_to_map) {
for (const KeyValue<Vector2, Vector2i> &E_cell : q.local_to_map) {
TileMapCell c = get_cell(q.layer, E_cell.value, true);

TileSetSource *source;
Expand Down
8 changes: 4 additions & 4 deletions scene/2d/tile_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class TileSetAtlasSource;

struct TileMapQuadrant {
struct CoordsWorldComparator {
_ALWAYS_INLINE_ bool operator()(const Vector2i &p_a, const Vector2i &p_b) const {
_ALWAYS_INLINE_ bool operator()(const Vector2 &p_a, const Vector2 &p_b) const {
// We sort the cells by their local coords, as it is needed by rendering.
if (p_a.y == p_b.y) {
return p_a.x > p_b.x;
Expand All @@ -60,8 +60,8 @@ struct TileMapQuadrant {
RBSet<Vector2i> cells;
// We need those two maps to sort by local position for rendering
// This is kind of workaround, it would be better to sort the cells directly in the "cells" set instead.
RBMap<Vector2i, Vector2i> map_to_local;
RBMap<Vector2i, Vector2i, CoordsWorldComparator> local_to_map;
RBMap<Vector2i, Vector2> map_to_local;
RBMap<Vector2, Vector2i, CoordsWorldComparator> local_to_map;

// Debug.
RID debug_canvas_item;
Expand Down Expand Up @@ -311,7 +311,7 @@ class TileMap : public Node2D {
void set_quadrant_size(int p_size);
int get_quadrant_size() const;

static void draw_tile(RID p_canvas_item, const Vector2i &p_position, const Ref<TileSet> p_tile_set, int p_atlas_source_id, const Vector2i &p_atlas_coords, int p_alternative_tile, int p_frame = -1, Color p_modulation = Color(1.0, 1.0, 1.0, 1.0), const TileData *p_tile_data_override = nullptr);
static void draw_tile(RID p_canvas_item, const Vector2 &p_position, const Ref<TileSet> p_tile_set, int p_atlas_source_id, const Vector2i &p_atlas_coords, int p_alternative_tile, int p_frame = -1, Color p_modulation = Color(1.0, 1.0, 1.0, 1.0), const TileData *p_tile_data_override = nullptr);

// Layers management.
int get_layers_count() const;
Expand Down

0 comments on commit 9e6b680

Please sign in to comment.