public void SmoothWeights(List<Node> targetNodes)
{
float[,] weights = new float[nodes.Count,bindPoses.Count];
Array.Clear(weights,0,weights.Length);
List<int> usedIndices = new List<int>();
for (int i = 0; i < nodes.Count; i++)
{
usedIndices.Clear();
BoneWeight weight = boneWeights[i];
if(weight.boneIndex0 >= 0)
{
weights[i,weight.boneIndex0] = weight.weight0;
usedIndices.Add(weight.boneIndex0);
}
if(weight.boneIndex1 >= 0 && !usedIndices.Contains(weight.boneIndex1))
{
weights[i,weight.boneIndex1] = weight.weight1;
usedIndices.Add(weight.boneIndex1);
}
if(weight.boneIndex2 >= 0 && !usedIndices.Contains(weight.boneIndex2))
{
weights[i,weight.boneIndex2] = weight.weight2;
usedIndices.Add(weight.boneIndex2);
}
if(weight.boneIndex3 >= 0 && !usedIndices.Contains(weight.boneIndex3))
{
weights[i,weight.boneIndex3] = weight.weight3;
usedIndices.Add(weight.boneIndex3);
}
}
float[] denominator = new float[nodes.Count];
float[,] smoothedWeights = new float[nodes.Count,bindPoses.Count];
Array.Clear(smoothedWeights,0,smoothedWeights.Length);
for (int i = 0; i < indices.Count / 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
int j1 = (j + 1) % 3;
int j2 = (j + 2) % 3;
for(int k = 0; k < bindPoses.Count; ++k)
{
smoothedWeights[indices[i*3 + j],k] += weights[indices[i*3 + j1],k] + weights[indices[i*3 + j2],k];
}
denominator[indices[i*3 + j]] += 2;
}
}
for (int i = 0; i < nodes.Count; ++i)
{
for (int j = 0; j < bindPoses.Count; ++j)
{
smoothedWeights[i,j] /= denominator[i];
}
}
float[,] smoothedWeightsTransposed = new float[bindPoses.Count,nodes.Count];
for (int i = 0; i < nodes.Count; ++i)
{
for (int j = 0; j < bindPoses.Count; ++j)
{
smoothedWeightsTransposed[j,i] = smoothedWeights[i,j];
}
}
FillBoneWeights(targetNodes, smoothedWeightsTransposed);
isDirty = true;
}