From 73ff7bf6d19e6baa3a48a935bfda737aeacc5461 Mon Sep 17 00:00:00 2001 From: Sayamame-beans <61457993+Sayamame-beans@users.noreply.github.com> Date: Mon, 23 Sep 2024 09:07:39 +0900 Subject: [PATCH] chore: unify FixAPose functions into SetupOutfit.FixAPose --- Editor/Inspector/MergeArmatureEditor.cs | 56 +------------------------ Editor/SetupOutfit.cs | 25 ++++++----- 2 files changed, 16 insertions(+), 65 deletions(-) diff --git a/Editor/Inspector/MergeArmatureEditor.cs b/Editor/Inspector/MergeArmatureEditor.cs index 7a956b39..0fd215b4 100644 --- a/Editor/Inspector/MergeArmatureEditor.cs +++ b/Editor/Inspector/MergeArmatureEditor.cs @@ -194,7 +194,7 @@ namespace nadena.dev.modular_avatar.core.editor if (posReset_convertATPose) { - FixAPose(mama.transform); + SetupOutfit.FixAPose(RuntimeUtil.FindAvatarTransformInParents(mergeTarget.transform).gameObject, mama.transform, false); } if (posReset_heuristicRootScale && !suppressRootScale) @@ -211,60 +211,6 @@ namespace nadena.dev.modular_avatar.core.editor mama.ResetArmatureLock(); } - void FixAPose(Transform outfitArmature) - { - if (mergeTarget == null) return; - if (rootAnimator == null) return; - - FixSingleArm(HumanBodyBones.LeftShoulder); - FixSingleArm(HumanBodyBones.RightShoulder); - FixSingleArm(HumanBodyBones.LeftUpperArm); - FixSingleArm(HumanBodyBones.RightUpperArm); - - void FixSingleArm(HumanBodyBones arm) - { - var lowerArm = (HumanBodyBones)((int)arm + 2); - - // check if the rotation of the arm differs - var avatarArm = rootAnimator.GetBoneTransform(arm); - var outfitArm = avatarToOutfit(avatarArm); - - var avatarLowerArm = rootAnimator.GetBoneTransform(lowerArm); - var outfitLowerArm = avatarToOutfit(avatarLowerArm); - - if (outfitArm == null) return; - if (outfitLowerArm == null) return; - - if (Vector3.Dot((outfitLowerArm.position - outfitArm.position).normalized, (avatarLowerArm.position - avatarArm.position).normalized) > 0.999f) return; - - // Rotate the outfit arm to ensure these two bone orientations match. - Undo.RecordObject(outfitArm, "Merge Armature: Force outfit position"); - var relRot = Quaternion.FromToRotation( - outfitLowerArm.position - outfitArm.position, - avatarLowerArm.position - avatarArm.position - ); - outfitArm.rotation = relRot * outfitArm.rotation; - PrefabUtility.RecordPrefabInstancePropertyModifications(outfitArm); - } - - Transform avatarToOutfit(Transform avBone) - { - if (avBone == null) return null; - if (!avBone.IsChildOf(mergeTarget.transform)) return null; - var parts = RuntimeUtil.RelativePath(mergeTarget, avBone.gameObject) - .Split('/'); - var outfitPath = string.Join("/", parts.Select(p => mama.prefix + p + mama.suffix)); - var candidate = outfitArmature.transform.Find(outfitPath); - - if (candidate == null) return null; - - var merger = candidate.GetComponentInParent(); - if (merger != mama) return null; - - return candidate; - } - } - void AdjustRootScale() { // Adjust the overall scale of the avatar based on wingspan (arm length) diff --git a/Editor/SetupOutfit.cs b/Editor/SetupOutfit.cs index f6d28c53..2bc2c394 100644 --- a/Editor/SetupOutfit.cs +++ b/Editor/SetupOutfit.cs @@ -229,7 +229,7 @@ namespace nadena.dev.modular_avatar.core.editor } } - private static void FixAPose(GameObject avatarRoot, Transform outfitArmature) + internal static void FixAPose(GameObject avatarRoot, Transform outfitArmature, bool strictMode = true) { var mergeArmature = outfitArmature.GetComponent(); if (mergeArmature == null) return; @@ -249,7 +249,7 @@ namespace nadena.dev.modular_avatar.core.editor { var lowerArm = (HumanBodyBones)((int)arm + 2); - // check if the rotation of the arm differs, but distances and origin point are the same + // check if the rotation of the arm differs(, but distances and origin point are the same when strictMode) var avatarArm = rootAnimator.GetBoneTransform(arm); var outfitArm = avatarToOutfit(avatarArm); @@ -259,22 +259,27 @@ namespace nadena.dev.modular_avatar.core.editor if (outfitArm == null) return; if (outfitLowerArm == null) return; - if ((avatarArm.position - outfitArm.position).magnitude > 0.001f) return; + if (strictMode) + { + if ((avatarArm.position - outfitArm.position).magnitude > 0.001f) return; - // check relative distance to lower arm as well - var avatarArmLength = (avatarLowerArm.position - avatarArm.position).magnitude; - var outfitArmLength = (outfitLowerArm.position - outfitArm.position).magnitude; + // check relative distance to lower arm as well + var avatarArmLength = (avatarLowerArm.position - avatarArm.position).magnitude; + var outfitArmLength = (outfitLowerArm.position - outfitArm.position).magnitude; - if (Mathf.Abs(avatarArmLength - outfitArmLength) > 0.001f) return; + if (Mathf.Abs(avatarArmLength - outfitArmLength) > 0.001f) return; + } else { + if (Vector3.Dot((outfitLowerArm.position - outfitArm.position).normalized, (avatarLowerArm.position - avatarArm.position).normalized) > 0.999f) return; + } - // Rotate the outfit arm to ensure these two points match. + // Rotate the outfit arm to ensure these two bone orientations match. + Undo.RecordObject(outfitArm, "Convert A/T Pose"); var relRot = Quaternion.FromToRotation( outfitLowerArm.position - outfitArm.position, avatarLowerArm.position - avatarArm.position ); outfitArm.rotation = relRot * outfitArm.rotation; PrefabUtility.RecordPrefabInstancePropertyModifications(outfitArm); - EditorUtility.SetDirty(outfitArm); } Transform avatarToOutfit(Transform avBone) @@ -542,4 +547,4 @@ namespace nadena.dev.modular_avatar.core.editor return avatarHips != null && outfitHips != null; } } -} \ No newline at end of file +}