diff --git a/Editor/Processors/AutomaticConfiguration/AutomaticConfigurationProcessor.AutoFreezeBlendShape.cs b/Editor/Processors/AutomaticConfiguration/AutomaticConfigurationProcessor.AutoFreezeBlendShape.cs new file mode 100644 index 000000000..30bd1ca7a --- /dev/null +++ b/Editor/Processors/AutomaticConfiguration/AutomaticConfigurationProcessor.AutoFreezeBlendShape.cs @@ -0,0 +1,39 @@ +using System.Linq; +using UnityEditor; +using UnityEngine; + +namespace Anatawa12.AvatarOptimizer.Processors +{ + partial class AutomaticConfigurationProcessor + { + private void AutoFreezeBlendShape() + { + foreach (var skinnedMeshRenderer in _session.GetComponents()) + { + var mesh = skinnedMeshRenderer.sharedMesh; + + // skip SMR without mesh + if (!mesh) continue; + // skip configured mesh + if (skinnedMeshRenderer.GetComponent()) continue; + + var modifies = GetModifiedProperties(skinnedMeshRenderer); + var notChanged = Enumerable.Range(0, mesh.blendShapeCount) + .Select(i => mesh.GetBlendShapeName(i)) + .Where(name => !modifies.Contains($"blendShape.{name}")) + .ToArray(); + + if (notChanged.Length == 0) continue; + + var freeze = skinnedMeshRenderer.gameObject.AddComponent(); + var serialized = new SerializedObject(freeze); + var editorUtil = PrefabSafeSet.EditorUtil.Create( + serialized.FindProperty(nameof(FreezeBlendShape.shapeKeysSet)), + 0, p => p.stringValue, (p, v) => p.stringValue = v); + foreach (var shape in notChanged) + editorUtil.GetElementOf(shape).EnsureAdded(); + serialized.ApplyModifiedPropertiesWithoutUndo(); + } + } + } +} \ No newline at end of file diff --git a/Editor/Processors/AutomaticConfiguration/AutomaticConfigurationProcessor.AutoFreezeBlendShape.cs.meta b/Editor/Processors/AutomaticConfiguration/AutomaticConfigurationProcessor.AutoFreezeBlendShape.cs.meta new file mode 100644 index 000000000..f8237b03d --- /dev/null +++ b/Editor/Processors/AutomaticConfiguration/AutomaticConfigurationProcessor.AutoFreezeBlendShape.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 856c21da5d1f4eb29a5c3593c2825ba9 +timeCreated: 1689553831 \ No newline at end of file diff --git a/Editor/Processors/AutomaticConfiguration/AutomaticConfigurationProcessor.cs b/Editor/Processors/AutomaticConfiguration/AutomaticConfigurationProcessor.cs index b24f1b3be..1f9b339e3 100644 --- a/Editor/Processors/AutomaticConfiguration/AutomaticConfigurationProcessor.cs +++ b/Editor/Processors/AutomaticConfiguration/AutomaticConfigurationProcessor.cs @@ -20,6 +20,7 @@ public void Process(OptimizerSession session) // TODO: implement GatherAnimationModifications(); + AutoFreezeBlendShape(); } private IReadOnlyCollection GetModifiedProperties(Component component)