1
0
mirror of synced 2024-11-30 18:24:39 +01:00

Some more fixes and optmizations.

This commit is contained in:
KillzXGaming 2019-06-10 16:30:06 -04:00
parent ea626cac07
commit cce69d12e2
23 changed files with 157 additions and 34 deletions

Binary file not shown.

View File

@ -318,22 +318,22 @@ namespace FirstPlugin
if (bfresEditor == null) if (bfresEditor == null)
{ {
BFRESRender.UpdateModelList();
HasModels = BFRESRender.models.Count > 0; HasModels = BFRESRender.models.Count > 0;
foreach (var model in BFRESRender.models)
{
if (model.shapes.Count > 0)
{
HasShapes = true;
break;
}
}
bfresEditor = new BfresEditor(HasModels); bfresEditor = new BfresEditor(HasModels);
bfresEditor.Dock = DockStyle.Fill; bfresEditor.Dock = DockStyle.Fill;
LibraryGUI.Instance.LoadEditor(bfresEditor); LibraryGUI.Instance.LoadEditor(bfresEditor);
} }
for (int i = 0; i < BFRESRender.models.Count; i++)
{
if (BFRESRender.models[i].shapes.Count > 0)
{
HasShapes = true;
break;
}
}
bool ViewportToggled = bfresEditor.DisplayViewport; bool ViewportToggled = bfresEditor.DisplayViewport;
if (SelectedSection is FTEX) if (SelectedSection is FTEX)
@ -434,6 +434,7 @@ namespace FirstPlugin
bool IsSimpleEditor = PluginRuntime.UseSimpleBfresEditor; bool IsSimpleEditor = PluginRuntime.UseSimpleBfresEditor;
if (SelectedSection is BFRES && HasShapes) if (SelectedSection is BFRES && HasShapes)
bfresEditor.FrameCamera(BFRESRender); bfresEditor.FrameCamera(BFRESRender);
@ -746,12 +747,16 @@ namespace FirstPlugin
if (Models != null) if (Models != null)
{ {
foreach (FMDL mdl in Models) foreach (FMDL mdl in Models)
{
BFRESRender.models.Add(mdl);
DrawableContainer.Drawables.Add(mdl.Skeleton); DrawableContainer.Drawables.Add(mdl.Skeleton);
}
} }
} }
public void Unload() public void Unload()
{ {
BFRESRender.Destroy(); BFRESRender.Destroy();
DrawableContainer.Drawables.Clear();
ObjectEditor.RemoveContainer(DrawableContainer); ObjectEditor.RemoveContainer(DrawableContainer);
@ -761,9 +766,15 @@ namespace FirstPlugin
{ {
if (((BFRESGroupNode)node).Type == BRESGroupType.Textures) if (((BFRESGroupNode)node).Type == BRESGroupType.Textures)
{ {
for (int i = 0; i < ((BFRESGroupNode)node).Nodes.Count; i++)
((FTEX)((BFRESGroupNode)node).Nodes[i]).Unload();
if (PluginRuntime.ftexContainers.Contains(((BFRESGroupNode)node))) if (PluginRuntime.ftexContainers.Contains(((BFRESGroupNode)node)))
PluginRuntime.ftexContainers.Remove(((BFRESGroupNode)node)); PluginRuntime.ftexContainers.Remove(((BFRESGroupNode)node));
} }
((BFRESGroupNode)node).ResourceNodes.Clear();
((BFRESGroupNode)node).Nodes.Clear();
} }
if (node is BNTX) if (node is BNTX)

View File

@ -1247,9 +1247,14 @@ namespace Bfres.Structs
try try
{ {
var skel = GetParentModel().Skeleton; var skel = GetParentModel().Skeleton;
Matrix4 trans = Matrix4.Identity; Matrix4 trans = Matrix4.Identity;
if (IsSingleBind) if (IsSingleBind)
{ {
bool IsRigidIndex = skel.IsIndexRigid(BoneIndex);
if (!IsRigidIndex)
return position;
if (BoneIndex >= skel.Node_Array.Length || BoneIndex == -1) if (BoneIndex >= skel.Node_Array.Length || BoneIndex == -1)
{ {
STConsole.WriteLine($"Invalid bone index to bind bone to mesh {Text} {BoneIndex} ", System.Drawing.Color.Red); STConsole.WriteLine($"Invalid bone index to bind bone to mesh {Text} {BoneIndex} ", System.Drawing.Color.Red);

View File

@ -146,6 +146,13 @@ namespace Bfres.Structs
} }
} }
public bool IsIndexRigid(int index)
{
//Get the bone and see if the rigid index matches
int BoneIndex = Node_Array[index];
return bones[BoneIndex].RigidMatrixIndex == index;
}
public void AddBone(Bone bone) public void AddBone(Bone bone)
{ {
node.Skeleton.Bones.Add(bone); node.Skeleton.Bones.Add(bone);

View File

@ -136,6 +136,7 @@ namespace Bfres.Structs
public override void Unload() public override void Unload()
{ {
texture = null;
DisposeRenderable(); DisposeRenderable();
Nodes.Clear(); Nodes.Clear();
} }

View File

@ -282,9 +282,14 @@ namespace FirstPlugin
try try
{ {
if (model.Skeleton.bones.Count > 0) { if (model.Skeleton.bones.Count > 0) {
Matrix4 NoBindFix = model.Skeleton.bones[fshp.BoneIndex].Transform; //Check if the bones are a rigid type
v.pos = Vector3.TransformPosition(v.pos, NoBindFix); //In game it seems to not transform if they are not rigid
v.nrm = Vector3.TransformNormal(v.nrm, NoBindFix); if (model.Skeleton.bones[fshp.BoneIndex].RigidMatrixIndex != -1)
{
Matrix4 NoBindFix = model.Skeleton.bones[fshp.BoneIndex].Transform;
v.pos = Vector3.TransformPosition(v.pos, NoBindFix);
v.nrm = Vector3.TransformNormal(v.nrm, NoBindFix);
}
} }
} }
catch //Matrix failed. Print the coordinate data of the bone catch //Matrix failed. Print the coordinate data of the bone

View File

@ -179,8 +179,12 @@ namespace FirstPlugin
} }
public void Unload() public void Unload()
{ {
foreach (var tex in Textures) foreach (var tex in Textures.Values)
tex.Value.DisposeRenderable(); {
tex.Texture.TextureData.Clear();
tex.Texture = null;
tex.DisposeRenderable();
}
Textures.Clear(); Textures.Clear();
Nodes.Clear(); Nodes.Clear();

View File

@ -279,7 +279,7 @@ namespace FirstPlugin
// Formats.Add(typeof(BFLAN)); // Formats.Add(typeof(BFLAN));
// Formats.Add(typeof(BFLYT)); // Formats.Add(typeof(BFLYT));
Formats.Add(typeof(GFPAK)); Formats.Add(typeof(GFPAK));
Formats.Add(typeof(GFBMDL)); // Formats.Add(typeof(GFBMDL));
Formats.Add(typeof(NUTEXB)); Formats.Add(typeof(NUTEXB));
Formats.Add(typeof(NUT)); Formats.Add(typeof(NUT));
Formats.Add(typeof(GTXFile)); Formats.Add(typeof(GTXFile));
@ -310,7 +310,7 @@ namespace FirstPlugin
Formats.Add(typeof(SHARC)); Formats.Add(typeof(SHARC));
Formats.Add(typeof(SHARCFB)); Formats.Add(typeof(SHARCFB));
Formats.Add(typeof(NARC)); Formats.Add(typeof(NARC));
Formats.Add(typeof(SDF)); // Formats.Add(typeof(SDF));
Formats.Add(typeof(TMPK)); Formats.Add(typeof(TMPK));
Formats.Add(typeof(TEX3DS)); Formats.Add(typeof(TEX3DS));
Formats.Add(typeof(NXARC)); Formats.Add(typeof(NXARC));

View File

@ -6,7 +6,69 @@ using System.Threading.Tasks;
namespace CSharpImageLibrary.DDS namespace CSharpImageLibrary.DDS
{ {
class DDS_BlockHelpers public class DDS_BlockHelpers
{ {
//From https://github.com/KFreon/CSharpImageLibrary/blob/master/CSharpImageLibrary/DDS/DDS_BlockHelpers.cs
internal static void Decompress8BitBlock(byte[] source, int sourceStart, byte[] destination, int decompressedStart, int decompressedLineLength, bool isSigned)
{
byte min = source[sourceStart];
byte max = source[sourceStart + 1];
byte[] Colours = Build8BitPalette(min, max, isSigned);
// KFreon: Decompress pixels
ulong bitmask = (ulong)source[sourceStart + 2] << 0 | (ulong)source[sourceStart + 3] << 8 | (ulong)source[sourceStart + 4] << 16 | // KFreon: Read all 6 compressed bytes into single.
(ulong)source[sourceStart + 5] << 24 | (ulong)source[sourceStart + 6] << 32 | (ulong)source[sourceStart + 7] << 40;
// KFreon: Bitshift and mask compressed data to get 3 bit indicies, and retrieve indexed colour of pixel.
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
int index = i * 4 + j;
int destPos = decompressedStart + j * 4 + (i * decompressedLineLength);
destination[destPos] = Colours[bitmask >> (index * 3) & 0x7];
}
}
}
/// <summary>
/// Builds palette for 8 bit channel.
/// </summary>
/// <param name="min">First main colour (often actually minimum)</param>
/// <param name="max">Second main colour (often actually maximum)</param>
/// <param name="isSigned">true = sets signed alpha range (-254 -- 255), false = 0 -- 255</param>
/// <returns>8 byte colour palette.</returns>
internal static byte[] Build8BitPalette(byte min, byte max, bool isSigned)
{
byte[] Colours = new byte[8];
Colours[0] = min;
Colours[1] = max;
// KFreon: Choose which type of interpolation is required
if (min > max)
{
// KFreon: Interpolate other colours
Colours[2] = (byte)((6d * min + 1d * max) / 7d); // NO idea what the +3 is...not in the Microsoft spec, but seems to be everywhere else.
Colours[3] = (byte)((5d * min + 2d * max) / 7d);
Colours[4] = (byte)((4d * min + 3d * max) / 7d);
Colours[5] = (byte)((3d * min + 4d * max) / 7d);
Colours[6] = (byte)((2d * min + 5d * max) / 7d);
Colours[7] = (byte)((1d * min + 6d * max) / 7d);
}
else
{
// KFreon: Interpolate other colours and add Opacity or something...
Colours[2] = (byte)((4d * min + 1d * max) / 5d);
Colours[3] = (byte)((3d * min + 2d * max) / 5d);
Colours[4] = (byte)((2d * min + 3d * max) / 5d);
Colours[5] = (byte)((1d * min + 4d * max) / 5d);
Colours[6] = (byte)(isSigned ? -254 : 0); // KFreon: snorm and unorm have different alpha ranges
Colours[7] = 255;
}
return Colours;
}
} }
} }

View File

@ -1,5 +1,6 @@
using System; using System;
// https://github.com/KFreon/CSharpImageLibrary
namespace CSharpImageLibrary.DDS namespace CSharpImageLibrary.DDS
{ {
public static class Dxt public static class Dxt
@ -238,6 +239,19 @@ namespace CSharpImageLibrary.DDS
} }
} }
static byte ExpandTo255(double v)
{
if (double.IsNaN(v) || v == 0)
return 128;
else
return (byte)(((v + 1d) / 2d) * 255d);
}
internal static int GetDecompressedOffset(int start, int lineLength, int pixelIndex)
{
return start + (lineLength * (pixelIndex / 4)) + (pixelIndex % 4) * 4;
}
public static byte[] DecompressBc7(byte[] Data, int Width, int Height) public static byte[] DecompressBc7(byte[] Data, int Width, int Height)
{ {
var image = new byte [Height * Width * 4]; var image = new byte [Height * Width * 4];

View File

@ -338,7 +338,6 @@ namespace Switch_Toolbox.Library
int W = (width + 3) / 4; int W = (width + 3) / 4;
int H = (height + 3) / 4; int H = (height + 3) / 4;
byte[] Output = new byte[W * H * 64]; byte[] Output = new byte[W * H * 64];
for (int Y = 0; Y < H; Y++) for (int Y = 0; Y < H; Y++)

View File

@ -77,7 +77,7 @@ namespace Switch_Toolbox.Library.Forms
if (File == null) if (File == null)
File = ArchiveFileInfo.OpenFile(); File = ArchiveFileInfo.OpenFile();
if (File != null && IsConvertableText(File.GetType())) if (File != null && IsConvertableText(File.GetType()))
{ {
editor.FillEditor(((IConvertableTextFormat)File).ConvertToString()); editor.FillEditor(((IConvertableTextFormat)File).ConvertToString());
@ -92,7 +92,7 @@ namespace Switch_Toolbox.Library.Forms
{ {
foreach (var inter in type.GetInterfaces()) foreach (var inter in type.GetInterfaces())
{ {
if (inter.IsGenericType && inter.GetGenericTypeDefinition() == typeof(IEditor<>)) if (inter.IsGenericType && inter.GetGenericTypeDefinition() == typeof(IConvertableTextFormat))
{ {
return true; return true;
} }

View File

@ -275,6 +275,7 @@ namespace Switch_Toolbox.Library.Forms
private void BtnClose_Click(object sender, System.EventArgs e) private void BtnClose_Click(object sender, System.EventArgs e)
{ {
Close(); Close();
GC.Collect();
} }
private void BtnClose_MouseEnter(object sender, System.EventArgs e) private void BtnClose_MouseEnter(object sender, System.EventArgs e)

View File

@ -40,7 +40,8 @@ namespace Switch_Toolbox.Library
public IFileFormat OpenFile() public IFileFormat OpenFile()
{ {
return STFileLoader.OpenFileFormat(FileName, FileData, true); return STFileLoader.OpenFileFormat(
IOExtensions.RemoveIllegaleFolderNameCharacters(FileName), FileData, true);
} }
public virtual void Replace() public virtual void Replace()
@ -82,7 +83,19 @@ namespace Switch_Toolbox.Library
protected byte[] _fileData = null; protected byte[] _fileData = null;
public string FileName { get; set; } = string.Empty; //Full File Name //Full File Name
private string _fileName = string.Empty;
public string FileName
{
get
{
return _fileName;
}
set
{
_fileName = value;
}
}
public string Name { get; set; } = string.Empty; //File Name (No Path) public string Name { get; set; } = string.Empty; //File Name (No Path)
public virtual byte[] FileData public virtual byte[] FileData
{ {

View File

@ -97,21 +97,16 @@ namespace Toolbox
if (Runtime.UseOpenGL) if (Runtime.UseOpenGL)
{ {
//Create an instance of this to help load open gl data easier and quicker after boot
var viewport = new Viewport(ObjectEditor.GetDrawableContainers(), false);
ShaderTools.executableDir = Runtime.ExecutableDir; ShaderTools.executableDir = Runtime.ExecutableDir;
if (OpenTK.Graphics.GraphicsContext.CurrentContext != null)
{
OpenTKSharedResources.InitializeSharedResources();
Runtime.OpenTKInitialized = true; OpenTKSharedResources.InitializeSharedResources();
Runtime.renderer = GL.GetString(StringName.Renderer); Runtime.OpenTKInitialized = true;
Runtime.openGLVersion = GL.GetString(StringName.Version);
Runtime.GLSLVersion = GL.GetString(StringName.ShadingLanguageVersion); // Runtime.renderer = GL.GetString(StringName.Renderer);
ParseGLVersion(); // Runtime.openGLVersion = GL.GetString(StringName.Version);
} // Runtime.GLSLVersion = GL.GetString(StringName.ShadingLanguageVersion);
// ParseGLVersion();
} }
LoadPLugins(); LoadPLugins();
@ -1129,6 +1124,7 @@ namespace Toolbox
{ {
OnMdiWindowClosed(); OnMdiWindowClosed();
child.Close(); child.Close();
GC.Collect();
return; return;
} }
} }

View File

@ -59,6 +59,11 @@
<HintPath>..\..\..\NintenTools.Bfres-master\NintenTools.Bfres\src\ConsoleApp1\OpenTK.dll</HintPath> <HintPath>..\..\..\NintenTools.Bfres-master\NintenTools.Bfres\src\ConsoleApp1\OpenTK.dll</HintPath>
<Private>False</Private> <Private>False</Private>
</Reference> </Reference>
<Reference Include="OpenTK.GLControl, Version=3.0.1.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>Lib\OpenTK.GLControl.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />