Skip to content

Commit

Permalink
fix: viseme may be broken
Browse files Browse the repository at this point in the history
  • Loading branch information
anatawa12 committed Oct 5, 2023
1 parent c0344f2 commit 47a0482
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ public InternalAutoFreezeMeaninglessBlendShapeProcessor(InternalAutoFreezeMeanin
public override void Process(OptimizerSession session, MeshInfo2 target)
{
var meaningfulBlendShapes = new HashSet<string>();
var preserve = Component.Preserve;
if (preserve != null) meaningfulBlendShapes.UnionWith(preserve);

foreach (var vertex in target.Vertices)
meaningfulBlendShapes.UnionWith(vertex.BlendShapes.Keys);

var freezeBlendShape = Target.GetComponent<FreezeBlendShape>();
var set = freezeBlendShape.shapeKeysSet.GetAsSet();
set.UnionWith(target.BlendShapes.Where(x => !meaningfulBlendShapes.Contains(x.name)).Select(x => x.name));
set.UnionWith(target.BlendShapes.Where(x => !meaningfulBlendShapes.Contains(x.name))
.Select(x => x.name));
freezeBlendShape.shapeKeysSet.SetValueNonPrefab(set);
}

Expand Down
61 changes: 60 additions & 1 deletion Editor/Processors/TraceAndOptimize/AutoFreezeBlendShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using UnityEditor;
using UnityEngine;
using VRC.SDK3.Avatars.Components;
using VRC.SDKBase;

namespace Anatawa12.AvatarOptimizer.Processors.TraceAndOptimizes
{
Expand Down Expand Up @@ -71,13 +73,70 @@ public void Process()
serialized.ApplyModifiedPropertiesWithoutUndo();
}

var preserveBlendShapes = ComputePreserveBlendShapes();

// second optimization: remove meaningless blendShapes
foreach (var skinnedMeshRenderer in _session.GetComponents<SkinnedMeshRenderer>())
{
if (_exclusions.Contains(skinnedMeshRenderer.gameObject)) continue; // manual exclusion
skinnedMeshRenderer.gameObject.GetOrAddComponent<FreezeBlendShape>();
skinnedMeshRenderer.gameObject.GetOrAddComponent<InternalAutoFreezeMeaninglessBlendShape>();
var internalMeaningless = skinnedMeshRenderer.gameObject.GetOrAddComponent<InternalAutoFreezeMeaninglessBlendShape>();
preserveBlendShapes.TryGetValue(skinnedMeshRenderer, out internalMeaningless.Preserve);
}
}

private Dictionary<SkinnedMeshRenderer, HashSet<string>> ComputePreserveBlendShapes()
{
// some BlendShapes manipulated by VRC Avatar Descriptor must exists
var preserveBlendShapes = new Dictionary<SkinnedMeshRenderer, HashSet<string>>();
var descriptor = _session.GetRootComponent<VRCAvatarDescriptor>();
switch (descriptor.lipSync)
{
case VRC_AvatarDescriptor.LipSyncStyle.VisemeBlendShape when descriptor.VisemeSkinnedMesh != null:
{
var skinnedMeshRenderer = descriptor.VisemeSkinnedMesh;
if (!preserveBlendShapes.TryGetValue(skinnedMeshRenderer, out var set))
preserveBlendShapes.Add(skinnedMeshRenderer, set = new HashSet<string>());
set.UnionWith(descriptor.VisemeBlendShapes);
break;
}
case VRC_AvatarDescriptor.LipSyncStyle.JawFlapBlendShape when descriptor.VisemeSkinnedMesh != null:
{
var skinnedMeshRenderer = descriptor.VisemeSkinnedMesh;
if (!preserveBlendShapes.TryGetValue(skinnedMeshRenderer, out var set))
preserveBlendShapes.Add(skinnedMeshRenderer, set = new HashSet<string>());
set.Add(descriptor.MouthOpenBlendShapeName);
break;
}
}

if (descriptor.enableEyeLook)
{
switch (descriptor.customEyeLookSettings.eyelidType)
{
case VRCAvatarDescriptor.EyelidType.None:
break;
case VRCAvatarDescriptor.EyelidType.Bones:
break;
case VRCAvatarDescriptor.EyelidType.Blendshapes
when descriptor.customEyeLookSettings.eyelidsSkinnedMesh != null:
{
var skinnedMeshRenderer = descriptor.customEyeLookSettings.eyelidsSkinnedMesh;
if (!preserveBlendShapes.TryGetValue(skinnedMeshRenderer, out var set))
preserveBlendShapes.Add(skinnedMeshRenderer, set = new HashSet<string>());

var mesh = skinnedMeshRenderer.sharedMesh;
set.UnionWith(
from index in descriptor.customEyeLookSettings.eyelidsBlendshapes
where 0 <= index && index < mesh.blendShapeCount
select mesh.GetBlendShapeName(index)
);
}
break;
}
}

return preserveBlendShapes;
}
}
}
3 changes: 3 additions & 0 deletions Runtime/InternalAutoFreezeMeaninglessBlendShape.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEngine;

namespace Anatawa12.AvatarOptimizer
Expand All @@ -7,5 +9,6 @@ namespace Anatawa12.AvatarOptimizer
[DisallowMultipleComponent]
internal class InternalAutoFreezeMeaninglessBlendShape : EditSkinnedMeshComponent
{
[CanBeNull] internal HashSet<string> Preserve;
}
}

0 comments on commit 47a0482

Please sign in to comment.