Skip to content

Commit

Permalink
Fix synced animator controller layers breaking when layers get merged…
Browse files Browse the repository at this point in the history
… or removed. #138
  • Loading branch information
d4rkc0d3r committed Dec 22, 2024
1 parent 99e363f commit a5ea908
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Fix meshes with different cast & receive shadow settings getting merged together.
* Fix ParticleSystems with skinned mesh renderer shape breaking when merging meshes & materials.
* Add missing MMD blendshape names to the list of known MMD blendshape names. [(more)](https://github.com/d4rkc0d3r/d4rkAvatarOptimizer/issues/137)
* Fix synced animator controller layers breaking when layers get merged or removed. [(more)](https://github.com/d4rkc0d3r/d4rkAvatarOptimizer/issues/138)

## v3.9.2
### Bug Fixes
Expand Down
3 changes: 3 additions & 0 deletions Editor/AnimatorOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class AnimatorOptimizer
private HashSet<string> boolsToChangeToFloat = new HashSet<string>();
private HashSet<string> intsToChangeToFloat = new HashSet<string>();
private List<(EditorCurveBinding binding, float value)> constantCurvesToAdd = new List<(EditorCurveBinding binding, float value)>();
private bool isFxLayer = false;

private AnimatorOptimizer(AnimatorController target, AnimatorController source)
{
Expand Down Expand Up @@ -61,6 +62,7 @@ public static AnimatorController Run(AnimatorController source, string path, Dic
optimizer.fxLayerMap = new Dictionary<int, int>(fxLayerMap);
optimizer.layersToMerge = new HashSet<int>(layersToMerge);
optimizer.layersToDestroy = new HashSet<int>(layersToDestroy);
optimizer.isFxLayer = constantCurvesToAdd != null;
optimizer.constantCurvesToAdd = constantCurvesToAdd ?? new List<(EditorCurveBinding binding, float value)>();
return optimizer.Run();
}
Expand Down Expand Up @@ -390,6 +392,7 @@ private AnimatorControllerLayer CloneLayer(AnimatorControllerLayer old, bool isF
iKPass = old.iKPass,
name = old.name,
syncedLayerAffectsTiming = old.syncedLayerAffectsTiming,
syncedLayerIndex = isFxLayer && fxLayerMap.TryGetValue(old.syncedLayerIndex, out int newLayerIndex) ? newLayerIndex : old.syncedLayerIndex,
stateMachine = CloneStateMachine(old.stateMachine)
};
CloneTransitions(old.stateMachine, n.stateMachine);
Expand Down
18 changes: 18 additions & 0 deletions Editor/d4rkAvatarOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,15 @@ public List<List<string>> AnalyzeFXLayerMergeAbility()
continue;
}
var layer = fxLayerLayers[i];
if (layer.syncedLayerIndex != -1)
{
errorMessages[i].Add($"synced with layer {layer.syncedLayerIndex}");
if (layer.syncedLayerIndex >= 0 && layer.syncedLayerIndex < fxLayerLayers.Length)
{
errorMessages[layer.syncedLayerIndex].Insert(0, $"layer {i} is synced with this layer");
}
continue;
}
var stateMachine = layer.stateMachine;
if (stateMachine == null)
{
Expand Down Expand Up @@ -2098,6 +2107,8 @@ bool IsPossibleBinding(EditorCurveBinding binding)
if (i <= 2 && MMDCompatibility)
break;
var layer = fxLayerLayers[i];
if (layer.syncedLayerIndex != -1)
continue;
bool isNotFirstLayerOrLastNonUselessLayerCanBeFirst = i != 0 ||
(lastNonUselessLayer < fxLayerLayers.Length && fxLayerLayers[lastNonUselessLayer].avatarMask == layer.avatarMask
&& fxLayerLayers[lastNonUselessLayer].defaultWeight == 1 && !isAffectedByLayerWeightControl.Contains(lastNonUselessLayer));
Expand Down Expand Up @@ -2130,6 +2141,13 @@ bool IsPossibleBinding(EditorCurveBinding binding)
}
lastNonUselessLayer = i;
}
for (int i = 0; i < fxLayerLayers.Length; i++)
{
if (fxLayerLayers[i].syncedLayerIndex != -1)
{
uselessLayers.Remove(fxLayerLayers[i].syncedLayerIndex);
}
}
Profiler.EndSection();
return cache_FindUselessFXLayers = uselessLayers;
}
Expand Down

0 comments on commit a5ea908

Please sign in to comment.