mirror of
https://github.com/NotAKidoS/NAK_CVR_Mods.git
synced 2025-09-03 14:59:23 +00:00
[DesktopVRIK] wip
This commit is contained in:
parent
3c27df5274
commit
a3cee41021
3 changed files with 106 additions and 53 deletions
|
@ -184,7 +184,7 @@ internal class DesktopVRIKSystem : MonoBehaviour
|
||||||
// Last Movement Parent Info
|
// Last Movement Parent Info
|
||||||
Vector3 _movementPosition;
|
Vector3 _movementPosition;
|
||||||
Quaternion _movementRotation;
|
Quaternion _movementRotation;
|
||||||
CVRMovementParent _currentParent;
|
CVRMovementParent _movementParent;
|
||||||
|
|
||||||
DesktopVRIKSystem()
|
DesktopVRIKSystem()
|
||||||
{
|
{
|
||||||
|
@ -316,38 +316,48 @@ internal class DesktopVRIKSystem : MonoBehaviour
|
||||||
avatarTransform.localRotation = Quaternion.identity;
|
avatarTransform.localRotation = Quaternion.identity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnSetupAvatarDesktop()
|
public void OnSetupAvatarDesktop(Animator animator)
|
||||||
{
|
{
|
||||||
if (!Setting_Enabled) return;
|
if (!Setting_Enabled) return;
|
||||||
|
|
||||||
CalibrateDesktopVRIK();
|
// only run for humanoid avatars
|
||||||
ResetDesktopVRIK();
|
if (animator != null && animator.avatar != null && animator.avatar.isHuman)
|
||||||
|
{
|
||||||
|
CalibrateDesktopVRIK();
|
||||||
|
ResetDesktopVRIK();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool OnSetupIKScaling(float scaleDifference)
|
public bool OnSetupIKScaling(float scaleDifference)
|
||||||
{
|
{
|
||||||
if (avatarVRIK == null) return false;
|
if (avatarVRIK == null) return false;
|
||||||
|
|
||||||
|
_scaleDifference = scaleDifference;
|
||||||
|
|
||||||
VRIKUtils.ApplyScaleToVRIK
|
VRIKUtils.ApplyScaleToVRIK
|
||||||
(
|
(
|
||||||
avatarVRIK,
|
avatarVRIK,
|
||||||
_vrikInitialFootDistance,
|
_vrikInitialFootDistance,
|
||||||
_vrikInitialStepThreshold,
|
_vrikInitialStepThreshold,
|
||||||
_vrikInitialStepHeight,
|
_vrikInitialStepHeight,
|
||||||
scaleDifference
|
_scaleDifference
|
||||||
);
|
);
|
||||||
|
|
||||||
_scaleDifference = scaleDifference;
|
//VRIKUtils.SetFootsteps
|
||||||
|
//(
|
||||||
avatarIKSolver.Reset();
|
// avatarVRIK,
|
||||||
|
// _vrikInitialFootPosLeft * _scaleDifference,
|
||||||
|
// _vrikInitialFootPosRight * _scaleDifference,
|
||||||
|
// _vrikInitialFootRotLeft,
|
||||||
|
// _vrikInitialFootRotRight
|
||||||
|
//);
|
||||||
|
|
||||||
ResetDesktopVRIK();
|
ResetDesktopVRIK();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnPlayerSetupUpdate(bool isEmotePlaying)
|
public void OnPlayerSetupUpdate(bool isEmotePlaying)
|
||||||
{
|
{
|
||||||
if (avatarVRIK == null) return;
|
|
||||||
|
|
||||||
if (isEmotePlaying == _ikEmotePlaying) return;
|
if (isEmotePlaying == _ikEmotePlaying) return;
|
||||||
_ikEmotePlaying = isEmotePlaying;
|
_ikEmotePlaying = isEmotePlaying;
|
||||||
|
|
||||||
|
@ -360,39 +370,49 @@ internal class DesktopVRIKSystem : MonoBehaviour
|
||||||
ResetDesktopVRIK();
|
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
|
// Check if PlayerSetup.ResetIk() was called for movement parent
|
||||||
CVRMovementParent currentParent = movementSystem._currentParent;
|
CVRMovementParent currentParent = movementSystem._currentParent;
|
||||||
if (currentParent == null || currentParent._referencePoint == null) return false;
|
if (currentParent != null && currentParent._referencePoint != null)
|
||||||
|
|
||||||
// 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)
|
|
||||||
{
|
{
|
||||||
// Add platform motion to IK solver
|
// Get current position
|
||||||
avatarIKSolver.AddPlatformMotion(deltaPosition, deltaRotation, platformPivot);
|
var currentPosition = currentParent._referencePoint.position;
|
||||||
ResetDesktopVRIK();
|
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
|
// if not for movementparent, reset ik solver
|
||||||
_currentParent = currentParent;
|
avatarIKSolver.Reset();
|
||||||
_movementPosition = currentPosition;
|
ResetDesktopVRIK();
|
||||||
_movementRotation = currentRotation;
|
//IKResetFootsteps();
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnPreSolverUpdate()
|
public void OnPreSolverUpdate()
|
||||||
|
|
|
@ -28,40 +28,70 @@ namespace NAK.DesktopVRIK.HarmonyPatches;
|
||||||
class PlayerSetupPatches
|
class PlayerSetupPatches
|
||||||
{
|
{
|
||||||
[HarmonyPostfix]
|
[HarmonyPostfix]
|
||||||
[HarmonyPatch(typeof(PlayerSetup), "Start")]
|
[HarmonyPatch(typeof(PlayerSetup), nameof(PlayerSetup.Start))]
|
||||||
static void Postfix_PlayerSetup_Start(ref PlayerSetup __instance)
|
static void Postfix_PlayerSetup_Start(ref PlayerSetup __instance)
|
||||||
{
|
{
|
||||||
__instance.gameObject.AddComponent<DesktopVRIKSystem>();
|
__instance.gameObject.AddComponent<DesktopVRIKSystem>();
|
||||||
}
|
}
|
||||||
|
|
||||||
[HarmonyPostfix]
|
[HarmonyPostfix]
|
||||||
[HarmonyPatch(typeof(PlayerSetup), "SetupAvatarDesktop")]
|
[HarmonyPatch(typeof(PlayerSetup), nameof(PlayerSetup.SetupAvatarDesktop))]
|
||||||
static void Postfix_PlayerSetup_SetupAvatarDesktop(ref Animator ____animator)
|
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]
|
[HarmonyPostfix]
|
||||||
[HarmonyPatch(typeof(PlayerSetup), "Update")]
|
[HarmonyPatch(typeof(PlayerSetup), nameof(PlayerSetup.Update))]
|
||||||
static void Postfix_PlayerSetup_Update(ref bool ____emotePlaying)
|
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]
|
[HarmonyPrefix]
|
||||||
[HarmonyPatch(typeof(PlayerSetup), "SetupIKScaling")]
|
[HarmonyPatch(typeof(PlayerSetup), nameof(PlayerSetup.SetupIKScaling))]
|
||||||
private static bool Prefix_PlayerSetup_SetupIKScaling(float height, ref Vector3 ___scaleDifference)
|
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]
|
[HarmonyPrefix]
|
||||||
[HarmonyPatch(typeof(PlayerSetup), "ResetIk")]
|
[HarmonyPatch(typeof(PlayerSetup), nameof(PlayerSetup.ResetIk))]
|
||||||
static bool Prefix_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;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -135,10 +135,12 @@ public static class VRIKUtils
|
||||||
var footsteps = locomotionSolver.footsteps;
|
var footsteps = locomotionSolver.footsteps;
|
||||||
var footstepLeft = footsteps[0];
|
var footstepLeft = footsteps[0];
|
||||||
var footstepRight = footsteps[1];
|
var footstepRight = footsteps[1];
|
||||||
|
|
||||||
|
var root = vrik.references.root;
|
||||||
var rootWorldRot = vrik.references.root.rotation;
|
var rootWorldRot = vrik.references.root.rotation;
|
||||||
footstepLeft.Reset(rootWorldRot, vrik.transform.TransformPoint(footPosLeft), rootWorldRot * footRotLeft);
|
// hack, use parent transform instead as setting feet position moves root
|
||||||
footstepRight.Reset(rootWorldRot, vrik.transform.TransformPoint(footPosRight), rootWorldRot * footRotRight);
|
footstepLeft.Reset(rootWorldRot, root.parent.TransformPoint(footPosLeft), rootWorldRot * footRotLeft);
|
||||||
|
footstepRight.Reset(rootWorldRot, root.parent.TransformPoint(footPosRight), rootWorldRot * footRotRight);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetupHeadIKTarget(VRIK vrik)
|
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)
|
public static void ApplyScaleToVRIK(VRIK vrik, float footDistance, float stepThreshold, float stepHeight, float modifier)
|
||||||
{
|
{
|
||||||
vrik.solver.locomotion.footDistance = footDistance * modifier;
|
var locomotionSolver = vrik.solver.locomotion;
|
||||||
vrik.solver.locomotion.stepThreshold = stepThreshold * modifier;
|
locomotionSolver.footDistance = footDistance * modifier;
|
||||||
ScaleStepHeight(vrik.solver.locomotion.stepHeight, stepHeight * modifier);
|
locomotionSolver.stepThreshold = stepThreshold * modifier;
|
||||||
|
ScaleStepHeight(locomotionSolver.stepHeight, stepHeight * modifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ScaleStepHeight(AnimationCurve stepHeightCurve, float mag)
|
private static void ScaleStepHeight(AnimationCurve stepHeightCurve, float mag)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue