Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for AtlasTexture in draw_polygon() #91724

Merged
merged 1 commit into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions scene/main/canvas_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -851,18 +852,28 @@ void CanvasItem::draw_polygon(const Vector<Point2> &p_points, const Vector<Color
ERR_THREAD_GUARD;
ERR_DRAW_GUARD;

RID rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
const Ref<AtlasTexture> atlas = p_texture;
if (atlas.is_valid() && atlas->get_atlas().is_valid()) {
const Ref<Texture2D> &texture = atlas->get_atlas();
const Vector2 atlas_size = texture->get_size();

const Vector2 remap_min = atlas->get_region().position / atlas_size;
const Vector2 remap_max = atlas->get_region().get_end() / atlas_size;

RenderingServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, p_colors, p_uvs, rid);
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<Point2> &p_points, const Color &p_color, const Vector<Point2> &p_uvs, Ref<Texture2D> p_texture) {
ERR_THREAD_GUARD;
ERR_DRAW_GUARD;

Vector<Color> 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<Mesh> &p_mesh, const Ref<Texture2D> &p_texture, const Transform2D &p_transform, const Color &p_modulate) {
Expand Down
Loading