From f1da5060d0bafbcb5bb5cd692d5b847496ff3448 Mon Sep 17 00:00:00 2001 From: KillzXGaming Date: Mon, 26 Apr 2021 20:35:44 -0400 Subject: [PATCH] BFRES : Cleanup how indices are checked and updated fixing some skinning bugs on import. --- .../BFRES/Bfres Structs/SubFiles/FMDL.cs | 6 +- .../BFRES/Bfres Structs/SubFiles/FMDL/FSKL.cs | 80 +------------------ .../FileFormats/BFRES/BfresSwitch.cs | 19 +---- .../FileFormats/BFRES/BfresWiiU.cs | 14 ---- 4 files changed, 7 insertions(+), 112 deletions(-) diff --git a/File_Format_Library/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL.cs b/File_Format_Library/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL.cs index 8666d6e9..dc0cf343 100644 --- a/File_Format_Library/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL.cs +++ b/File_Format_Library/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL.cs @@ -1110,7 +1110,6 @@ namespace Bfres.Structs } } - //Genericate indices //Check for rigged bones for (int ob = 0; ob < ImportedObjects.Count; ob++) @@ -1125,8 +1124,6 @@ namespace Bfres.Structs } } - Skeleton.CalculateIndices(); - List smoothSkinningIndices = new List(); List rigidSkinningIndices = new List(); @@ -1215,7 +1212,6 @@ namespace Bfres.Structs } } - if (Skeleton.node.SkeletonU != null) { Skeleton.node.SkeletonU.MatrixToBoneList = new List(); @@ -1229,6 +1225,8 @@ namespace Bfres.Structs Skeleton.node.Skeleton.MatrixToBoneList.Add((ushort)skinningIndices[i]); } + Skeleton.CalculateIndices(); + if (materials.Count <= 0) { //Force material creation if there is none present diff --git a/File_Format_Library/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL/FSKL.cs b/File_Format_Library/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL/FSKL.cs index 56a5633d..5c241529 100644 --- a/File_Format_Library/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL/FSKL.cs +++ b/File_Format_Library/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL/FSKL.cs @@ -28,20 +28,13 @@ namespace Bfres.Structs if (Skeleton.InverseModelMatrices == null) Skeleton.InverseModelMatrices = new List(); - if (Skeleton.MatrixToBoneList == null) - Skeleton.MatrixToBoneList = new List(); - //Generate index list - List SmoothIndices = new List(); List SmoothMatrices = new List(); - List RigidIndices = new List(); foreach (var Bone in Skeleton.Bones.Values) Bone.InverseMatrix = new Syroot.Maths.Matrix3x4(1,0,0,0, 0,1,0,0, 0,0,1,0); - - ushort SmoothIndex = 0; foreach (BfresBone bn in bones) { ushort BoneIndex = 0; @@ -49,13 +42,8 @@ namespace Bfres.Structs { if (bn.Text == Bone.Name) { - if (bn.UseSmoothMatrix || bn.SmoothMatrixIndex > -1) + if (bn.UseSmoothMatrix) { - bn.SmoothMatrixIndex = (short)SmoothIndex++; - - Bone.SmoothMatrixIndex = bn.SmoothMatrixIndex; - SmoothIndices.Add(BoneIndex); - var mat = MatrixExenstion.GetMatrixInverted(bn); Bone.InverseMatrix = mat; SmoothMatrices.Add(mat); @@ -64,30 +52,7 @@ namespace Bfres.Structs BoneIndex++; } } - - //Rigid Indices come after smooth indices. Start from the last smooth index - ushort RigidIndex = (ushort)(SmoothIndices.Count); - foreach (BfresBone bn in bones) - { - ushort BoneIndex = 0; - foreach (var Bone in Skeleton.Bones.Values) - { - if (bn.Text == Bone.Name) - { - if (bn.UseRigidMatrix || bn.RigidMatrixIndex != -1) - { - bn.RigidMatrixIndex = (short)RigidIndex++; - Bone.RigidMatrixIndex = bn.RigidMatrixIndex; - RigidIndices.Add(BoneIndex); - } - } - BoneIndex++; - } - } - - //Rigid indices at the end - var AllIndices = SmoothIndices.Concat(RigidIndices).ToList(); - Skeleton.MatrixToBoneList = AllIndices.ToArray(); + Skeleton.InverseModelMatrices = SmoothMatrices; } else @@ -96,16 +61,8 @@ namespace Bfres.Structs if (Skeleton.InverseModelMatrices == null) Skeleton.InverseModelMatrices = new List(); - if (Skeleton.MatrixToBoneList == null) - Skeleton.MatrixToBoneList = new List(); - //Generate index list - List SmoothIndices = new List(); List SmoothMatrices = new List(); - List RigidIndices = new List(); - - ushort SmoothIndex = 0; - foreach (BfresBone bn in bones) { ushort BoneIndex = 0; @@ -115,10 +72,6 @@ namespace Bfres.Structs { if (bn.UseSmoothMatrix || bn.SmoothMatrixIndex != -1) { - bn.SmoothMatrixIndex = (short)SmoothIndex++; - Bone.SmoothMatrixIndex = bn.SmoothMatrixIndex; - SmoothIndices.Add(BoneIndex); - var mat = MatrixExenstion.GetMatrixInverted(bn); SmoothMatrices.Add(mat); } @@ -126,31 +79,6 @@ namespace Bfres.Structs BoneIndex++; } } - - - //Rigid Indices come after smooth indices. Start from the last smooth index - ushort RigidIndex = (ushort)(SmoothIndices.Count); - foreach (BfresBone bn in bones) - { - ushort BoneIndex = 0; - foreach (var Bone in Skeleton.Bones) - { - if (bn.Text == Bone.Name) - { - if (bn.UseRigidMatrix || bn.RigidMatrixIndex != -1) - { - bn.RigidMatrixIndex = (short)RigidIndex++; - Bone.RigidMatrixIndex = bn.RigidMatrixIndex; - RigidIndices.Add(BoneIndex); - } - } - BoneIndex++; - } - } - - //Rigid indices at the end - var AllIndices = SmoothIndices.Concat(RigidIndices).ToList(); - Skeleton.MatrixToBoneList = AllIndices.ToArray(); Skeleton.InverseModelMatrices = SmoothMatrices; } } @@ -615,9 +543,9 @@ namespace Bfres.Structs } public class BfresBone : STBone, IContextMenuNode { - public bool UseSmoothMatrix { get; set; } + public bool UseSmoothMatrix => SmoothMatrixIndex != -1; - public bool UseRigidMatrix { get; set; } + public bool UseRigidMatrix => RigidMatrixIndex != -1; public string BoneName { diff --git a/File_Format_Library/FileFormats/BFRES/BfresSwitch.cs b/File_Format_Library/FileFormats/BFRES/BfresSwitch.cs index 5efbeb0b..54228e84 100644 --- a/File_Format_Library/FileFormats/BFRES/BfresSwitch.cs +++ b/File_Format_Library/FileFormats/BFRES/BfresSwitch.cs @@ -567,8 +567,6 @@ namespace FirstPlugin bone.Text = bn.Name; bone.RigidMatrixIndex = bn.RigidMatrixIndex; bone.SmoothMatrixIndex = bn.SmoothMatrixIndex; - bone.UseRigidMatrix = bn.RigidMatrixIndex != -1; - bone.UseSmoothMatrix = bn.SmoothMatrixIndex != -1; bone.BillboardIndex = bn.BillboardIndex; if (SetParent) @@ -597,7 +595,6 @@ namespace FirstPlugin public static void SaveSkeleton(FSKL fskl, List Bones) { fskl.node.Skeleton.Bones.Clear(); - fskl.node.Skeleton.MatrixToBoneList = new List(); fskl.node.Skeleton.InverseModelMatrices = new List(); ushort SmoothIndex = 0; @@ -609,17 +606,11 @@ namespace FirstPlugin BfresBone bn = new BfresBone(fskl); bn.CloneBaseInstance(genericBone); bn.Text = genericBone.Text; - bn.UseRigidMatrix = bn.RigidMatrixIndex != -1; - bn.UseSmoothMatrix = bn.SmoothMatrixIndex != -1; - + //Set the bfres bone data if (bn.Bone == null) bn.Bone = new Bone(); bn.GenericToBfresBone(); - - if (bn.SmoothMatrixIndex != short.MaxValue) - fskl.node.Skeleton.MatrixToBoneList.Add(SmoothIndex++); - fskl.node.Skeleton.InverseModelMatrices.Add(Syroot.Maths.Matrix3x4.Zero); //Check duplicated names @@ -639,14 +630,6 @@ namespace FirstPlugin fskl.update(); fskl.reset(); - - fskl.Node_Array = new int[fskl.node.Skeleton.MatrixToBoneList.Count]; - int nodes = 0; - foreach (ushort node in fskl.node.Skeleton.MatrixToBoneList) - { - fskl.Node_Array[nodes] = node; - nodes++; - } } public static void SetShape(this FSHP s, Shape shp) diff --git a/File_Format_Library/FileFormats/BFRES/BfresWiiU.cs b/File_Format_Library/FileFormats/BFRES/BfresWiiU.cs index 6d4ac7f2..47af37d3 100644 --- a/File_Format_Library/FileFormats/BFRES/BfresWiiU.cs +++ b/File_Format_Library/FileFormats/BFRES/BfresWiiU.cs @@ -381,8 +381,6 @@ namespace FirstPlugin bone.RigidMatrixIndex = bn.RigidMatrixIndex; bone.SmoothMatrixIndex = bn.SmoothMatrixIndex; bone.BillboardIndex = bn.BillboardIndex; - bone.UseRigidMatrix = bn.RigidMatrixIndex != -1; - bone.UseSmoothMatrix = bn.SmoothMatrixIndex != -1; if (SetParent) bone.parentIndex = bn.ParentIndex; if (bn.FlagsRotation == BoneFlagsRotation.Quaternion) @@ -954,7 +952,6 @@ namespace FirstPlugin fskl.node.SkeletonU = new Skeleton(); fskl.node.SkeletonU.Bones.Clear(); - fskl.node.SkeletonU.MatrixToBoneList = new List(); fskl.node.SkeletonU.InverseModelMatrices = new List(); fskl.node.Nodes.Clear(); @@ -973,9 +970,6 @@ namespace FirstPlugin bn.BoneU = new Bone(); bn.GenericToBfresBone(); - if (bn.SmoothMatrixIndex != short.MaxValue) - fskl.node.SkeletonU.MatrixToBoneList.Add(SmoothIndex++); - //Check duplicated names List names = fskl.bones.Select(o => o.Text).ToList(); bn.Text = Utils.RenameDuplicateString(names, bn.Text); @@ -996,14 +990,6 @@ namespace FirstPlugin fskl.update(); fskl.reset(); - - fskl.Node_Array = new int[fskl.node.SkeletonU.MatrixToBoneList.Count]; - int nodes = 0; - foreach (ushort node in fskl.node.SkeletonU.MatrixToBoneList) - { - fskl.Node_Array[nodes] = node; - nodes++; - } } public static void SaveVertexBuffer(FSHP fshp) {