diff --git a/DesktopVRIK/DesktopVRIKSystem.cs b/DesktopVRIK/DesktopVRIKSystem.cs index 6fdbb4c..8cf1b76 100644 --- a/DesktopVRIK/DesktopVRIKSystem.cs +++ b/DesktopVRIK/DesktopVRIKSystem.cs @@ -184,7 +184,7 @@ internal class DesktopVRIKSystem : MonoBehaviour // Last Movement Parent Info Vector3 _movementPosition; Quaternion _movementRotation; - CVRMovementParent _currentParent; + CVRMovementParent _movementParent; DesktopVRIKSystem() { @@ -316,38 +316,48 @@ internal class DesktopVRIKSystem : MonoBehaviour avatarTransform.localRotation = Quaternion.identity; } - public void OnSetupAvatarDesktop() + public void OnSetupAvatarDesktop(Animator animator) { if (!Setting_Enabled) return; - CalibrateDesktopVRIK(); - ResetDesktopVRIK(); + // only run for humanoid avatars + if (animator != null && animator.avatar != null && animator.avatar.isHuman) + { + CalibrateDesktopVRIK(); + ResetDesktopVRIK(); + } } public bool OnSetupIKScaling(float scaleDifference) { if (avatarVRIK == null) return false; + _scaleDifference = scaleDifference; + VRIKUtils.ApplyScaleToVRIK ( avatarVRIK, _vrikInitialFootDistance, _vrikInitialStepThreshold, _vrikInitialStepHeight, - scaleDifference + _scaleDifference ); - _scaleDifference = scaleDifference; - - avatarIKSolver.Reset(); + //VRIKUtils.SetFootsteps + //( + // avatarVRIK, + // _vrikInitialFootPosLeft * _scaleDifference, + // _vrikInitialFootPosRight * _scaleDifference, + // _vrikInitialFootRotLeft, + // _vrikInitialFootRotRight + //); + ResetDesktopVRIK(); return true; } public void OnPlayerSetupUpdate(bool isEmotePlaying) { - if (avatarVRIK == null) return; - if (isEmotePlaying == _ikEmotePlaying) return; _ikEmotePlaying = isEmotePlaying; @@ -360,39 +370,49 @@ internal class DesktopVRIKSystem : MonoBehaviour ResetDesktopVRIK(); } - public bool OnPlayerSetupResetIk() + public void OnPlayerSetupSetSitting() { - if (avatarVRIK == null) return false; + avatarIKSolver.Reset(); + ResetDesktopVRIK(); + } + public void OnPlayerSetupResetIk() + { // Check if PlayerSetup.ResetIk() was called for movement parent CVRMovementParent currentParent = movementSystem._currentParent; - if (currentParent == null || currentParent._referencePoint == null) return false; - - // Get current position - var currentPosition = currentParent._referencePoint.position; - var currentRotation = Quaternion.Euler(0f, currentParent.transform.rotation.eulerAngles.y, 0f); - - // Convert to delta position (how much changed since last frame) - var deltaPosition = currentPosition - _movementPosition; - var deltaRotation = Quaternion.Inverse(_movementRotation) * currentRotation; - - // desktop pivots from playerlocal transform - var platformPivot = transform.position; - - // Prevent targeting other parent position - if (_currentParent == currentParent) + if (currentParent != null && currentParent._referencePoint != null) { - // Add platform motion to IK solver - avatarIKSolver.AddPlatformMotion(deltaPosition, deltaRotation, platformPivot); - ResetDesktopVRIK(); + // Get current position + var currentPosition = currentParent._referencePoint.position; + var currentRotation = Quaternion.Euler(0f, currentParent.transform.rotation.eulerAngles.y, 0f); + + // Convert to delta position (how much changed since last frame) + var deltaPosition = currentPosition - _movementPosition; + var deltaRotation = Quaternion.Inverse(_movementRotation) * currentRotation; + + // desktop pivots from playerlocal transform + var platformPivot = transform.position; + + // Prevent targeting other parent position + if (_movementParent == currentParent) + { + // Add platform motion to IK solver + avatarIKSolver.AddPlatformMotion(deltaPosition, deltaRotation, platformPivot); + ResetDesktopVRIK(); + } + + // Store for next frame + _movementParent = currentParent; + _movementPosition = currentPosition; + _movementRotation = currentRotation; + + return; } - - // Store for next frame - _currentParent = currentParent; - _movementPosition = currentPosition; - _movementRotation = currentRotation; - - return true; + + // if not for movementparent, reset ik solver + avatarIKSolver.Reset(); + ResetDesktopVRIK(); + //IKResetFootsteps(); } public void OnPreSolverUpdate() diff --git a/DesktopVRIK/HarmonyPatches.cs b/DesktopVRIK/HarmonyPatches.cs index e973986..c4417e4 100644 --- a/DesktopVRIK/HarmonyPatches.cs +++ b/DesktopVRIK/HarmonyPatches.cs @@ -28,40 +28,70 @@ namespace NAK.DesktopVRIK.HarmonyPatches; class PlayerSetupPatches { [HarmonyPostfix] - [HarmonyPatch(typeof(PlayerSetup), "Start")] + [HarmonyPatch(typeof(PlayerSetup), nameof(PlayerSetup.Start))] static void Postfix_PlayerSetup_Start(ref PlayerSetup __instance) { __instance.gameObject.AddComponent(); } [HarmonyPostfix] - [HarmonyPatch(typeof(PlayerSetup), "SetupAvatarDesktop")] + [HarmonyPatch(typeof(PlayerSetup), nameof(PlayerSetup.SetupAvatarDesktop))] static void Postfix_PlayerSetup_SetupAvatarDesktop(ref Animator ____animator) { - if (____animator != null && ____animator.avatar != null && ____animator.avatar.isHuman) + // only intercept if DesktopVRIK is being used + if (DesktopVRIKSystem.Instance != null) { - DesktopVRIKSystem.Instance?.OnSetupAvatarDesktop(); + DesktopVRIKSystem.Instance.OnSetupAvatarDesktop(____animator); } } [HarmonyPostfix] - [HarmonyPatch(typeof(PlayerSetup), "Update")] + [HarmonyPatch(typeof(PlayerSetup), nameof(PlayerSetup.Update))] static void Postfix_PlayerSetup_Update(ref bool ____emotePlaying) { - DesktopVRIKSystem.Instance?.OnPlayerSetupUpdate(____emotePlaying); + // only intercept if DesktopVRIK is being used + if (DesktopVRIKSystem.Instance?.avatarVRIK != null) + { + DesktopVRIKSystem.Instance.OnPlayerSetupUpdate(____emotePlaying); + } } [HarmonyPrefix] - [HarmonyPatch(typeof(PlayerSetup), "SetupIKScaling")] + [HarmonyPatch(typeof(PlayerSetup), nameof(PlayerSetup.SetupIKScaling))] private static bool Prefix_PlayerSetup_SetupIKScaling(float height, ref Vector3 ___scaleDifference) { - return !(bool)DesktopVRIKSystem.Instance?.OnSetupIKScaling(1f + ___scaleDifference.y); + // only intercept if DesktopVRIK is being used + if (DesktopVRIKSystem.Instance?.avatarVRIK != null) + { + DesktopVRIKSystem.Instance.OnSetupIKScaling(1f + ___scaleDifference.y); + return false; + } + + return true; + } + + [HarmonyPostfix] + [HarmonyPatch(typeof(PlayerSetup), nameof(PlayerSetup.SetSitting))] + static void Postfix_PlayerSetup_SetSitting() + { + // only intercept if DesktopVRIK is being used + if (DesktopVRIKSystem.Instance?.avatarVRIK != null) + { + DesktopVRIKSystem.Instance.OnPlayerSetupSetSitting(); + } } [HarmonyPrefix] - [HarmonyPatch(typeof(PlayerSetup), "ResetIk")] + [HarmonyPatch(typeof(PlayerSetup), nameof(PlayerSetup.ResetIk))] static bool Prefix_PlayerSetup_ResetIk() { - return !(bool)DesktopVRIKSystem.Instance?.OnPlayerSetupResetIk(); + // only intercept if DesktopVRIK is being used + if (DesktopVRIKSystem.Instance?.avatarVRIK != null) + { + DesktopVRIKSystem.Instance.OnPlayerSetupResetIk(); + return false; + } + + return true; } } \ No newline at end of file diff --git a/DesktopVRIK/VRIKUtils.cs b/DesktopVRIK/VRIKUtils.cs index 4850b4d..53ef94b 100644 --- a/DesktopVRIK/VRIKUtils.cs +++ b/DesktopVRIK/VRIKUtils.cs @@ -135,10 +135,12 @@ public static class VRIKUtils var footsteps = locomotionSolver.footsteps; var footstepLeft = footsteps[0]; var footstepRight = footsteps[1]; - + + var root = vrik.references.root; var rootWorldRot = vrik.references.root.rotation; - footstepLeft.Reset(rootWorldRot, vrik.transform.TransformPoint(footPosLeft), rootWorldRot * footRotLeft); - footstepRight.Reset(rootWorldRot, vrik.transform.TransformPoint(footPosRight), rootWorldRot * footRotRight); + // hack, use parent transform instead as setting feet position moves root + footstepLeft.Reset(rootWorldRot, root.parent.TransformPoint(footPosLeft), rootWorldRot * footRotLeft); + footstepRight.Reset(rootWorldRot, root.parent.TransformPoint(footPosRight), rootWorldRot * footRotRight); } public static void SetupHeadIKTarget(VRIK vrik) @@ -155,9 +157,10 @@ public static class VRIKUtils public static void ApplyScaleToVRIK(VRIK vrik, float footDistance, float stepThreshold, float stepHeight, float modifier) { - vrik.solver.locomotion.footDistance = footDistance * modifier; - vrik.solver.locomotion.stepThreshold = stepThreshold * modifier; - ScaleStepHeight(vrik.solver.locomotion.stepHeight, stepHeight * modifier); + var locomotionSolver = vrik.solver.locomotion; + locomotionSolver.footDistance = footDistance * modifier; + locomotionSolver.stepThreshold = stepThreshold * modifier; + ScaleStepHeight(locomotionSolver.stepHeight, stepHeight * modifier); } private static void ScaleStepHeight(AnimationCurve stepHeightCurve, float mag)