From 65d1217939998a59da84f6ed70ef9912aa781da2 Mon Sep 17 00:00:00 2001 From: Alex <12881812+xezno@users.noreply.github.com> Date: Tue, 10 Sep 2024 23:41:45 +0100 Subject: [PATCH] Compile & load non-square textures correctly --- Source/Mocha.Engine/Render/Assets/Texture.cs | 4 ++-- .../Handlers/Texture/TextureCompiler.cs | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Source/Mocha.Engine/Render/Assets/Texture.cs b/Source/Mocha.Engine/Render/Assets/Texture.cs index 849649a1..1b5e4526 100644 --- a/Source/Mocha.Engine/Render/Assets/Texture.cs +++ b/Source/Mocha.Engine/Render/Assets/Texture.cs @@ -33,7 +33,7 @@ public Texture( string path, bool isSrgb = true ) var mipCount = textureFormat.Data.MipCount; var format = GetRenderTextureFormat( textureFormat.Data.Format, isSrgb ); - NativeTexture = new( Path, DataWidth, DataHeight ); + NativeTexture = new( Path, Width, Height ); // Flatten mip data into one big buffer List textureData = new List(); @@ -42,7 +42,7 @@ public Texture( string path, bool isSrgb = true ) textureData.AddRange( mipData[i] ); } - NativeTexture.SetData( DataWidth, DataHeight, (uint)mipCount, textureData.ToInterop(), (int)format ); + NativeTexture.SetData( Width, Height, (uint)mipCount, textureData.ToInterop(), (int)format ); } /// diff --git a/Source/MochaTool.AssetCompiler/Handlers/Texture/TextureCompiler.cs b/Source/MochaTool.AssetCompiler/Handlers/Texture/TextureCompiler.cs index 0ae554a9..edac9146 100644 --- a/Source/MochaTool.AssetCompiler/Handlers/Texture/TextureCompiler.cs +++ b/Source/MochaTool.AssetCompiler/Handlers/Texture/TextureCompiler.cs @@ -134,7 +134,8 @@ private static CompressionFormat TextureFormatToCompressionFormat( TextureFormat private static byte[] BlockCompression( byte[] data, uint width, uint height, uint mip, TextureFormat format ) { var targetWidth = MathX.CalcMipSize( (int)width, (int)mip ); - byte[] resizedData = new byte[4 * targetWidth * targetWidth]; + var targetHeight = MathX.CalcMipSize( (int)height, (int)mip ); + byte[] resizedData = new byte[4 * targetWidth * targetHeight]; Rgba32[] pixels = new Rgba32[width * height]; for ( int i = 0; i < pixels.Length; i++ ) @@ -149,7 +150,7 @@ private static byte[] BlockCompression( byte[] data, uint width, uint height, ui using ( var image = Image.LoadPixelData( pixels.AsSpan(), (int)width, (int)height ) ) { - image.Mutate( x => x.Resize( targetWidth, targetWidth, KnownResamplers.Lanczos5 ) ); + image.Mutate( x => x.Resize( targetWidth, targetHeight, KnownResamplers.Lanczos5 ) ); image.CopyPixelDataTo( resizedData ); } @@ -160,7 +161,7 @@ private static byte[] BlockCompression( byte[] data, uint width, uint height, ui encoder.OutputOptions.Format = TextureFormatToCompressionFormat( format ); encoder.OutputOptions.FileFormat = OutputFileFormat.Dds; - return encoder.EncodeToRawBytes( resizedData, (int)targetWidth, (int)targetWidth, PixelFormat.Rgba32, 0, out _, out _ ); + return encoder.EncodeToRawBytes( resizedData, (int)targetWidth, (int)targetHeight, PixelFormat.Rgba32, 0, out _, out _ ); } private static bool IsPowerOfTwo( int x )