fix: heuristic matching for "びしょ濡れのしずくちゃん" (#1370)

This commit is contained in:
bd_ 2024-11-25 19:09:12 -08:00 committed by GitHub
parent a7ef0d6635
commit 80d17f8284
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 168 additions and 22 deletions

View File

@ -232,6 +232,9 @@ namespace nadena.dev.modular_avatar.core.editor
internal static readonly Regex Regex_VRM_Bone = new Regex(@"^([LRC])_(.*)$"); internal static readonly Regex Regex_VRM_Bone = new Regex(@"^([LRC])_(.*)$");
internal static ImmutableHashSet<string> AllBoneNames =
boneNamePatterns.SelectMany(x => x).Select(NormalizeName).ToImmutableHashSet();
internal static string NormalizeName(string name) internal static string NormalizeName(string name)
{ {
name = name.ToLowerInvariant(); name = name.ToLowerInvariant();
@ -247,6 +250,8 @@ namespace nadena.dev.modular_avatar.core.editor
private static void InsertboneNamePatternsToRuntime() private static void InsertboneNamePatternsToRuntime()
{ {
ModularAvatarMergeArmature.boneNamePatterns = boneNamePatterns; ModularAvatarMergeArmature.boneNamePatterns = boneNamePatterns;
ModularAvatarMergeArmature.AllBoneNames = AllBoneNames;
ModularAvatarMergeArmature.NormalizeBoneName = NormalizeName;
} }
static HeuristicBoneMapper() static HeuristicBoneMapper()

View File

@ -26,6 +26,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using nadena.dev.modular_avatar.core.armature_lock; using nadena.dev.modular_avatar.core.armature_lock;
using UnityEngine; using UnityEngine;
using UnityEngine.Serialization; using UnityEngine.Serialization;
@ -49,6 +51,10 @@ namespace nadena.dev.modular_avatar.core
[HelpURL("https://modular-avatar.nadena.dev/docs/reference/merge-armature?lang=auto")] [HelpURL("https://modular-avatar.nadena.dev/docs/reference/merge-armature?lang=auto")]
public class ModularAvatarMergeArmature : AvatarTagComponent, IHaveObjReferences public class ModularAvatarMergeArmature : AvatarTagComponent, IHaveObjReferences
{ {
// Injected by HeuristicBoneMapper
internal static Func<string, string> NormalizeBoneName;
internal static ImmutableHashSet<string> AllBoneNames;
public AvatarObjectReference mergeTarget = new AvatarObjectReference(); public AvatarObjectReference mergeTarget = new AvatarObjectReference();
public GameObject mergeTargetObject => mergeTarget.Get(this); public GameObject mergeTargetObject => mergeTarget.Get(this);
@ -203,6 +209,66 @@ namespace nadena.dev.modular_avatar.core
} }
} }
class PSCandidate
{
public string prefix, suffix;
public int matches;
public PSCandidate CountMatches(ModularAvatarMergeArmature merger)
{
var target = merger.mergeTarget.Get(merger).transform;
var source = merger.transform;
var oldPrefix = merger.prefix;
var oldSuffix = merger.suffix;
try
{
merger.prefix = prefix;
merger.suffix = suffix;
matches = merger.GetBonesForLock().Count;
return this;
}
finally
{
merger.prefix = oldPrefix;
merger.suffix = oldSuffix;
}
}
/// <summary>
/// Counts the number of children which take the form prefix // heuristic bone name // suffix
/// </summary>
/// <returns></returns>
public PSCandidate CountHeuristicMatches(Transform root)
{
int count = 1;
Walk(root);
matches = count;
return this;
void Walk(Transform t)
{
foreach (Transform child in t)
{
if (child.name.StartsWith(prefix) && child.name.EndsWith(suffix))
{
var boneName = child.name.Substring(prefix.Length, child.name.Length - prefix.Length - suffix.Length);
boneName = NormalizeBoneName(boneName);
if (AllBoneNames.Contains(boneName))
{
count++;
Walk(child);
}
}
}
}
}
}
public void InferPrefixSuffix() public void InferPrefixSuffix()
{ {
// We only infer if targeting the armature (below the Hips bone) // We only infer if targeting the armature (below the Hips bone)
@ -215,33 +281,64 @@ namespace nadena.dev.modular_avatar.core
// We also require that the attached object has exactly one child (presumably the hips) // We also require that the attached object has exactly one child (presumably the hips)
if (transform.childCount != 1) return; if (transform.childCount != 1) return;
List<PSCandidate> candidates = new();
// always consider the current configuration
candidates.Add(new PSCandidate() {prefix = prefix, suffix = suffix}.CountMatches(this));
// Infer the prefix and suffix by comparing the names of the mergeTargetObject's hips with the child of the // Infer the prefix and suffix by comparing the names of the mergeTargetObject's hips with the child of the
// GameObject we're attached to. // GameObject we're attached to.
var baseName = hips.name; var baseName = hips.name;
var mergeName = transform.GetChild(0).name; var mergeHips = transform.GetChild(0);
var isInferred = false; var mergeName = mergeHips.name;
foreach (var hipNameCandidate in boneNamePatterns[(int)HumanBodyBones.Hips]) // Classic substring match
{
var prefixLength = mergeName.IndexOf(baseName, StringComparison.InvariantCulture);
if (prefixLength >= 0)
{
var suffixLength = mergeName.Length - prefixLength - baseName.Length;
candidates.Add(new PSCandidate()
{
prefix = mergeName.Substring(0, prefixLength),
suffix = mergeName.Substring(mergeName.Length - suffixLength)
}.CountMatches(this));
}
}
// Heuristic match - try to see if we get a better prefix/suffix pattern if we allow for fuzzy-matching of
// bone names. Since our goal is to minimize unnecessary renaming (and potentially failing matches), we do
// this only if the number of heuristic matches is more than twice the number of matches from the static
// pattern above, as using this will force most bones to be renamed.
foreach (var hipNameCandidate in
boneNamePatterns[(int)HumanBodyBones.Hips].OrderByDescending(p => p.Length))
{ {
var prefixLength = mergeName.IndexOf(hipNameCandidate, StringComparison.InvariantCultureIgnoreCase); var prefixLength = mergeName.IndexOf(hipNameCandidate, StringComparison.InvariantCultureIgnoreCase);
if (prefixLength < 0) continue; if (prefixLength < 0) continue;
var suffixLength = mergeName.Length - prefixLength - hipNameCandidate.Length; var suffixLength = mergeName.Length - prefixLength - hipNameCandidate.Length;
prefix = mergeName.Substring(0, prefixLength); var prefix = mergeName.Substring(0, prefixLength);
suffix = mergeName.Substring(mergeName.Length - suffixLength); var suffix = mergeName.Substring(mergeName.Length - suffixLength);
isInferred = true;
var candidate = new PSCandidate
{
prefix = prefix,
suffix = suffix
}.CountHeuristicMatches(mergeHips);
candidate.matches = (candidate.matches + 1) / 2;
candidates.Add(candidate);
break; break;
} }
if (!isInferred) { // Also check with old method as fallback // Select which candidate to use
var prefixLength = mergeName.IndexOf(baseName, StringComparison.InvariantCulture); var selected = candidates.OrderByDescending(c => c.matches).FirstOrDefault();
if (prefixLength < 0) return; if (selected != null && selected.matches > 0)
{
var suffixLength = mergeName.Length - prefixLength - baseName.Length; prefix = selected.prefix;
suffix = selected.suffix;
prefix = mergeName.Substring(0, prefixLength);
suffix = mergeName.Substring(mergeName.Length - suffixLength);
} }
if (prefix == "J_Bip_C_") if (prefix == "J_Bip_C_")

View File

@ -17,7 +17,7 @@ public class InferPrefixSuffixTest : TestBase
var outfit = CreateChild(root, "Outfit"); var outfit = CreateChild(root, "Outfit");
var outfit_armature = CreateChild(outfit, "armature"); var outfit_armature = CreateChild(outfit, "armature");
var outfit_hips = CreateChild(outfit_armature, "hips"); var outfit_hips = CreateChild(outfit_armature, "hip");
var outfit_mama = outfit_armature.AddComponent<ModularAvatarMergeArmature>(); var outfit_mama = outfit_armature.AddComponent<ModularAvatarMergeArmature>();
outfit_mama.mergeTarget = new AvatarObjectReference(); outfit_mama.mergeTarget = new AvatarObjectReference();
@ -42,7 +42,8 @@ public class InferPrefixSuffixTest : TestBase
var outfit = CreateChild(root, "Outfit"); var outfit = CreateChild(root, "Outfit");
var outfit_armature = CreateChild(outfit, "armature"); var outfit_armature = CreateChild(outfit, "armature");
var outfit_hips = CreateChild(outfit_armature, "pre_Hips.suf"); var outfit_hips = CreateChild(outfit_armature, "pre_hips.suf");
var outfit_mama = outfit_armature.AddComponent<ModularAvatarMergeArmature>(); var outfit_mama = outfit_armature.AddComponent<ModularAvatarMergeArmature>();
outfit_mama.mergeTarget = new AvatarObjectReference(); outfit_mama.mergeTarget = new AvatarObjectReference();
@ -51,12 +52,23 @@ public class InferPrefixSuffixTest : TestBase
outfit_mama.InferPrefixSuffix(); outfit_mama.InferPrefixSuffix();
// Initially, we determine "hip" to be the match
Assert.AreEqual("pre_", outfit_mama.prefix);
Assert.AreEqual("s.suf", outfit_mama.suffix);
// Now, add the legs.
var outfit_left_leg = CreateChild(outfit_hips, "pre_upleg.l.suf");
var outfit_right_leg = CreateChild(outfit_hips, "pre_upleg.r.suf");
// Now, we match 3 with ".suf" vs 1 with "s.suf", so the inference should change.
outfit_mama.InferPrefixSuffix();
Assert.AreEqual("pre_", outfit_mama.prefix); Assert.AreEqual("pre_", outfit_mama.prefix);
Assert.AreEqual(".suf", outfit_mama.suffix); Assert.AreEqual(".suf", outfit_mama.suffix);
} }
[Test] [Test]
public void TestSameHipsName_Success() public void TestSameHipsName_Multiple()
{ {
var root = CreateCommonPrefab("shapell.fbx"); var root = CreateCommonPrefab("shapell.fbx");
#if MA_VRCSDK3_AVATARS #if MA_VRCSDK3_AVATARS
@ -68,6 +80,8 @@ public class InferPrefixSuffixTest : TestBase
var outfit = CreateChild(root, "Outfit"); var outfit = CreateChild(root, "Outfit");
var outfit_armature = CreateChild(outfit, "armature"); var outfit_armature = CreateChild(outfit, "armature");
var outfit_hips = CreateChild(outfit_armature, "pre_TEST_HI2.suf"); // Make it a little bit different name to confirm it matches the current implementation var outfit_hips = CreateChild(outfit_armature, "pre_TEST_HI2.suf"); // Make it a little bit different name to confirm it matches the current implementation
var outfit_spine = CreateChild(outfit_hips, "pre_Spine2.suf");
var outfit_chest = CreateChild(outfit_spine, "pre_Bust2.suf");
var outfit_mama = outfit_armature.AddComponent<ModularAvatarMergeArmature>(); var outfit_mama = outfit_armature.AddComponent<ModularAvatarMergeArmature>();
outfit_mama.mergeTarget = new AvatarObjectReference(); outfit_mama.mergeTarget = new AvatarObjectReference();
@ -81,7 +95,7 @@ public class InferPrefixSuffixTest : TestBase
} }
[Test] [Test]
public void TestSameHipsName_Fail() public void TestSameHipsName_Single()
{ {
var root = CreateCommonPrefab("shapell.fbx"); var root = CreateCommonPrefab("shapell.fbx");
#if MA_VRCSDK3_AVATARS #if MA_VRCSDK3_AVATARS
@ -101,8 +115,38 @@ public class InferPrefixSuffixTest : TestBase
outfit_mama.InferPrefixSuffix(); outfit_mama.InferPrefixSuffix();
// Current(v1.10.x) InferPrefixSuffix fail to infer prefix/suffix when avatar has unique prefix/suffix and outfit has their name Assert.AreEqual("pre_", outfit_mama.prefix);
Assert.AreNotEqual("pre_", outfit_mama.prefix); Assert.AreEqual(".suf", outfit_mama.suffix);
Assert.AreNotEqual(".suf", outfit_mama.suffix); }
[Test]
public void TestSpuriousMatch()
{
var root = CreateCommonPrefab("shapell.fbx");
#if MA_VRCSDK3_AVATARS
root.AddComponent<VRC.SDK3.Avatars.Components.VRCAvatarDescriptor>();
#endif
var animator = root.GetComponent<Animator>();
var root_hips = animator.GetBoneTransform(HumanBodyBones.Hips);
var root_armature = root_hips.parent;
root_hips.gameObject.name = "bone_pelvis";
root_armature.gameObject.name = "bone_root";
animator.GetBoneTransform(HumanBodyBones.Spine).gameObject.name = "bone_Spine";
var outfit = CreateChild(root, "Outfit");
var outfit_armature = CreateChild(outfit, "bone_root");
var outfit_hips = CreateChild(outfit_armature, "bone_pelvis");
var outfit_spine = CreateChild(outfit_hips, "bone_Spine");
var outfit_mama = outfit_armature.AddComponent<ModularAvatarMergeArmature>();
outfit_mama.mergeTarget = new AvatarObjectReference();
outfit_mama.mergeTarget.referencePath = RuntimeUtil.RelativePath(root, root_armature.gameObject);
outfit_mama.LockMode = ArmatureLockMode.BaseToMerge;
outfit_mama.InferPrefixSuffix();
Assert.AreEqual("", outfit_mama.prefix);
Assert.AreEqual("", outfit_mama.suffix);
} }
} }