Skip to content

Commit ea9a3c5

Browse files
Fixed skin indexing and normalization issues (#1142)
1 parent 99da3bb commit ea9a3c5

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

Maya/Exporter/BabylonExporter.Mesh.cs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,14 +1020,17 @@ private GlobalVertex ExtractVertex(MFnMesh mFnMesh, int polygonId, int vertexInd
10201020
OrderByDescending(ref weightByInfluenceIndex);
10211021

10221022
int bonesCount = indexByNodeName.Count; // number total of bones/influences for the mesh
1023+
10231024
float weight0 = 0;
10241025
float weight1 = 0;
10251026
float weight2 = 0;
10261027
float weight3 = 0;
1028+
10271029
ushort bone0 = 0;
10281030
ushort bone1 = 0;
10291031
ushort bone2 = 0;
10301032
ushort bone3 = 0;
1033+
10311034
int nbBones = weightByInfluenceIndex.Count; // number of bones/influences for this vertex
10321035

10331036
if (nbBones == 0)
@@ -1067,36 +1070,43 @@ private GlobalVertex ExtractVertex(MFnMesh mFnMesh, int polygonId, int vertexInd
10671070

10681071
if (nbBones > 4)
10691072
{
1070-
bone0 = (ushort)weightByInfluenceIndex.ElementAt(4).Key;
1071-
weight0 = (float)weightByInfluenceIndex.ElementAt(4).Value;
1072-
weight1 = 0;
1073-
weight2 = 0;
1074-
weight3 = 0;
1073+
ushort bone4 = (ushort)weightByInfluenceIndex.ElementAt(4).Key;
1074+
float weight4 = (float)weightByInfluenceIndex.ElementAt(4).Value;
1075+
1076+
ushort bone5 = 0;
1077+
float weight5 = 0;
1078+
1079+
ushort bone6 = 0;
1080+
float weight6 = 0;
1081+
1082+
ushort bone7 = 0;
1083+
float weight7 = 0;
10751084

10761085
if (nbBones > 5)
10771086
{
1078-
bone1 = (ushort)weightByInfluenceIndex.ElementAt(5).Key;
1079-
weight1 = (float)weightByInfluenceIndex.ElementAt(4).Value;
1087+
bone5 = (ushort)weightByInfluenceIndex.ElementAt(5).Key;
1088+
weight5 = (float)weightByInfluenceIndex.ElementAt(5).Value;
10801089

10811090
if (nbBones > 6)
10821091
{
1083-
bone2 = (ushort)weightByInfluenceIndex.ElementAt(6).Key;
1084-
weight2 = (float)weightByInfluenceIndex.ElementAt(4).Value;
1092+
bone6 = (ushort)weightByInfluenceIndex.ElementAt(6).Key;
1093+
weight6 = (float)weightByInfluenceIndex.ElementAt(6).Value;
10851094

10861095
if (nbBones > 7)
10871096
{
1088-
bone3 = (ushort)weightByInfluenceIndex.ElementAt(7).Key;
1089-
weight3 = (float)weightByInfluenceIndex.ElementAt(7).Value;
1097+
bone7 = (ushort)weightByInfluenceIndex.ElementAt(7).Key;
1098+
weight7 = (float)weightByInfluenceIndex.ElementAt(7).Value;
10901099
}
10911100
}
10921101
}
10931102

1094-
float[] weightsExtra = { weight0, weight1, weight2, weight3 };
1103+
float[] weightsExtra = { weight4, weight5, weight6, weight7 };
10951104
vertex.WeightsExtra = weightsExtra;
1096-
ushort[] boneIndexesExtra = { bone0, bone1, bone2, bone3 };
1105+
ushort[] boneIndexesExtra = { bone4, bone5, bone6, bone7 };
10971106
vertex.BonesIndicesExtra = boneIndexesExtra;
10981107
}
10991108
}
1109+
11001110
return vertex;
11011111
}
11021112

Maya/Exporter/BabylonExporter.Skeleton.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using Utilities;
78

89
namespace Maya2Babylon
910
{
@@ -397,7 +398,6 @@ private BabylonBone[] ExportBones(MFnSkinCluster skin)
397398
return bones.ToArray();
398399
}
399400

400-
401401
/// <summary>
402402
/// Normalize the value in the dictionary.
403403
/// </summary>
@@ -408,7 +408,8 @@ private void Normalize(ref Dictionary<int, double> intByDouble)
408408
if (intByDouble.Count != 0)
409409
{
410410
double totalValue = intByDouble.Values.Sum();
411-
if (totalValue != 1)
411+
412+
if (!MathUtilities.IsAlmostEqualTo(totalValue, 1.00))
412413
{
413414
for (int index = 0; index < intByDouble.Count; index++)
414415
{

SharedProjects/Utilities/MathUtilities.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace Utilities
66
static class MathUtilities
77
{
88
public const float Epsilon = 1E-7f;
9+
public const double EpsilonDouble = 1E-7;
10+
911
public static float GetLerpFactor(float from, float to, float value)
1012
{
1113
return (value - from) / (to - from);
@@ -57,6 +59,11 @@ public static bool IsAlmostEqualTo(float first, float second, float epsilon = Ep
5759
return Math.Abs(first - second) <= epsilon;
5860
}
5961

62+
public static bool IsAlmostEqualTo(double first, double second, double epsilon = EpsilonDouble)
63+
{
64+
return Math.Abs(first - second) <= epsilon;
65+
}
66+
6067
/// <summary>
6168
/// This is used to round floating
6269
/// </summary>

0 commit comments

Comments
 (0)