-
-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatically Merge Blendshape #1300
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fcfaf85
chore: rename freezeBlendShape to optimizeBlendShape
anatawa12 fae42d2
feat: automatically merge similarly animated meshes
anatawa12 be2988a
docs(changelog): Automatically Merge Blendshape
anatawa12 d2c89d0
docs(trace-and-optimize): improve Optimize BlendShape description
anatawa12 6c5417e
fix(AutoMergeBlendShape): unexpected public
anatawa12 c8c44cb
chore(ja/en): update some text
Sayamame-beans 28b55e9
docs: Apply suggestions from code review
anatawa12 2d62fe3
Merge branch 'master' into auto-merge-blendshape
anatawa12 c230c5d
docs(ja): Apply suggestion from Code Review
Sayamame-beans File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
Editor/Processors/TraceAndOptimize/AutoMergeBlendShape.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Anatawa12.AvatarOptimizer.AnimatorParsersV2; | ||
using Anatawa12.AvatarOptimizer.Processors.SkinnedMeshes; | ||
using nadena.dev.ndmf; | ||
using Unity.Collections; | ||
using Unity.Jobs; | ||
using UnityEngine; | ||
|
||
namespace Anatawa12.AvatarOptimizer.Processors.TraceAndOptimizes; | ||
|
||
class AutoMergeBlendShape: TraceAndOptimizePass<AutoMergeBlendShape> | ||
{ | ||
public override string DisplayName => "T&O: Auto Merge Blend Shape"; | ||
|
||
protected override void Execute(BuildContext context, TraceAndOptimizeState state) | ||
{ | ||
if (!state.OptimizeBlendShape) return; | ||
if (state.SkipAutoMergeBlendShape) return; | ||
|
||
foreach (var skinnedMeshRenderer in context.GetComponents<SkinnedMeshRenderer>()) | ||
{ | ||
if (state.Exclusions.Contains(skinnedMeshRenderer.gameObject)) continue; // manual exclusion | ||
|
||
ErrorReport.WithContextObject(skinnedMeshRenderer, () => DoAutoMerge(context.GetMeshInfoFor(skinnedMeshRenderer), context)); | ||
} | ||
} | ||
|
||
private static void DoAutoMerge(MeshInfo2 meshInfo2, BuildContext context) | ||
{ | ||
var animationComponent = context.GetAnimationComponent(meshInfo2.SourceRenderer); | ||
var groups = new Dictionary<MergeKey, List<string>>(); | ||
|
||
foreach (var (name, weight) in meshInfo2.BlendShapes) | ||
{ | ||
if (MergeKey.Create(weight, name, animationComponent) is { } key) | ||
{ | ||
if (!groups.TryGetValue(key, out var list)) | ||
groups.Add(key, list = new List<string>()); | ||
list.Add(name); | ||
} | ||
} | ||
|
||
// nothing to merge | ||
if (groups.Values.All(x => x.Count <= 1)) return; | ||
|
||
// prepare merge | ||
var buffers = meshInfo2.Vertices.Select(x => x.BlendShapeBuffer).ToArray(); | ||
|
||
// bulk remove to optimize removing blendshape process | ||
var removeNames = new HashSet<string>(); | ||
|
||
var i = 0; | ||
// there is thing to merge | ||
foreach (var (key, names) in groups) | ||
{ | ||
// validate the blendShapes are simple enough to merge | ||
// if not, skip | ||
foreach (var buffer in buffers) | ||
{ | ||
float? commonFrameWeight = null; | ||
|
||
foreach (var name in names) | ||
{ | ||
if (buffer.Shapes.TryGetValue(name, out var shapeShape)) | ||
{ | ||
if (shapeShape.FrameCount != 1) goto next_shape; | ||
var frameWeight = shapeShape.Frames[0].Weight; | ||
if (commonFrameWeight is { } common) | ||
{ | ||
if (!common.Equals(frameWeight)) goto next_shape; | ||
} | ||
else | ||
{ | ||
commonFrameWeight = frameWeight; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// validation passed, merge | ||
var newName = $"AAO_Merged_{string.Join("_", names)}_{i++}"; | ||
|
||
// process meshInfo2 | ||
meshInfo2.BlendShapes.Add((newName, key.defaultWeight)); | ||
removeNames.UnionWith(names); | ||
|
||
// actually merge data | ||
foreach (var buffer in buffers) | ||
{ | ||
BlendShapeShape? newShapeShape = null; | ||
|
||
foreach (var name in names) | ||
{ | ||
if (buffer.Shapes.Remove(name, out var shapeShape)) | ||
{ | ||
if (newShapeShape == null) | ||
{ | ||
newShapeShape = shapeShape; | ||
} | ||
else | ||
{ | ||
var vertices = new MergeBlendShapeJob | ||
{ | ||
vertices = buffer.DeltaVertices[newShapeShape.Frames[0].BufferIndex], | ||
toMerge = buffer.DeltaVertices[shapeShape.Frames[0].BufferIndex], | ||
}.Schedule(buffer.VertexCount, 64); | ||
var normals = new MergeBlendShapeJob | ||
{ | ||
vertices = buffer.DeltaNormals[newShapeShape.Frames[0].BufferIndex], | ||
toMerge = buffer.DeltaNormals[shapeShape.Frames[0].BufferIndex], | ||
}.Schedule(buffer.VertexCount, 64); | ||
var tangents = new MergeBlendShapeJob | ||
{ | ||
vertices = buffer.DeltaTangents[newShapeShape.Frames[0].BufferIndex], | ||
toMerge = buffer.DeltaTangents[shapeShape.Frames[0].BufferIndex], | ||
}.Schedule(buffer.VertexCount, 64); | ||
JobHandle.CombineDependencies(vertices, normals, tangents).Complete(); | ||
} | ||
} | ||
} | ||
|
||
// null means there are no shapes to merge for this buffer | ||
if (newShapeShape != null) | ||
{ | ||
buffer.Shapes.Add(newName, newShapeShape); | ||
} | ||
} | ||
|
||
next_shape: ; | ||
} | ||
|
||
// remove merged blendShapes | ||
meshInfo2.BlendShapes.RemoveAll(x => removeNames.Contains(x.name)); | ||
} | ||
|
||
readonly struct MergeKey : IEquatable<MergeKey> | ||
{ | ||
public readonly float defaultWeight; | ||
public readonly EqualsHashSet<AnimationLocation> animationLocations; | ||
|
||
public MergeKey(float defaultWeight, EqualsHashSet<AnimationLocation> animationLocations) | ||
{ | ||
this.defaultWeight = defaultWeight; | ||
this.animationLocations = animationLocations; | ||
} | ||
|
||
public static MergeKey? Create(float defaultWeight, string name, AnimationComponentInfo<PropertyInfo> animationComponent) | ||
{ | ||
var node = animationComponent.GetFloatNode($"blendShape.{name}"); | ||
// to merge, all nodes must be AnimatorPropModNode | ||
if (!node.ComponentNodes.All(x => x is AnimatorPropModNode<FloatValueInfo>)) return null; | ||
var animationLocations = AnimationLocation.CollectAnimationLocation(node).ToEqualsHashSet(); | ||
return new MergeKey(defaultWeight, animationLocations); | ||
} | ||
|
||
public bool Equals(MergeKey other) => | ||
defaultWeight.Equals(other.defaultWeight) && | ||
animationLocations.Equals(other.animationLocations); | ||
|
||
public override bool Equals(object? obj) => obj is MergeKey other && Equals(other); | ||
public override int GetHashCode() => HashCode.Combine(defaultWeight, animationLocations); | ||
public static bool operator ==(MergeKey left, MergeKey right) => left.Equals(right); | ||
public static bool operator !=(MergeKey left, MergeKey right) => !left.Equals(right); | ||
} | ||
|
||
struct MergeBlendShapeJob : IJobParallelFor | ||
{ | ||
public NativeArray<Vector3> vertices; | ||
public NativeArray<Vector3> toMerge; | ||
|
||
public void Execute(int index) => vertices[index] += toMerge[index]; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Editor/Processors/TraceAndOptimize/AutoMergeBlendShape.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この辺りの記述古くなったりしている?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. なので放置した。 #1297