Skip to content

Commit

Permalink
Calibration storing
Browse files Browse the repository at this point in the history
Parameters restoration
  • Loading branch information
SDraw committed Sep 18, 2022
1 parent a1270d3 commit 0ebafcc
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Merged set of MelonLoader mods for ChilloutVR.

**State table for game build 2022r168p1:**
**State table for game build 2022r168p2:**
| Full name | Short name | Latest version | Available in [CVRMA](https://github.com/knah/CVRMelonAssistant) | Current Status | Notes |
|-----------|------------|----------------|-----------------------------------------------------------------|----------------|-------|
| Avatar Change Info | ml_aci | 1.0.2 | Yes | Working |
| Avatar Motion Tweaker | ml_amt | 1.1.0 | On review | Working |
| Desktop Reticle Switch | ml_drs | 1.0.0 | Yes | Working |
| Four Point Tracking | ml_fpt | 1.0.5 | On review | Working |
| Four Point Tracking | ml_fpt | 1.0.6 | On review | Working |
| Leap Motion Extension | ml_lme | 1.1.8 | Yes | Working |
| Server Connection Info | ml_sci | 1.0.1 | Yes | Working |
107 changes: 89 additions & 18 deletions ml_fpt/Main.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
using ABI_RC.Core.InteractionSystem;
using ABI.CCK.Scripts;
using ABI_RC.Core.InteractionSystem;
using ABI_RC.Core.Player;
using ABI_RC.Core.Savior;
using ABI_RC.Core.UI;
using System.Collections.Generic;
using UnityEngine;

namespace ml_fpt
{
public class FourPointTracking : MelonLoader.MelonMod
{
static FourPointTracking ms_instance = null;

bool m_ready = false;

IndexIK m_indexIk = null;
CVR_IK_Calibrator m_ikCalibrator = null;
RootMotion.FinalIK.VRIK m_vrIK = null;
RuntimeAnimatorController m_runtimeAnimator = null;
List<CVRAdvancedSettingsFileProfileValue> m_aasParameters = null;

bool m_calibrationActive = false;
object m_calibrationTask = null;

int m_hipsTrackerIndex = -1;
Transform m_hips = null;

Dictionary<string, System.Tuple<Vector3, Quaternion>> m_avatarCalibrations = null;

public override void OnApplicationStart()
{
ms_instance = this;

m_avatarCalibrations = new Dictionary<string, System.Tuple<Vector3, Quaternion>>();

HarmonyInstance.Patch(
typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.ClearAvatar)),
null,
new HarmonyLib.HarmonyMethod(typeof(FourPointTracking).GetMethod(nameof(OnAvatarClear_Postfix), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static))
);
HarmonyInstance.Patch(
typeof(PlayerSetup).GetMethod("SetupAvatarGeneral", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic),
null,
new HarmonyLib.HarmonyMethod(typeof(FourPointTracking).GetMethod(nameof(OnSetupAvatarGeneral_Postfix), System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic))
);

MelonLoader.MelonCoroutines.Start(WaitForMainMenuView());
MelonLoader.MelonCoroutines.Start(WaitForLocalPlayer());
Expand Down Expand Up @@ -72,19 +84,15 @@ void StartCalibration()
{
if(m_ready && !m_calibrationActive && PlayerSetup.Instance._inVr && !PlayerSetup.Instance.avatarIsLoading && PlayerSetup.Instance._animator.isHuman && !m_ikCalibrator.inFullbodyCalibration && !m_ikCalibrator.avatarCalibratedAsFullBody)
{
for(int i = 0; i < PlayerSetup.Instance._trackerManager.trackerNames.Length; i++)
{
if((PlayerSetup.Instance._trackerManager.trackerNames[i] == "vive_tracker_waist") && PlayerSetup.Instance._trackerManager.trackers[i].active)
{
m_hipsTrackerIndex = i;
break;
}
}

m_hipsTrackerIndex = GetHipsTracker();
if(m_hipsTrackerIndex != -1)
{
m_avatarCalibrations.Remove(MetaPort.Instance.currentAvatarGuid);

m_runtimeAnimator = PlayerSetup.Instance._animator.runtimeAnimatorController;
m_aasParameters = PlayerSetup.Instance.animatorManager.GetAdditionalSettingsCurrent();
PlayerSetup.Instance._animator.runtimeAnimatorController = PlayerSetup.Instance.tPoseAnimatorController;
PlayerSetup.Instance.animatorManager.SetAnimator(PlayerSetup.Instance._animator, PlayerSetup.Instance.tPoseAnimatorController);

m_hips = PlayerSetup.Instance._animator.GetBoneTransform(HumanBodyBones.Hips);
m_vrIK = PlayerSetup.Instance._animator.GetComponent<RootMotion.FinalIK.VRIK>();
Expand All @@ -109,14 +117,14 @@ void StartCalibration()
else
ShowMenuAlert("Calibraton requirements aren't met: be in VR, be not in FBT or avatar calibration, humanoid avatar");
}

System.Collections.IEnumerator CalibrationTask()
{
while(m_calibrationActive)
{
if(m_vrIK != null)
m_vrIK.enabled = false;

m_ikCalibrator.enabled = false;
m_indexIk.enabled = false;

Expand All @@ -127,6 +135,14 @@ System.Collections.IEnumerator CalibrationTask()
PlayerSetup.Instance._trackerManager.trackers[m_hipsTrackerIndex].target.transform.position = m_hips.position;
PlayerSetup.Instance._trackerManager.trackers[m_hipsTrackerIndex].target.transform.rotation = m_hips.rotation;

m_avatarCalibrations.Add(
MetaPort.Instance.currentAvatarGuid,
new System.Tuple<Vector3, Quaternion>(
PlayerSetup.Instance._trackerManager.trackers[m_hipsTrackerIndex].target.transform.localPosition,
PlayerSetup.Instance._trackerManager.trackers[m_hipsTrackerIndex].target.transform.localRotation
)
);

if(m_vrIK != null)
{
m_vrIK.solver.spine.pelvisTarget = PlayerSetup.Instance._trackerManager.trackers[m_hipsTrackerIndex].target;
Expand All @@ -139,7 +155,16 @@ System.Collections.IEnumerator CalibrationTask()

m_indexIk.enabled = true;
m_ikCalibrator.enabled = true;

PlayerSetup.Instance._animator.runtimeAnimatorController = m_runtimeAnimator;
PlayerSetup.Instance.animatorManager.SetAnimator(PlayerSetup.Instance._animator, m_runtimeAnimator);
if(m_aasParameters != null)
{
foreach(var l_param in m_aasParameters)
{
PlayerSetup.Instance.animatorManager.SetAnimatorParameter(l_param.name, l_param.value);
}
}

m_ikCalibrator.leftHandModel.SetActive(false);
m_ikCalibrator.rightHandModel.SetActive(false);
Expand All @@ -151,7 +176,7 @@ System.Collections.IEnumerator CalibrationTask()

ShowHudNotification("Calibration completed");
}

yield return null;
}

Expand All @@ -165,11 +190,12 @@ void OverrideIKWeight()
m_vrIK.solver.IKPositionWeight = 0f;
}
}

void Reset()
{
m_vrIK = null;
m_runtimeAnimator = null;
m_aasParameters = null;
m_calibrationActive = false;
m_calibrationTask = null;
m_hipsTrackerIndex = -1;
Expand All @@ -185,7 +211,7 @@ void OnAvatarClear()
{
if(m_calibrationTask != null)
MelonLoader.MelonCoroutines.Stop(m_calibrationTask);

m_indexIk.enabled = true;
m_ikCalibrator.enabled = true;

Expand All @@ -210,6 +236,37 @@ void OnAvatarClear()
}
}

static void OnSetupAvatarGeneral_Postfix() => ms_instance?.OnSetupAvatarGeneral();
void OnSetupAvatarGeneral()
{
try
{
if(m_ready && PlayerSetup.Instance._inVr && PlayerSetup.Instance._animator.isHuman && !m_ikCalibrator.avatarCalibratedAsFullBody)
{
int l_hipsTracker = GetHipsTracker();
if((l_hipsTracker != -1) && m_avatarCalibrations.TryGetValue(MetaPort.Instance.currentAvatarGuid, out var l_stored))
{
var l_vrIK = PlayerSetup.Instance._animator.GetComponent<RootMotion.FinalIK.VRIK>();
if(l_vrIK != null)
{
l_vrIK.solver.spine.pelvisTarget = PlayerSetup.Instance._trackerManager.trackers[l_hipsTracker].target;
l_vrIK.solver.spine.pelvisPositionWeight = 1f;
l_vrIK.solver.spine.pelvisRotationWeight = 1f;

l_vrIK.solver.spine.pelvisTarget.localPosition = l_stored.Item1;
l_vrIK.solver.spine.pelvisTarget.localRotation = l_stored.Item2;

ShowHudNotification("Applied saved calibration");
}
}
}
}
catch(System.Exception e)
{
MelonLoader.MelonLogger.Error(e);
}
}

static void ShowHudNotification(string p_message)
{
if(CohtmlHud.Instance != null)
Expand All @@ -221,5 +278,19 @@ static void ShowMenuAlert(string p_message)
if(ViewManager.Instance != null)
ViewManager.Instance.TriggerAlert("4-Point Tracking", p_message, 0, false);
}

static int GetHipsTracker()
{
int l_result = -1;
for(int i = 0; i < PlayerSetup.Instance._trackerManager.trackerNames.Length; i++)
{
if((PlayerSetup.Instance._trackerManager.trackerNames[i] == "vive_tracker_waist") && PlayerSetup.Instance._trackerManager.trackers[i].active)
{
l_result = i;
break;
}
}
return l_result;
}
}
}
}
6 changes: 3 additions & 3 deletions ml_fpt/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Reflection;

[assembly: AssemblyTitle("FourPointTracking")]
[assembly: AssemblyVersion("1.0.5")]
[assembly: AssemblyFileVersion("1.0.5")]
[assembly: AssemblyVersion("1.0.6")]
[assembly: AssemblyFileVersion("1.0.6")]

[assembly: MelonLoader.MelonInfo(typeof(ml_fpt.FourPointTracking), "FourPointTracking", "1.0.5", "SDraw", "https://github.com/SDraw/ml_mods_cvr")]
[assembly: MelonLoader.MelonInfo(typeof(ml_fpt.FourPointTracking), "FourPointTracking", "1.0.6", "SDraw", "https://github.com/SDraw/ml_mods_cvr")]
[assembly: MelonLoader.MelonGame(null, "ChilloutVR")]
[assembly: MelonLoader.MelonPlatform(MelonLoader.MelonPlatformAttribute.CompatiblePlatforms.WINDOWS_X64)]
[assembly: MelonLoader.MelonPlatformDomain(MelonLoader.MelonPlatformDomainAttribute.CompatibleDomains.MONO)]
6 changes: 2 additions & 4 deletions ml_fpt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@ This mod adds ability to use 4-point tracking.
* Press trigger on both controllers

# Notes
* You have to recalibrate each time you change avatar

# Planned
* No need for recalibration upon avatar change
* Calibration is saved per avatar for game session.
* AAS parameters are restored after calibration.

0 comments on commit 0ebafcc

Please sign in to comment.