From 49b72a071156acfdd6df643d2b51e3336e3e87f6 Mon Sep 17 00:00:00 2001 From: SDraw Date: Wed, 14 Sep 2022 00:09:09 +0300 Subject: [PATCH] Rework of IK override --- ml_amt/Main.cs | 9 ++++ ml_amt/MotionTweaker.cs | 75 +++++++++++++++++++++---------- ml_amt/Properties/AssemblyInfo.cs | 6 +-- ml_amt/Settings.cs | 37 +++++++++++++-- ml_amt/resources/menu.js | 61 +++++++++++++++++++++++++ 5 files changed, 158 insertions(+), 30 deletions(-) diff --git a/ml_amt/Main.cs b/ml_amt/Main.cs index 263e10d..b700cf4 100644 --- a/ml_amt/Main.cs +++ b/ml_amt/Main.cs @@ -13,6 +13,7 @@ namespace ml_amt ms_instance = this; Settings.Init(); + Settings.IKOverrideChange += this.OnIKOverrideChange; Settings.CrouchLimitChange += this.OnCrouchLimitChange; HarmonyInstance.Patch( @@ -35,8 +36,16 @@ namespace ml_amt yield return null; m_localTweaker = PlayerSetup.Instance.gameObject.AddComponent(); + m_localTweaker.SetIKOverride(Settings.IKOverride); + m_localTweaker.SetCrouchLimit(Settings.CrouchLimit); } + + void OnIKOverrideChange(bool p_state) + { + if(m_localTweaker != null) + m_localTweaker.SetIKOverride(p_state); + } void OnCrouchLimitChange(float p_value) { if(m_localTweaker != null) diff --git a/ml_amt/MotionTweaker.cs b/ml_amt/MotionTweaker.cs index 3a6386a..3489224 100644 --- a/ml_amt/MotionTweaker.cs +++ b/ml_amt/MotionTweaker.cs @@ -27,18 +27,27 @@ namespace ml_amt public int m_hash; // For local only } + enum PoseState + { + Standing = 0, + Crouching, + Proning + } + static readonly Vector4 ms_pointVector = new Vector4(0f, 0f, 0f, 1f); - CVR_IK_Calibrator m_ikCalibrator = null; VRIK m_vrIk = null; + float m_locomotionWeight = 1f; // Original weight - bool m_ready = false; + bool m_avatarReady = false; - bool m_standing = true; + bool m_ikOverride = true; float m_currentUpright = 1f; - float m_locomotionWeight = 1f; + PoseState m_poseState = PoseState.Standing; + float m_crouchLimit = 0.65f; bool m_customCrouchLimit = false; + float m_proneLimit = 0.3f; // Unused bool m_customLocomotionOffset = false; Vector3 m_locomotionOffset = Vector3.zero; @@ -50,32 +59,24 @@ namespace ml_amt m_parameters = new List(); } - void Start() - { - m_ikCalibrator = this.GetComponent(); - } - void Update() { - if(m_ready) + if(m_avatarReady) { // Update upright Matrix4x4 l_hmdMatrix = PlayerSetup.Instance.transform.GetMatrix().inverse * (PlayerSetup.Instance._inVr ? PlayerSetup.Instance.vrHeadTracker.transform.GetMatrix() : PlayerSetup.Instance.desktopCameraRig.transform.GetMatrix()); float l_currentHeight = Mathf.Clamp((l_hmdMatrix * ms_pointVector).y, 0f, float.MaxValue); float l_avatarViewHeight = Mathf.Clamp(PlayerSetup.Instance.GetViewPointHeight() * PlayerSetup.Instance._avatar.transform.localScale.y, 0f, float.MaxValue); m_currentUpright = Mathf.Clamp((((l_currentHeight > 0f) && (l_avatarViewHeight > 0f)) ? (l_currentHeight / l_avatarViewHeight) : 0f), 0f, 1f); - bool l_standing = (m_currentUpright > m_crouchLimit); + PoseState l_poseState = (m_currentUpright <= m_proneLimit) ? PoseState.Proning : ((m_currentUpright <= m_crouchLimit) ? PoseState.Crouching : PoseState.Standing); - if(!m_ikCalibrator.avatarCalibratedAsFullBody && (m_vrIk != null) && m_vrIk.enabled && !PlayerSetup.Instance._movementSystem.sitting && (PlayerSetup.Instance._movementSystem.movementVector.magnitude <= Mathf.Epsilon)) + if(m_ikOverride && (m_vrIk != null) && m_vrIk.enabled) { - m_locomotionWeight = Mathf.Lerp(m_locomotionWeight, l_standing ? 1f : 0f, 0.5f); - m_vrIk.solver.locomotion.weight = m_locomotionWeight; - - if(l_standing && (m_standing != l_standing)) + if((m_poseState != l_poseState) && (l_poseState == PoseState.Standing)) ms_rootVelocity.SetValue(m_vrIk.solver, Vector3.zero); } - m_standing = l_standing; + m_poseState = l_poseState; if(m_parameters.Count > 0) { @@ -104,12 +105,10 @@ namespace ml_amt public void OnAvatarClear() { - m_ready = false; + m_avatarReady = false; m_vrIk = null; - m_standing = true; + m_poseState = PoseState.Standing; m_parameters.Clear(); - m_locomotionWeight = 1f; - m_crouchLimit = 0.65f; m_customCrouchLimit = false; m_customLocomotionOffset = false; m_locomotionOffset = Vector3.zero; @@ -155,11 +154,20 @@ namespace ml_amt // Apply VRIK tweaks if(m_vrIk != null) { - if(m_customLocomotionOffset && (m_vrIk.solver?.locomotion != null)) + if(m_customLocomotionOffset) m_vrIk.solver.locomotion.offset = m_locomotionOffset; + + m_vrIk.solver.OnPreUpdate += this.OnIKPreUpdate; + m_vrIk.solver.OnPostUpdate += this.OnIKPostUpdate; } - m_ready = true; + m_avatarReady = true; + } + + + public void SetIKOverride(bool p_state) + { + m_ikOverride = p_state; } public void SetCrouchLimit(float p_value) @@ -167,5 +175,26 @@ namespace ml_amt if(!m_customCrouchLimit) m_crouchLimit = Mathf.Clamp(p_value, 0f, 1f); } + + public void SetProneLimit(float p_value) + { + m_proneLimit = Mathf.Clamp(p_value, 0f, 1f); + } + + void OnIKPreUpdate() + { + if(m_ikOverride) + { + m_locomotionWeight = m_vrIk.solver.locomotion.weight; + if(m_poseState != PoseState.Standing) + m_vrIk.solver.locomotion.weight = 0f; + } + } + + void OnIKPostUpdate() + { + if(m_ikOverride) + m_vrIk.solver.locomotion.weight = m_locomotionWeight; + } } } diff --git a/ml_amt/Properties/AssemblyInfo.cs b/ml_amt/Properties/AssemblyInfo.cs index 3b92295..f986b17 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.0.6")] -[assembly: AssemblyFileVersion("1.0.6")] +[assembly: AssemblyVersion("1.0.7")] +[assembly: AssemblyFileVersion("1.0.7")] -[assembly: MelonLoader.MelonInfo(typeof(ml_amt.AvatarMotionTweaker), "AvatarMotionTweaker", "1.0.6", "SDraw", "https://github.com/SDraw/ml_mods_cvr")] +[assembly: MelonLoader.MelonInfo(typeof(ml_amt.AvatarMotionTweaker), "AvatarMotionTweaker", "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_amt/Settings.cs b/ml_amt/Settings.cs index 7484c4a..c3435d5 100644 --- a/ml_amt/Settings.cs +++ b/ml_amt/Settings.cs @@ -2,7 +2,6 @@ using cohtml; using System; using System.Collections.Generic; -using UnityEngine; namespace ml_amt { @@ -10,14 +9,17 @@ namespace ml_amt { enum ModSetting { - CrouchLimit = 0 + IKOverride = 0, + CrouchLimit }; + static bool ms_ikOverride = true; static float ms_crouchLimit = 0.65f; static MelonLoader.MelonPreferences_Category ms_category = null; static List ms_entries = null; + static public event Action IKOverrideChange; static public event Action CrouchLimitChange; public static void Init() @@ -25,6 +27,7 @@ namespace ml_amt ms_category = MelonLoader.MelonPreferences.CreateCategory("AMT"); ms_entries = new List(); + ms_entries.Add(ms_category.CreateEntry(ModSetting.IKOverride.ToString(), true)); ms_entries.Add(ms_category.CreateEntry(ModSetting.CrouchLimit.ToString(), 65)); Load(); @@ -44,6 +47,7 @@ namespace ml_amt ViewManager.Instance.gameMenuView.Listener.ReadyForBindings += () => { ViewManager.Instance.gameMenuView.View.BindCall("MelonMod_AMT_Call_InpSlider", new Action(OnSliderUpdate)); + ViewManager.Instance.gameMenuView.View.BindCall("MelonMod_AMT_Call_InpToggle", new Action(OnToggleUpdate)); }; ViewManager.Instance.gameMenuView.Listener.FinishLoad += (_) => { @@ -55,6 +59,7 @@ namespace ml_amt static void Load() { + ms_ikOverride = (bool)ms_entries[(int)ModSetting.IKOverride].BoxedValue; ms_crouchLimit = ((int)ms_entries[(int)ModSetting.CrouchLimit].BoxedValue) * 0.01f; } @@ -66,18 +71,42 @@ namespace ml_amt { case ModSetting.CrouchLimit: { - ms_crouchLimit = ((int)ms_entries[(int)ModSetting.CrouchLimit].BoxedValue) * 0.01f; + ms_crouchLimit = int.Parse(p_value) * 0.01f; CrouchLimitChange?.Invoke(ms_crouchLimit); - } break; + } + break; } ms_entries[(int)l_setting].BoxedValue = int.Parse(p_value); } } + static void OnToggleUpdate(string p_name, string p_value) + { + if(Enum.TryParse(p_name, out ModSetting l_setting)) + { + switch(l_setting) + { + case ModSetting.IKOverride: + { + ms_ikOverride = bool.Parse(p_value); + IKOverrideChange?.Invoke(ms_ikOverride); + } + break; + } + + ms_entries[(int)l_setting].BoxedValue = bool.Parse(p_value); + } + } + public static float CrouchLimit { get => ms_crouchLimit; } + + public static bool IKOverride + { + get => ms_ikOverride; + } } } diff --git a/ml_amt/resources/menu.js b/ml_amt/resources/menu.js index 7cfdc7c..a01f83b 100644 --- a/ml_amt/resources/menu.js +++ b/ml_amt/resources/menu.js @@ -123,6 +123,54 @@ function inp_slider_mod_amt(_obj, _callbackName) { } } +// Modified from original `inp` types, because I have no js knowledge to hook stuff +function inp_toggle_mod_amt(_obj, _callbackName) { + this.obj = _obj; + this.callbackName = _callbackName; + this.value = _obj.getAttribute('data-current'); + this.name = _obj.id; + this.type = _obj.getAttribute('data-type'); + + var self = this; + + this.mouseDown = function (_e) { + self.value = self.value == "True" ? "False" : "True"; + self.updateState(); + } + + this.updateState = function () { + self.obj.classList.remove("checked"); + if (self.value == "True") { + self.obj.classList.add("checked"); + } + + engine.call(self.callbackName, self.name, self.value); + } + + _obj.addEventListener('mousedown', this.mouseDown); + + this.getValue = function () { + return self.value; + } + + this.updateValue = function (value) { + self.value = value; + + self.obj.classList.remove("checked"); + if (self.value == "True") { + self.obj.classList.add("checked"); + } + } + + this.updateValue(this.value); + + return { + name: this.name, + value: this.getValue, + updateValue: this.updateValue + } +} + // Add own menu { let l_block = document.createElement('div'); @@ -132,6 +180,13 @@ function inp_slider_mod_amt(_obj, _callbackName) {
+
+
IK override:
+
+
+
+
+
Legs locomotion upright limit:
@@ -146,4 +201,10 @@ function inp_slider_mod_amt(_obj, _callbackName) { for (var i = 0; i < l_sliders.length; i++) { g_modSettingsAMT[g_modSettingsAMT.length] = new inp_slider_mod_amt(l_sliders[i], 'MelonMod_AMT_Call_InpSlider'); } + + // Update toggles in new menu block + let l_toggles = l_block.querySelectorAll('.inp_toggle'); + for (var i = 0; i < l_toggles.length; i++) { + g_modSettingsAMT[g_modSettingsAMT.length] = new inp_toggle_mod_amt(l_toggles[i], 'MelonMod_AMT_Call_InpToggle'); + } }