Skip to content

Commit

Permalink
Merge pull request #1324 from anatawa12/fix-animation-merge-blendhsape
Browse files Browse the repository at this point in the history
fix: animation broken with auto merge blendShape
  • Loading branch information
anatawa12 authored Nov 3, 2024
2 parents 0790cfa + 027a05e commit 2b2e8ed
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog].
### Removed

### Fixed
- Animation broken with auto merge blendShape `#1324`

### Security

Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ The format is based on [Keep a Changelog].
- When you changed shader for an material, properties for previously used shaders might be remain
- This may increase your avatar size by unexpectedly including unused textures
- Right-click menu option to create a new GameObject with a specified component [`#1290`](https://github.com/anatawa12/AvatarOptimizer/pull/1290)
- Automatically Merge Blendshape `#1300`
- Automatically Merge Blendshape `#1300` `#1324`
- This is new automatic optimization in Trace and Optimize
- This is a part of "Optimize BlendShape" optimization.
- AAO 1.8.0 introduced BlendShape support for Merge Skinned Mesh, but new default mode "Rename to avoid conflicts" would increase number of BlendShape.
Expand Down
22 changes: 22 additions & 0 deletions Editor/Processors/TraceAndOptimize/AutoMergeBlendShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ private static void DoAutoMerge(MeshInfo2 meshInfo2, BuildContext context)
// bulk remove to optimize removing blendshape process
var removeNames = new HashSet<string>();

var newNames = new Dictionary<string, string>();

var i = 0;
// there is thing to merge
foreach (var (key, names) in groups)
Expand Down Expand Up @@ -82,6 +84,12 @@ private static void DoAutoMerge(MeshInfo2 meshInfo2, BuildContext context)
// validation passed, merge
var newName = $"AAO_Merged_{string.Join("_", names)}_{i++}";

foreach (var name in names)
{
newNames.Add(name, newName);
context.RecordMoveProperty(meshInfo2.SourceRenderer, $"blendShape.{name}", $"blendShape.{newName}");
}

// process meshInfo2
meshInfo2.BlendShapes.Add((newName, key.defaultWeight));
removeNames.UnionWith(names);
Expand Down Expand Up @@ -131,8 +139,22 @@ private static void DoAutoMerge(MeshInfo2 meshInfo2, BuildContext context)
next_shape: ;
}

var prevNames = meshInfo2.BlendShapes.Select(x => x.name).ToList();

// remove merged blendShapes
meshInfo2.BlendShapes.RemoveAll(x => removeNames.Contains(x.name));

for (var index = 0; index < prevNames.Count; index++)
{
var name = prevNames[index];
var newName = newNames.GetValueOrDefault(name, name);
var newIndex = meshInfo2.BlendShapes.FindIndex(x => x.name == newName);
if (newIndex != -1)
{
context.RecordMoveProperty(meshInfo2.SourceRenderer,
VProp.BlendShapeIndex(index), VProp.BlendShapeIndex(newIndex));
}
}
}

readonly struct MergeKey : IEquatable<MergeKey>
Expand Down

0 comments on commit 2b2e8ed

Please sign in to comment.