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

View file

@ -1247,9 +1247,14 @@ namespace Bfres.Structs
try
{
var skel = GetParentModel().Skeleton;
Matrix4 trans = Matrix4.Identity;
if (IsSingleBind)
{
bool IsRigidIndex = skel.IsIndexRigid(BoneIndex);
if (!IsRigidIndex)
return position;
if (BoneIndex >= skel.Node_Array.Length || BoneIndex == -1)
{
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)
{
node.Skeleton.Bones.Add(bone);

View file

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

View file

@ -282,9 +282,14 @@ namespace FirstPlugin
try
{
if (model.Skeleton.bones.Count > 0) {
Matrix4 NoBindFix = model.Skeleton.bones[fshp.BoneIndex].Transform;
v.pos = Vector3.TransformPosition(v.pos, NoBindFix);
v.nrm = Vector3.TransformNormal(v.nrm, NoBindFix);
//Check if the bones are a rigid type
//In game it seems to not transform if they are not rigid
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

View file

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

View file

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

View file

@ -6,7 +6,69 @@ using System.Threading.Tasks;
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;
// https://github.com/KFreon/CSharpImageLibrary
namespace CSharpImageLibrary.DDS
{
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)
{
var image = new byte [Height * Width * 4];

View file

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

View file

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

View file

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

View file

@ -40,7 +40,8 @@ namespace Switch_Toolbox.Library
public IFileFormat OpenFile()
{
return STFileLoader.OpenFileFormat(FileName, FileData, true);
return STFileLoader.OpenFileFormat(
IOExtensions.RemoveIllegaleFolderNameCharacters(FileName), FileData, true);
}
public virtual void Replace()
@ -82,7 +83,19 @@ namespace Switch_Toolbox.Library
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 virtual byte[] FileData
{

View file

@ -97,21 +97,16 @@ namespace Toolbox
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;
if (OpenTK.Graphics.GraphicsContext.CurrentContext != null)
{
OpenTKSharedResources.InitializeSharedResources();
Runtime.OpenTKInitialized = true;
OpenTKSharedResources.InitializeSharedResources();
Runtime.renderer = GL.GetString(StringName.Renderer);
Runtime.openGLVersion = GL.GetString(StringName.Version);
Runtime.GLSLVersion = GL.GetString(StringName.ShadingLanguageVersion);
ParseGLVersion();
}
Runtime.OpenTKInitialized = true;
// Runtime.renderer = GL.GetString(StringName.Renderer);
// Runtime.openGLVersion = GL.GetString(StringName.Version);
// Runtime.GLSLVersion = GL.GetString(StringName.ShadingLanguageVersion);
// ParseGLVersion();
}
LoadPLugins();
@ -1129,6 +1124,7 @@ namespace Toolbox
{
OnMdiWindowClosed();
child.Close();
GC.Collect();
return;
}
}

View file

@ -59,6 +59,11 @@
<HintPath>..\..\..\NintenTools.Bfres-master\NintenTools.Bfres\src\ConsoleApp1\OpenTK.dll</HintPath>
<Private>False</Private>
</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.Core" />
<Reference Include="System.Xml.Linq" />