Skip to content

Commit

Permalink
Rework of IK override
Browse files Browse the repository at this point in the history
  • Loading branch information
SDraw committed Sep 13, 2022
1 parent 4e3d5dd commit 49b72a0
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 30 deletions.
9 changes: 9 additions & 0 deletions ml_amt/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public override void OnApplicationStart()
ms_instance = this;

Settings.Init();
Settings.IKOverrideChange += this.OnIKOverrideChange;
Settings.CrouchLimitChange += this.OnCrouchLimitChange;

HarmonyInstance.Patch(
Expand All @@ -35,8 +36,16 @@ System.Collections.IEnumerator WaitForLocalPlayer()
yield return null;

m_localTweaker = PlayerSetup.Instance.gameObject.AddComponent<MotionTweaker>();
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)
Expand Down
75 changes: 52 additions & 23 deletions ml_amt/MotionTweaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,27 @@ struct AdditionalParameterInfo
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;
Expand All @@ -50,32 +59,24 @@ public MotionTweaker()
m_parameters = new List<AdditionalParameterInfo>();
}

void Start()
{
m_ikCalibrator = this.GetComponent<CVR_IK_Calibrator>();
}

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)
{
Expand Down Expand Up @@ -104,12 +105,10 @@ void Update()

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;
Expand Down Expand Up @@ -155,17 +154,47 @@ public void OnSetupAvatarGeneral()
// 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)
{
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;
}
}
}
6 changes: 3 additions & 3 deletions ml_amt/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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)]
37 changes: 33 additions & 4 deletions ml_amt/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@
using cohtml;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace ml_amt
{
static class Settings
{
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<MelonLoader.MelonPreferences_Entry> ms_entries = null;

static public event Action<bool> IKOverrideChange;
static public event Action<float> CrouchLimitChange;

public static void Init()
{
ms_category = MelonLoader.MelonPreferences.CreateCategory("AMT");

ms_entries = new List<MelonLoader.MelonPreferences_Entry>();
ms_entries.Add(ms_category.CreateEntry(ModSetting.IKOverride.ToString(), true));
ms_entries.Add(ms_category.CreateEntry(ModSetting.CrouchLimit.ToString(), 65));

Load();
Expand All @@ -44,6 +47,7 @@ static System.Collections.IEnumerator WaitMainMenuUi()
ViewManager.Instance.gameMenuView.Listener.ReadyForBindings += () =>
{
ViewManager.Instance.gameMenuView.View.BindCall("MelonMod_AMT_Call_InpSlider", new Action<string, string>(OnSliderUpdate));
ViewManager.Instance.gameMenuView.View.BindCall("MelonMod_AMT_Call_InpToggle", new Action<string, string>(OnToggleUpdate));
};
ViewManager.Instance.gameMenuView.Listener.FinishLoad += (_) =>
{
Expand All @@ -55,6 +59,7 @@ static System.Collections.IEnumerator WaitMainMenuUi()

static void Load()
{
ms_ikOverride = (bool)ms_entries[(int)ModSetting.IKOverride].BoxedValue;
ms_crouchLimit = ((int)ms_entries[(int)ModSetting.CrouchLimit].BoxedValue) * 0.01f;
}

Expand All @@ -66,18 +71,42 @@ static void OnSliderUpdate(string p_name, string p_value)
{
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;
}
}
}
61 changes: 61 additions & 0 deletions ml_amt/resources/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -132,6 +180,13 @@ function inp_slider_mod_amt(_obj, _callbackName) {
<div class ="subcategory-description"></div>
</div>
<div class ="row-wrapper">
<div class ="option-caption">IK override: </div>
<div class ="option-input">
<div id="IKOverride" class ="inp_toggle no-scroll" data-current="true"></div>
</div>
</div>
<div class ="row-wrapper">
<div class ="option-caption">Legs locomotion upright limit: </div>
<div class ="option-input">
Expand All @@ -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');
}
}

0 comments on commit 49b72a0

Please sign in to comment.