From 0cebf27e6aeb39ead464745c649a350ae8bb7726 Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Sun, 29 Jan 2023 17:51:28 +0900 Subject: [PATCH] fix: runtime behavior of FreezeBlendShape --- Editor/FreezeBlendShapeEditor.cs | 6 ++---- .../SkinnedMeshes/FreezeBlendShapeProcessor.cs | 9 ++++++--- Runtime/FreezeBlendShape.cs | 11 ++++++++++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Editor/FreezeBlendShapeEditor.cs b/Editor/FreezeBlendShapeEditor.cs index 0c78b1997..ca173294b 100644 --- a/Editor/FreezeBlendShapeEditor.cs +++ b/Editor/FreezeBlendShapeEditor.cs @@ -37,10 +37,8 @@ void SetShapeKeys(HashSet frozenKeys) } // Update ShapeKeys - if (component.freezeFlags == null || component.shapeKeys.Length != component.freezeFlags.Length) - SetShapeKeys(new HashSet(component.shapeKeys)); - else if (!component.shapeKeys.SequenceEqual(shapes)) - SetShapeKeys(new HashSet(component.shapeKeys.Where((_, i) => component.freezeFlags[i]))); + if (component.IsTraditionalForm || !component.shapeKeys.SequenceEqual(shapes)) + SetShapeKeys(component.FreezingShapeKeys); serializedObject.Update(); for (var i = 0; i < component.shapeKeys.Length; i++) diff --git a/Editor/Processors/SkinnedMeshes/FreezeBlendShapeProcessor.cs b/Editor/Processors/SkinnedMeshes/FreezeBlendShapeProcessor.cs index e2e7745c3..96f7e372d 100644 --- a/Editor/Processors/SkinnedMeshes/FreezeBlendShapeProcessor.cs +++ b/Editor/Processors/SkinnedMeshes/FreezeBlendShapeProcessor.cs @@ -15,7 +15,7 @@ public FreezeBlendShapeProcessor(FreezeBlendShape component) : base(component) public override void Process(OptimizerSession session, MeshInfo2 target, MeshInfo2Holder meshInfo2Holder) { - var freezeNames = new HashSet(Component.shapeKeys); + var freezeNames = Component.FreezingShapeKeys; var freezes = new BitArray(target.BlendShapes.Count); for (var i = 0; i < target.BlendShapes.Count; i++) freezes[i] = freezeNames.Contains(target.BlendShapes[i].name); @@ -56,8 +56,11 @@ class MeshInfoComputer : AbstractMeshInfoComputer public MeshInfoComputer(FreezeBlendShapeProcessor processor, IMeshInfoComputer upstream) : base(upstream) => _processor = processor; - public override string[] BlendShapes() => - base.BlendShapes().Where(x => !_processor.Component.shapeKeys.Contains(x)).ToArray(); + public override string[] BlendShapes() + { + var set = _processor.Component.FreezingShapeKeys; + return base.BlendShapes().Where(x => !set.Contains(x)).ToArray(); + } } } } diff --git a/Runtime/FreezeBlendShape.cs b/Runtime/FreezeBlendShape.cs index 0a273068f..6d6b2773b 100644 --- a/Runtime/FreezeBlendShape.cs +++ b/Runtime/FreezeBlendShape.cs @@ -1,3 +1,6 @@ +using System; +using System.Collections.Generic; +using System.Linq; using UnityEngine; namespace Anatawa12.AvatarOptimizer @@ -8,7 +11,13 @@ public class FreezeBlendShape : EditSkinnedMeshComponent { // Traditional Way: list of frozen ShapeKeys // New Way: list of all ShapeKeys and flags. - public string[] shapeKeys; + public string[] shapeKeys = Array.Empty(); public bool[] freezeFlags; + + public bool IsTraditionalForm => freezeFlags == null || shapeKeys.Length != freezeFlags.Length; + public HashSet FreezingShapeKeys => + IsTraditionalForm + ? new HashSet(shapeKeys) + : new HashSet(shapeKeys.Where((_, i) => freezeFlags[i])); } }