Skip to content

Commit f9993f1

Browse files
committed
add resizing for incorrectly sized sprites
1 parent 3e9ed62 commit f9993f1

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

Patchwork.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.1</TargetFramework>
55
<AssemblyName>Patchwork</AssemblyName>
66
<Product>Patchwork</Product>
7-
<Version>1.0.1</Version>
7+
<Version>1.0.2</Version>
88
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
99
<LangVersion>latest</LangVersion>
1010
<RestoreAdditionalProjectSources>

handlers/SpriteLoader.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ public static void LoadCollection(tk2dSpriteCollectionData collection)
5252
else if (def.flipped == tk2dSpriteDefinition.FlipMode.TPackerCW)
5353
spriteTex = TexUtil.RotateCW(spriteTex);
5454

55-
// Blit the sprite into the atlas texture
5655
Rect spriteRect = SpriteUtil.GetSpriteRect(def, baseTex);
56+
if (spriteRect.width != spriteTex.width || spriteRect.height != spriteTex.height)
57+
{
58+
Plugin.Logger.LogError($"Sprite {collection.name}/{matname}/{def.name} size mismatch: expected {spriteRect.width}x{spriteRect.height}, got {spriteTex.width}x{spriteTex.height}. Resizing, which may cause distortion.");
59+
spriteTex = TexUtil.ResizeTexture(spriteTex, (int)spriteRect.width, (int)spriteRect.height);
60+
}
61+
5762
Color[] pixels = spriteTex.GetPixels();
5863
baseTex.SetPixels((int)spriteRect.x, (int)spriteRect.y, (int)spriteRect.width, (int)spriteRect.height, pixels);
5964
baseTex.Apply();

util/TexUtil.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,18 @@ public static Texture2D LoadFromPNG(string path)
8787
}
8888
return tex;
8989
}
90+
91+
public static Texture2D ResizeTexture(Texture2D source, int newWidth, int newHeight)
92+
{
93+
RenderTexture rt = RenderTexture.GetTemporary(newWidth, newHeight);
94+
Graphics.Blit(source, rt);
95+
RenderTexture previous = RenderTexture.active;
96+
RenderTexture.active = rt;
97+
Texture2D resized = new(newWidth, newHeight);
98+
resized.ReadPixels(new Rect(0, 0, newWidth, newHeight), 0, 0);
99+
resized.Apply();
100+
RenderTexture.active = previous;
101+
RenderTexture.ReleaseTemporary(rt);
102+
return resized;
103+
}
90104
}

0 commit comments

Comments
 (0)