Skip to content

Commit 4c5a495

Browse files
committed
Round Atlastexture size
1 parent 24d7451 commit 4c5a495

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

doc/classes/AtlasTexture.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
</member>
2323
<member name="region" type="Rect2" setter="set_region" getter="get_region" default="Rect2(0, 0, 0, 0)">
2424
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.
25+
[b]Note:[/b] The image size is always an integer, so the actual region size is rounded down.
2526
</member>
2627
<member name="resource_local_to_scene" type="bool" setter="set_local_to_scene" getter="is_local_to_scene" overrides="Resource" default="false" />
2728
</members>

scene/resources/atlas_texture.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,24 @@
3131
#include "atlas_texture.h"
3232

3333
int AtlasTexture::get_width() const {
34-
if (region.size.width == 0) {
34+
if (rounded_region.size.width == 0) {
3535
if (atlas.is_valid()) {
3636
return atlas->get_width();
3737
}
3838
return 1;
3939
} else {
40-
return region.size.width + margin.size.width;
40+
return rounded_region.size.width + margin.size.width;
4141
}
4242
}
4343

4444
int AtlasTexture::get_height() const {
45-
if (region.size.height == 0) {
45+
if (rounded_region.size.height == 0) {
4646
if (atlas.is_valid()) {
4747
return atlas->get_height();
4848
}
4949
return 1;
5050
} else {
51-
return region.size.height + margin.size.height;
51+
return rounded_region.size.height + margin.size.height;
5252
}
5353
}
5454

@@ -94,6 +94,7 @@ void AtlasTexture::set_region(const Rect2 &p_region) {
9494
return;
9595
}
9696
region = p_region;
97+
rounded_region = Rect2(p_region.position, p_region.size.floor());
9798
emit_changed();
9899
}
99100

@@ -123,7 +124,7 @@ bool AtlasTexture::has_filter_clip() const {
123124
}
124125

125126
Rect2 AtlasTexture::_get_region_rect() const {
126-
Rect2 rc = region;
127+
Rect2 rc = rounded_region;
127128
if (atlas.is_valid()) {
128129
if (rc.size.width == 0) {
129130
rc.size.width = atlas->get_width();
@@ -196,14 +197,14 @@ bool AtlasTexture::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect,
196197

197198
Rect2 src = p_src_rect;
198199
if (src.size == Size2()) {
199-
src.size = region.size;
200+
src.size = rounded_region.size;
200201
}
201202
if (src.size == Size2() && atlas.is_valid()) {
202203
src.size = atlas->get_size();
203204
}
204205
Vector2 scale = p_rect.size / src.size;
205206

206-
src.position += (region.position - margin.position);
207+
src.position += (rounded_region.position - margin.position);
207208
Rect2 src_clipped = _get_region_rect().intersection(src);
208209
if (src_clipped.size == Size2()) {
209210
return false;
@@ -227,8 +228,8 @@ bool AtlasTexture::is_pixel_opaque(int p_x, int p_y) const {
227228
return true;
228229
}
229230

230-
int x = p_x + region.position.x - margin.position.x;
231-
int y = p_y + region.position.y - margin.position.y;
231+
int x = p_x + rounded_region.position.x - margin.position.x;
232+
int y = p_y + rounded_region.position.y - margin.position.y;
232233

233234
// Margin edge may outside of atlas.
234235
if (x < 0 || x >= atlas->get_width()) {

scene/resources/atlas_texture.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class AtlasTexture : public Texture2D {
4141

4242
protected:
4343
Ref<Texture2D> atlas;
44-
Rect2 region;
44+
Rect2 region; // Only for property value.
45+
Rect2 rounded_region; // Region with rounded size (image size is always integer).
4546
Rect2 margin;
4647
bool filter_clip = false;
4748

0 commit comments

Comments
 (0)