chore: unify FixAPose functions into SetupOutfit.FixAPose

This commit is contained in:
Sayamame-beans 2024-09-23 09:07:39 +09:00
parent eed6d52c9a
commit 73ff7bf6d1
2 changed files with 16 additions and 65 deletions

View File

@ -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<ModularAvatarMergeArmature>();
if (merger != mama) return null;
return candidate;
}
}
void AdjustRootScale()
{
// Adjust the overall scale of the avatar based on wingspan (arm length)

View File

@ -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<ModularAvatarMergeArmature>();
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;
}
}
}
}