From 47b58b19427457c99361aa7f4dbb28787ed71dc3 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 7 May 2022 21:39:36 +0200 Subject: [PATCH] Premultiply textures when using RawContentManager --- CHANGELOG.md | 4 ++++ MLEM.Data/Content/Texture2DReader.cs | 17 ++++++++++++++++- Sandbox/Content/Textures/Test.png | Bin 896 -> 899 bytes 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bdafc7d..7d3cbf1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,10 @@ Removals - Marked old Draw and DrawTransformed overloads as obsolete in favor of SpriteBatchContext ones - Marked Tooltip.Paragraph as obsolete in favor of new Paragraphs collection +### MLEM.Data +Improvements +- Premultiply textures when using RawContentManager + ## 5.3.0 ### MLEM Additions diff --git a/MLEM.Data/Content/Texture2DReader.cs b/MLEM.Data/Content/Texture2DReader.cs index 275a019..89ec323 100644 --- a/MLEM.Data/Content/Texture2DReader.cs +++ b/MLEM.Data/Content/Texture2DReader.cs @@ -1,5 +1,7 @@ using System.IO; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using MLEM.Extensions; namespace MLEM.Data.Content { /// @@ -11,7 +13,20 @@ namespace MLEM.Data.Content { existing.Reload(stream); return existing; } else { - return Texture2D.FromStream(manager.GraphicsDevice, stream); + // premultiply the texture's color to be in line with the pipeline's texture reader + // TODO this can be converted to use https://github.com/MonoGame/MonoGame/pull/7369 in the future + using (var texture = Texture2D.FromStream(manager.GraphicsDevice, stream)) { + var ret = new Texture2D(manager.GraphicsDevice, texture.Width, texture.Height); + using (var textureData = texture.GetTextureData()) { + using (var retData = ret.GetTextureData()) { + for (var x = 0; x < ret.Width; x++) { + for (var y = 0; y < ret.Height; y++) + retData[x, y] = Color.FromNonPremultiplied(textureData[x, y].ToVector4()); + } + } + } + return ret; + } } } diff --git a/Sandbox/Content/Textures/Test.png b/Sandbox/Content/Textures/Test.png index 7d88c33500f8b645481467247adf547c8218eba0..6bf0e3bed9e25c017f62ac987558815e9b6ee832 100644 GIT binary patch delta 767 zcmV-OXaV#nUSQ^(P*iii|N!z()s!My5{fX?Cfl->wmGC?W!E6$C9M$@~SQm z20A%B^q)UFIMCCed^#Gr^zcyOD@=$qO;?hvt8P3VyYYCuHX#w8m2Lvwycd^uZ=eaj zBn^Yt^B@WGJU3y$(=>H?o|~|pFz_V7{jpLV@L_uF1K;%ge=)q}^E}T@)5A9fe^VXe zvr--K;`09jAAiGaHoH?D;`3{OzJs5pshdnDW-^(W_WYk^nF#|vo6VGEnOSKB(XA{3 zn{`0wU@%xJm*yrm7t^W9i(=)PuSMZ^0_{1!y(W-ul7QgfucTcOE&zw=u?k5)S^>^l z<%{XmXw5cIssrBsJ6&}I{{ERauTGtFH?ech$r$6`H$T-$^5ypRRaNP_Tvl?A}W6-w!t2`bBNn_AoyR(ua|8g_D3(8b`^o z^x@l5-KQ*DDqqfKMv_iPqosB(rc)zH=jZ3^n!lH`v$L(P$A4zFt8$ngOOpPSH+6Y1 z(8=MU|NPa#fnEmX)6vMKhldJZVM3&7x{_pFb>s2ajmP7)35ocubQ|#Iy|})A15NNH zX&Ag-2T73Uxd{WFrm4&G+=T6ffhP$bkCp0x57T2G_@?JS#PE*K^E@|A58o90O?8OR zN_D`C>;DUU41crP>|S+<&#wXc4t|=ZZZese$z)>M^M96QCJgv&HdB^mW~C8Cx3UOq z)&Ze|!C;`;Ns`jwp20(OO)ZGaPXRBUlkEW_e`}lA zCEwy7!~G=AxjKN(>F8*rli~BN8=d_4E~y?Bu-j~VZjaKatw)ELZK5B38(7R2Px(9W zL~MIe7eh1j_t7@u>U)5@O-oGDgxb=(LuMqK{ox-5#ST`OP