From 857e34fce706369dc9678edc10d2a43a80a64d30 Mon Sep 17 00:00:00 2001 From: KillzXGaming Date: Mon, 29 Jul 2019 18:09:53 -0400 Subject: [PATCH] Fix Format_8 encoding for weights --- .../BFRES/Bfres Structs/SubFiles/FMDL/FSHP.cs | 50 +++++++++++++++++++ .../FileFormats/Custom/CsvModels.cs | 23 +-------- .../GUI/BFRES/BfresModelImportSettings.cs | 2 +- 3 files changed, 52 insertions(+), 23 deletions(-) diff --git a/File_Format_Library/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL/FSHP.cs b/File_Format_Library/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL/FSHP.cs index 564fd05d..3db4b51a 100644 --- a/File_Format_Library/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL/FSHP.cs +++ b/File_Format_Library/FileFormats/BFRES/Bfres Structs/SubFiles/FMDL/FSHP.cs @@ -461,6 +461,17 @@ namespace Bfres.Structs return uvMaps; } + public VertexAttribute GetWeightAttribute(int index) + { + foreach (var attribute in vertexAttributes) + { + if (attribute.Name == $"_w{index}") + return attribute; + } + + return null; + } + private void Rename(object sender, EventArgs args) { RenameDialog dialog = new RenameDialog(); @@ -1644,6 +1655,7 @@ namespace Bfres.Structs float[] weightsA = new float[4]; int[] indicesA = new int[4]; + if (vtx.boneWeights.Count >= 1) weightsA[0] = vtx.boneWeights[0]; if (vtx.boneWeights.Count >= 2) @@ -1653,6 +1665,44 @@ namespace Bfres.Structs if (vtx.boneWeights.Count >= 4) weightsA[3] = vtx.boneWeights[3]; + var WeightAttribute = GetWeightAttribute(0); + + if (WeightAttribute != null) + { + if (WeightAttribute.Format == ResGFX.AttribFormat.Format_8_UNorm || + WeightAttribute.Format == ResGFX.AttribFormat.Format_8_8_UNorm || + WeightAttribute.Format == ResGFX.AttribFormat.Format_8_8_8_8_UNorm) + { + //Produce identical results for the weight output as BFRES_Vertex.py + //This should prevent encoding back and exploding + int MaxWeight = 255; + for (int i = 0; i < 4; i++) + { + if (vtx.boneWeights.Count < i + 1) + { + weightsA[i] = 0; + MaxWeight = 0; + } + else + { + int weight = (int)(vtx.boneWeights[i] * 255); + if (vtx.boneWeights.Count == i + 1) + weight = MaxWeight; + + if (weight >= MaxWeight) + { + weight = MaxWeight; + MaxWeight = 0; + } + else + MaxWeight -= weight; + + weightsA[i] = weight / 255f; + } + } + } + } + if (vtx.boneIds.Count >= 1) indicesA[0] = vtx.boneIds[0]; if (vtx.boneIds.Count >= 2) diff --git a/File_Format_Library/FileFormats/Custom/CsvModels.cs b/File_Format_Library/FileFormats/Custom/CsvModels.cs index 43751b33..cc00733a 100644 --- a/File_Format_Library/FileFormats/Custom/CsvModels.cs +++ b/File_Format_Library/FileFormats/Custom/CsvModels.cs @@ -274,29 +274,8 @@ namespace FirstPlugin { foreach (string bn in obj.bones[v]) obj.vertices[v].boneNames.Add(bn); - - //Produce identical results for the weight output as BFRES_Vertex.py - //This should prevent encoding back and exploding - float MaxWeight = 1.0f; - int i = 0; foreach (float f in obj.weightsT[v]) - { - float weight = f; - if (i + 1 == obj.weightsT.Count) - weight = MaxWeight; - - if (weight >= MaxWeight) - { - weight = MaxWeight; - MaxWeight = 0; - } - else - MaxWeight -= weight; - - obj.vertices[v].boneWeights.Add(weight); - - i++; - } + obj.vertices[v].boneWeights.Add(f); } int vID = 0; diff --git a/File_Format_Library/GUI/BFRES/BfresModelImportSettings.cs b/File_Format_Library/GUI/BFRES/BfresModelImportSettings.cs index 2f4dab3c..2e99045d 100644 --- a/File_Format_Library/GUI/BFRES/BfresModelImportSettings.cs +++ b/File_Format_Library/GUI/BFRES/BfresModelImportSettings.cs @@ -312,7 +312,7 @@ namespace FirstPlugin comboBoxFormatWeights.Items.Add(AttribFormat.Format_16_16_UNorm); comboBoxFormatWeights.Items.Add(AttribFormat.Format_8_8_UNorm); comboBoxFormatWeights.Items.Add(AttribFormat.Format_8_UNorm); - comboBoxFormatWeights.SelectedIndex = 0; + comboBoxFormatWeights.SelectedIndex = 2; comboBoxFormatTangents.Items.Add(AttribFormat.Format_32_32_32_32_Single); comboBoxFormatTangents.Items.Add(AttribFormat.Format_16_16_16_16_Single);