Skip to content

Commit

Permalink
Add margins to the left and top edges of the atlas. (#8657)
Browse files Browse the repository at this point in the history
* add margin from top and left size of the atlas

* fix tests
  • Loading branch information
AGulev committed Mar 11, 2024
1 parent 6fd84fb commit f4db7d9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void testAtlasGeneratorMaxRectMargin() throws Exception {
TextureSetResult result = TextureSetGenerator.generate(images, imageTrimModes, ids, iterator, 5, 0, 0, true, false, null, 0, 0);
BufferedImage image = result.images.get(0);
assertThat(image.getWidth(), is(32));
assertThat(image.getHeight(), is(32));
assertThat(image.getHeight(), is(64));

for (UVTransform uv : result.uvTransforms)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,36 +137,39 @@ public void testBasic4() {

@Test
public void testBasicMargin1() {
int size = 16;
List<TextureSetLayout.Rect> rectangles
= Arrays.asList(rect("0", 0, 16, 16),
rect("1", 1, 16, 16),
rect("2", 2, 16, 16),
rect("3", 3, 16, 16));
= Arrays.asList(rect("0", 0, size, size),
rect("1", 1, size, size),
rect("2", 2, size, size),
rect("3", 3, size, size));

Layout layout = packedLayout(2, rectangles).get(0);
int margin = 2;
Layout layout = packedLayout(margin, rectangles).get(0);
assertThat(layout.getWidth(), is(64));
assertThat(layout.getHeight(), is(64));
assertRect(layout, 0, "0", 0, 0, 0);
assertRect(layout, 1, "1", 1, 0, (16 + 2));
assertRect(layout, 2, "2", 2, (16 + 2), 0);
assertRect(layout, 3, "3", 3, 0, (16 + 2) * 2);
assertRect(layout, 0, "0", 0, margin, margin);
assertRect(layout, 1, "1", 1, margin, (margin + size + margin));
assertRect(layout, 2, "2", 2, (margin + size + margin), margin);
assertRect(layout, 3, "3", 3, margin, margin + (size + margin) * 2);
}

@Test
public void testBasicMargin2() {
int size = 15;
List<TextureSetLayout.Rect> rectangles
= Arrays.asList(rect("0", 0, 15, 15),
rect("1", 1, 15, 15),
rect("2", 2, 15, 15),
rect("3", 3, 15, 15));

Layout layout = packedLayout(2, rectangles).get(0);
= Arrays.asList(rect("0", 0, size, size),
rect("1", 1, size, size),
rect("2", 2, size, size),
rect("3", 3, size, size));
int margin = 2;
Layout layout = packedLayout(margin, rectangles).get(0);
assertThat(layout.getWidth(), is(64));
assertThat(layout.getHeight(), is(64));
assertRect(layout, 0, "0", 0, 0, 0);
assertRect(layout, 1, "1", 1, 0, (15 + 2));
assertRect(layout, 2, "2", 2, (15 + 2), 0);
assertRect(layout, 3, "3", 3, 0, (15 + 2) * 2);
assertRect(layout, 0, "0", 0, margin, margin);
assertRect(layout, 1, "1", 1, margin, (margin + size + margin));
assertRect(layout, 2, "2", 2, (margin + size + margin), margin);
assertRect(layout, 3, "3", 3, margin, margin +(size + margin) * margin);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public List<Layout> createLayout(List<Rect> srcRects) {
ArrayList<Rect> rects = new ArrayList<Rect>(page.outputRects.size());
for(RectNode node : page.outputRects) {
Rect finalRect = new Rect(node.rect.getId(), node.rect.getIndex(), node.rect.getPage(),
node.rect.getX(), node.rect.getY(), node.rect.getWidth() - settings.paddingX, node.rect.getHeight() - settings.paddingY,
node.rect.getX() + settings.paddingX, node.rect.getY() + settings.paddingY,
node.rect.getWidth() - settings.paddingX, node.rect.getHeight() - settings.paddingY,
node.rect.getRotated());
rects.add(finalRect);
}
Expand All @@ -92,17 +93,17 @@ private Page packPage(ArrayList<RectNode> inputRects) {
minWidth = Math.min(minWidth, rect.getWidth());
minHeight = Math.min(minHeight, rect.getHeight());
if (settings.rotation) {
if ((rect.getWidth() > settings.maxPageWidth || rect.getHeight() > settings.maxPageHeight)
&& (rect.getWidth() > settings.maxPageHeight || rect.getHeight() > settings.maxPageWidth)) {
if ((rect.getWidth() > settings.maxPageWidth - settings.paddingX || rect.getHeight() > settings.maxPageHeight - settings.paddingY)
&& (rect.getWidth() > settings.maxPageHeight - settings.paddingY || rect.getHeight() > settings.maxPageWidth - settings.paddingX)) {
throw new RuntimeException("Image does not fit with max page size " + settings.maxPageWidth + "x" + settings.maxPageHeight
+ " and padding " + settings.paddingX + "," + settings.paddingY + ": " + rect);
}
} else {
if (rect.getWidth() > settings.maxPageWidth) {
if (rect.getWidth() > settings.maxPageWidth - settings.paddingX) {
throw new RuntimeException("Image does not fit with max page width " + settings.maxPageWidth + " and paddingX "
+ settings.paddingX + ": " + rect);
}
if (rect.getHeight() > settings.maxPageHeight && (!settings.rotation || rect.getWidth() > settings.maxPageHeight)) {
if (rect.getHeight() > settings.maxPageHeight - settings.paddingY) {
throw new RuntimeException("Image does not fit in max page height " + settings.maxPageHeight + " and paddingY "
+ settings.paddingY + ": " + rect);
}
Expand All @@ -119,14 +120,14 @@ private Page packPage(ArrayList<RectNode> inputRects) {
BinarySearch sizeSearch = new BinarySearch(minSize, maxSize);
int size = sizeSearch.reset();
while (size != -1) {
Page result = packAtSize(true, size, size, inputRects);
Page result = packAtSize(true, size - settings.paddingX, size - settings.paddingY, inputRects);
bestResult = getBest(bestResult, result);
size = sizeSearch.next(result == null);
}

// Rects don't fit on one page. Fill a whole page and return.
if (bestResult == null) {
bestResult = packAtSize(false, maxSize, maxSize, inputRects);
bestResult = packAtSize(false, maxSize - settings.paddingX, maxSize - settings.paddingY, inputRects);
}

bestResult.width = Math.max(bestResult.width, bestResult.height);
Expand All @@ -139,7 +140,7 @@ private Page packPage(ArrayList<RectNode> inputRects) {
while (true) {
Page bestWidthResult = null;
while (width != -1) {
Page result = packAtSize(true, width, height, inputRects);
Page result = packAtSize(true, width - settings.paddingX, height - settings.paddingY, inputRects);
bestWidthResult = getBest(bestWidthResult, result);
width = widthSearch.next(result == null);
}
Expand All @@ -152,7 +153,7 @@ private Page packPage(ArrayList<RectNode> inputRects) {
}
// Rects don't fit on one page. Fill a whole page and return.
if (bestResult == null) {
bestResult = packAtSize(false, settings.maxPageWidth, settings.maxPageHeight, inputRects);
bestResult = packAtSize(false, settings.maxPageWidth - settings.paddingX, settings.maxPageHeight - settings.paddingY, inputRects);
}
}
return bestResult;
Expand Down

0 comments on commit f4db7d9

Please sign in to comment.