From 810406fb940b40d2f0999dee8a1a3e5467b86dbd Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 19 Nov 2022 17:12:57 +0100 Subject: [PATCH] Added the ability for UniformTextureAtlases to have padding for each region --- CHANGELOG.md | 1 + MLEM/Textures/UniformTextureAtlas.cs | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d3c972..5f41d43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ Additions - Added TokenizedString.Realign - Added GetFlags and GetUniqueFlags to EnumHelper - Added GetDownTime, GetUpTime, GetTimeSincePress, WasModifierDown and WasDown to Keybind and Combination +- Added the ability for UniformTextureAtlases to have padding for each region - **Added the ability to find paths to one of multiple goals using AStar** Improvements diff --git a/MLEM/Textures/UniformTextureAtlas.cs b/MLEM/Textures/UniformTextureAtlas.cs index 9162033..a67867f 100644 --- a/MLEM/Textures/UniformTextureAtlas.cs +++ b/MLEM/Textures/UniformTextureAtlas.cs @@ -32,6 +32,12 @@ namespace MLEM.Textures { /// The height of reach region, based on the texture's height and the amount of regions /// public readonly int RegionHeight; + /// + /// The padding that each texture region has around itself, in pixels, which will be taken away from each side of objects created and returned by this texture atlas. + /// Creating a texture atlas with padding can be useful if texture bleeding issues occur due to texture coordinate rounding. + /// + public readonly int RegionPadding; + /// /// The texture to use for this atlas. /// Note that stores the actual area that we depend on. @@ -68,10 +74,12 @@ namespace MLEM.Textures { /// The texture region to use for this atlas /// The amount of texture regions in the x direction /// The amount of texture regions in the y direction - public UniformTextureAtlas(TextureRegion region, int regionAmountX, int regionAmountY) { + /// The padding that each texture region has around itself, in pixels, which will be taken away from each side of objects created and returned by this texture atlas. + public UniformTextureAtlas(TextureRegion region, int regionAmountX, int regionAmountY, int regionPadding = 0) { this.Region = region; this.RegionAmountX = regionAmountX; this.RegionAmountY = regionAmountY; + this.RegionPadding = regionPadding; this.RegionWidth = region.Width / regionAmountX; this.RegionHeight = region.Height / regionAmountY; } @@ -82,14 +90,16 @@ namespace MLEM.Textures { /// The texture to use for this atlas /// The amount of texture regions in the x direction /// The amount of texture regions in the y direction - public UniformTextureAtlas(Texture2D texture, int regionAmountX, int regionAmountY) : this(new TextureRegion(texture), regionAmountX, regionAmountY) {} + /// The padding that each texture region has around itself, in pixels, which will be taken away from each side of objects created and returned by this texture atlas. + public UniformTextureAtlas(Texture2D texture, int regionAmountX, int regionAmountY, int regionPadding = 0) : + this(new TextureRegion(texture), regionAmountX, regionAmountY, regionPadding) {} private TextureRegion GetOrAddRegion(Rectangle rect) { if (this.regions.TryGetValue(rect, out var region)) return region; region = new TextureRegion(this.Region, - rect.X * this.RegionWidth, rect.Y * this.RegionHeight, - rect.Width * this.RegionWidth, rect.Height * this.RegionHeight); + rect.X * this.RegionWidth + this.RegionPadding, rect.Y * this.RegionHeight + this.RegionPadding, + rect.Width * this.RegionWidth - 2 * this.RegionPadding, rect.Height * this.RegionHeight - 2 * this.RegionPadding); this.regions.Add(rect, region); return region; }