Skip to content

Commit

Permalink
Merge pull request #1376 from JohnnyonFlame/master
Browse files Browse the repository at this point in the history
Fixes and Improvements to NewTextureRepacker.
  • Loading branch information
colinator27 authored Oct 31, 2023
2 parents e308ae8 + 94a4790 commit 0e57128
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions UndertaleModTool/Scripts/Resource Repackers/NewTextureRepacker.csx
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,18 @@ public class Split : Rect
public class TextureAtlas
{

public int Size;
public int Width;
public int Height;
public int Padding;
public List<Split> Splits;
public List<Rect> Textures;

public TextureAtlas(int Size, int Padding)
public TextureAtlas(int Width, int Height, int Padding)
{
this.Splits = new List<Split> { new Split(0, 0, Size, Size) };
this.Splits = new List<Split> { new Split(0, 0, Width, Height) };
this.Textures = new List<Rect>();
this.Size = Size;
this.Width = Width;
this.Height = Height;
this.Padding = Padding;
}

Expand All @@ -131,7 +133,7 @@ public class TextureAtlas

// Best Long Side fit.
var bestFit = findBestFit(pWidth, pHeight,
split => Math.Min(pWidth - split.Width, pHeight - split.Height)
split => Math.Max(split.Width - pWidth, split.Height - pHeight)
);

// No space available, return null
Expand Down Expand Up @@ -289,12 +291,12 @@ int doItemGrouping(TPageItem item)
// return false;
// }

List<TextureAtlas> layoutPageItemList(List<TPageItem> items, int pageSize, int padding)
List<TextureAtlas> layoutPageItemList(List<TPageItem> items, int pageSizeWidth, int pageSizeHeight, int padding)
{
var atlas_list = new List<TextureAtlas>();
while (items.Count > 0)
{
var atlas = new TextureAtlas(pageSize, padding);
var atlas = new TextureAtlas(pageSizeWidth, pageSizeHeight, padding);
foreach (var page in items)
{
// If failed to allocate atlas space, then retry with a new one
Expand All @@ -320,11 +322,11 @@ List<TextureAtlas> layoutPageItemList(List<TPageItem> items, int pageSize, int p
return atlas_list;
}

async Task<List<TextureAtlas>> layoutPageItemLists<K>(ILookup<K, TPageItem> lookup, int pageSize, int padding)
async Task<List<TextureAtlas>> layoutPageItemLists<K>(ILookup<K, TPageItem> lookup, int pageSizeWidth, int pageSizeHeight, int padding)
{
return await Task.Run(() => lookup
.AsParallel()
.Select(list => layoutPageItemList(list.ToList(), pageSize, padding))
.Select(list => layoutPageItemList(list.ToList(), pageSizeWidth, pageSizeHeight, padding))
.SelectMany(item => item)
.ToList());
}
Expand All @@ -338,8 +340,10 @@ private static int NearestPowerOf2(uint x)
EnsureDataLoaded();

// User Configurable:: Atlas page size and item padding
var pageSize = 512;
var padding = 2;
var pageSizeWidth = 1024;
var pageSizeHeight = 1024;

var padding = 1;

// User Configurable:: Dimension cutoffs (gets thrown off the atlas pool)
var maxDims = 256;
Expand All @@ -353,17 +357,9 @@ List<TPageItem> potBlacklist = new List<TPageItem>();
// Ensure pageSize is POT
if (forcePOT)
{
pageSize = NearestPowerOf2((uint)pageSize);
}

// Sanity checks
if (maxDims <= 0 || maxDims + padding * 2 >= pageSize)
{
maxDims = pageSize - padding * 2;
maxArea = maxDims * maxDims;
pageSizeWidth = NearestPowerOf2((uint)pageSizeWidth);
pageSizeHeight = NearestPowerOf2((uint)pageSizeHeight);
}
if (maxArea <= 0)
maxArea = maxDims * maxDims;

bool reuseTextures = false;

Expand Down Expand Up @@ -417,7 +413,7 @@ await Task.Run(() =>

// Layout all the texture items (grouped by doItemGrouping) into atlases
ResetProgress("Laying out texture items");
var atlases = await layoutPageItemLists(texPageLookup, pageSize, padding);
var atlases = await layoutPageItemLists(texPageLookup, pageSizeWidth, pageSizeHeight, padding);

int lastTextPage = Data.EmbeddedTextures.Count - 1;

Expand All @@ -444,7 +440,7 @@ await Task.Run(() =>
UndertaleEmbeddedTexture tex = new UndertaleEmbeddedTexture();
tex.Name = new UndertaleString("Texture " + ++lastTextPage);
Data.EmbeddedTextures.Add(tex);
Bitmap img = new Bitmap(atlas.Size, atlas.Size, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Bitmap img = new Bitmap(atlas.Width, atlas.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
// DPI fix
img.SetResolution(96.0F, 96.0F);
Expand Down

0 comments on commit 0e57128

Please sign in to comment.