Browse Source

Merge pull request #246 from AtomicGameEngine/JME-ATOMIC-SKINREDUX

On more than 4 bone influences drop least significant instead of error
JoshEngebretson 10 years ago
parent
commit
98ec7a7e2b
1 changed files with 25 additions and 19 deletions
  1. 25 19
      Source/ToolCore/Import/OpenAssetUtils.cpp

+ 25 - 19
Source/ToolCore/Import/OpenAssetUtils.cpp

@@ -315,11 +315,10 @@ bool GetBlendData(OutModel& model, aiMesh* mesh, PODVector<unsigned>& boneMappin
     {
     {
         if (mesh->mNumBones > maxBones)
         if (mesh->mNumBones > maxBones)
         {
         {
-            errorMessage =
-                "Geometry (submesh) has over " + String(maxBones) + " bone influences. Try splitting to more submeshes\n"
+            errorMessage = "Geometry (submesh) has over " + String(maxBones) + " bone influences. Try splitting to more submeshes\n"
                 "that each stay at " + String(maxBones) + " bones or below.";
                 "that each stay at " + String(maxBones) + " bones or below.";
 
 
-             return false;
+            return false;
         }
         }
         boneMappings.Resize(mesh->mNumBones);
         boneMappings.Resize(mesh->mNumBones);
         for (unsigned i = 0; i < mesh->mNumBones; ++i)
         for (unsigned i = 0; i < mesh->mNumBones; ++i)
@@ -335,11 +334,6 @@ bool GetBlendData(OutModel& model, aiMesh* mesh, PODVector<unsigned>& boneMappin
                 unsigned vertex = bone->mWeights[j].mVertexId;
                 unsigned vertex = bone->mWeights[j].mVertexId;
                 blendIndices[vertex].Push(i);
                 blendIndices[vertex].Push(i);
                 blendWeights[vertex].Push(bone->mWeights[j].mWeight);
                 blendWeights[vertex].Push(bone->mWeights[j].mWeight);
-                if (blendWeights[vertex].Size() > 4)
-                {
-                    errorMessage = "More than 4 bone influences on vertex";
-                    return false;
-                }
             }
             }
         }
         }
     }
     }
@@ -351,27 +345,40 @@ bool GetBlendData(OutModel& model, aiMesh* mesh, PODVector<unsigned>& boneMappin
             String boneName = FromAIString(bone->mName);
             String boneName = FromAIString(bone->mName);
             unsigned globalIndex = GetBoneIndex(model, boneName);
             unsigned globalIndex = GetBoneIndex(model, boneName);
             if (globalIndex == M_MAX_UNSIGNED)
             if (globalIndex == M_MAX_UNSIGNED)
-            {
-                errorMessage = "Bone " + boneName + " not found";
-                return false;
-            }
+                ErrorExit("Bone " + boneName + " not found");
             for (unsigned j = 0; j < bone->mNumWeights; ++j)
             for (unsigned j = 0; j < bone->mNumWeights; ++j)
             {
             {
                 unsigned vertex = bone->mWeights[j].mVertexId;
                 unsigned vertex = bone->mWeights[j].mVertexId;
                 blendIndices[vertex].Push(globalIndex);
                 blendIndices[vertex].Push(globalIndex);
                 blendWeights[vertex].Push(bone->mWeights[j].mWeight);
                 blendWeights[vertex].Push(bone->mWeights[j].mWeight);
-                if (blendWeights[vertex].Size() > 4)
-                {
-                    errorMessage = "More than 4 bone influences on vertex";
-                    return false;
-                }
             }
             }
         }
         }
     }
     }
 
 
-    // Normalize weights now if necessary
+    // Normalize weights now if necessary, also remove too many influences
     for (unsigned i = 0; i < blendWeights.Size(); ++i)
     for (unsigned i = 0; i < blendWeights.Size(); ++i)
     {
     {
+        if (blendWeights[i].Size() > 4)
+        {
+            PrintLine("Warning: more than 4 bone influences in vertex " + String(i));
+
+            while (blendWeights[i].Size() > 4)
+            {
+                unsigned lowestIndex = 0;
+                float lowest = M_INFINITY;
+                for (unsigned j = 0; j < blendWeights[i].Size(); ++j)
+                {
+                    if (blendWeights[i][j] < lowest)
+                    {
+                        lowest = blendWeights[i][j];
+                        lowestIndex = j;
+                    }
+                }
+                blendWeights[i].Erase(lowestIndex);
+                blendIndices[i].Erase(lowestIndex);
+            }
+        }
+
         float sum = 0.0f;
         float sum = 0.0f;
         for (unsigned j = 0; j < blendWeights[i].Size(); ++j)
         for (unsigned j = 0; j < blendWeights[i].Size(); ++j)
             sum += blendWeights[i][j];
             sum += blendWeights[i][j];
@@ -382,7 +389,6 @@ bool GetBlendData(OutModel& model, aiMesh* mesh, PODVector<unsigned>& boneMappin
         }
         }
     }
     }
 
 
-
     return true;
     return true;
 }
 }