From 314d1e7c20387f3056409143f0ccaeea2dcfe58b Mon Sep 17 00:00:00 2001 From: KillzXGaming Date: Thu, 19 Sep 2019 18:02:10 -0400 Subject: [PATCH] Some layout editor improvments. Convert white colors to srgb for more accurate colors. Fix displaying bflims with using bc4 alpha and no alpha. Preview alpha theshhold for color blending. Fix window pane alpha if frame vertex colors are disabled. --- .../FileFormats/Layout/BxlytShader.cs | 8 +++ .../FileFormats/Layout/BxlytToGL.cs | 53 ++++++++++++------- .../FileFormats/Layout/CAFE/BFLYT.cs | 17 +++++- .../FileFormats/Layout/CAFE/BflytShader.cs | 9 +++- .../Layout/CAFE/Materials/TevStage.cs | 12 ++--- .../FileFormats/Layout/CTR/BclytShader.cs | 2 + .../FileFormats/Layout/Common.cs | 33 ++++++++++++ .../FileFormats/Layout/Rev/BrlytShader.cs | 2 + .../FileFormats/Texture/BFLIM.cs | 18 +++++-- Toolbox/Shader/Layout/Legacy/Bflyt.frag | 19 ++++++- 10 files changed, 140 insertions(+), 33 deletions(-) diff --git a/File_Format_Library/FileFormats/Layout/BxlytShader.cs b/File_Format_Library/FileFormats/Layout/BxlytShader.cs index 208f9049..3faca783 100644 --- a/File_Format_Library/FileFormats/Layout/BxlytShader.cs +++ b/File_Format_Library/FileFormats/Layout/BxlytShader.cs @@ -93,6 +93,14 @@ namespace LayoutBXLYT GL.Uniform1(uniforms[name], value); } + public void SetBool(string name, bool value) + { + int intValue = value == true ? 1 : 0; + + if (uniforms.ContainsKey(name)) + GL.Uniform1(uniforms[name], intValue); + } + public void SetColor(string name, Color color) { if (uniforms.ContainsKey(name)) diff --git a/File_Format_Library/FileFormats/Layout/BxlytToGL.cs b/File_Format_Library/FileFormats/Layout/BxlytToGL.cs index a95b0995..83374307 100644 --- a/File_Format_Library/FileFormats/Layout/BxlytToGL.cs +++ b/File_Format_Library/FileFormats/Layout/BxlytToGL.cs @@ -60,7 +60,7 @@ namespace LayoutBXLYT { var pic1Pane = pane as Cafe.BFLYT.PIC1; - Color[] Colors = new Color[] { + Color[] Colors = new Color[] { pic1Pane.ColorBottomRight.Color, pic1Pane.ColorBottomLeft.Color, pic1Pane.ColorTopLeft.Color, @@ -90,9 +90,6 @@ namespace LayoutBXLYT DrawRectangle(pane, pane.Rectangle, TexCoords, Colors, false, effectiveAlpha); mat.Shader.Disable(); - - GL.BindTexture(TextureTarget.Texture2D, 0); - GL.PopAttrib(); } else if (pane is BCLYT.PIC1) { @@ -128,9 +125,6 @@ namespace LayoutBXLYT DrawRectangle(pane, pane.Rectangle, TexCoords, Colors, false, effectiveAlpha); mat.Shader.Disable(); - - GL.BindTexture(TextureTarget.Texture2D, 0); - GL.PopAttrib(); } else if (pane is BRLYT.PIC1) { @@ -166,10 +160,12 @@ namespace LayoutBXLYT DrawRectangle(pane, pane.Rectangle, TexCoords, Colors, false, effectiveAlpha); mat.Shader.Disable(); - - GL.BindTexture(TextureTarget.Texture2D, 0); - GL.PopAttrib(); } + + + GL.BindTexture(TextureTarget.Texture2D, 0); + GL.PopAttrib(); + GL.UseProgram(0); } public static void DrawBoundryPane(BasePane pane, byte effectiveAlpha, List SelectedPanes) @@ -345,17 +341,14 @@ namespace LayoutBXLYT if (!window.UseVertexColorForAll) { colors = new Color[] { - Color.White, Color.White, - Color.White, Color.White, + Color.FromArgb(effectiveAlpha, 255,255,255), + Color.FromArgb(effectiveAlpha, 255,255,255), + Color.FromArgb(effectiveAlpha, 255,255,255), + Color.FromArgb(effectiveAlpha, 255,255,255), }; } - //Apply pane alpha - for (int i = 0; i < colors.Length; i++) - { - uint setalpha = (uint)((colors[i].A * effectiveAlpha) / 255); - colors[i] = Color.FromArgb((int)setalpha, colors[i]); - } + switch (window.FrameCount) { @@ -478,6 +471,14 @@ namespace LayoutBXLYT var matBL = window.WindowFrames[2].Material; var matBR = window.WindowFrames[3].Material; + //Todo check this + if (window.UseOneMaterialForAll) + { + /* matTR = matTL; + matBL = matTL; + matBR = matTL;*/ + } + if (matTL.TextureMaps.Length > 0) { SetupShaders(matTL, Textures); @@ -568,6 +569,18 @@ namespace LayoutBXLYT var matL = window.WindowFrames[6].Material; var matR = window.WindowFrames[7].Material; + //Todo check this + if (window.UseOneMaterialForAll) + { + /* matTR = matTL; + matBL = matTL; + matBR = matTL; + matT = matTL; + matB = matTL; + matL = matTL; + matR = matTL;*/ + } + if (matTL.TextureMaps.Length > 0) { SetupShaders(matTL, Textures); @@ -973,8 +986,8 @@ namespace LayoutBXLYT { for (int i = 0; i < colors.Length; i++) { - uint setalpha = (uint)((colors[i].A * alpha) / 255); - colors[i] = Color.FromArgb((int)setalpha, colors[i]); + float outAlpha = BasePane.MixColors(colors[i].A, alpha); + colors[i] = Color.FromArgb(Utils.FloatToIntClamp(outAlpha), colors[i]); } if (LayoutEditor.UseLegacyGL) diff --git a/File_Format_Library/FileFormats/Layout/CAFE/BFLYT.cs b/File_Format_Library/FileFormats/Layout/CAFE/BFLYT.cs index 2b1168ae..2247a5c3 100644 --- a/File_Format_Library/FileFormats/Layout/CAFE/BFLYT.cs +++ b/File_Format_Library/FileFormats/Layout/CAFE/BFLYT.cs @@ -209,6 +209,16 @@ namespace LayoutBXLYT.Cafe if (!textures.ContainsKey(bflim.FileName)) textures.Add(bflim.FileName, bflim); } + Console.WriteLine("file " + file); + if (Utils.GetExtension(file) == ".bntx") + { + BNTX bntx = (BNTX)STFileLoader.OpenFileFormat(file); + foreach (var tex in bntx.Textures) + { + if (!textures.ContainsKey(tex.Key)) + textures.Add(tex.Key, tex.Value); + } + } } } @@ -1926,6 +1936,12 @@ namespace LayoutBXLYT.Cafe [DisplayName("Name"), CategoryAttribute("General")] public override string Name { get; set; } + [DisplayName("Thresholding Alpha Interpolation"), CategoryAttribute("Alpha")] + public override bool ThresholdingAlphaInterpolation + { + get { return Convert.ToBoolean((flags >> 18) & 0x1); } + } + [DisplayName("Black Color"), CategoryAttribute("Color")] public STColor8 BlackColor { get; set; } @@ -2010,7 +2026,6 @@ namespace LayoutBXLYT.Cafe var hasIndParam = Convert.ToBoolean((flags >> 14) & 0x1); var projTexGenParamCount = Convert.ToUInt32((flags >> 15) & 0x3); var hasFontShadowParam = Convert.ToBoolean((flags >> 17) & 0x1); - var thresholdingAlphaInterpolation = Convert.ToBoolean((flags >> 18) & 0x1); TextureMaps = new TextureRef[texCount]; TextureTransforms = new TextureTransform[mtxCount]; diff --git a/File_Format_Library/FileFormats/Layout/CAFE/BflytShader.cs b/File_Format_Library/FileFormats/Layout/CAFE/BflytShader.cs index dca63ac3..b15812ec 100644 --- a/File_Format_Library/FileFormats/Layout/CAFE/BflytShader.cs +++ b/File_Format_Library/FileFormats/Layout/CAFE/BflytShader.cs @@ -31,6 +31,7 @@ namespace LayoutBXLYT SetInt("textures0", 0); SetInt("textures1", 0); SetInt("textures2", 0); + SetBool("ThresholdingAlphaInterpolation", false); SetVec2("uvScale0", new Vector2(1,1)); SetFloat("uvRotate0", 0); @@ -47,18 +48,22 @@ namespace LayoutBXLYT SetFloat("uvRotate0", 0); SetVec2("uvTranslate0", new Vector2(0, 0)); SetInt("flipTexture", 0); + SetBool("ThresholdingAlphaInterpolation", material.ThresholdingAlphaInterpolation); BindTextureUniforms(); + int id = 1; for (int i = 0; i < material.TextureMaps.Length; i++) { if (textures.ContainsKey(material.TextureMaps[i].Name)) { - GL.ActiveTexture(TextureUnit.Texture0 + i); - SetInt($"textures{i}", 0); + GL.ActiveTexture(TextureUnit.Texture0 + id); + SetInt($"textures{i}", id); bool isBinded = BxlytToGL.BindGLTexture(material.TextureMaps[i], textures[material.TextureMaps[i].Name]); if (isBinded) SetInt($"hasTexture{i}", 1); + + id++; } } diff --git a/File_Format_Library/FileFormats/Layout/CAFE/Materials/TevStage.cs b/File_Format_Library/FileFormats/Layout/CAFE/Materials/TevStage.cs index a8c91646..bc2343fd 100644 --- a/File_Format_Library/FileFormats/Layout/CAFE/Materials/TevStage.cs +++ b/File_Format_Library/FileFormats/Layout/CAFE/Materials/TevStage.cs @@ -4,21 +4,21 @@ namespace LayoutBXLYT.Cafe { public class TevStage { - public byte RGBMode { get; set; } - public byte AlphaMode { get; set; } + public TevMode RGBMode { get; set; } + public TevMode AlphaMode { get; set; } public ushort unk { get; set; } public TevStage(FileReader reader, BFLYT.Header header) { - RGBMode = reader.ReadByte(); - AlphaMode = reader.ReadByte(); + RGBMode = (TevMode)reader.ReadByte(); + AlphaMode = (TevMode)reader.ReadByte(); unk = reader.ReadUInt16(); } public void Write(FileWriter writer) { - writer.Write(RGBMode); - writer.Write(AlphaMode); + writer.Write(RGBMode, false); + writer.Write(AlphaMode, false); writer.Write(unk); } } diff --git a/File_Format_Library/FileFormats/Layout/CTR/BclytShader.cs b/File_Format_Library/FileFormats/Layout/CTR/BclytShader.cs index 743b25e4..90c1055d 100644 --- a/File_Format_Library/FileFormats/Layout/CTR/BclytShader.cs +++ b/File_Format_Library/FileFormats/Layout/CTR/BclytShader.cs @@ -30,6 +30,7 @@ namespace LayoutBXLYT SetInt("textures0", 0); SetInt("textures1", 0); SetInt("textures2", 0); + SetBool("ThresholdingAlphaInterpolation", false); SetVec2("uvScale0", new Vector2(1, 1)); SetFloat("uvRotate0", 0); @@ -46,6 +47,7 @@ namespace LayoutBXLYT SetFloat("uvRotate0", 0); SetVec2("uvTranslate0", new Vector2(0, 0)); SetInt("flipTexture", 0); + SetBool("ThresholdingAlphaInterpolation", material.ThresholdingAlphaInterpolation); BindTextureUniforms(); diff --git a/File_Format_Library/FileFormats/Layout/Common.cs b/File_Format_Library/FileFormats/Layout/Common.cs index 345cf792..3005115a 100644 --- a/File_Format_Library/FileFormats/Layout/Common.cs +++ b/File_Format_Library/FileFormats/Layout/Common.cs @@ -124,6 +124,20 @@ namespace LayoutBXLYT parentTransform.W); } + public static float MixColors(params float[] c) + { + float a = c[0]; + for (int i = 1; i < c.Length; i++) + { + a *= c[i]; + } + for (int i = 1; i < c.Length; i++) + { + a /= 255f; + } + return a / 255f; + } + //Get the previous transform from the parent origin private Vector4 ParentOriginTransform(Vector4 points) { @@ -439,6 +453,22 @@ namespace LayoutBXLYT Rotate270 = 5 } + public enum TevMode : byte + { + Replace, + Modulate, + Add, + AddSigned, + Interpolate, + Subtract, + AddMultiplicate, + MultiplcateAdd, + Overlay, + Indirect, + BlendIndirect, + EachIndirect, + } + public interface IWindowPane { bool UseOneMaterialForAll { get; set; } @@ -756,6 +786,9 @@ namespace LayoutBXLYT [DisplayName("Name"), CategoryAttribute("General")] public virtual string Name { get; set; } + [DisplayName("Thresholding Alpha Interpolation"), CategoryAttribute("Alpha")] + public virtual bool ThresholdingAlphaInterpolation { get; set; } + [Browsable(false)] public virtual BxlytShader Shader { get; set; } diff --git a/File_Format_Library/FileFormats/Layout/Rev/BrlytShader.cs b/File_Format_Library/FileFormats/Layout/Rev/BrlytShader.cs index b4f8c7f2..bcd44ae0 100644 --- a/File_Format_Library/FileFormats/Layout/Rev/BrlytShader.cs +++ b/File_Format_Library/FileFormats/Layout/Rev/BrlytShader.cs @@ -30,6 +30,7 @@ namespace LayoutBXLYT SetInt("textures0", 0); SetInt("textures1", 0); SetInt("textures2", 0); + SetBool("ThresholdingAlphaInterpolation", false); SetVec2("uvScale0", new Vector2(1, 1)); SetFloat("uvRotate0", 0); @@ -46,6 +47,7 @@ namespace LayoutBXLYT SetFloat("uvRotate0", 0); SetVec2("uvTranslate0", new Vector2(0, 0)); SetInt("flipTexture", 0); + SetBool("ThresholdingAlphaInterpolation", material.ThresholdingAlphaInterpolation); BindTextureUniforms(); diff --git a/File_Format_Library/FileFormats/Texture/BFLIM.cs b/File_Format_Library/FileFormats/Texture/BFLIM.cs index f800dc4d..7a198258 100644 --- a/File_Format_Library/FileFormats/Texture/BFLIM.cs +++ b/File_Format_Library/FileFormats/Texture/BFLIM.cs @@ -265,6 +265,7 @@ namespace FirstPlugin bflim.Width = bflim.image.Width; bflim.Height = bflim.image.Height; + bflim.ImageData = ftex.texture.Data; var form = new GenericEditorForm(false, bflim.OpenForm()); LibraryGUI.CreateMdiWindow(form); @@ -410,15 +411,18 @@ namespace FirstPlugin image = new Image(Is3DS); image.Read(reader); + bool isBc4Alpha = image.BflimFormat == 16; + if (Is3DS) Format = Formats3DS[image.BflimFormat]; else Format = FormatsWiiU[image.BflimFormat]; + Width = image.Width; Height = image.Height; - LoadComponents(Format); + LoadComponents(Format, isBc4Alpha); uint ImageSize = reader.ReadUInt32(); @@ -430,7 +434,7 @@ namespace FirstPlugin } } - private void LoadComponents(TEX_FORMAT Format) + private void LoadComponents(TEX_FORMAT Format, bool isBc4Alpha) { switch (Format) { @@ -446,7 +450,15 @@ namespace FirstPlugin RedChannel = STChannelType.Red; GreenChannel = STChannelType.Red; BlueChannel = STChannelType.Red; - AlphaChannel = STChannelType.Red; + AlphaChannel = STChannelType.One; + + if (isBc4Alpha) + { + RedChannel = STChannelType.One; + GreenChannel = STChannelType.One; + BlueChannel = STChannelType.One; + AlphaChannel = STChannelType.Red; + } break; } } diff --git a/Toolbox/Shader/Layout/Legacy/Bflyt.frag b/Toolbox/Shader/Layout/Legacy/Bflyt.frag index 43522fc4..57a134c8 100644 --- a/Toolbox/Shader/Layout/Legacy/Bflyt.frag +++ b/Toolbox/Shader/Layout/Legacy/Bflyt.frag @@ -12,8 +12,12 @@ uniform int hasTexture0; uniform int hasTexture1; uniform int hasTexture2; +uniform int ThresholdingAlphaInterpolation; + uniform sampler2D uvTestPattern; +#define gamma 2.2 + void main() { vec4 textureMap0 = vec4(1); @@ -28,11 +32,24 @@ void main() if (debugShading == 0) { - vec3 whiteInterpolation = whiteColor.rgb * textureMap0.rgb; + // Convert to sRGB. + vec3 whiteColorSRGB = pow(whiteColor.rgb, vec3(1.0 / gamma)); + + vec3 whiteInterpolation = whiteColorSRGB.rgb * textureMap0.rgb; vec3 blackInterpolation = (vec3(1) - textureMap0.rgb) * blackColor.rgb; + vec3 colorBlend = whiteInterpolation + blackInterpolation; float alpha = textureMap0.a * whiteColor.a; + if (ThresholdingAlphaInterpolation != 0) + { + //Todo these need to interpolate and be smoother + if (textureMap0.a >= whiteColor.a) alpha = 1.0; + if (textureMap0.a <= blackColor.a) alpha = 0.0; + // if (blackColor.a < alpha && alpha < whiteColor.a) + // alpha = mix(0.0, 1.0, textureMap0.a); + } + gl_FragColor = gl_Color * vec4(colorBlend,alpha); } else if (debugShading == 5)