Merge remote-tracking branch 'origin/main' into 1.18

This commit is contained in:
embeddedt 2023-03-11 11:29:52 -05:00
commit cf9fa6e055
No known key found for this signature in database
GPG Key ID: A69433EC199B5613

View File

@ -158,8 +158,8 @@ public class StbStitcher {
longestHeight = Mth.smallestEncompassingPowerOfTwo(longestHeight); longestHeight = Mth.smallestEncompassingPowerOfTwo(longestHeight);
/* /*
* The atlas needs to be at least this wide and tall to accomodate oddly shaped sprites. If this is * The atlas needs to be at least this wide and tall to accommodate oddly shaped sprites. If this is
* not enough, keep doubling the smaller of the two values until its big enough. * not enough, keep doubling the smaller of the two values until it's big enough.
*/ */
while((longestWidth*longestHeight) < totalArea) { while((longestWidth*longestHeight) < totalArea) {
if(longestWidth <= longestHeight) if(longestWidth <= longestHeight)
@ -168,6 +168,13 @@ public class StbStitcher {
longestHeight *= 2; longestHeight *= 2;
} }
/*
* Sometimes our guess is off and we actually need a bigger atlas. We will try up to 4 times to double
* the atlas size, if that fails then give up.
*/
int numTries = 0;
while(true) {
numTries++;
// Internal node structure needed for STB // Internal node structure needed for STB
try (STBRPNode.Buffer nodes = STBRPNode.malloc(longestWidth + 10)) { try (STBRPNode.Buffer nodes = STBRPNode.malloc(longestWidth + 10)) {
// Initialize the rect packer // Initialize the rect packer
@ -181,10 +188,6 @@ public class StbStitcher {
// Ensure that everything is properly packed! // Ensure that everything is properly packed!
if (!rect.was_packed()) { if (!rect.was_packed()) {
ModernFix.LOGGER.error("Stitcher ran out of space with target atlas size " + longestWidth + "x" + longestHeight + ":");
for(Stitcher.Holder h : holders) {
ModernFix.LOGGER.error(" - " + h.spriteInfo.name() + ", " + h.spriteInfo.width() + "x" + h.spriteInfo.height());
}
throw new StitcherException(holder.spriteInfo, throw new StitcherException(holder.spriteInfo,
Stream.of(holders).map(arg -> arg.spriteInfo).collect(ImmutableList.toImmutableList())); Stream.of(holders).map(arg -> arg.spriteInfo).collect(ImmutableList.toImmutableList()));
} }
@ -195,6 +198,22 @@ public class StbStitcher {
} }
return Pair.of(Pair.of(longestWidth, longestHeight), infoList); return Pair.of(Pair.of(longestWidth, longestHeight), infoList);
} catch (StitcherException e) {
if(numTries >= 4) {
// If we get here, we weren't able to stitch. Throw an error.
ModernFix.LOGGER.error("Stitcher ran out of space with target atlas size " + longestWidth + "x" + longestHeight + ":");
for(Stitcher.Holder h : holders) {
ModernFix.LOGGER.error(" - " + h.spriteInfo.name() + ", " + h.spriteInfo.width() + "x" + h.spriteInfo.height());
}
throw e;
} else {
// double the atlas size and try again
if(longestWidth <= longestHeight)
longestWidth *= 2;
else
longestHeight *= 2;
}
}
} }
} }
} }