From 55827868f9faecd14a40f75ab0405bf94321e616 Mon Sep 17 00:00:00 2001 From: bd_ Date: Thu, 19 Jan 2023 21:42:11 +0900 Subject: [PATCH] fix: keep position/keep rotation modes not applied in editor --- .../EditModeTests/BoneProxyTest.cs | 46 +++++++++++++++++++ .../Runtime/ModularAvatarBoneProxy.cs | 19 ++++++-- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/Assets/_ModularAvatar/EditModeTests/BoneProxyTest.cs b/Assets/_ModularAvatar/EditModeTests/BoneProxyTest.cs index d09852c2..725dfe1a 100644 --- a/Assets/_ModularAvatar/EditModeTests/BoneProxyTest.cs +++ b/Assets/_ModularAvatar/EditModeTests/BoneProxyTest.cs @@ -44,6 +44,52 @@ namespace modular_avatar_tests private void AssertAttachmentMode(BoneProxyAttachmentMode attachmentMode, bool expectSnapPos, bool expectSnapRot) + { + AssertAttachmentModeAtBuild(attachmentMode, expectSnapPos, expectSnapRot); + AssertAttachmentModeInEditor(attachmentMode, expectSnapPos, expectSnapRot); + } + + private void AssertAttachmentModeInEditor(BoneProxyAttachmentMode attachmentMode, bool expectSnapPos, + bool expectSnapRot) + { + // Unset gets converted in the custom inspector; until it is, we don't snap (since we need to know the + // position to heuristically set the snapping mode). + if (attachmentMode == BoneProxyAttachmentMode.Unset) return; + + var root = CreateRoot("root"); + var bone = CreateChild(root, "bone"); + var proxy = CreateChild(root, "proxy"); + + var boneProxy = proxy.AddComponent(); + boneProxy.target = bone.transform; + boneProxy.attachmentMode = attachmentMode; + + bone.transform.localPosition = Vector3.one; + bone.transform.localRotation = Quaternion.Euler(123, 45, 6); + + boneProxy.Update(); + + if (expectSnapPos) + { + Assert.LessOrEqual(Vector3.Distance(proxy.transform.position, bone.transform.position), 0.0001f); + } + else + { + Assert.GreaterOrEqual(Vector3.Distance(proxy.transform.position, bone.transform.position), 0.0001f); + } + + if (expectSnapRot) + { + Assert.LessOrEqual(Quaternion.Angle(proxy.transform.rotation, bone.transform.rotation), 0.0001f); + } + else + { + Assert.GreaterOrEqual(Quaternion.Angle(proxy.transform.rotation, bone.transform.rotation), 0.0001f); + } + } + + private void AssertAttachmentModeAtBuild(BoneProxyAttachmentMode attachmentMode, bool expectSnapPos, + bool expectSnapRot) { var root = CreateRoot("root"); var bone = CreateChild(root, "bone"); diff --git a/Packages/nadena.dev.modular-avatar/Runtime/ModularAvatarBoneProxy.cs b/Packages/nadena.dev.modular-avatar/Runtime/ModularAvatarBoneProxy.cs index f9c4d339..f0bff1ff 100644 --- a/Packages/nadena.dev.modular-avatar/Runtime/ModularAvatarBoneProxy.cs +++ b/Packages/nadena.dev.modular-avatar/Runtime/ModularAvatarBoneProxy.cs @@ -106,14 +106,25 @@ namespace nadena.dev.modular_avatar.core RuntimeUtil.OnHierarchyChanged -= ClearCache; } - private void Update() + internal void Update() { - if (!RuntimeUtil.isPlaying && target != null && attachmentMode == BoneProxyAttachmentMode.AsChildAtRoot) + if (!RuntimeUtil.isPlaying && target != null) { var targetTransform = target.transform; var myTransform = transform; - myTransform.position = targetTransform.position; - myTransform.rotation = targetTransform.rotation; + switch (attachmentMode) + { + case BoneProxyAttachmentMode.AsChildAtRoot: + myTransform.position = targetTransform.position; + myTransform.rotation = targetTransform.rotation; + break; + case BoneProxyAttachmentMode.AsChildKeepPosition: + myTransform.rotation = targetTransform.rotation; + break; + case BoneProxyAttachmentMode.AsChildKeepRotation: + myTransform.position = targetTransform.position; + break; + } } }