Skip to content

Commit

Permalink
Fix crash when the avatar has a skinned mesh with out of bounds bone …
Browse files Browse the repository at this point in the history
…indices. #129
  • Loading branch information
d4rkc0d3r committed Oct 15, 2024
1 parent 4d266aa commit 7409c20
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v3.9.1
### Bug Fixes
* Fix crash when the avatar has a skinned mesh with out of bounds bone indices. [(more)](https://github.com/d4rkc0d3r/d4rkAvatarOptimizer/issues/129)

## v3.9.0
### Features
* Generated shaders and materials are now stripped of all properties that got baked into the shader.
Expand Down
20 changes: 16 additions & 4 deletions Editor/d4rkAvatarOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2242,15 +2242,23 @@ void AddDependency(Transform t, Object obj)
var meshBones = skinnedMesh.bones;
var usedBoneIDs = new bool[meshBones.Length];
var boneWeights = skinnedMesh.sharedMesh.boneWeights;
int outOfRangeBoneCount = 0;
void MarkUsedBone(int boneIndex) {
if (boneIndex < 0 || boneIndex >= meshBones.Length) {
outOfRangeBoneCount++;
return;
}
usedBoneIDs[boneIndex] = true;
}
for (int i = 0; i < boneWeights.Length; i++)
{
usedBoneIDs[boneWeights[i].boneIndex0] = true;
MarkUsedBone(boneWeights[i].boneIndex0);
if (boneWeights[i].weight1 > 0) {
usedBoneIDs[boneWeights[i].boneIndex1] = true;
MarkUsedBone(boneWeights[i].boneIndex1);
if (boneWeights[i].weight2 > 0) {
usedBoneIDs[boneWeights[i].boneIndex2] = true;
MarkUsedBone(boneWeights[i].boneIndex2);
if (boneWeights[i].weight3 > 0) {
usedBoneIDs[boneWeights[i].boneIndex3] = true;
MarkUsedBone(boneWeights[i].boneIndex3);
}
}
}
Expand All @@ -2262,6 +2270,10 @@ void AddDependency(Transform t, Object obj)
AddDependency(meshBones[i], skinnedMesh);
}
}
if (outOfRangeBoneCount > 0)
{
Debug.LogWarning($"Skinned mesh renderer {GetPathToRoot(skinnedMesh)} has {outOfRangeBoneCount} out of range bone indices");
}
}
foreach (var behavior in GetComponentsInChildren<Behaviour>(true)
.Where(b => b != null && (b.GetType().Name.Contains("Constraint") || b.GetType().FullName.StartsWithSimple("RootMotion.FinalIK"))))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "d4rkpl4y3r.d4rkavataroptimizer",
"displayName": "d4rkAvatarOptimizer",
"version": "3.9.0",
"version": "3.9.1",
"unity": "2019.4",
"description": "An optimizer aiming to reduce mesh & material count and more of VRChat 3.0 avatars.",
"dependencies": {},
Expand Down

0 comments on commit 7409c20

Please sign in to comment.