Skip to content

Commit 5ee12a4

Browse files
committed
Clean weights as soon as models are loaded from file.
1 parent ad4a1df commit 5ee12a4

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

xivModdingFramework/Models/DataContainers/TTModel.cs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,8 @@ public static TTModel LoadFromFile(string filePath, Action<bool, string> logging
16421642
// Convert the model to FFXIV's internal weirdness.
16431643
ModelModifiers.MakeImportReady(model, loggingFunction);
16441644

1645+
ModelModifiers.CleanWeights(model, loggingFunction);
1646+
16451647
return model;
16461648
}
16471649

@@ -2907,17 +2909,39 @@ public static void CheckCommonUserErrors(TTModel model, Action<bool, string> log
29072909

29082910
bool anyAlpha = false;
29092911
bool anyColor = false;
2910-
bool anyColor2 = false;
2912+
bool fullWhiteColor2 = true;
29112913
bool anyWeirdUV1s = false;
29122914
bool anyWeirdUV2s = false;
2915+
bool anyWeirdUV3s = false;
2916+
2917+
var firstUv2 = p.Vertices[0].UV2;
2918+
var firstUv3 = p.Vertices[0].UV3;
29132919

29142920
foreach (var v in p.Vertices)
29152921
{
29162922
anyAlpha = anyAlpha || (v.VertexColor[3] > 0);
29172923
anyColor = anyColor || (v.VertexColor[0] > 0 || v.VertexColor[1] > 0 || v.VertexColor[2] > 0);
2918-
anyColor2 = anyColor2 || (v.VertexColor2[0] > 0 || v.VertexColor2[1] > 0 || v.VertexColor2[2] > 0 || v.VertexColor2[3] > 0);
2924+
2925+
if(fullWhiteColor2 == true &&
2926+
(v.VertexColor2[0] < 255
2927+
|| v.VertexColor2[1] < 255
2928+
|| v.VertexColor2[2] < 255))
2929+
{
2930+
fullWhiteColor2 = false;
2931+
}
2932+
29192933
anyWeirdUV1s = anyWeirdUV1s || (v.UV1.X > 2 || v.UV1.X < -2 || v.UV1.Y > 2 || v.UV1.Y < -2);
2920-
anyWeirdUV2s = anyWeirdUV2s || (v.UV2.X > 2 || v.UV2.X < -2 || v.UV2.Y > 2 || v.UV2.Y < -2);
2934+
anyWeirdUV2s = anyWeirdUV2s || ((v.UV2.X > 2 || v.UV2.X < -2 || v.UV2.Y > 2 || v.UV2.Y < -2) && v.UV2 != firstUv2);
2935+
anyWeirdUV3s = anyWeirdUV3s || ((v.UV3.X > 2 || v.UV3.X < -2 || v.UV3.Y > 2 || v.UV3.Y < -2) && v.UV3 != firstUv3);
2936+
2937+
if((v.UV2.X > 2 || v.UV2.X < -2 || v.UV2.Y > 2 || v.UV2.Y < -2))
2938+
{
2939+
Trace.WriteLine(v.UV2);
2940+
}
2941+
if ((v.UV2.X > 2 || v.UV2.X < -2 || v.UV2.Y > 2 || v.UV2.Y < -2))
2942+
{
2943+
Trace.WriteLine(v.UV2);
2944+
}
29212945
}
29222946

29232947
if (!anyAlpha)
@@ -2929,9 +2953,10 @@ public static void CheckCommonUserErrors(TTModel model, Action<bool, string> log
29292953
{
29302954
loggingFunction(true, "Mesh: " + mIdx + " Part: " + pIdx + " has a fully black Vertex Color channel. This can have unexpected results on in-game rendering. Was this intended?");
29312955
}
2932-
if (!anyColor)
2956+
2957+
if (fullWhiteColor2)
29332958
{
2934-
// TODO: Do we care about this? Who knows.
2959+
loggingFunction(true, "Mesh: " + mIdx + " Part: " + pIdx + " has a fully white Vertex Color 2 channel. This may turn the model into wiggly jiggly jello in game.");
29352960
}
29362961

29372962
if (anyWeirdUV1s)
@@ -2944,6 +2969,11 @@ public static void CheckCommonUserErrors(TTModel model, Action<bool, string> log
29442969
loggingFunction(true, "Mesh: " + mIdx + " Part: " + pIdx + " has unusual UV2 data. This can have unexpected results on decal placement or opacity. Was this intended?");
29452970
}
29462971

2972+
if (anyWeirdUV3s)
2973+
{
2974+
loggingFunction(true, "Mesh: " + mIdx + " Part: " + pIdx + " has unusual UV3 data. This can have unexpected results on some shaders. Was this intended?");
2975+
}
2976+
29472977
pIdx++;
29482978
}
29492979
mIdx++;

xivModdingFramework/Models/FileTypes/Mdl.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3972,7 +3972,6 @@ private static int WriteVertex(VertexByteData importData, Dictionary<VertexUsage
39723972
{
39733973
if (vertexInfoList[VertexUsageType.BoneIndex][0] == VertexDataType.UByte8)
39743974
{
3975-
ModelModifiers.CleanWeight(v, 8, loggingFunction);
39763975

39773976
// 8 Byte stye...
39783977
importData.VertexData0.Add(v.Weights[0]);
@@ -3994,7 +3993,6 @@ private static int WriteVertex(VertexByteData importData, Dictionary<VertexUsage
39943993
importData.VertexData0.Add(v.BoneIds[7]);
39953994
} else
39963995
{
3997-
ModelModifiers.CleanWeight(v, 4, loggingFunction);
39983996
// 4 byte style ...
39993997
importData.VertexData0.Add(v.Weights[0]);
40003998
importData.VertexData0.Add(v.Weights[1]);

xivModdingFramework/Models/Helpers/ModelModifiers.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,15 +1349,6 @@ public static async Task ApplyRacialDeform(TTModel model, XivRace targetRace, bo
13491349
// And each vertex in that part...
13501350
foreach (var v in p.Vertices)
13511351
{
1352-
// Normalize weights before transforming to ensure consistent results.
1353-
if (usageInfo.NeedsEightWeights)
1354-
{
1355-
ModelModifiers.CleanWeight(v, 8, loggingFunction);
1356-
} else
1357-
{
1358-
ModelModifiers.CleanWeight(v, 4, loggingFunction);
1359-
}
1360-
13611352
Vector3 position = Vector3.Zero;
13621353
Vector3 normal = Vector3.Zero;
13631354
Vector3 binormal = Vector3.Zero;
@@ -1587,6 +1578,9 @@ public static void CleanWeights(TTModel model, Action<bool, string> loggingFunct
15871578
{
15881579
return;
15891580
}
1581+
1582+
var usage = model.GetUsageInfo();
1583+
15901584
var mIdx = 0;
15911585
foreach (var m in model.MeshGroups)
15921586
{
@@ -1598,7 +1592,14 @@ public static void CleanWeights(TTModel model, Action<bool, string> loggingFunct
15981592
var vIdx = 0;
15991593
foreach (var v in p.Vertices)
16001594
{
1601-
var majorCorrection = CleanWeight(v, model.MdlVersion == 5 ? 4 : 8, loggingFunction);
1595+
bool majorCorrection = false;
1596+
if(usage.NeedsEightWeights)
1597+
{
1598+
majorCorrection = CleanWeight(v, 8, loggingFunction);
1599+
} else
1600+
{
1601+
majorCorrection = CleanWeight(v, 4, loggingFunction);
1602+
}
16021603
if (majorCorrection)
16031604
{
16041605
perPartMajorCorrections++;

0 commit comments

Comments
 (0)