From fd48185bdd729313da176742148d1d7bf581c633 Mon Sep 17 00:00:00 2001 From: SDraw Date: Tue, 18 Oct 2022 21:46:10 +0300 Subject: [PATCH] Minor code changes and fixes Sometimes I dream about cheese --- README.md | 10 ++--- ml_amt/Main.cs | 51 +++------------------- ml_amt/MotionTweaker.cs | 27 +++++++++++- ml_amt/Properties/AssemblyInfo.cs | 6 +-- ml_dht/HeadTracked.cs | 13 ++++++ ml_dht/Main.cs | 37 ++++++---------- ml_dht/Properties/AssemblyInfo.cs | 6 +-- ml_fpt/Main.cs | 27 +++++++++--- ml_fpt/Properties/AssemblyInfo.cs | 6 +-- ml_lme/GestureMatcher.cs | 6 +-- ml_lme/LeapTracked.cs | 64 +++++++++++++++++----------- ml_lme/Main.cs | 71 ++++++++----------------------- ml_lme/Properties/AssemblyInfo.cs | 6 +-- ml_lme/Utils.cs | 30 +++++++++++++ 14 files changed, 183 insertions(+), 177 deletions(-) diff --git a/README.md b/README.md index aa61388..f567cb0 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ Merged set of MelonLoader mods for ChilloutVR. -**State table for game build 2022r169:** +**State table for game build 2022r169p1:** | Full name | Short name | Latest version | Available in [CVRMA](https://github.com/knah/CVRMelonAssistant) | Current Status | Notes | |-----------|------------|----------------|-----------------------------------------------------------------|----------------|-------| | Avatar Change Info | ml_aci | 1.0.3 | Yes | Working | -| Avatar Motion Tweaker | ml_amt | 1.1.3 | Yes | Working | -| Desktop Head Tracking | ml_dht | 1.0.6 | Yes | Working | +| Avatar Motion Tweaker | ml_amt | 1.1.4 | On review | Working | +| Desktop Head Tracking | ml_dht | 1.0.7 | On review | Working | | Desktop Reticle Switch | ml_drs | 1.0.0 | Yes | Working | -| Four Point Tracking | ml_fpt | 1.0.8 | Yes | Working | -| Leap Motion Extension | ml_lme | 1.2.3 | On review | Working | +| Four Point Tracking | ml_fpt | 1.0.9 | On review | Working | +| Leap Motion Extension | ml_lme | 1.2.4 | On review | Working | | Server Connection Info | ml_sci | 1.0.2 | Yes | Working | diff --git a/ml_amt/Main.cs b/ml_amt/Main.cs index 39d3589..69966dc 100644 --- a/ml_amt/Main.cs +++ b/ml_amt/Main.cs @@ -15,14 +15,6 @@ public override void OnInitializeMelon() ms_instance = this; Settings.Init(); - Settings.IKOverrideCrouchChange += this.OnIKOverrideCrouchChange; - Settings.CrouchLimitChange += this.OnCrouchLimitChange; - Settings.IKOverrideProneChange += this.OnIKOverrideProneChange; - Settings.ProneLimitChange += this.OnProneLimitChange; - Settings.PoseTransitionsChange += this.OnPoseTransitonsChange; - Settings.AdjustedMovementChange += this.OnAdjustedMovementChange; - Settings.IKOverrideFlyChange += this.OnIKOverrideFlyChange; - Settings.DetectEmotesChange += this.OnDetectEmotesChange; HarmonyInstance.Patch( typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.ClearAvatar)), @@ -54,45 +46,12 @@ System.Collections.IEnumerator WaitForLocalPlayer() m_localTweaker.SetDetectEmotes(Settings.DetectEmotes); } - void OnIKOverrideCrouchChange(bool p_state) + public override void OnDeinitializeMelon() { - if(m_localTweaker != null) - m_localTweaker.SetIKOverrideCrouch(p_state); - } - void OnCrouchLimitChange(float p_value) - { - if(m_localTweaker != null) - m_localTweaker.SetCrouchLimit(p_value); - } - void OnIKOverrideProneChange(bool p_state) - { - if(m_localTweaker != null) - m_localTweaker.SetIKOverrideProne(p_state); - } - void OnProneLimitChange(float p_value) - { - if(m_localTweaker != null) - m_localTweaker.SetProneLimit(p_value); - } - void OnPoseTransitonsChange(bool p_state) - { - if(m_localTweaker != null) - m_localTweaker.SetPoseTransitions(p_state); - } - void OnAdjustedMovementChange(bool p_state) - { - if(m_localTweaker != null) - m_localTweaker.SetAdjustedMovement(p_state); - } - void OnIKOverrideFlyChange(bool p_state) - { - if(m_localTweaker != null) - m_localTweaker.SetIKOverrideFly(p_state); - } - void OnDetectEmotesChange(bool p_state) - { - if(m_localTweaker != null) - m_localTweaker.SetDetectEmotes(p_state); + if(ms_instance == this) + ms_instance = null; + + m_localTweaker = null; } static void OnAvatarClear_Postfix() => ms_instance?.OnAvatarClear(); diff --git a/ml_amt/MotionTweaker.cs b/ml_amt/MotionTweaker.cs index 1c3e634..4299e8a 100644 --- a/ml_amt/MotionTweaker.cs +++ b/ml_amt/MotionTweaker.cs @@ -78,6 +78,30 @@ public MotionTweaker() { m_parameters = new List(); } + + void Start() + { + Settings.IKOverrideCrouchChange += this.SetIKOverrideCrouch; + Settings.CrouchLimitChange += this.SetCrouchLimit; + Settings.IKOverrideProneChange += this.SetIKOverrideProne; + Settings.ProneLimitChange += this.SetProneLimit; + Settings.PoseTransitionsChange += this.SetPoseTransitions; + Settings.AdjustedMovementChange += this.SetAdjustedMovement; + Settings.IKOverrideFlyChange += this.SetIKOverrideFly; + Settings.DetectEmotesChange += this.SetDetectEmotes; + } + + void OnDestroy() + { + Settings.IKOverrideCrouchChange -= this.SetIKOverrideCrouch; + Settings.CrouchLimitChange -= this.SetCrouchLimit; + Settings.IKOverrideProneChange -= this.SetIKOverrideProne; + Settings.ProneLimitChange -= this.SetProneLimit; + Settings.PoseTransitionsChange -= this.SetPoseTransitions; + Settings.AdjustedMovementChange -= this.SetAdjustedMovement; + Settings.IKOverrideFlyChange -= this.SetIKOverrideFly; + Settings.DetectEmotesChange -= this.SetDetectEmotes; + } void Update() { @@ -123,13 +147,12 @@ void Update() m_poseState = l_poseState; + m_emoteActive = false; if(m_detectEmotes && (m_locomotionLayer >= 0)) { AnimatorStateInfo l_animState = PlayerSetup.Instance._animator.GetCurrentAnimatorStateInfo(m_locomotionLayer); m_emoteActive = (l_animState.tagHash == ms_emoteHash); } - else - m_emoteActive = false; if(m_parameters.Count > 0) { diff --git a/ml_amt/Properties/AssemblyInfo.cs b/ml_amt/Properties/AssemblyInfo.cs index b076412..55fcc54 100644 --- a/ml_amt/Properties/AssemblyInfo.cs +++ b/ml_amt/Properties/AssemblyInfo.cs @@ -1,10 +1,10 @@ using System.Reflection; [assembly: AssemblyTitle("AvatarMotionTweaker")] -[assembly: AssemblyVersion("1.1.3")] -[assembly: AssemblyFileVersion("1.1.3")] +[assembly: AssemblyVersion("1.1.4")] +[assembly: AssemblyFileVersion("1.1.4")] -[assembly: MelonLoader.MelonInfo(typeof(ml_amt.AvatarMotionTweaker), "AvatarMotionTweaker", "1.1.3", "SDraw", "https://github.com/SDraw/ml_mods_cvr")] +[assembly: MelonLoader.MelonInfo(typeof(ml_amt.AvatarMotionTweaker), "AvatarMotionTweaker", "1.1.4", "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)] \ No newline at end of file diff --git a/ml_dht/HeadTracked.cs b/ml_dht/HeadTracked.cs index 52eb053..b1b3da1 100644 --- a/ml_dht/HeadTracked.cs +++ b/ml_dht/HeadTracked.cs @@ -34,6 +34,19 @@ class HeadTracked : MonoBehaviour void Start() { m_camera = PlayerSetup.Instance.desktopCamera.transform; + + Settings.EnabledChange += this.SetEnabled; + Settings.SmoothingChange += this.SetSmoothing; + Settings.MirroredChange += this.SetMirrored; + Settings.FaceOverrideChange += this.SetFaceOverride; + } + + void OnDestroy() + { + Settings.EnabledChange -= this.SetEnabled; + Settings.SmoothingChange -= this.SetSmoothing; + Settings.MirroredChange -= this.SetMirrored; + Settings.FaceOverrideChange -= this.SetFaceOverride; } public void UpdateTrackingData(ref TrackingData p_data) diff --git a/ml_dht/Main.cs b/ml_dht/Main.cs index 9c825b6..327195f 100644 --- a/ml_dht/Main.cs +++ b/ml_dht/Main.cs @@ -20,10 +20,6 @@ public override void OnInitializeMelon() ms_instance = this; Settings.Init(); - Settings.EnabledChange += this.OnEnabledChanged; - Settings.MirroredChange += this.OnMirroredChanged; - Settings.SmoothingChange += this.OnSmoothingChanged; - Settings.FaceOverrideChange += this.OnFaceOverrideChange; m_mapReader = new MemoryMapReader(); m_buffer = new byte[1024]; @@ -64,6 +60,18 @@ System.Collections.IEnumerator WaitForPlayer() m_localTracked.SetEnabled(Settings.Enabled); m_localTracked.SetMirrored(Settings.Mirrored); m_localTracked.SetSmoothing(Settings.Smoothing); + m_localTracked.SetFaceOverride(Settings.FaceOverride); + } + + public override void OnDeinitializeMelon() + { + if(ms_instance == this) + ms_instance = null; + + m_mapReader?.Close(); + m_mapReader = null; + m_buffer = null; + m_localTracked = null; } public override void OnUpdate() @@ -76,27 +84,6 @@ public override void OnUpdate() } } - void OnEnabledChanged(bool p_state) - { - if(m_localTracked != null) - m_localTracked.SetEnabled(p_state); - } - void OnMirroredChanged(bool p_state) - { - if(m_localTracked != null) - m_localTracked.SetMirrored(p_state); - } - void OnSmoothingChanged(float p_value) - { - if(m_localTracked != null) - m_localTracked.SetSmoothing(p_value); - } - void OnFaceOverrideChange(bool p_state) - { - if(m_localTracked != null) - m_localTracked.SetFaceOverride(p_state); - } - static void OnCalibrateAvatar_Postfix() => ms_instance?.OnCalibrateAvatar(); void OnCalibrateAvatar() { diff --git a/ml_dht/Properties/AssemblyInfo.cs b/ml_dht/Properties/AssemblyInfo.cs index 033e316..5659d52 100644 --- a/ml_dht/Properties/AssemblyInfo.cs +++ b/ml_dht/Properties/AssemblyInfo.cs @@ -1,10 +1,10 @@ using System.Reflection; [assembly: AssemblyTitle("DesktopHeadTracking")] -[assembly: AssemblyVersion("1.0.6")] -[assembly: AssemblyFileVersion("1.0.6")] +[assembly: AssemblyVersion("1.0.7")] +[assembly: AssemblyFileVersion("1.0.7")] -[assembly: MelonLoader.MelonInfo(typeof(ml_dht.DesktopHeadTracking), "DesktopHeadTracking", "1.0.6", "SDraw", "https://github.com/SDraw/ml_mods_cvr")] +[assembly: MelonLoader.MelonInfo(typeof(ml_dht.DesktopHeadTracking), "DesktopHeadTracking", "1.0.7", "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)] \ No newline at end of file diff --git a/ml_fpt/Main.cs b/ml_fpt/Main.cs index ecfd992..e26b8d2 100644 --- a/ml_fpt/Main.cs +++ b/ml_fpt/Main.cs @@ -18,7 +18,7 @@ public class FourPointTracking : MelonLoader.MelonMod bool m_ready = false; - IndexIK m_indexIk = null; + IndexIK m_indexIK = null; RootMotion.FinalIK.VRIK m_vrIK = null; RuntimeAnimatorController m_runtimeAnimator = null; List m_aasParameters = null; @@ -78,11 +78,28 @@ System.Collections.IEnumerator WaitForLocalPlayer() while(PlayerSetup.Instance == null) yield return null; - m_indexIk = PlayerSetup.Instance.gameObject.GetComponent(); + m_indexIK = PlayerSetup.Instance.gameObject.GetComponent(); m_ready = true; } + public override void OnDeinitializeMelon() + { + if(ms_instance == this) + ms_instance = null; + + m_ready = false; + m_aasParameters?.Clear(); + m_aasParameters = null; + m_avatarCalibrations?.Clear(); + m_avatarCalibrations = null; + m_hipsTrackerIndex = -1; + + if(m_calibrationTask != null) + MelonLoader.MelonCoroutines.Stop(m_calibrationTask); + m_calibrationTask = null; + } + void StartCalibration() { if(m_ready && !m_calibrationActive && PlayerSetup.Instance._inVr && !PlayerSetup.Instance.avatarIsLoading && PlayerSetup.Instance._animator.isHuman && !BodySystem.isCalibrating && !BodySystem.isCalibratedAsFullBody) @@ -128,7 +145,7 @@ System.Collections.IEnumerator CalibrationTask() if(m_vrIK != null) m_vrIK.enabled = false; - m_indexIk.enabled = false; + m_indexIK.enabled = false; PlayerSetup.Instance._trackerManager.trackers[m_hipsTrackerIndex].ShowLine(true, m_hips); @@ -155,7 +172,7 @@ System.Collections.IEnumerator CalibrationTask() m_vrIK.enabled = true; } - m_indexIk.enabled = true; + m_indexIK.enabled = true; PlayerSetup.Instance._animator.runtimeAnimatorController = m_runtimeAnimator; PlayerSetup.Instance.animatorManager.SetAnimator(PlayerSetup.Instance._animator, m_runtimeAnimator); @@ -213,7 +230,7 @@ void OnAvatarClear() if(m_calibrationTask != null) MelonLoader.MelonCoroutines.Stop(m_calibrationTask); - m_indexIk.enabled = true; + m_indexIK.enabled = true; IKSystem.Instance.leftHandModel.SetActive(false); IKSystem.Instance.rightHandModel.SetActive(false); diff --git a/ml_fpt/Properties/AssemblyInfo.cs b/ml_fpt/Properties/AssemblyInfo.cs index 327873f..388da0e 100644 --- a/ml_fpt/Properties/AssemblyInfo.cs +++ b/ml_fpt/Properties/AssemblyInfo.cs @@ -1,10 +1,10 @@ using System.Reflection; [assembly: AssemblyTitle("FourPointTracking")] -[assembly: AssemblyVersion("1.0.8")] -[assembly: AssemblyFileVersion("1.0.8")] +[assembly: AssemblyVersion("1.0.9")] +[assembly: AssemblyFileVersion("1.0.9")] -[assembly: MelonLoader.MelonInfo(typeof(ml_fpt.FourPointTracking), "FourPointTracking", "1.0.8", "SDraw", "https://github.com/SDraw/ml_mods_cvr")] +[assembly: MelonLoader.MelonInfo(typeof(ml_fpt.FourPointTracking), "FourPointTracking", "1.0.9", "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)] \ No newline at end of file diff --git a/ml_lme/GestureMatcher.cs b/ml_lme/GestureMatcher.cs index 178ac99..75bcdb7 100644 --- a/ml_lme/GestureMatcher.cs +++ b/ml_lme/GestureMatcher.cs @@ -22,7 +22,7 @@ public class GesturesData public bool[] m_handsPresenses = null; public Vector3[] m_handsPositons = null; public Quaternion[] m_handsRotations = null; - public Vector3[] m_elbowPositions = null; + public Vector3[] m_elbowsPositions = null; public float[] m_leftFingersBends = null; public float[] m_leftFingersSpreads = null; public float[] m_rightFingersBends = null; @@ -33,7 +33,7 @@ public GesturesData() m_handsPresenses = new bool[ms_handsCount]; m_handsPositons = new Vector3[ms_handsCount]; m_handsRotations = new Quaternion[ms_handsCount]; - m_elbowPositions = new Vector3[ms_handsCount]; + m_elbowsPositions = new Vector3[ms_handsCount]; m_leftFingersBends = new float[ms_fingersCount]; m_leftFingersSpreads = new float[ms_fingersCount]; m_rightFingersBends = new float[ms_fingersCount]; @@ -63,7 +63,7 @@ public static void GetGestures(Leap.Frame p_frame, ref GesturesData p_data) p_data.m_handsPresenses[l_sideID] = true; FillHandPosition(l_hand, ref p_data.m_handsPositons[l_sideID]); FillHandRotation(l_hand, ref p_data.m_handsRotations[l_sideID]); - FillElbowPosition(l_hand, ref p_data.m_elbowPositions[l_sideID]); + FillElbowPosition(l_hand, ref p_data.m_elbowsPositions[l_sideID]); switch(l_sideID) { case 0: diff --git a/ml_lme/LeapTracked.cs b/ml_lme/LeapTracked.cs index 7dae46d..7ef3a7a 100644 --- a/ml_lme/LeapTracked.cs +++ b/ml_lme/LeapTracked.cs @@ -53,40 +53,36 @@ void Start() m_rightHandTarget.localPosition = Vector3.zero; m_rightHandTarget.localRotation = Quaternion.identity; } + + Settings.EnabledChange += this.SetEnabled; + Settings.FingersOnlyChange += this.SetFingersOnly; + Settings.TrackElbowsChange += this.SetTrackElbows; + } + + void OnDestroy() + { + Settings.EnabledChange -= this.SetEnabled; + Settings.FingersOnlyChange -= this.SetFingersOnly; + Settings.TrackElbowsChange -= this.SetTrackElbows; } public void SetEnabled(bool p_state) { m_enabled = p_state; - if(m_indexIK != null) - { - m_indexIK.activeControl = (m_enabled || Utils.AreKnucklesInUse()); - CVRInputManager.Instance.individualFingerTracking = (m_enabled || Utils.AreKnucklesInUse()); - } - - if((m_leftIK != null) && (m_rightIK != null)) - { - m_leftIK.enabled = (m_enabled && !m_fingersOnly); - m_rightIK.enabled = (m_enabled && !m_fingersOnly); - } - + RefreshFingersTracking(); + RefreshArmIK(); if(!m_enabled || m_fingersOnly) - RestoreIK(); + RestoreVRIK(); } public void SetFingersOnly(bool p_state) { m_fingersOnly = p_state; - if((m_leftIK != null) && (m_rightIK != null)) - { - m_leftIK.enabled = (m_enabled && !m_fingersOnly); - m_rightIK.enabled = (m_enabled && !m_fingersOnly); - } - + RefreshArmIK(); if(!m_enabled || m_fingersOnly) - RestoreIK(); + RestoreVRIK(); } public void SetTrackElbows(bool p_state) @@ -126,7 +122,7 @@ public void UpdateTracking(GestureMatcher.GesturesData p_gesturesData) m_leftIK.solver.IKPositionWeight = Mathf.Lerp(m_leftIK.solver.IKPositionWeight, (p_gesturesData.m_handsPresenses[0] && !m_fingersOnly) ? 1f : 0f, 0.25f); m_leftIK.solver.IKRotationWeight = Mathf.Lerp(m_leftIK.solver.IKRotationWeight, (p_gesturesData.m_handsPresenses[0] && !m_fingersOnly) ? 1f : 0f, 0.25f); m_rightIK.solver.IKPositionWeight = Mathf.Lerp(m_rightIK.solver.IKPositionWeight, (p_gesturesData.m_handsPresenses[1] && !m_fingersOnly) ? 1f : 0f, 0.25f); - m_rightIK.solver.IKRotationWeight = Mathf.Lerp(m_rightIK.solver.IKPositionWeight, (p_gesturesData.m_handsPresenses[1] && !m_fingersOnly) ? 1f : 0f, 0.25f); + m_rightIK.solver.IKRotationWeight = Mathf.Lerp(m_rightIK.solver.IKRotationWeight, (p_gesturesData.m_handsPresenses[1] && !m_fingersOnly) ? 1f : 0f, 0.25f); } if(p_gesturesData.m_handsPresenses[0]) @@ -203,6 +199,7 @@ public void UpdateTracking(GestureMatcher.GesturesData p_gesturesData) public void OnAvatarClear() { m_vrIK = null; + m_armsWeights = Vector2.zero; m_leftIK = null; m_rightIK = null; m_leftTargetActive = false; @@ -212,8 +209,6 @@ public void OnAvatarClear() m_leftHandTarget.localRotation = Quaternion.identity; m_rightHandTarget.localPosition = Vector3.zero; m_rightHandTarget.localRotation = Quaternion.identity; - - m_armsWeights.Set(0f, 0f); } public void OnCalibrateAvatar() @@ -223,8 +218,7 @@ public void OnCalibrateAvatar() if(m_indexIK != null) { m_indexIK.avatarAnimator = PlayerSetup.Instance._animator; - m_indexIK.activeControl = (m_enabled || Utils.AreKnucklesInUse()); - CVRInputManager.Instance.individualFingerTracking = (m_enabled || Utils.AreKnucklesInUse()); + RefreshFingersTracking(); } if(PlayerSetup.Instance._animator.isHuman) @@ -330,7 +324,7 @@ void OnIKPostUpdate() m_vrIK.solver.rightArm.rotationWeight = m_armsWeights.y; } - void RestoreIK() + void RestoreVRIK() { if(m_vrIK != null) { @@ -350,5 +344,23 @@ void RestoreIK() } } } + + void RefreshArmIK() + { + if((m_leftIK != null) && (m_rightIK != null)) + { + m_leftIK.enabled = (m_enabled && !m_fingersOnly); + m_rightIK.enabled = (m_enabled && !m_fingersOnly); + } + } + + void RefreshFingersTracking() + { + if(m_indexIK != null) + { + m_indexIK.activeControl = (m_enabled || Utils.AreKnucklesInUse()); + CVRInputManager.Instance.individualFingerTracking = (m_enabled || Utils.AreKnucklesInUse()); + } + } } } diff --git a/ml_lme/Main.cs b/ml_lme/Main.cs index 70fdcc0..d25d22f 100644 --- a/ml_lme/Main.cs +++ b/ml_lme/Main.cs @@ -8,9 +8,6 @@ namespace ml_lme public class LeapMotionExtension : MelonLoader.MelonMod { - static readonly Quaternion ms_hmdRotationFix = new Quaternion(0f, 0.7071068f, 0.7071068f, 0f); - static readonly Quaternion ms_screentopRotationFix = new Quaternion(0f, 0f, -1f, 0f); - static LeapMotionExtension ms_instance = null; Leap.Controller m_leapController = null; @@ -32,13 +29,11 @@ public override void OnInitializeMelon() Settings.Init(); Settings.EnabledChange += this.OnEnableChange; Settings.DesktopOffsetChange += this.OnDesktopOffsetChange; - Settings.FingersOnlyChange += this.OnFingersOptionChange; Settings.ModelVisibilityChange += this.OnModelVisibilityChange; Settings.TrackingModeChange += this.OnTrackingModeChange; Settings.RootAngleChange += this.OnRootAngleChange; Settings.HeadAttachChange += this.OnHeadAttachChange; Settings.HeadOffsetChange += this.OnHeadOffsetChange; - Settings.TrackElbowsChange += this.OnTrackElbowsChange; m_leapController = new Leap.Controller(); m_leapController.Device += this.OnLeapDeviceInitialized; @@ -109,15 +104,28 @@ System.Collections.IEnumerator CreateTrackingObjects() // Player setup m_leapTracked = PlayerSetup.Instance.gameObject.AddComponent(); m_leapTracked.SetTransforms(m_leapHands[0].transform, m_leapHands[1].transform, m_leapElbows[0].transform, m_leapElbows[1].transform); + m_leapTracked.SetEnabled(Settings.Enabled); m_leapTracked.SetTrackElbows(Settings.TrackElbows); + m_leapTracked.SetFingersOnly(Settings.FingersOnly); OnEnableChange(Settings.Enabled); - OnFingersOptionChange(Settings.FingersOnly); OnModelVisibilityChange(Settings.ModelVisibility); OnTrackingModeChange(Settings.TrackingMode); OnHeadAttachChange(Settings.HeadAttach); // Includes offsets and parenting } + public override void OnDeinitializeMelon() + { + if(ms_instance == this) + ms_instance = null; + + m_leapController?.StopConnection(); + m_leapController?.Dispose(); + m_leapController = null; + + m_gesturesData = null; + } + public override void OnUpdate() { if(Settings.Enabled) @@ -138,12 +146,12 @@ public override void OnUpdate() { Vector3 l_pos = m_gesturesData.m_handsPositons[i]; Quaternion l_rot = m_gesturesData.m_handsRotations[i]; - ReorientateLeapToUnity(ref l_pos, ref l_rot, Settings.TrackingMode); + Utils.LeapToUnity(ref l_pos, ref l_rot, Settings.TrackingMode); m_leapHands[i].transform.localPosition = l_pos; m_leapHands[i].transform.localRotation = l_rot; - l_pos = m_gesturesData.m_elbowPositions[i]; - ReorientateLeapToUnity(ref l_pos, ref l_rot, Settings.TrackingMode); + l_pos = m_gesturesData.m_elbowsPositions[i]; + Utils.LeapToUnity(ref l_pos, ref l_rot, Settings.TrackingMode); m_leapElbows[i].transform.localPosition = l_pos; } } @@ -165,9 +173,6 @@ void OnEnableChange(bool p_state) } else m_leapController?.StopConnection(); - - if(m_leapTracked != null) - m_leapTracked.SetEnabled(p_state); } void OnDesktopOffsetChange(Vector3 p_offset) @@ -181,12 +186,6 @@ void OnDesktopOffsetChange(Vector3 p_offset) } } - void OnFingersOptionChange(bool p_state) - { - if(m_leapTracked != null) - m_leapTracked.SetFingersOnly(p_state); - } - void OnModelVisibilityChange(bool p_state) { if(m_leapControllerModel != null) @@ -270,12 +269,6 @@ void OnHeadOffsetChange(Vector3 p_offset) m_leapTrackingRoot.transform.localPosition = p_offset; } } - - void OnTrackElbowsChange(bool p_state) - { - if(m_leapTracked != null) - m_leapTracked.SetTrackElbows(p_state); - } // Internal utility void UpdateDeviceTrackingMode() @@ -347,7 +340,7 @@ void OnAvatarClear() MelonLoader.MelonLogger.Error(e); } } - + static void OnCalibrateAvatar_Postfix() => ms_instance?.OnCalibrateAvatar(); void OnCalibrateAvatar() { @@ -363,33 +356,5 @@ void OnCalibrateAvatar() MelonLoader.MelonLogger.Error(e); } } - - // Utilities - static void ReorientateLeapToUnity(ref Vector3 p_pos, ref Quaternion p_rot, Settings.LeapTrackingMode p_mode) - { - p_pos *= 0.001f; - p_pos.z *= -1f; - p_rot.x *= -1f; - p_rot.y *= -1f; - - switch(p_mode) - { - case Settings.LeapTrackingMode.Screentop: - { - p_pos.x *= -1f; - p_pos.y *= -1f; - p_rot = (ms_screentopRotationFix * p_rot); - } - break; - - case Settings.LeapTrackingMode.HMD: - { - p_pos.x *= -1f; - Utils.Swap(ref p_pos.y, ref p_pos.z); - p_rot = (ms_hmdRotationFix * p_rot); - } - break; - } - } } } diff --git a/ml_lme/Properties/AssemblyInfo.cs b/ml_lme/Properties/AssemblyInfo.cs index 3ec803a..3873ef0 100644 --- a/ml_lme/Properties/AssemblyInfo.cs +++ b/ml_lme/Properties/AssemblyInfo.cs @@ -1,10 +1,10 @@ using System.Reflection; [assembly: AssemblyTitle("LeapMotionExtension")] -[assembly: AssemblyVersion("1.2.3")] -[assembly: AssemblyFileVersion("1.2.3")] +[assembly: AssemblyVersion("1.2.4")] +[assembly: AssemblyFileVersion("1.2.4")] -[assembly: MelonLoader.MelonInfo(typeof(ml_lme.LeapMotionExtension), "LeapMotionExtension", "1.2.3", "SDraw", "https://github.com/SDraw/ml_mods_cvr")] +[assembly: MelonLoader.MelonInfo(typeof(ml_lme.LeapMotionExtension), "LeapMotionExtension", "1.2.4", "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)] diff --git a/ml_lme/Utils.cs b/ml_lme/Utils.cs index ef789c2..7813d3e 100644 --- a/ml_lme/Utils.cs +++ b/ml_lme/Utils.cs @@ -6,12 +6,42 @@ namespace ml_lme { static class Utils { + static readonly Quaternion ms_hmdRotationFix = new Quaternion(0f, 0.7071068f, 0.7071068f, 0f); + static readonly Quaternion ms_screentopRotationFix = new Quaternion(0f, 0f, -1f, 0f); + public static bool AreKnucklesInUse() => PlayerSetup.Instance._trackerManager.trackerNames.Contains("knuckles"); public static Matrix4x4 GetMatrix(this Transform p_transform, bool p_pos = true, bool p_rot = true, bool p_scl = false) { return Matrix4x4.TRS(p_pos ? p_transform.position : Vector3.zero, p_rot ? p_transform.rotation : Quaternion.identity, p_scl ? p_transform.lossyScale : Vector3.one); } + + public static void LeapToUnity(ref Vector3 p_pos, ref Quaternion p_rot, Settings.LeapTrackingMode p_mode) + { + p_pos *= 0.001f; + p_pos.z *= -1f; + p_rot.x *= -1f; + p_rot.y *= -1f; + + switch(p_mode) + { + case Settings.LeapTrackingMode.Screentop: + { + p_pos.x *= -1f; + p_pos.y *= -1f; + p_rot = (ms_screentopRotationFix * p_rot); + } + break; + + case Settings.LeapTrackingMode.HMD: + { + p_pos.x *= -1f; + Swap(ref p_pos.y, ref p_pos.z); + p_rot = (ms_hmdRotationFix * p_rot); + } + break; + } + } public static void Swap(ref T lhs, ref T rhs) {