Load BRLYT and BCLYT picture panes and bound boxes

This commit is contained in:
KillzXGaming 2019-09-08 20:47:19 -04:00
parent aa374d6241
commit b05b526f3f
13 changed files with 638 additions and 307 deletions

View file

@ -87,6 +87,8 @@ namespace FirstPlugin
CanRenameFiles = true;
CanReplaceFiles = true;
files.Clear();
using (var reader = new FileReader(stream))
{
_savedDirectories.Clear();
@ -159,6 +161,7 @@ namespace FirstPlugin
entry.FileData = reader.ReadBytes((int)entry.Size);
}
entry.FileName = entry.Name;
files.Add(entry);
Directories[dir].AddNode(entry);
}

View file

@ -0,0 +1,290 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Threading.Tasks;
using OpenTK.Graphics.OpenGL;
using Toolbox.Library;
using Toolbox.Library.IO;
using OpenTK;
namespace LayoutBXLYT
{
public class BxlytToGL
{
public static int ConvertTextureWrap(WrapMode wrapMode)
{
switch (wrapMode)
{
case WrapMode.Clamp: return (int)TextureWrapMode.Clamp;
case WrapMode.Mirror: return (int)TextureWrapMode.MirroredRepeat;
case WrapMode.Repeat: return (int)TextureWrapMode.Repeat;
default: return (int)TextureWrapMode.Clamp;
}
}
public static int ConvertMagFilterMode(FilterMode filterMode)
{
switch (filterMode)
{
case FilterMode.Linear: return (int)TextureMagFilter.Linear;
case FilterMode.Near: return (int)TextureMagFilter.Nearest;
default: return (int)TextureMagFilter.Linear;
}
}
public static int ConvertMinFilterMode(FilterMode filterMode)
{
switch (filterMode)
{
case FilterMode.Linear: return (int)TextureMinFilter.Linear;
case FilterMode.Near: return (int)TextureMinFilter.Nearest;
default: return (int)TextureMinFilter.Linear;
}
}
public static void DrawPictureBox(BasePane pane, byte effectiveAlpha, Dictionary<string, STGenericTexture> Textures)
{
Vector2[] TexCoords = new Vector2[] {
new Vector2(1,1),
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,0)
};
if (pane is Cafe.BFLYT.PIC1)
{
var pic1Pane = pane as Cafe.BFLYT.PIC1;
Color[] Colors = new Color[] {
pic1Pane.ColorTopLeft.Color,
pic1Pane.ColorTopRight.Color,
pic1Pane.ColorBottomRight.Color,
pic1Pane.ColorBottomLeft.Color,
};
var mat = pic1Pane.Material;
if (mat.Shader == null)
{
mat.Shader = new BflytShader(mat);
mat.Shader.Compile();
}
mat.Shader.Enable();
((BflytShader)mat.Shader).SetMaterials(Textures);
if (pic1Pane.TexCoords.Length > 0)
{
TexCoords = new Vector2[] {
pic1Pane.TexCoords[0].TopLeft.ToTKVector2(),
pic1Pane.TexCoords[0].TopRight.ToTKVector2(),
pic1Pane.TexCoords[0].BottomRight.ToTKVector2(),
pic1Pane.TexCoords[0].BottomLeft.ToTKVector2(),
};
}
DrawRectangle(pane.Rectangle, TexCoords, Colors, false, effectiveAlpha);
mat.Shader.Disable();
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.PopAttrib();
}
else if (pane is BCLYT.PIC1)
{
var pic1Pane = pane as BCLYT.PIC1;
Color[] Colors = new Color[] {
pic1Pane.ColorTopLeft.Color,
pic1Pane.ColorTopRight.Color,
pic1Pane.ColorBottomRight.Color,
pic1Pane.ColorBottomLeft.Color,
};
var mat = pic1Pane.Material;
if (mat.Shader == null)
{
mat.Shader = new BclytShader(mat);
mat.Shader.Compile();
}
mat.Shader.Enable();
((BclytShader)mat.Shader).SetMaterials(Textures);
if (pic1Pane.TexCoords.Length > 0)
{
TexCoords = new Vector2[] {
pic1Pane.TexCoords[0].TopLeft.ToTKVector2(),
pic1Pane.TexCoords[0].TopRight.ToTKVector2(),
pic1Pane.TexCoords[0].BottomRight.ToTKVector2(),
pic1Pane.TexCoords[0].BottomLeft.ToTKVector2(),
};
}
DrawRectangle(pane.Rectangle, TexCoords, Colors, false, effectiveAlpha);
mat.Shader.Disable();
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.PopAttrib();
}
else if (pane is BRLYT.PIC1)
{
var pic1Pane = pane as BRLYT.PIC1;
Color[] Colors = new Color[] {
pic1Pane.ColorTopLeft.Color,
pic1Pane.ColorTopRight.Color,
pic1Pane.ColorBottomRight.Color,
pic1Pane.ColorBottomLeft.Color,
};
var mat = pic1Pane.Material;
if (mat.Shader == null)
{
mat.Shader = new BrlytShader(mat);
mat.Shader.Compile();
}
mat.Shader.Enable();
((BrlytShader)mat.Shader).SetMaterials(Textures);
if (pic1Pane.TexCoords.Length > 0)
{
TexCoords = new Vector2[] {
pic1Pane.TexCoords[0].TopLeft.ToTKVector2(),
pic1Pane.TexCoords[0].TopRight.ToTKVector2(),
pic1Pane.TexCoords[0].BottomRight.ToTKVector2(),
pic1Pane.TexCoords[0].BottomLeft.ToTKVector2(),
};
}
DrawRectangle(pane.Rectangle, TexCoords, Colors, false, effectiveAlpha);
mat.Shader.Disable();
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.PopAttrib();
}
}
public static void DrawBoundryPane(BasePane pane, byte effectiveAlpha, List<BasePane> SelectedPanes)
{
Vector2[] TexCoords = new Vector2[] {
new Vector2(1,1),
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,0)
};
Color color = Color.DarkGreen;
if (SelectedPanes.Contains(pane))
color = Color.Red;
color = Color.FromArgb(70, color);
Color[] Colors = new Color[] {
color,
color,
color,
color,
};
BxlytToGL.DrawRectangle(pane.Rectangle, TexCoords, Colors, false, effectiveAlpha);
}
public static bool BindGLTexture(BxlytTextureRef tex, STGenericTexture texture)
{
if (texture.RenderableTex == null || !texture.RenderableTex.GLInitialized)
texture.LoadOpenGLTexture();
//If the texture is still not initialized then return
if (!texture.RenderableTex.GLInitialized)
return false;
GL.BindTexture(TextureTarget.Texture2D, texture.RenderableTex.TexID);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureSwizzleR, ConvertChannel(texture.RedChannel));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureSwizzleG, ConvertChannel(texture.GreenChannel));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureSwizzleB, ConvertChannel(texture.BlueChannel));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureSwizzleA, ConvertChannel(texture.AlphaChannel));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, ConvertTextureWrap(tex.WrapModeU));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, ConvertTextureWrap(tex.WrapModeV));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, ConvertMagFilterMode(tex.MaxFilterMode));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, ConvertMinFilterMode(tex.MinFilterMode));
return true;
}
private static int ConvertChannel(STChannelType type)
{
switch (type)
{
case STChannelType.Red: return (int)TextureSwizzle.Red;
case STChannelType.Green: return (int)TextureSwizzle.Green;
case STChannelType.Blue: return (int)TextureSwizzle.Blue;
case STChannelType.Alpha: return (int)TextureSwizzle.Alpha;
case STChannelType.Zero: return (int)TextureSwizzle.Zero;
case STChannelType.One: return (int)TextureSwizzle.One;
default: return 0;
}
}
enum TextureSwizzle
{
Zero = All.Zero,
One = All.One,
Red = All.Red,
Green = All.Green,
Blue = All.Blue,
Alpha = All.Alpha,
}
public static void DrawRectangle(CustomRectangle rect, Vector2[] texCoords,
Color[] colors, bool useLines = true, byte alpha = 255)
{
for (int i = 0; i < colors.Length; i++)
{
uint setalpha = (uint)((colors[i].A * alpha) / 255);
colors[i] = Color.FromArgb((int)setalpha, colors[i]);
}
if (useLines)
{
GL.Begin(PrimitiveType.LineLoop);
GL.Color4(colors[0]);
GL.Vertex2(rect.LeftPoint, rect.BottomPoint);
GL.Vertex2(rect.RightPoint, rect.BottomPoint);
GL.Vertex2(rect.RightPoint, rect.TopPoint);
GL.Vertex2(rect.LeftPoint, rect.TopPoint);
GL.End();
}
else
{
GL.Begin(PrimitiveType.Quads);
GL.Color4(colors[0]);
GL.MultiTexCoord2(TextureUnit.Texture0, texCoords[0].X, texCoords[0].Y);
GL.Vertex2(rect.LeftPoint, rect.BottomPoint);
GL.Color4(colors[1]);
GL.MultiTexCoord2(TextureUnit.Texture0, texCoords[1].X, texCoords[1].Y);
GL.Vertex2(rect.RightPoint, rect.BottomPoint);
GL.Color4(colors[2]);
GL.MultiTexCoord2(TextureUnit.Texture0, texCoords[2].X, texCoords[2].Y);
GL.Vertex2(rect.RightPoint, rect.TopPoint);
GL.Color4(colors[3]);
GL.MultiTexCoord2(TextureUnit.Texture0, texCoords[3].X, texCoords[3].Y);
GL.Vertex2(rect.LeftPoint, rect.TopPoint);
GL.End();
//Draw outline
GL.Begin(PrimitiveType.LineLoop);
GL.LineWidth(3);
GL.Color4(colors[0]);
GL.Vertex2(rect.LeftPoint, rect.BottomPoint);
GL.Vertex2(rect.RightPoint, rect.BottomPoint);
GL.Vertex2(rect.RightPoint, rect.TopPoint);
GL.Vertex2(rect.LeftPoint, rect.TopPoint);
GL.End();
}
}
}
}

View file

@ -1181,6 +1181,8 @@ namespace LayoutBXLYT.Cafe
{
public override string Signature { get; } = "prt1";
private bool hasSearchedParts = false;
public PRT1() : base()
{
@ -1202,6 +1204,8 @@ namespace LayoutBXLYT.Cafe
public BasePane GetExternalPane()
{
if (hasSearchedParts) return null;
if (ExternalLayout == null)
ExternalLayout = SearchExternalFile();
@ -1214,6 +1218,8 @@ namespace LayoutBXLYT.Cafe
//Get textures if possible from the external parts file
public void UpdateTextureData(Dictionary<string, STGenericTexture> textures)
{
if (hasSearchedParts) return;
if (ExternalLayout == null)
{
ExternalLayout = SearchExternalFile();
@ -1231,6 +1237,8 @@ namespace LayoutBXLYT.Cafe
private BFLYT SearchExternalFile()
{
hasSearchedParts = false;
var fileFormat = layoutFile.FileInfo;
string path = FileManager.GetSourcePath(fileFormat);
@ -1240,13 +1248,12 @@ namespace LayoutBXLYT.Cafe
string folder = Path.GetDirectoryName(path);
foreach (var file in Directory.GetFiles(folder))
{
Console.WriteLine("checking " + file.Contains(LayoutFile));
if (file.Contains(LayoutFile))
{
var openedFile = STFileLoader.OpenFileFormat(file);
if (openedFile is IArchiveFile)
{
var bflyt = new BFLYT();
BFLYT bflyt = null;
SearchArchive((IArchiveFile)openedFile, ref bflyt);
if (bflyt != null)
return bflyt;
@ -1280,15 +1287,21 @@ namespace LayoutBXLYT.Cafe
foreach (var file in archiveFile.Files)
{
if (file.FileName.Contains(LayoutFile))
if (file.FileName.Contains(".lyarc"))
{
var openedFile = file.OpenFile();
if (openedFile is IArchiveFile)
SearchArchive((IArchiveFile)openedFile, ref layoutFile);
}
else if (file.FileName.Contains(LayoutFile))
{
Console.WriteLine("Part found! " + file.FileName);
var openedFile = file.OpenFile();
if (openedFile is IArchiveFile)
SearchArchive((IArchiveFile)openedFile, ref layoutFile);
else if (openedFile is BFLYT)
{
Console.WriteLine("Part found! " + file.FileName);
layoutFile = openedFile as BFLYT;
layoutFile.IFileInfo = new IFileInfo();
layoutFile.IFileInfo.ArchiveParent = layoutFile.IFileInfo.ArchiveParent;

View file

@ -50,7 +50,7 @@ namespace LayoutBXLYT
{
GL.ActiveTexture(TextureUnit.Texture0);
SetInt("textures0", 0);
bool isBinded = LayoutViewer.BindGLTexture(material.TextureMaps[0], textures[textureMap0]);
bool isBinded = BxlytToGL.BindGLTexture(material.TextureMaps[0], textures[textureMap0]);
if (isBinded)
SetInt("hasTexture0", 1);
}
@ -81,10 +81,10 @@ namespace LayoutBXLYT
if (material.TextureMaps.Length > 0)
{
var tex = material.TextureMaps[0];
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, ConvertTextureWrap(tex.WrapModeU));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, ConvertTextureWrap(tex.WrapModeV));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, ConvertMagFilterMode(tex.MaxFilterMode));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, ConvertMinFilterMode(tex.MinFilterMode));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, BxlytToGL.ConvertTextureWrap(tex.WrapModeU));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, BxlytToGL.ConvertTextureWrap(tex.WrapModeV));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, BxlytToGL.ConvertMagFilterMode(tex.MaxFilterMode));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, BxlytToGL.ConvertMinFilterMode(tex.MinFilterMode));
}
}
@ -105,36 +105,5 @@ namespace LayoutBXLYT
return System.IO.File.ReadAllText(path);
}
}
private static int ConvertTextureWrap(TextureRef.WrapMode wrapMode)
{
switch (wrapMode)
{
case TextureRef.WrapMode.Clamp: return (int)TextureWrapMode.Clamp;
case TextureRef.WrapMode.Mirror: return (int)TextureWrapMode.MirroredRepeat;
case TextureRef.WrapMode.Repeat: return (int)TextureWrapMode.Repeat;
default: return (int)TextureWrapMode.Clamp;
}
}
private static int ConvertMagFilterMode(TextureRef.FilterMode filterMode)
{
switch (filterMode)
{
case TextureRef.FilterMode.Linear: return (int)TextureMagFilter.Linear;
case TextureRef.FilterMode.Near: return (int)TextureMagFilter.Nearest;
default: return (int)TextureRef.FilterMode.Linear;
}
}
private static int ConvertMinFilterMode(TextureRef.FilterMode filterMode)
{
switch (filterMode)
{
case TextureRef.FilterMode.Linear: return (int)TextureMinFilter.Linear;
case TextureRef.FilterMode.Near: return (int)TextureMinFilter.Nearest;
default: return (int)TextureRef.FilterMode.Linear;
}
}
}
}

View file

@ -2,29 +2,28 @@
namespace LayoutBXLYT.Cafe
{
public class TextureRef
public class TextureRef : BxlytTextureRef
{
public string Name { get; set; }
public short ID;
byte flag1;
byte flag2;
public WrapMode WrapModeU
public override WrapMode WrapModeU
{
get { return (WrapMode)(flag1 & 0x3); }
}
public WrapMode WrapModeV
public override WrapMode WrapModeV
{
get { return (WrapMode)(flag2 & 0x3); }
}
public FilterMode MinFilterMode
public override FilterMode MinFilterMode
{
get { return (FilterMode)((flag1 >> 2) & 0x3); }
}
public FilterMode MaxFilterMode
public override FilterMode MaxFilterMode
{
get { return (FilterMode)((flag2 >> 2) & 0x3); }
}
@ -47,18 +46,5 @@ namespace LayoutBXLYT.Cafe
writer.Write(flag1);
writer.Write(flag2);
}
public enum FilterMode
{
Near = 0,
Linear = 1
}
public enum WrapMode
{
Clamp = 0,
Repeat = 1,
Mirror = 2
}
}
}

View file

@ -736,7 +736,7 @@ namespace LayoutBXLYT
}
}
public bool InfluenceAlpha
public override bool InfluenceAlpha
{
get { return (_flags1 & 0x2) == 0x2; }
set
@ -750,8 +750,6 @@ namespace LayoutBXLYT
public byte Origin { get; set; }
public byte Alpha { get; set; }
public byte PartsScale { get; set; }
public byte PaneMagFlags { get; set; }
@ -921,7 +919,7 @@ namespace LayoutBXLYT
uint texCount = Convert.ToUInt32(flags & 3);
uint mtxCount = Convert.ToUInt32(flags >> 2) & 3;
for (int i = 0; i < texCount; i++)
TextureMaps.Add(new TextureRef(reader));
TextureMaps.Add(new TextureRef(reader, header));
for (int i = 0; i < mtxCount; i++)
TextureTransforms.Add(new TextureTransform(reader));
@ -965,38 +963,41 @@ namespace LayoutBXLYT
}
}
public class TextureRef
public class TextureRef : BxlytTextureRef
{
public short ID;
byte flag1;
byte flag2;
public WrapMode WrapModeU
public override WrapMode WrapModeU
{
get { return (WrapMode)(flag1 & 0x3); }
}
public WrapMode WrapModeV
public override WrapMode WrapModeV
{
get { return (WrapMode)(flag2 & 0x3); }
}
public FilterMode MinFilterMode
public override FilterMode MinFilterMode
{
get { return (FilterMode)((flag1 >> 2) & 0x3); }
}
public FilterMode MaxFilterMode
public override FilterMode MaxFilterMode
{
get { return (FilterMode)((flag2 >> 2) & 0x3); }
}
public TextureRef() {}
public TextureRef(FileReader reader) {
public TextureRef(FileReader reader, Header header) {
ID = reader.ReadInt16();
flag1 = reader.ReadByte();
flag2 = reader.ReadByte();
if (header.Textures.Count > 0)
Name = header.Textures[ID];
}
public void Write(FileWriter writer)
@ -1005,19 +1006,6 @@ namespace LayoutBXLYT
writer.Write(flag1);
writer.Write(flag2);
}
public enum FilterMode
{
Near = 0,
Linear = 1
}
public enum WrapMode
{
Clamp = 0,
Repeat = 1,
Mirror = 2
}
}
public class FNL1 : SectionCommon

View file

@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using OpenTK.Graphics.OpenGL;
using OpenTK;
using Toolbox.Library;
namespace LayoutBXLYT
{
public class BclytShader : BxlytShader
{
public BCLYT.Material material;
public BclytShader(BCLYT.Material mat) : base()
{
material = mat;
LoadShaders();
}
public override void OnCompiled()
{
SetColor("whiteColor", Color.FromArgb(255,255,255,255));
SetColor("blackColor", Color.FromArgb(0, 0, 0, 0));
SetInt("debugShading", 0);
SetInt("hasTexture0", 0);
SetInt("numTextureMaps", 0);
SetVec2("uvScale0", new Vector2(1,1));
SetFloat("uvRotate0", 0);
SetVec2("uvTranslate0", new Vector2(0, 0));
}
public void SetMaterials(Dictionary<string, STGenericTexture> textures)
{
SetColor("whiteColor", Color.FromArgb(255, 255, 255, 255));
SetColor("blackColor", Color.FromArgb(0, 0, 0, 0));
SetInt("debugShading", (int)Runtime.LayoutEditor.Shading);
SetInt("numTextureMaps", material.TextureMaps.Count);
BindTextureUniforms();
string textureMap0 = "";
if (material.TextureMaps.Count > 0)
textureMap0 = material.GetTexture(0);
if (textures.ContainsKey(textureMap0))
{
GL.ActiveTexture(TextureUnit.Texture0);
SetInt("textures0", 0);
bool isBinded = BxlytToGL.BindGLTexture(material.TextureMaps[0], textures[textureMap0]);
if (isBinded)
SetInt("hasTexture0", 1);
}
if (material.TextureTransforms.Count > 0)
{
var transform = material.TextureTransforms[0];
float shiftX = 0;
float shiftY = 0;
if (transform.Scale.X < 0)
shiftX = 1;
if (transform.Scale.Y < 0)
shiftY = 1;
SetVec2("uvScale0",new Vector2(transform.Scale.X, transform.Scale.Y));
SetFloat("uvRotate0", transform.Rotate);
SetVec2("uvTranslate0",new Vector2(shiftX + transform.Translate.X, shiftY + transform.Translate.Y));
}
}
private void BindTextureUniforms()
{
//Do uv test pattern
GL.ActiveTexture(TextureUnit.Texture10);
GL.Uniform1(GL.GetUniformLocation(program, "uvTestPattern"), 10);
GL.BindTexture(TextureTarget.Texture2D, RenderTools.uvTestPattern.RenderableTex.TexID);
if (material.TextureMaps.Count > 0)
{
var tex = material.TextureMaps[0];
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, BxlytToGL.ConvertTextureWrap(tex.WrapModeU));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, BxlytToGL.ConvertTextureWrap(tex.WrapModeV));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, BxlytToGL.ConvertMagFilterMode(tex.MaxFilterMode));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, BxlytToGL.ConvertMinFilterMode(tex.MinFilterMode));
}
}
public override string VertexShader
{
get
{
string path = System.IO.Path.Combine(Runtime.ExecutableDir, "Shader", "Layout", "Bflyt.vert");
return System.IO.File.ReadAllText(path);
}
}
public override string FragmentShader
{
get
{
string path = System.IO.Path.Combine(Runtime.ExecutableDir, "Shader", "Layout", "Bflyt.frag");
return System.IO.File.ReadAllText(path);
}
}
}
}

View file

@ -191,6 +191,19 @@ namespace LayoutBXLYT
}
}
public enum FilterMode
{
Near = 0,
Linear = 1
}
public enum WrapMode
{
Clamp = 0,
Repeat = 1,
Mirror = 2
}
public enum OriginX : byte
{
Center = 0,
@ -210,6 +223,16 @@ namespace LayoutBXLYT
UserData UserData { get; set; }
}
public class BxlytTextureRef
{
public string Name { get; set; }
public virtual WrapMode WrapModeU { get; set; }
public virtual WrapMode WrapModeV { get; set; }
public virtual FilterMode MinFilterMode { get; set; }
public virtual FilterMode MaxFilterMode { get; set; }
}
public class UserData : SectionCommon
{
public List<UserDataEntry> Entries { get; set; }

View file

@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using Toolbox;
using System.Windows.Forms;
using Toolbox.Library;
@ -12,6 +12,7 @@ using FirstPlugin.Forms;
using Syroot.Maths;
using SharpYaml.Serialization;
using FirstPlugin;
using static Toolbox.Library.IO.Bit;
namespace LayoutBXLYT
{
@ -120,8 +121,9 @@ namespace LayoutBXLYT
file.FileFormat = tpl;
foreach (var tex in tpl.IconTextureList)
{
//Only need the first texture
if (!textures.ContainsKey(tex.Text))
textures.Add(tex.Text, tex);
textures.Add(file.FileName, tex);
}
}
}
@ -656,6 +658,12 @@ namespace LayoutBXLYT
return ParentLayout.TextureList.Textures[mat.TextureMaps[index].ID];
}
[TypeConverter(typeof(ExpandableObjectConverter))]
public Material Material
{
get { return ParentLayout.MaterialList.Materials[MaterialIndex]; }
}
private BRLYT.Header ParentLayout;
public PIC1() : base() {
@ -720,7 +728,7 @@ namespace LayoutBXLYT
}
}
public bool InfluenceAlpha
public override bool InfluenceAlpha
{
get { return (_flags1 & 0x2) == 0x2; }
set
@ -739,8 +747,6 @@ namespace LayoutBXLYT
public byte Origin { get; set; }
public byte Alpha { get; set; }
public byte PartsScale { get; set; }
public byte PaneMagFlags { get; set; }
@ -869,10 +875,8 @@ namespace LayoutBXLYT
public class Material : BxlytMaterial
{
public string Name { get; set; }
public STColor16 ForeColor { get; set; }
public STColor16 BackColor { get; set; }
public STColor16 WhiteColor { get; set; }
public STColor16 BlackColor { get; set; }
public STColor16 ColorRegister3 { get; set; }
public STColor8 TevColor1 { get; set; }
@ -884,7 +888,6 @@ namespace LayoutBXLYT
public List<TextureTransform> TextureTransforms { get; set; }
private uint flags;
private int unknown;
private BRLYT.Header ParentLayout;
public string GetTexture(int index)
@ -910,8 +913,8 @@ namespace LayoutBXLYT
Name = reader.ReadString(0x14, true);
ForeColor = reader.ReadColor16RGBA();
BackColor = reader.ReadColor16RGBA();
WhiteColor = reader.ReadColor16RGBA();
BlackColor = reader.ReadColor16RGBA();
ColorRegister3 = reader.ReadColor16RGBA();
TevColor1 = reader.ReadColor8RGBA();
TevColor2 = reader.ReadColor8RGBA();
@ -919,10 +922,12 @@ namespace LayoutBXLYT
TevColor4 = reader.ReadColor8RGBA();
flags = reader.ReadUInt32();
uint texCount = Convert.ToUInt32(flags & 3);
uint mtxCount = Convert.ToUInt32(flags >> 2) & 3;
uint indSrtCount = ExtractBits(flags, 2, 17);
uint texCoordGenCount = ExtractBits(flags, 4, 20);
uint mtxCount = ExtractBits(flags, 4, 24);
uint texCount = ExtractBits(flags, 4, 28);
for (int i = 0; i < texCount; i++)
TextureMaps.Add(new TextureRef(reader));
TextureMaps.Add(new TextureRef(reader, header));
for (int i = 0; i < mtxCount; i++)
TextureTransforms.Add(new TextureTransform(reader));
@ -966,35 +971,38 @@ namespace LayoutBXLYT
}
}
public class TextureRef
public class TextureRef : BxlytTextureRef
{
public short ID;
byte flag1;
byte flag2;
public WrapMode WrapModeU
public override WrapMode WrapModeU
{
get { return (WrapMode)(flag1 & 0x3); }
}
public WrapMode WrapModeV
public override WrapMode WrapModeV
{
get { return (WrapMode)(flag2 & 0x3); }
}
public FilterMode MinFilterMode
public override FilterMode MinFilterMode
{
get { return (FilterMode)((flag1 >> 2) & 0x3); }
}
public FilterMode MaxFilterMode
public override FilterMode MaxFilterMode
{
get { return (FilterMode)((flag2 >> 2) & 0x3); }
}
public TextureRef() {}
public TextureRef(FileReader reader) {
public TextureRef(FileReader reader, Header header) {
if (header.Textures.Count > 0)
Name = header.Textures[ID];
ID = reader.ReadInt16();
flag1 = reader.ReadByte();
flag2 = reader.ReadByte();
@ -1006,19 +1014,6 @@ namespace LayoutBXLYT
writer.Write(flag1);
writer.Write(flag2);
}
public enum FilterMode
{
Near = 0,
Linear = 1
}
public enum WrapMode
{
Clamp = 0,
Repeat = 1,
Mirror = 2
}
}
public class FNL1 : SectionCommon
@ -1038,11 +1033,15 @@ namespace LayoutBXLYT
reader.Seek(2); //padding
long pos = reader.Position;
uint[] offsets = reader.ReadUInt32s(numFonts);
for (int i = 0; i < offsets.Length; i++)
for (int i = 0; i < numFonts; i++)
{
reader.SeekBegin(offsets[i] + pos);
uint offset = reader.ReadUInt32();
reader.ReadUInt32();//padding
using (reader.TemporarySeek(offset + pos, System.IO.SeekOrigin.Begin))
{
Fonts.Add(reader.ReadZeroTerminatedString());
}
}
}
@ -1051,14 +1050,14 @@ namespace LayoutBXLYT
writer.Write((ushort)Fonts.Count);
writer.Seek(2);
//Fill empty spaces for offsets later
//Fill empty spaces for offsets later and also add padding
long pos = writer.Position;
writer.Write(new uint[Fonts.Count]);
writer.Write(new uint[Fonts.Count * 2]);
//Save offsets and strings
for (int i = 0; i < Fonts.Count; i++)
{
writer.WriteUint32Offset(pos + (i * 4), pos);
writer.WriteUint32Offset(pos + (i * 8), pos);
writer.WriteString(Fonts[i]);
}
}
@ -1081,12 +1080,14 @@ namespace LayoutBXLYT
reader.Seek(2); //padding
long pos = reader.Position;
uint[] offsets = reader.ReadUInt32s(numTextures);
for (int i = 0; i < offsets.Length; i++)
for (int i = 0; i < numTextures; i++)
{
reader.SeekBegin(offsets[i] + pos);
Textures.Add(reader.ReadZeroTerminatedString());
uint offset = reader.ReadUInt32();
reader.ReadUInt32();//padding
using (reader.TemporarySeek(offset + pos, System.IO.SeekOrigin.Begin)) {
Textures.Add(reader.ReadZeroTerminatedString());
}
}
}
@ -1095,14 +1096,14 @@ namespace LayoutBXLYT
writer.Write((ushort)Textures.Count);
writer.Seek(2);
//Fill empty spaces for offsets later
//Fill empty spaces for offsets later and also add another for padding
long pos = writer.Position;
writer.Write(new uint[Textures.Count]);
writer.Write(new uint[Textures.Count * 2]);
//Save offsets and strings
for (int i = 0; i < Textures.Count; i++)
{
writer.WriteUint32Offset(pos + (i * 4), pos);
writer.WriteUint32Offset(pos + (i * 8), pos);
writer.WriteString(Textures[i]);
}
}

View file

@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using OpenTK.Graphics.OpenGL;
using OpenTK;
using Toolbox.Library;
namespace LayoutBXLYT
{
public class BrlytShader : BxlytShader
{
public BRLYT.Material material;
public BrlytShader(BRLYT.Material mat) : base()
{
material = mat;
LoadShaders();
}
public override void OnCompiled()
{
SetColor("whiteColor", Color.FromArgb(255,255,255,255));
SetColor("blackColor", Color.FromArgb(0, 0, 0, 0));
SetInt("debugShading", 0);
SetInt("hasTexture0", 0);
SetInt("numTextureMaps", 0);
SetVec2("uvScale0", new Vector2(1,1));
SetFloat("uvRotate0", 0);
SetVec2("uvTranslate0", new Vector2(0, 0));
}
public void SetMaterials(Dictionary<string, STGenericTexture> textures)
{
SetColor("whiteColor", Color.FromArgb(255, material.WhiteColor.Color));
SetColor("blackColor", material.BlackColor.Color);
SetInt("debugShading", (int)Runtime.LayoutEditor.Shading);
SetInt("numTextureMaps", material.TextureMaps.Count);
BindTextureUniforms();
string textureMap0 = "";
if (material.TextureMaps.Count > 0)
textureMap0 = material.GetTexture(0);
if (textures.ContainsKey(textureMap0))
{
GL.ActiveTexture(TextureUnit.Texture0);
SetInt("textures0", 0);
bool isBinded = BxlytToGL.BindGLTexture(material.TextureMaps[0], textures[textureMap0]);
if (isBinded)
SetInt("hasTexture0", 1);
}
if (material.TextureTransforms.Count > 0)
{
var transform = material.TextureTransforms[0];
float shiftX = 0;
float shiftY = 0;
if (transform.Scale.X < 0)
shiftX = 1;
if (transform.Scale.Y < 0)
shiftY = 1;
SetVec2("uvScale0",new Vector2(transform.Scale.X, transform.Scale.Y));
SetFloat("uvRotate0", transform.Rotate);
SetVec2("uvTranslate0",new Vector2(shiftX + transform.Translate.X, shiftY + transform.Translate.Y));
}
}
private void BindTextureUniforms()
{
//Do uv test pattern
GL.ActiveTexture(TextureUnit.Texture10);
GL.Uniform1(GL.GetUniformLocation(program, "uvTestPattern"), 10);
GL.BindTexture(TextureTarget.Texture2D, RenderTools.uvTestPattern.RenderableTex.TexID);
if (material.TextureMaps.Count > 0)
{
var tex = material.TextureMaps[0];
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, BxlytToGL.ConvertTextureWrap(tex.WrapModeU));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, BxlytToGL.ConvertTextureWrap(tex.WrapModeV));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, BxlytToGL.ConvertMagFilterMode(tex.MaxFilterMode));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, BxlytToGL.ConvertMinFilterMode(tex.MinFilterMode));
}
}
public override string VertexShader
{
get
{
string path = System.IO.Path.Combine(Runtime.ExecutableDir, "Shader", "Layout", "Bflyt.vert");
return System.IO.File.ReadAllText(path);
}
}
public override string FragmentShader
{
get
{
string path = System.IO.Path.Combine(Runtime.ExecutableDir, "Shader", "Layout", "Bflyt.frag");
return System.IO.File.ReadAllText(path);
}
}
}
}

View file

@ -295,6 +295,7 @@
<Compile Include="FileFormats\HyruleWarriors\G1M\G1M.cs" />
<Compile Include="FileFormats\HyruleWarriors\LINKDATA.cs" />
<Compile Include="FileFormats\Layout\BxlytShader.cs" />
<Compile Include="FileFormats\Layout\BxlytToGL.cs" />
<Compile Include="FileFormats\Layout\CAFE\Materials\AlphaCompare.cs" />
<Compile Include="FileFormats\Layout\CAFE\Materials\BlendMode.cs" />
<Compile Include="FileFormats\Layout\CAFE\Materials\FontShadowParameter.cs" />
@ -306,9 +307,11 @@
<Compile Include="FileFormats\Layout\CAFE\Materials\TextureTransform.cs" />
<Compile Include="FileFormats\Layout\CTR\BCLYT.cs" />
<Compile Include="FileFormats\Layout\CAFE\FLYT.cs" />
<Compile Include="FileFormats\Layout\CTR\BrlytShader.cs" />
<Compile Include="FileFormats\Layout\LayoutTextureLoader.cs" />
<Compile Include="FileFormats\Layout\PaneTreeWrapper.cs" />
<Compile Include="FileFormats\Layout\Common.cs" />
<Compile Include="FileFormats\Layout\Rev\BrlytShader.cs" />
<Compile Include="FileFormats\Layout\Rev\BRLYT.cs" />
<Compile Include="FileFormats\Message\MSBP.cs" />
<Compile Include="FileFormats\CrashBandicoot\IGZ_TEX.cs" />

View file

@ -56,10 +56,7 @@ namespace LayoutBXLYT
Textures = textures;
if (bxlyt.Textures.Count > 0)
{
if (bxlyt.FileInfo is BFLYT)
Textures = ((BFLYT)bxlyt.FileInfo).GetTextures();
else if (bxlyt.FileInfo is BCLYT)
Textures = ((BCLYT)bxlyt.FileInfo).GetTextures();
Textures = bxlyt.GetTextures;
}
}
@ -185,7 +182,7 @@ namespace LayoutBXLYT
var rotate = partPane.Rotate + pane.Rotate;
GL.Translate(translate.X + translate.X, translate.Y, 0);
GL.Rotate(rotate.X, 1,0,0);
GL.Rotate(rotate.X, 1, 0, 0);
GL.Rotate(rotate.Y, 0, 1, 0);
GL.Rotate(rotate.Z, 0, 0, 1);
GL.Scale(scale.X, scale.Y, 1);
@ -207,24 +204,16 @@ namespace LayoutBXLYT
if (!isRoot)
{
if (pane is BFLYT.PIC1)
DrawPicturePane((BFLYT.PIC1)pane, effectiveAlpha, stage);
else if (pane is BCLYT.PIC1)
DrawPicturePane((BCLYT.PIC1)pane, effectiveAlpha);
else if (pane is BRLYT.PIC1)
DrawPicturePane((BRLYT.PIC1)pane, effectiveAlpha);
if (pane is BFLYT.PIC1 || pane is BCLYT.PIC1 || pane is BRLYT.PIC1)
BxlytToGL.DrawPictureBox(pane, effectiveAlpha, Textures);
else if (pane is BFLYT.BND1 || pane is BCLYT.BND1 || pane is BRLYT.BND1)
BxlytToGL.DrawBoundryPane(pane, effectiveAlpha, SelectedPanes);
else if (pane is BFLYT.WND1)
DrawWindowPane((BFLYT.WND1)pane, effectiveAlpha);
else if (pane is BFLYT.BND1)
DrawBoundryPane((BFLYT.BND1)pane, effectiveAlpha);
else if (pane is BFLYT.PRT1)
DrawPartsPane((BFLYT.PRT1)pane, effectiveAlpha, parentAlphaInfluence);
else if (pane is BFLYT.PAN1)
DrawDefaultPane((BFLYT.PAN1)pane);
else if (pane is BCLYT.PAN1)
DrawDefaultPane((BCLYT.PAN1)pane);
else if (pane is BRLYT.PAN1)
DrawDefaultPane((BRLYT.PAN1)pane);
else
DrawDefaultPane(pane);
}
else
isRoot = false;
@ -265,31 +254,6 @@ namespace LayoutBXLYT
GL.End();
}
private void DrawBoundryPane(BFLYT.BND1 pane, byte effectiveAlpha)
{
Vector2[] TexCoords = new Vector2[] {
new Vector2(1,1),
new Vector2(0,1),
new Vector2(0,0),
new Vector2(1,0)
};
Color color = Color.DarkGreen;
if (SelectedPanes.Contains(pane))
color = Color.Red;
color = Color.FromArgb(70, color);
Color[] Colors = new Color[] {
color,
color,
color,
color,
};
DrawRectangle(pane.Rectangle, TexCoords, Colors, false, effectiveAlpha);
}
private void DrawDefaultPane(BasePane pane)
{
Vector2[] TexCoords = new Vector2[] {
@ -310,7 +274,7 @@ namespace LayoutBXLYT
color,
};
DrawRectangle(pane.Rectangle, TexCoords, Colors);
BxlytToGL.DrawRectangle(pane.Rectangle, TexCoords, Colors);
}
private void DrawPartsPane(BFLYT.PRT1 pane, byte effectiveAlpha, bool parentInfluenceAlpha)
@ -386,7 +350,7 @@ namespace LayoutBXLYT
};
}
DrawRectangle(pane.Rectangle, TexCoords, Colors, false, effectiveAlpha);
BxlytToGL.DrawRectangle(pane.Rectangle, TexCoords, Colors, false, effectiveAlpha);
mat.Shader.Disable();
@ -428,7 +392,7 @@ namespace LayoutBXLYT
};
}
DrawRectangle(pane.Rectangle, TexCoords, Colors, false, effectiveAlpha);
BxlytToGL.DrawRectangle(pane.Rectangle, TexCoords, Colors, false, effectiveAlpha);
GL.BindTexture(TextureTarget.Texture2D, 0);
}
@ -468,7 +432,7 @@ namespace LayoutBXLYT
};
}
DrawRectangle(pane.Rectangle, TexCoords, Colors, false, effectiveAlpha);
BxlytToGL.DrawRectangle(pane.Rectangle, TexCoords, Colors, false, effectiveAlpha);
GL.BindTexture(TextureTarget.Texture2D, 0);
}
@ -509,7 +473,7 @@ namespace LayoutBXLYT
};
}
DrawRectangle(pane.Rectangle, TexCoords, Colors, false, effectiveAlpha);
BxlytToGL.DrawRectangle(pane.Rectangle, TexCoords, Colors, false, effectiveAlpha);
mat.Shader.Disable();
@ -576,131 +540,6 @@ namespace LayoutBXLYT
}
}
public void DrawRectangle(CustomRectangle rect, Vector2[] texCoords,
Color[] colors, bool useLines = true, byte alpha = 255)
{
for (int i = 0; i < colors.Length; i++)
{
uint setalpha = (uint)((colors[i].A * alpha) / 255);
colors[i] = Color.FromArgb((int)setalpha, colors[i]);
}
if (useLines)
{
GL.Begin(PrimitiveType.LineLoop);
GL.Color4(colors[0]);
GL.Vertex2(rect.LeftPoint, rect.BottomPoint);
GL.Vertex2(rect.RightPoint, rect.BottomPoint);
GL.Vertex2(rect.RightPoint, rect.TopPoint);
GL.Vertex2(rect.LeftPoint, rect.TopPoint);
GL.End();
}
else
{
GL.Begin(PrimitiveType.Quads);
GL.Color4(colors[0]);
GL.MultiTexCoord2(TextureUnit.Texture0, texCoords[0].X, texCoords[0].Y);
GL.Vertex2(rect.LeftPoint, rect.BottomPoint);
GL.Color4(colors[1]);
GL.MultiTexCoord2(TextureUnit.Texture0, texCoords[1].X, texCoords[1].Y);
GL.Vertex2(rect.RightPoint, rect.BottomPoint);
GL.Color4(colors[2]);
GL.MultiTexCoord2(TextureUnit.Texture0, texCoords[2].X, texCoords[2].Y);
GL.Vertex2(rect.RightPoint, rect.TopPoint);
GL.Color4(colors[3]);
GL.MultiTexCoord2(TextureUnit.Texture0, texCoords[3].X, texCoords[3].Y);
GL.Vertex2(rect.LeftPoint, rect.TopPoint);
GL.End();
//Draw outline
GL.Begin(PrimitiveType.LineLoop);
GL.LineWidth(3);
GL.Color4(colors[0]);
GL.Vertex2(rect.LeftPoint, rect.BottomPoint);
GL.Vertex2(rect.RightPoint, rect.BottomPoint);
GL.Vertex2(rect.RightPoint, rect.TopPoint);
GL.Vertex2(rect.LeftPoint, rect.TopPoint);
GL.End();
}
}
enum TextureSwizzle
{
Zero = All.Zero,
One = All.One,
Red = All.Red,
Green = All.Green,
Blue = All.Blue,
Alpha = All.Alpha,
}
public static bool BindGLTexture(TextureRef tex, STGenericTexture texture)
{
if (texture.RenderableTex == null || !texture.RenderableTex.GLInitialized)
texture.LoadOpenGLTexture();
//If the texture is still not initialized then return
if (!texture.RenderableTex.GLInitialized)
return false;
GL.BindTexture(TextureTarget.Texture2D, texture.RenderableTex.TexID);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureSwizzleR, ConvertChannel(texture.RedChannel));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureSwizzleG, ConvertChannel(texture.GreenChannel));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureSwizzleB, ConvertChannel(texture.BlueChannel));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureSwizzleA, ConvertChannel(texture.AlphaChannel));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, ConvertTextureWrap(tex.WrapModeU));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, ConvertTextureWrap(tex.WrapModeV));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, ConvertMagFilterMode(tex.MaxFilterMode));
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, ConvertMinFilterMode(tex.MinFilterMode));
return true;
}
private static int ConvertChannel(STChannelType type)
{
switch (type)
{
case STChannelType.Red: return (int)TextureSwizzle.Red;
case STChannelType.Green: return (int)TextureSwizzle.Green;
case STChannelType.Blue: return (int)TextureSwizzle.Blue;
case STChannelType.Alpha: return (int)TextureSwizzle.Alpha;
case STChannelType.Zero: return (int)TextureSwizzle.Zero;
case STChannelType.One: return (int)TextureSwizzle.One;
default: return 0;
}
}
private static int ConvertTextureWrap(TextureRef.WrapMode wrapMode)
{
switch (wrapMode)
{
case TextureRef.WrapMode.Clamp: return (int)TextureWrapMode.Clamp;
case TextureRef.WrapMode.Mirror: return (int)TextureWrapMode.MirroredRepeat;
case TextureRef.WrapMode.Repeat: return (int)TextureWrapMode.Repeat;
default: return (int)TextureWrapMode.Clamp;
}
}
private static int ConvertMagFilterMode(TextureRef.FilterMode filterMode)
{
switch (filterMode)
{
case TextureRef.FilterMode.Linear: return (int)TextureMagFilter.Linear;
case TextureRef.FilterMode.Near: return (int)TextureMagFilter.Nearest;
default: return (int)TextureRef.FilterMode.Linear;
}
}
private static int ConvertMinFilterMode(TextureRef.FilterMode filterMode)
{
switch (filterMode)
{
case TextureRef.FilterMode.Linear: return (int)TextureMinFilter.Linear;
case TextureRef.FilterMode.Near: return (int)TextureMinFilter.Nearest;
default: return (int)TextureRef.FilterMode.Linear;
}
}
private void DrawBackground()
{
if (backgroundTex == null)

View file

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Toolbox.Library.IO
{
class Bit
public class Bit
{
//From https://github.com/shibbo/flyte/blob/337383c01c50dff155e4b4e170d248118db0c0aa/flyte/utils/Bit.cs
public static uint ExtractBits(uint val, int numBits, int startBit)