From 2cf912255dd7c8d9e1207a976c0405fc4647cf49 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 11 Feb 2021 01:09:07 +0100 Subject: [PATCH] allow the texture packer to forcibly be square or a power of two --- MLEM.Data/RuntimeTexturePacker.cs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/MLEM.Data/RuntimeTexturePacker.cs b/MLEM.Data/RuntimeTexturePacker.cs index 7ee4242..2c9b892 100644 --- a/MLEM.Data/RuntimeTexturePacker.cs +++ b/MLEM.Data/RuntimeTexturePacker.cs @@ -17,6 +17,8 @@ namespace MLEM.Data { private readonly List textures = new List(); private readonly bool autoIncreaseMaxWidth; + private readonly bool forcePowerOfTwo; + private readonly bool forceSquare; /// /// The generated packed texture. @@ -42,9 +44,13 @@ namespace MLEM.Data { /// /// The maximum width that the packed texture can have. Defaults to 2048. /// Whether the maximum width should be increased if there is a texture to be packed that is wider than . Defaults to false. - public RuntimeTexturePacker(int maxWidth = 2048, bool autoIncreaseMaxWidth = false) { + /// Whether the resulting should have a width and height that is a power of two + /// Whether the resulting should be square regardless of required size + public RuntimeTexturePacker(int maxWidth = 2048, bool autoIncreaseMaxWidth = false, bool forcePowerOfTwo = false, bool forceSquare = false) { this.maxWidth = maxWidth; this.autoIncreaseMaxWidth = autoIncreaseMaxWidth; + this.forcePowerOfTwo = forcePowerOfTwo; + this.forceSquare = forceSquare; } /// @@ -97,9 +103,17 @@ namespace MLEM.Data { stopwatch.Stop(); this.LastCalculationTime = stopwatch.Elapsed; - // generate texture based on required size + // figure out texture size var width = this.textures.Max(t => t.PackedArea.Right); var height = this.textures.Max(t => t.PackedArea.Bottom); + if (this.forcePowerOfTwo) { + width = ToPowerOfTwo(width); + height = ToPowerOfTwo(height); + } + if (this.forceSquare) + width = height = Math.Max(width, height); + + // generate texture this.PackedTexture = new Texture2D(device, width, height); device.Disposing += (o, a) => this.PackedTexture.Dispose(); @@ -155,6 +169,13 @@ namespace MLEM.Data { } } + private static int ToPowerOfTwo(int value) { + var ret = 1; + while (ret < value) + ret <<= 1; + return ret; + } + private class Request { public readonly TextureRegion Texture;