Merge pull request #30 from ammaraskar/texture_bug

Fix bug with loading non-square textures.
This commit is contained in:
Nahuel Rocchetti 2023-07-18 13:37:56 -03:00 committed by GitHub
commit b04b6c9f20
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 2 deletions

View file

@ -73,8 +73,9 @@ namespace OpenTS2.Content.DBPF.Scenegraph
texture.SetPixelData(pixelData, currentMipLevel);
currentMipLevel++;
width /= 2;
height /= 2;
// Make sure the width and height are always at least 1-pixel.
width = Math.Max(width / 2, 1);
height = Math.Max(height / 2, 1);
}
texture.Apply();
return texture;

View file

@ -84,4 +84,24 @@ public class ScenegraphTextureCodecTest
// Check that the last mip is a LIFO reference.
Assert.That(imageBlock.SubImages[0].MipMap[8], Is.InstanceOf<LifoReferenceMip>());
}
[Test]
public void TestLoadsTextureThatSqueezesToOnePixel()
{
// This test is for an edge case of resolutions like 32x16 with 6 mip levels. If we naively go down the path
// of dividing the shorter side by 2 for each mip level we end up with:
//
// mip level: 6 -> 5 -> 4 -> 3 -> 2 -> 1
// pixels: 16 -> 8 -> 4 -> 2 -> 1 -> 0
//
// but we can't have a 1x0 resolution texture, so gotta make sure we clamp that lower value to at least 1
// pixel :)
var textureAsset =
ContentProvider.Get().GetAsset<ScenegraphTextureAsset>(new ResourceKey("small_non_square_txtr", 0x1C0532FA,
TypeIDs.SCENEGRAPH_TXTR));
// Check that the smallest mip is a 1x1 image.
var texture = textureAsset.GetSelectedImageAsUnityTexture(ContentProvider.Get());
Assert.That(texture.GetPixels32(5).Length, Is.EqualTo(1));
}
}