From 9e33d5e8298980ae7c628cef9228313e5965789b Mon Sep 17 00:00:00 2001 From: CroakFang <1605296300@qq.com> Date: Sun, 14 Jan 2024 04:32:51 +0800 Subject: [PATCH] Add IK Control For Pose Mode --- .../RootMotion/FinalIK/_Integration.meta | 8 +++ .../RootMotion/PuppetMaster/_Integration.meta | 8 +++ Assets/Scripts/Pose/Handles/HandleManager.cs | 46 ++++++++++++-- Assets/Scripts/Pose/PoseManager.cs | 13 +++- .../Pose/RuntimeGizmo/TransformGizmo.cs | 2 +- Assets/Scripts/Pose/SerializableBone.cs | 3 +- Assets/Scripts/UmaContainerCharacter.cs | 60 ++++++++++++++++++- ProjectSettings/ProjectVersion.txt | 4 +- 8 files changed, 131 insertions(+), 13 deletions(-) create mode 100644 Assets/Plugins/RootMotion/FinalIK/_Integration.meta create mode 100644 Assets/Plugins/RootMotion/PuppetMaster/_Integration.meta diff --git a/Assets/Plugins/RootMotion/FinalIK/_Integration.meta b/Assets/Plugins/RootMotion/FinalIK/_Integration.meta new file mode 100644 index 0000000..c9983ed --- /dev/null +++ b/Assets/Plugins/RootMotion/FinalIK/_Integration.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: fd35cb16d5e64408eb3f5b252bc0c154 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/RootMotion/PuppetMaster/_Integration.meta b/Assets/Plugins/RootMotion/PuppetMaster/_Integration.meta new file mode 100644 index 0000000..cdd554e --- /dev/null +++ b/Assets/Plugins/RootMotion/PuppetMaster/_Integration.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: c0d8b3c418dba4ac2a3443e7fcfe727c +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Pose/Handles/HandleManager.cs b/Assets/Scripts/Pose/Handles/HandleManager.cs index 251d467..c5c5e5a 100644 --- a/Assets/Scripts/Pose/Handles/HandleManager.cs +++ b/Assets/Scripts/Pose/Handles/HandleManager.cs @@ -11,8 +11,9 @@ public class HandleManager : MonoBehaviour public Material LineRendererMaterial; - public List EnabledHandles = new List() { BoneTags.Humanoid }; + public List EnabledHandles { get => IKMode ? IKHandles : enabledHandles; } public bool EnabledLines = true; + public bool IKMode = false; public static bool InteractionInProgress; @@ -21,6 +22,9 @@ public class HandleManager : MonoBehaviour public static System.Action RegisterRuntimeGizmoUndoAction; public static System.Action> RegisterRuntimeGizmoUndoActions; + private List enabledHandles = new List() { BoneTags.Humanoid }; + private List IKHandles = new List() { BoneTags.IK }; + public struct RuntimeGizmoUndoData { public Transform NewPosition; @@ -32,10 +36,40 @@ public class HandleManager : MonoBehaviour var camera = Camera.main; var poseModeOn = UmaViewerUI.Instance.PoseManager.PoseModeOn; - foreach(var handle in AllHandles) + if (Input.GetKeyDown(KeyCode.Space)) + { + IKMode = !IKMode; + if (IKMode) + { + var ik = UmaViewerUI.Instance.PoseManager.PoseIK; + if (ik) + { + foreach (var effector in ik.solver.effectors) + { + if (effector.target) + { + effector.target.localPosition = Vector3.zero; + effector.target.localRotation = Quaternion.identity; + } + } + ik.solver.StoreDefaultLocalState(); + ik.enabled = true; + } + } + else + { + var ik = UmaViewerUI.Instance.PoseManager.PoseIK; + if (ik) + { + ik.enabled = false; + } + } + }; + + foreach (var handle in AllHandles) { handle.ForceDisplayOff(!poseModeOn); - handle.UpdateManual(camera, EnabledLines); + handle.UpdateManual(camera, EnabledLines && !IKMode); if (handle.Popup.gameObject.activeInHierarchy) { @@ -85,13 +119,13 @@ public class HandleManager : MonoBehaviour public void ToggleBonesVisible(string tag) { var enumTag = (BoneTags)System.Enum.Parse(typeof(BoneTags), tag); - if (EnabledHandles.Contains(enumTag)) + if (enabledHandles.Contains(enumTag)) { - EnabledHandles.Remove(enumTag); + enabledHandles.Remove(enumTag); } else { - EnabledHandles.Add(enumTag); + enabledHandles.Add(enumTag); } } diff --git a/Assets/Scripts/Pose/PoseManager.cs b/Assets/Scripts/Pose/PoseManager.cs index 057532c..7dd86ed 100644 --- a/Assets/Scripts/Pose/PoseManager.cs +++ b/Assets/Scripts/Pose/PoseManager.cs @@ -1,4 +1,7 @@ using Newtonsoft.Json; +using RootMotion; +using RootMotion.Dynamics; +using RootMotion.FinalIK; using System; using System.Collections.Generic; using System.IO; @@ -35,6 +38,8 @@ public class PoseManager : MonoBehaviour public GameObject HandleCanvas; public GameObject HelpPanel; + public FullBodyBipedIK PoseIK; + private void Update() { if (Input.GetKeyDown(KeyCode.H)) @@ -115,8 +120,9 @@ public class PoseManager : MonoBehaviour ui.SetDynamicBoneEnable(false); HandleCanvas.SetActive(true); - if (builder.CurrentUMAContainer != null && builder.CurrentUMAContainer.UmaAnimator != null) + if (builder.CurrentUMAContainer && builder.CurrentUMAContainer.UmaAnimator) { + PoseIK = builder.CurrentUMAContainer.CreatePoseIK(); builder.CurrentUMAContainer.UmaAnimator.enabled = false; } @@ -140,6 +146,11 @@ public class PoseManager : MonoBehaviour builder.CurrentUMAContainer.UmaAnimator.enabled = true; } + if (PoseIK) + { + Destroy(PoseIK); + } + PoseModeOn = false; } } \ No newline at end of file diff --git a/Assets/Scripts/Pose/RuntimeGizmo/TransformGizmo.cs b/Assets/Scripts/Pose/RuntimeGizmo/TransformGizmo.cs index b27a3f6..d931784 100644 --- a/Assets/Scripts/Pose/RuntimeGizmo/TransformGizmo.cs +++ b/Assets/Scripts/Pose/RuntimeGizmo/TransformGizmo.cs @@ -157,7 +157,7 @@ namespace RuntimeGizmos UndoRedoManager.Insert(commandGroup); }; - PoseManager.SetTooltip(0, $"H - show/hide this\r\n\r\nUndo:\t\t{ActionKey} + {UndoAction}\r\nRedo:\t\t{ActionKey} + {RedoAction}\r\n\r\nMove only: \t{changeModeHotkey} + {SetMoveType}\r\nRotate only: {changeModeHotkey} + {SetRotateType}\r\nScale only: \t{changeModeHotkey} + {SetScaleType}\r\nAll:\t \t{changeModeHotkey} + {SetAllTransformType}"); + PoseManager.SetTooltip(0, $"H - show/hide this\r\n\r\nUndo:\t\t{ActionKey} + {UndoAction}\r\nRedo:\t\t{ActionKey} + {RedoAction}\r\n\r\nIK Mode: \t{KeyCode.Space}\r\nMove only: \t{changeModeHotkey} + {SetMoveType}\r\nRotate only: {changeModeHotkey} + {SetRotateType}\r\nScale only: \t{changeModeHotkey} + {SetScaleType}\r\nAll:\t \t{changeModeHotkey} + {SetAllTransformType}"); PoseManager.SetTooltip(1, $"\r\n\r\nSpace Toggle:\t{changeModeHotkey} + {SetSpaceToggle}\r\nPivot Toggle:\t{changeModeHotkey} + {SetPivotModeToggle}\r\nScale type toggle: \t{changeModeHotkey} + {SetScaleTypeToggle}\r\nCenter type toggle:{changeModeHotkey} + {SetCenterTypeToggle}"); } diff --git a/Assets/Scripts/Pose/SerializableBone.cs b/Assets/Scripts/Pose/SerializableBone.cs index dadcd58..0088f46 100644 --- a/Assets/Scripts/Pose/SerializableBone.cs +++ b/Assets/Scripts/Pose/SerializableBone.cs @@ -52,6 +52,7 @@ public class SerializableBone Dynamic, Humanoid, Finger, - Face + Face, + IK, } } diff --git a/Assets/Scripts/UmaContainerCharacter.cs b/Assets/Scripts/UmaContainerCharacter.cs index 4aa0f10..7359393 100644 --- a/Assets/Scripts/UmaContainerCharacter.cs +++ b/Assets/Scripts/UmaContainerCharacter.cs @@ -1,4 +1,5 @@ using Gallop; +using RootMotion; using RootMotion.Dynamics; using RootMotion.FinalIK; using System; @@ -85,6 +86,7 @@ public class UmaContainerCharacter : UmaContainer private BipedIK IK; private PuppetMaster PuppetMaster; private List _humanoidBones; + private UIHandleCharacterRoot handleRoot; public void Initialize(bool smile) { @@ -479,6 +481,46 @@ public class UmaContainerCharacter : UmaContainer PuppetMaster.FlattenHierarchy(); } + public FullBodyBipedIK CreatePoseIK() + { + var container = this; + BipedRagdollReferences r = BipedRagdollReferences.FromAvatar(UmaAnimator); + BipedReferences pr = new BipedReferences(); + var ik = container.gameObject.AddComponent(); + + pr.root = r.root; + pr.pelvis = r.hips; + pr.spine = new Transform[] { r.spine, r.chest }; + pr.leftThigh = r.leftUpperLeg; + pr.leftCalf = r.leftLowerLeg; + pr.leftFoot = r.leftFoot; + pr.rightThigh = r.rightUpperLeg; + pr.rightCalf = r.rightLowerLeg; + pr.rightFoot = r.rightFoot; + pr.leftUpperArm = r.leftUpperArm; + pr.leftForearm = r.leftLowerArm; + pr.leftHand = r.leftHand; + pr.rightUpperArm = r.rightUpperArm; + pr.rightForearm = r.rightLowerArm; + pr.rightHand = r.rightHand; + pr.head = r.head; + ik.SetReferences(pr, r.hips); + + foreach (var effector in ik.solver.effectors) + { + var handle = handleRoot.ChildHandles.Find(h => h.Owner.transform.parent == effector.bone && (h as UIHandleBone).Tags.Contains(BoneTags.IK)); + if (handle) + { + effector.target = handle.Owner.transform; + effector.positionWeight = 1; + effector.rotationWeight = 1; + } + } + + ik.enabled = false; + return ik; + } + public void LoadTextures(UmaDatabaseEntry entry) { foreach(Texture2D tex2D in entry.GetAll()) @@ -1380,13 +1422,25 @@ public class UmaContainerCharacter : UmaContainer UIHandleCharacterRoot rootHandle = UIHandleCharacterRoot.CreateAsChild(this); var humanBones = GetHumanBones(); - foreach(var bone in humanBones) + var ikNames = new[] { "Hip", "Wrist_L", "Wrist_R", "Ankle_L", "Ankle_R" }; + foreach (var bone in humanBones) { List tags = new List() { BoneTags.Humanoid }; if (bone.name.EndsWith("_L")) tags.Add(BoneTags.Left); if (bone.name.EndsWith("_R")) tags.Add(BoneTags.Right); - if(bone.name != "Hip") + if (ikNames.Any(x => x == bone.name)) + { + var go = new GameObject(bone.name + "_IK_Handle"); + go.transform.parent = bone.transform; + go.transform.localPosition = Vector3.zero; + go.transform.rotation = bone.transform.rotation; + go.transform.localScale = Vector3.one; + var handle = UIHandleBone.CreateAsChild(go, new List() { BoneTags.IK }); + rootHandle.ChildHandles.Add(handle); + } + + if (bone.name != "Hip") { var handle = UIHandleBone.CreateAsChild(bone, tags).WithLineRenderer(); rootHandle.ChildHandles.Add(handle); @@ -1428,6 +1482,8 @@ public class UmaContainerCharacter : UmaContainer var handle = UIHandleBone.CreateAsChild(bone.Bone, bone.Tags).SetColor(Color.gray).SetScale(0.15f).WithLineRenderer(); rootHandle.ChildHandles.Add(handle); } + + handleRoot = rootHandle; } public List SaveBones() diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index da76706..495468b 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1,2 +1,2 @@ -m_EditorVersion: 2020.3.48f1 -m_EditorVersionWithRevision: 2020.3.48f1 (b805b124c6b7) +m_EditorVersion: 2020.3.24f1 +m_EditorVersionWithRevision: 2020.3.24f1 (79c78de19888)