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.
This commit is contained in:
KillzXGaming 2019-09-19 18:02:10 -04:00
parent bb436b35e8
commit 314d1e7c20
10 changed files with 140 additions and 33 deletions

View file

@ -93,6 +93,14 @@ namespace LayoutBXLYT
GL.Uniform1(uniforms[name], value); 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) public void SetColor(string name, Color color)
{ {
if (uniforms.ContainsKey(name)) if (uniforms.ContainsKey(name))

View file

@ -60,7 +60,7 @@ namespace LayoutBXLYT
{ {
var pic1Pane = pane as Cafe.BFLYT.PIC1; var pic1Pane = pane as Cafe.BFLYT.PIC1;
Color[] Colors = new Color[] { Color[] Colors = new Color[] {
pic1Pane.ColorBottomRight.Color, pic1Pane.ColorBottomRight.Color,
pic1Pane.ColorBottomLeft.Color, pic1Pane.ColorBottomLeft.Color,
pic1Pane.ColorTopLeft.Color, pic1Pane.ColorTopLeft.Color,
@ -90,9 +90,6 @@ namespace LayoutBXLYT
DrawRectangle(pane, pane.Rectangle, TexCoords, Colors, false, effectiveAlpha); DrawRectangle(pane, pane.Rectangle, TexCoords, Colors, false, effectiveAlpha);
mat.Shader.Disable(); mat.Shader.Disable();
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.PopAttrib();
} }
else if (pane is BCLYT.PIC1) else if (pane is BCLYT.PIC1)
{ {
@ -128,9 +125,6 @@ namespace LayoutBXLYT
DrawRectangle(pane, pane.Rectangle, TexCoords, Colors, false, effectiveAlpha); DrawRectangle(pane, pane.Rectangle, TexCoords, Colors, false, effectiveAlpha);
mat.Shader.Disable(); mat.Shader.Disable();
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.PopAttrib();
} }
else if (pane is BRLYT.PIC1) else if (pane is BRLYT.PIC1)
{ {
@ -166,10 +160,12 @@ namespace LayoutBXLYT
DrawRectangle(pane, pane.Rectangle, TexCoords, Colors, false, effectiveAlpha); DrawRectangle(pane, pane.Rectangle, TexCoords, Colors, false, effectiveAlpha);
mat.Shader.Disable(); 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<BasePane> SelectedPanes) public static void DrawBoundryPane(BasePane pane, byte effectiveAlpha, List<BasePane> SelectedPanes)
@ -345,17 +341,14 @@ namespace LayoutBXLYT
if (!window.UseVertexColorForAll) if (!window.UseVertexColorForAll)
{ {
colors = new Color[] { colors = new Color[] {
Color.White, Color.White, Color.FromArgb(effectiveAlpha, 255,255,255),
Color.White, Color.White, 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) switch (window.FrameCount)
{ {
@ -478,6 +471,14 @@ namespace LayoutBXLYT
var matBL = window.WindowFrames[2].Material; var matBL = window.WindowFrames[2].Material;
var matBR = window.WindowFrames[3].Material; var matBR = window.WindowFrames[3].Material;
//Todo check this
if (window.UseOneMaterialForAll)
{
/* matTR = matTL;
matBL = matTL;
matBR = matTL;*/
}
if (matTL.TextureMaps.Length > 0) if (matTL.TextureMaps.Length > 0)
{ {
SetupShaders(matTL, Textures); SetupShaders(matTL, Textures);
@ -568,6 +569,18 @@ namespace LayoutBXLYT
var matL = window.WindowFrames[6].Material; var matL = window.WindowFrames[6].Material;
var matR = window.WindowFrames[7].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) if (matTL.TextureMaps.Length > 0)
{ {
SetupShaders(matTL, Textures); SetupShaders(matTL, Textures);
@ -973,8 +986,8 @@ namespace LayoutBXLYT
{ {
for (int i = 0; i < colors.Length; i++) for (int i = 0; i < colors.Length; i++)
{ {
uint setalpha = (uint)((colors[i].A * alpha) / 255); float outAlpha = BasePane.MixColors(colors[i].A, alpha);
colors[i] = Color.FromArgb((int)setalpha, colors[i]); colors[i] = Color.FromArgb(Utils.FloatToIntClamp(outAlpha), colors[i]);
} }
if (LayoutEditor.UseLegacyGL) if (LayoutEditor.UseLegacyGL)

View file

@ -209,6 +209,16 @@ namespace LayoutBXLYT.Cafe
if (!textures.ContainsKey(bflim.FileName)) if (!textures.ContainsKey(bflim.FileName))
textures.Add(bflim.FileName, bflim); 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")] [DisplayName("Name"), CategoryAttribute("General")]
public override string Name { get; set; } 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")] [DisplayName("Black Color"), CategoryAttribute("Color")]
public STColor8 BlackColor { get; set; } public STColor8 BlackColor { get; set; }
@ -2010,7 +2026,6 @@ namespace LayoutBXLYT.Cafe
var hasIndParam = Convert.ToBoolean((flags >> 14) & 0x1); var hasIndParam = Convert.ToBoolean((flags >> 14) & 0x1);
var projTexGenParamCount = Convert.ToUInt32((flags >> 15) & 0x3); var projTexGenParamCount = Convert.ToUInt32((flags >> 15) & 0x3);
var hasFontShadowParam = Convert.ToBoolean((flags >> 17) & 0x1); var hasFontShadowParam = Convert.ToBoolean((flags >> 17) & 0x1);
var thresholdingAlphaInterpolation = Convert.ToBoolean((flags >> 18) & 0x1);
TextureMaps = new TextureRef[texCount]; TextureMaps = new TextureRef[texCount];
TextureTransforms = new TextureTransform[mtxCount]; TextureTransforms = new TextureTransform[mtxCount];

View file

@ -31,6 +31,7 @@ namespace LayoutBXLYT
SetInt("textures0", 0); SetInt("textures0", 0);
SetInt("textures1", 0); SetInt("textures1", 0);
SetInt("textures2", 0); SetInt("textures2", 0);
SetBool("ThresholdingAlphaInterpolation", false);
SetVec2("uvScale0", new Vector2(1,1)); SetVec2("uvScale0", new Vector2(1,1));
SetFloat("uvRotate0", 0); SetFloat("uvRotate0", 0);
@ -47,18 +48,22 @@ namespace LayoutBXLYT
SetFloat("uvRotate0", 0); SetFloat("uvRotate0", 0);
SetVec2("uvTranslate0", new Vector2(0, 0)); SetVec2("uvTranslate0", new Vector2(0, 0));
SetInt("flipTexture", 0); SetInt("flipTexture", 0);
SetBool("ThresholdingAlphaInterpolation", material.ThresholdingAlphaInterpolation);
BindTextureUniforms(); BindTextureUniforms();
int id = 1;
for (int i = 0; i < material.TextureMaps.Length; i++) for (int i = 0; i < material.TextureMaps.Length; i++)
{ {
if (textures.ContainsKey(material.TextureMaps[i].Name)) if (textures.ContainsKey(material.TextureMaps[i].Name))
{ {
GL.ActiveTexture(TextureUnit.Texture0 + i); GL.ActiveTexture(TextureUnit.Texture0 + id);
SetInt($"textures{i}", 0); SetInt($"textures{i}", id);
bool isBinded = BxlytToGL.BindGLTexture(material.TextureMaps[i], textures[material.TextureMaps[i].Name]); bool isBinded = BxlytToGL.BindGLTexture(material.TextureMaps[i], textures[material.TextureMaps[i].Name]);
if (isBinded) if (isBinded)
SetInt($"hasTexture{i}", 1); SetInt($"hasTexture{i}", 1);
id++;
} }
} }

View file

@ -4,21 +4,21 @@ namespace LayoutBXLYT.Cafe
{ {
public class TevStage public class TevStage
{ {
public byte RGBMode { get; set; } public TevMode RGBMode { get; set; }
public byte AlphaMode { get; set; } public TevMode AlphaMode { get; set; }
public ushort unk { get; set; } public ushort unk { get; set; }
public TevStage(FileReader reader, BFLYT.Header header) public TevStage(FileReader reader, BFLYT.Header header)
{ {
RGBMode = reader.ReadByte(); RGBMode = (TevMode)reader.ReadByte();
AlphaMode = reader.ReadByte(); AlphaMode = (TevMode)reader.ReadByte();
unk = reader.ReadUInt16(); unk = reader.ReadUInt16();
} }
public void Write(FileWriter writer) public void Write(FileWriter writer)
{ {
writer.Write(RGBMode); writer.Write(RGBMode, false);
writer.Write(AlphaMode); writer.Write(AlphaMode, false);
writer.Write(unk); writer.Write(unk);
} }
} }

View file

@ -30,6 +30,7 @@ namespace LayoutBXLYT
SetInt("textures0", 0); SetInt("textures0", 0);
SetInt("textures1", 0); SetInt("textures1", 0);
SetInt("textures2", 0); SetInt("textures2", 0);
SetBool("ThresholdingAlphaInterpolation", false);
SetVec2("uvScale0", new Vector2(1, 1)); SetVec2("uvScale0", new Vector2(1, 1));
SetFloat("uvRotate0", 0); SetFloat("uvRotate0", 0);
@ -46,6 +47,7 @@ namespace LayoutBXLYT
SetFloat("uvRotate0", 0); SetFloat("uvRotate0", 0);
SetVec2("uvTranslate0", new Vector2(0, 0)); SetVec2("uvTranslate0", new Vector2(0, 0));
SetInt("flipTexture", 0); SetInt("flipTexture", 0);
SetBool("ThresholdingAlphaInterpolation", material.ThresholdingAlphaInterpolation);
BindTextureUniforms(); BindTextureUniforms();

View file

@ -124,6 +124,20 @@ namespace LayoutBXLYT
parentTransform.W); 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 //Get the previous transform from the parent origin
private Vector4 ParentOriginTransform(Vector4 points) private Vector4 ParentOriginTransform(Vector4 points)
{ {
@ -439,6 +453,22 @@ namespace LayoutBXLYT
Rotate270 = 5 Rotate270 = 5
} }
public enum TevMode : byte
{
Replace,
Modulate,
Add,
AddSigned,
Interpolate,
Subtract,
AddMultiplicate,
MultiplcateAdd,
Overlay,
Indirect,
BlendIndirect,
EachIndirect,
}
public interface IWindowPane public interface IWindowPane
{ {
bool UseOneMaterialForAll { get; set; } bool UseOneMaterialForAll { get; set; }
@ -756,6 +786,9 @@ namespace LayoutBXLYT
[DisplayName("Name"), CategoryAttribute("General")] [DisplayName("Name"), CategoryAttribute("General")]
public virtual string Name { get; set; } public virtual string Name { get; set; }
[DisplayName("Thresholding Alpha Interpolation"), CategoryAttribute("Alpha")]
public virtual bool ThresholdingAlphaInterpolation { get; set; }
[Browsable(false)] [Browsable(false)]
public virtual BxlytShader Shader { get; set; } public virtual BxlytShader Shader { get; set; }

View file

@ -30,6 +30,7 @@ namespace LayoutBXLYT
SetInt("textures0", 0); SetInt("textures0", 0);
SetInt("textures1", 0); SetInt("textures1", 0);
SetInt("textures2", 0); SetInt("textures2", 0);
SetBool("ThresholdingAlphaInterpolation", false);
SetVec2("uvScale0", new Vector2(1, 1)); SetVec2("uvScale0", new Vector2(1, 1));
SetFloat("uvRotate0", 0); SetFloat("uvRotate0", 0);
@ -46,6 +47,7 @@ namespace LayoutBXLYT
SetFloat("uvRotate0", 0); SetFloat("uvRotate0", 0);
SetVec2("uvTranslate0", new Vector2(0, 0)); SetVec2("uvTranslate0", new Vector2(0, 0));
SetInt("flipTexture", 0); SetInt("flipTexture", 0);
SetBool("ThresholdingAlphaInterpolation", material.ThresholdingAlphaInterpolation);
BindTextureUniforms(); BindTextureUniforms();

View file

@ -265,6 +265,7 @@ namespace FirstPlugin
bflim.Width = bflim.image.Width; bflim.Width = bflim.image.Width;
bflim.Height = bflim.image.Height; bflim.Height = bflim.image.Height;
bflim.ImageData = ftex.texture.Data; bflim.ImageData = ftex.texture.Data;
var form = new GenericEditorForm(false, bflim.OpenForm()); var form = new GenericEditorForm(false, bflim.OpenForm());
LibraryGUI.CreateMdiWindow(form); LibraryGUI.CreateMdiWindow(form);
@ -410,15 +411,18 @@ namespace FirstPlugin
image = new Image(Is3DS); image = new Image(Is3DS);
image.Read(reader); image.Read(reader);
bool isBc4Alpha = image.BflimFormat == 16;
if (Is3DS) if (Is3DS)
Format = Formats3DS[image.BflimFormat]; Format = Formats3DS[image.BflimFormat];
else else
Format = FormatsWiiU[image.BflimFormat]; Format = FormatsWiiU[image.BflimFormat];
Width = image.Width; Width = image.Width;
Height = image.Height; Height = image.Height;
LoadComponents(Format); LoadComponents(Format, isBc4Alpha);
uint ImageSize = reader.ReadUInt32(); 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) switch (Format)
{ {
@ -446,7 +450,15 @@ namespace FirstPlugin
RedChannel = STChannelType.Red; RedChannel = STChannelType.Red;
GreenChannel = STChannelType.Red; GreenChannel = STChannelType.Red;
BlueChannel = 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; break;
} }
} }

View file

@ -12,8 +12,12 @@ uniform int hasTexture0;
uniform int hasTexture1; uniform int hasTexture1;
uniform int hasTexture2; uniform int hasTexture2;
uniform int ThresholdingAlphaInterpolation;
uniform sampler2D uvTestPattern; uniform sampler2D uvTestPattern;
#define gamma 2.2
void main() void main()
{ {
vec4 textureMap0 = vec4(1); vec4 textureMap0 = vec4(1);
@ -28,11 +32,24 @@ void main()
if (debugShading == 0) 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 blackInterpolation = (vec3(1) - textureMap0.rgb) * blackColor.rgb;
vec3 colorBlend = whiteInterpolation + blackInterpolation; vec3 colorBlend = whiteInterpolation + blackInterpolation;
float alpha = textureMap0.a * whiteColor.a; 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); gl_FragColor = gl_Color * vec4(colorBlend,alpha);
} }
else if (debugShading == 5) else if (debugShading == 5)