Skip to content

Commit

Permalink
feat: Automatic configuration for FreezeBlendShape
Browse files Browse the repository at this point in the history
  • Loading branch information
anatawa12 committed Jul 17, 2023
1 parent 2a7599c commit f4d75f3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<SkinnedMeshRenderer>())
{
var mesh = skinnedMeshRenderer.sharedMesh;

// skip SMR without mesh
if (!mesh) continue;
// skip configured mesh
if (skinnedMeshRenderer.GetComponent<FreezeBlendShape>()) 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<FreezeBlendShape>();
var serialized = new SerializedObject(freeze);
var editorUtil = PrefabSafeSet.EditorUtil<string>.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();
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public void Process(OptimizerSession session)

// TODO: implement
GatherAnimationModifications();
AutoFreezeBlendShape();
}

private IReadOnlyCollection<string> GetModifiedProperties(Component component)
Expand Down

0 comments on commit f4d75f3

Please sign in to comment.