diff --git a/scene/main/canvas_item.cpp b/scene/main/canvas_item.cpp index cabba0f2ed2..50cae48e34c 100644 --- a/scene/main/canvas_item.cpp +++ b/scene/main/canvas_item.cpp @@ -34,6 +34,7 @@ #include "scene/2d/canvas_group.h" #include "scene/main/canvas_layer.h" #include "scene/main/window.h" +#include "scene/resources/atlas_texture.h" #include "scene/resources/canvas_item_material.h" #include "scene/resources/font.h" #include "scene/resources/multimesh.h" @@ -851,18 +852,28 @@ void CanvasItem::draw_polygon(const Vector &p_points, const Vectorget_rid() : RID(); + const Ref atlas = p_texture; + if (atlas.is_valid() && atlas->get_atlas().is_valid()) { + const Ref &texture = atlas->get_atlas(); + const Vector2 atlas_size = texture->get_size(); - RenderingServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, p_colors, p_uvs, rid); + const Vector2 remap_min = atlas->get_region().position / atlas_size; + const Vector2 remap_max = atlas->get_region().get_end() / atlas_size; + + PackedVector2Array uvs = p_uvs; + for (Vector2 &p : uvs) { + p.x = Math::remap(p.x, 0, 1, remap_min.x, remap_max.x); + p.y = Math::remap(p.y, 0, 1, remap_min.y, remap_max.y); + } + RenderingServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, p_colors, uvs, texture->get_rid()); + } else { + RID texture_rid = p_texture.is_valid() ? p_texture->get_rid() : RID(); + RenderingServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, p_colors, p_uvs, texture_rid); + } } void CanvasItem::draw_colored_polygon(const Vector &p_points, const Color &p_color, const Vector &p_uvs, Ref p_texture) { - ERR_THREAD_GUARD; - ERR_DRAW_GUARD; - - Vector colors = { p_color }; - RID rid = p_texture.is_valid() ? p_texture->get_rid() : RID(); - RenderingServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, colors, p_uvs, rid); + draw_polygon(p_points, { p_color }, p_uvs, p_texture); } void CanvasItem::draw_mesh(const Ref &p_mesh, const Ref &p_texture, const Transform2D &p_transform, const Color &p_modulate) {