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

Fix empty region in AtlasTexture #94365

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion doc/classes/AtlasTexture.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
The margin around the [member region]. Useful for small adjustments. If the [member Rect2.size] of this property ("w" and "h" in the editor) is set, the drawn texture is resized to fit within the margin.
</member>
<member name="region" type="Rect2" setter="set_region" getter="get_region" default="Rect2(0, 0, 0, 0)">
The region used to draw the [member atlas].
The region used to draw the [member atlas]. If either dimension of the region's size is [code]0[/code], the value from [member atlas] size will be used for that axis instead.
</member>
<member name="resource_local_to_scene" type="bool" setter="set_local_to_scene" getter="is_local_to_scene" overrides="Resource" default="false" />
</members>
Expand Down
48 changes: 27 additions & 21 deletions scene/resources/atlas_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ bool AtlasTexture::has_filter_clip() const {
return filter_clip;
}

Rect2 AtlasTexture::_get_region_rect() const {
Rect2 rc = region;
if (atlas.is_valid()) {
if (rc.size.width == 0) {
rc.size.width = atlas->get_width();
}
if (rc.size.height == 0) {
rc.size.height = atlas->get_height();
}
}
return rc;
}

void AtlasTexture::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_atlas", "atlas"), &AtlasTexture::set_atlas);
ClassDB::bind_method(D_METHOD("get_atlas"), &AtlasTexture::get_atlas);
Expand All @@ -142,25 +155,15 @@ void AtlasTexture::_bind_methods() {
}

void AtlasTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
if (!atlas.is_valid()) {
if (atlas.is_null()) {
return;
}

Rect2 rc = region;

if (rc.size.width == 0) {
rc.size.width = atlas->get_width();
}

if (rc.size.height == 0) {
rc.size.height = atlas->get_height();
}

const Rect2 rc = _get_region_rect();
atlas->draw_rect_region(p_canvas_item, Rect2(p_pos + margin.position, rc.size), rc, p_modulate, p_transpose, filter_clip);
}

void AtlasTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
if (!atlas.is_valid()) {
if (atlas.is_null()) {
return;
}

Expand All @@ -174,8 +177,8 @@ void AtlasTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile
}

void AtlasTexture::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, bool p_clip_uv) const {
//this might not necessarily work well if using a rect, needs to be fixed properly
if (!atlas.is_valid()) {
// This might not necessarily work well if using a rect, needs to be fixed properly.
if (atlas.is_null()) {
return;
}

Expand All @@ -195,10 +198,13 @@ bool AtlasTexture::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect,
if (src.size == Size2()) {
src.size = region.size;
}
if (src.size == Size2() && atlas.is_valid()) {
src.size = atlas->get_size();
}
Vector2 scale = p_rect.size / src.size;

src.position += (region.position - margin.position);
Rect2 src_clipped = region.intersection(src);
Rect2 src_clipped = _get_region_rect().intersection(src);
if (src_clipped.size == Size2()) {
return false;
}
Expand All @@ -217,14 +223,14 @@ bool AtlasTexture::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect,
}

bool AtlasTexture::is_pixel_opaque(int p_x, int p_y) const {
if (!atlas.is_valid()) {
if (atlas.is_null()) {
return true;
}

int x = p_x + region.position.x - margin.position.x;
int y = p_y + region.position.y - margin.position.y;

// margin edge may outside of atlas
// Margin edge may outside of atlas.
if (x < 0 || x >= atlas->get_width()) {
return false;
}
Expand All @@ -236,16 +242,16 @@ bool AtlasTexture::is_pixel_opaque(int p_x, int p_y) const {
}

Ref<Image> AtlasTexture::get_image() const {
if (atlas.is_null() || region.size.x <= 0 || region.size.y <= 0) {
if (atlas.is_null()) {
return Ref<Image>();
}

Ref<Image> atlas_image = atlas->get_image();
const Ref<Image> &atlas_image = atlas->get_image();
if (atlas_image.is_null()) {
return Ref<Image>();
}

return atlas_image->get_region(region);
return atlas_image->get_region(_get_region_rect());
}

AtlasTexture::AtlasTexture() {}
2 changes: 2 additions & 0 deletions scene/resources/atlas_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class AtlasTexture : public Texture2D {
GDCLASS(AtlasTexture, Texture2D);
RES_BASE_EXTENSION("atlastex");

Rect2 _get_region_rect() const;

protected:
Ref<Texture2D> atlas;
Rect2 region;
Expand Down
Loading