Rewrite atlas size calculation logic and re-enable fast texture stitching

This commit is contained in:
embeddedt 2023-03-11 09:38:13 -05:00
parent 14e266288a
commit 94bc711008
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
2 changed files with 23 additions and 10 deletions

View File

@ -53,7 +53,7 @@ public class ModernFixEarlyConfig {
this.addMixinRule("perf.cache_model_materials", true);
this.addMixinRule("perf.datapack_reload_exceptions", true);
this.addMixinRule("perf.async_locator", true);
this.addMixinRule("perf.faster_texture_stitching", false);
this.addMixinRule("perf.faster_texture_stitching", true);
this.addMixinRule("perf.kubejs", true);
this.addMixinRule("perf.faster_singleplayer_load", false);
/* Keep this off if JEI isn't installed to prevent breaking vanilla gameplay */

View File

@ -135,7 +135,8 @@ public class StbStitcher {
// Initialize the rectangles that we'll be using in the calculation
// While that's happening, sum up the area needed to fit all of the images
int sqSize = 0;
int totalArea = 0;
int longestWidth = 0, longestHeight = 0;
for (int j = 0; j < holderSize; ++j) {
Stitcher.Holder holder = holders[j];
@ -147,17 +148,29 @@ public class StbStitcher {
setWrapper(rect, j, width, height, 0, 0, false);
sqSize += (width * height);
totalArea += (width * height);
longestWidth = Math.max(longestWidth, width);
longestHeight = Math.max(longestHeight, height);
}
int size = Mth.smallestEncompassingPowerOfTwo((int) Math.sqrt(sqSize));
int width = size * 2; // needed to fix weirdness in 1.16
int height = size;
longestWidth = Mth.smallestEncompassingPowerOfTwo(longestWidth);
longestHeight = Mth.smallestEncompassingPowerOfTwo(longestHeight);
/*
* The atlas needs to be at least this wide and tall to accomodate oddly shaped sprites. If this is
* not enough, keep doubling the smaller of the two values until its big enough.
*/
while((longestWidth*longestHeight) < totalArea) {
if(longestWidth <= longestHeight)
longestWidth *= 2;
else
longestHeight *= 2;
}
// Internal node structure needed for STB
try (STBRPNode.Buffer nodes = STBRPNode.malloc(width + 10)) {
try (STBRPNode.Buffer nodes = STBRPNode.malloc(longestWidth + 10)) {
// Initialize the rect packer
STBRectPack.stbrp_init_target(ctx, width, height, nodes);
STBRectPack.stbrp_init_target(ctx, longestWidth, longestHeight, nodes);
// Perform rectangle packing
STBRectPack.stbrp_pack_rects(ctx, rectBuf);
@ -172,11 +185,11 @@ public class StbStitcher {
}
// Initialize the sprite now with the position and size that we've calculated so far
infoList.add(new LoadableSpriteInfo(holder.spriteInfo, width, height, getX(rect), getY(rect)));
infoList.add(new LoadableSpriteInfo(holder.spriteInfo, longestWidth, longestHeight, getX(rect), getY(rect)));
//holder.spriteInfo.initSprite(size, size, rect.x(), rect.y(), false);
}
return Pair.of(Pair.of(width, height), infoList);
return Pair.of(Pair.of(longestWidth, longestHeight), infoList);
}
}
}