Skip to content

Commit

Permalink
fix: runtime behavior of FreezeBlendShape
Browse files Browse the repository at this point in the history
  • Loading branch information
anatawa12 committed Jan 29, 2023
1 parent d9ed86d commit 0cebf27
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
6 changes: 2 additions & 4 deletions Editor/FreezeBlendShapeEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ void SetShapeKeys(HashSet<string> frozenKeys)
}

// Update ShapeKeys
if (component.freezeFlags == null || component.shapeKeys.Length != component.freezeFlags.Length)
SetShapeKeys(new HashSet<string>(component.shapeKeys));
else if (!component.shapeKeys.SequenceEqual(shapes))
SetShapeKeys(new HashSet<string>(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++)
Expand Down
9 changes: 6 additions & 3 deletions Editor/Processors/SkinnedMeshes/FreezeBlendShapeProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public FreezeBlendShapeProcessor(FreezeBlendShape component) : base(component)

public override void Process(OptimizerSession session, MeshInfo2 target, MeshInfo2Holder meshInfo2Holder)
{
var freezeNames = new HashSet<string>(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);
Expand Down Expand Up @@ -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();
}
}
}
}
11 changes: 10 additions & 1 deletion Runtime/FreezeBlendShape.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Anatawa12.AvatarOptimizer
Expand All @@ -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<string>();
public bool[] freezeFlags;

public bool IsTraditionalForm => freezeFlags == null || shapeKeys.Length != freezeFlags.Length;
public HashSet<string> FreezingShapeKeys =>
IsTraditionalForm
? new HashSet<string>(shapeKeys)
: new HashSet<string>(shapeKeys.Where((_, i) => freezeFlags[i]));
}
}

0 comments on commit 0cebf27

Please sign in to comment.