|
34 | 34 | #include "scene/2d/canvas_group.h" |
35 | 35 | #include "scene/main/canvas_layer.h" |
36 | 36 | #include "scene/main/window.h" |
| 37 | +#include "scene/resources/atlas_texture.h" |
37 | 38 | #include "scene/resources/canvas_item_material.h" |
38 | 39 | #include "scene/resources/font.h" |
39 | 40 | #include "scene/resources/multimesh.h" |
@@ -850,18 +851,28 @@ void CanvasItem::draw_polygon(const Vector<Point2> &p_points, const Vector<Color |
850 | 851 | ERR_THREAD_GUARD; |
851 | 852 | ERR_DRAW_GUARD; |
852 | 853 |
|
853 | | - RID rid = p_texture.is_valid() ? p_texture->get_rid() : RID(); |
| 854 | + const Ref<AtlasTexture> atlas = p_texture; |
| 855 | + if (atlas.is_valid() && atlas->get_atlas().is_valid()) { |
| 856 | + const Ref<Texture2D> &texture = atlas->get_atlas(); |
| 857 | + const Vector2 atlas_size = texture->get_size(); |
| 858 | + |
| 859 | + const Vector2 remap_min = atlas->get_region().position / atlas_size; |
| 860 | + const Vector2 remap_max = atlas->get_region().get_end() / atlas_size; |
854 | 861 |
|
855 | | - RenderingServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, p_colors, p_uvs, rid); |
| 862 | + PackedVector2Array uvs = p_uvs; |
| 863 | + for (Vector2 &p : uvs) { |
| 864 | + p.x = Math::remap(p.x, 0, 1, remap_min.x, remap_max.x); |
| 865 | + p.y = Math::remap(p.y, 0, 1, remap_min.y, remap_max.y); |
| 866 | + } |
| 867 | + RenderingServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, p_colors, uvs, texture->get_rid()); |
| 868 | + } else { |
| 869 | + RID texture_rid = p_texture.is_valid() ? p_texture->get_rid() : RID(); |
| 870 | + RenderingServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, p_colors, p_uvs, texture_rid); |
| 871 | + } |
856 | 872 | } |
857 | 873 |
|
858 | 874 | void CanvasItem::draw_colored_polygon(const Vector<Point2> &p_points, const Color &p_color, const Vector<Point2> &p_uvs, Ref<Texture2D> p_texture) { |
859 | | - ERR_THREAD_GUARD; |
860 | | - ERR_DRAW_GUARD; |
861 | | - |
862 | | - Vector<Color> colors = { p_color }; |
863 | | - RID rid = p_texture.is_valid() ? p_texture->get_rid() : RID(); |
864 | | - RenderingServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, colors, p_uvs, rid); |
| 875 | + draw_polygon(p_points, { p_color }, p_uvs, p_texture); |
865 | 876 | } |
866 | 877 |
|
867 | 878 | void CanvasItem::draw_mesh(const Ref<Mesh> &p_mesh, const Ref<Texture2D> &p_texture, const Transform2D &p_transform, const Color &p_modulate) { |
|
0 commit comments