From 6f391513179f327274a16f99d7165b0f4ea08d8e Mon Sep 17 00:00:00 2001 From: Sayamame-beans <61457993+Sayamame-beans@users.noreply.github.com> Date: Mon, 14 Oct 2024 01:16:12 +0900 Subject: [PATCH] chore: unify the process for get outfit's humanoid bones --- Editor/Inspector/MergeArmatureEditor.cs | 27 +----------- Editor/SetupOutfit.cs | 58 ++++++++++++++----------- 2 files changed, 33 insertions(+), 52 deletions(-) diff --git a/Editor/Inspector/MergeArmatureEditor.cs b/Editor/Inspector/MergeArmatureEditor.cs index 31fa9416..af51a31a 100644 --- a/Editor/Inspector/MergeArmatureEditor.cs +++ b/Editor/Inspector/MergeArmatureEditor.cs @@ -133,32 +133,7 @@ namespace nadena.dev.modular_avatar.core.editor outfitRoot = outfitRoot.parent; } - if (outfitAnimator != null) - { - var hipsCheck = outfitAnimator.GetBoneTransform(HumanBodyBones.Hips); - if (hipsCheck != null && hipsCheck.parent == outfitRoot.transform) - { - // Sometimes broken rigs can have the hips as a direct child of the root, instead of having - // an intermediate Armature object. We do not currently support this kind of rig, and so we'll - // assume the outfit's humanoid rig is broken and move on to heuristic matching. - outfitAnimator = null; - } else if (hipsCheck == null) { - outfitAnimator = null; - } - } - - Dictionary humanoidBones = null; - if (outfitAnimator != null) - { - humanoidBones = new Dictionary(); - foreach (HumanBodyBones boneIndex in Enum.GetValues(typeof(HumanBodyBones))) - { - var bone = boneIndex != HumanBodyBones.LastBone ? outfitAnimator.GetBoneTransform(boneIndex) : null; - if (bone == null) continue; - humanoidBones[bone] = boneIndex; - } - } - + var humanoidBones = SetupOutfit.GetOutfitHumanoidBones(outfitRoot, outfitAnimator); HeuristicBoneMapper.RenameBonesByHeuristic(target, humanoidBones: humanoidBones, avatarAnimator: avatarAnimator); } } diff --git a/Editor/SetupOutfit.cs b/Editor/SetupOutfit.cs index d2d59bc9..0fbf983c 100644 --- a/Editor/SetupOutfit.cs +++ b/Editor/SetupOutfit.cs @@ -158,32 +158,7 @@ namespace nadena.dev.modular_avatar.core.editor merge.InferPrefixSuffix(); var outfitAnimator = outfitRoot.GetComponent(); - if (outfitAnimator != null) - { - var hipsCheck = outfitAnimator.isHuman ? outfitAnimator.GetBoneTransform(HumanBodyBones.Hips) : null; - if (hipsCheck != null && hipsCheck.parent == outfitRoot.transform) - { - // Sometimes broken rigs can have the hips as a direct child of the root, instead of having - // an intermediate Armature object. We do not currently support this kind of rig, and so we'll - // assume the outfit's humanoid rig is broken and move on to heuristic matching. - outfitAnimator = null; - } else if (hipsCheck == null) { - outfitAnimator = null; - } - } - - Dictionary humanoidBones = null; - if (outfitAnimator != null) - { - humanoidBones = new Dictionary(); - foreach (HumanBodyBones boneIndex in Enum.GetValues(typeof(HumanBodyBones))) - { - var bone = boneIndex != HumanBodyBones.LastBone ? outfitAnimator.GetBoneTransform(boneIndex) : null; - if (bone == null) continue; - humanoidBones[bone] = boneIndex; - } - } - + var humanoidBones = GetOutfitHumanoidBones(outfitRoot.transform, outfitAnimator); var avatarAnimator = avatarRoot.GetComponent(); List subRoots = new List(); HeuristicBoneMapper.RenameBonesByHeuristic(merge, skipped: subRoots, humanoidBones: humanoidBones, avatarAnimator: avatarAnimator); @@ -257,6 +232,37 @@ namespace nadena.dev.modular_avatar.core.editor } } + internal static Dictionary GetOutfitHumanoidBones(Transform outfitRoot, Animator outfitAnimator) + { + if (outfitAnimator != null) + { + var hipsCheck = outfitAnimator.isHuman ? outfitAnimator.GetBoneTransform(HumanBodyBones.Hips) : null; + if (hipsCheck != null && hipsCheck.parent == outfitRoot) + { + // Sometimes broken rigs can have the hips as a direct child of the root, instead of having + // an intermediate Armature object. We do not currently support this kind of rig, and so we'll + // assume the outfit's humanoid rig is broken and move on to heuristic matching. + outfitAnimator = null; + } else if (hipsCheck == null) { + outfitAnimator = null; + } + } + + Dictionary humanoidBones = null; + if (outfitAnimator != null) + { + humanoidBones = new Dictionary(); + foreach (HumanBodyBones boneIndex in Enum.GetValues(typeof(HumanBodyBones))) + { + var bone = boneIndex != HumanBodyBones.LastBone ? outfitAnimator.GetBoneTransform(boneIndex) : null; + if (bone == null) continue; + humanoidBones[bone] = boneIndex; + } + } + + return humanoidBones; + } + private static void FixAPose(GameObject avatarRoot, Transform outfitArmature) { var mergeArmature = outfitArmature.GetComponent();