Skip to content

Commit

Permalink
Update to r170
Browse files Browse the repository at this point in the history
  • Loading branch information
SDraw committed Dec 24, 2022
1 parent dcd94ae commit a06d002
Show file tree
Hide file tree
Showing 16 changed files with 311 additions and 175 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Merged set of MelonLoader mods for ChilloutVR.
| 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.8 | Yes | Working |
| Desktop Head Tracking | ml_dht | 1.1.1 | Yes, pending update | Working |
| Avatar Motion Tweaker | ml_amt | 1.1.9 | Yes, pending review | Working |
| Desktop Head Tracking | ml_dht | 1.1.1 | Yes, pending review | Working |
| Desktop Reticle Switch | ml_drs | 1.0.0 | Yes | Working |
| Four Point Tracking | ml_fpt | 1.0.9 | Yes | Working |
| Leap Motion Extension | ml_lme | 1.2.7 | Yes | Working |
| Four Point Tracking | ml_fpt | 1.0.9 | Retired | Deprecated | Not needed after r170 update
| Leap Motion Extension | ml_lme | 1.2.8 | Yes, pending review | Working |
| Server Connection Info | ml_sci | 1.0.2 | Yes | Working |
58 changes: 58 additions & 0 deletions ml_amt/Main.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ABI_RC.Core.Player;
using ABI_RC.Systems.IK.SubSystems;
using System.Reflection;

namespace ml_amt
Expand All @@ -9,6 +10,8 @@ public class AvatarMotionTweaker : MelonLoader.MelonMod

MotionTweaker m_localTweaker = null;

static bool ms_fbtDetour = false;

public override void OnInitializeMelon()
{
if(ms_instance == null)
Expand All @@ -26,6 +29,27 @@ public override void OnInitializeMelon()
null,
new HarmonyLib.HarmonyMethod(typeof(AvatarMotionTweaker).GetMethod(nameof(OnSetupAvatar_Postfix), BindingFlags.Static | BindingFlags.NonPublic))
);
HarmonyInstance.Patch(
typeof(BodySystem).GetMethod(nameof(BodySystem.Calibrate)),
null,
new HarmonyLib.HarmonyMethod(typeof(AvatarMotionTweaker).GetMethod(nameof(OnCalibrate_Postfix), BindingFlags.Static | BindingFlags.NonPublic))
);
HarmonyInstance.Patch(
typeof(BodySystem).GetMethod(nameof(BodySystem.FBTAvailable)),
new HarmonyLib.HarmonyMethod(typeof(AvatarMotionTweaker).GetMethod(nameof(OnFBTAvailable_Prefix), BindingFlags.Static | BindingFlags.NonPublic)),
null
);

HarmonyInstance.Patch(
typeof(PlayerSetup).GetMethod("Update", BindingFlags.NonPublic | BindingFlags.Instance),
new HarmonyLib.HarmonyMethod(typeof(AvatarMotionTweaker).GetMethod(nameof(FBTDetour_Prefix), BindingFlags.Static | BindingFlags.NonPublic)),
new HarmonyLib.HarmonyMethod(typeof(AvatarMotionTweaker).GetMethod(nameof(FBTDetour_Postfix), BindingFlags.Static | BindingFlags.NonPublic))
);
HarmonyInstance.Patch(
typeof(PlayerSetup).GetMethod("FixedUpdate", BindingFlags.NonPublic | BindingFlags.Instance),
new HarmonyLib.HarmonyMethod(typeof(AvatarMotionTweaker).GetMethod(nameof(FBTDetour_Prefix), BindingFlags.Static | BindingFlags.NonPublic)),
new HarmonyLib.HarmonyMethod(typeof(AvatarMotionTweaker).GetMethod(nameof(FBTDetour_Postfix), BindingFlags.Static | BindingFlags.NonPublic))
);

MelonLoader.MelonCoroutines.Start(WaitForLocalPlayer());
}
Expand Down Expand Up @@ -83,5 +107,39 @@ void OnSetupAvatar()
MelonLoader.MelonLogger.Error(l_exception);
}
}

static void OnCalibrate_Postfix() => ms_instance?.OnCalibrate();
void OnCalibrate()
{
try
{
if(m_localTweaker != null)
m_localTweaker.OnCalibrate();
}
catch(System.Exception l_exception)
{
MelonLoader.MelonLogger.Error(l_exception);
}
}

// FBT detection override
static void FBTDetour_Prefix()
{
ms_fbtDetour = true;
}
static void FBTDetour_Postfix()
{
ms_fbtDetour = false;
}
static bool OnFBTAvailable_Prefix(ref bool __result)
{
if(ms_fbtDetour && !BodySystem.isCalibratedAsFullBody)
{
__result = false;
return false;
}

return true;
}
}
}
72 changes: 46 additions & 26 deletions ml_amt/MotionTweaker.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ABI_RC.Core.Player;
using ABI_RC.Systems.IK.SubSystems;
using ABI_RC.Systems.MovementSystem;
using RootMotion.FinalIK;
using System.Collections.Generic;
Expand Down Expand Up @@ -51,6 +52,8 @@ enum PoseState
float m_locomotionWeight = 1f; // Original weight
float m_avatarScale = 1f; // Instantiated scale
Transform m_avatarHips = null;
float m_viewPointHeight = 1f;
bool m_isInVR = false;

bool m_avatarReady = false;
bool m_compatibleAvatar = false;
Expand Down Expand Up @@ -91,6 +94,8 @@ public MotionTweaker()

void Start()
{
m_isInVR = Utils.IsInVR();

Settings.IKOverrideCrouchChange += this.SetIKOverrideCrouch;
Settings.CrouchLimitChange += this.SetCrouchLimit;
Settings.IKOverrideProneChange += this.SetIKOverrideProne;
Expand Down Expand Up @@ -126,20 +131,20 @@ void Update()
m_moving = !Mathf.Approximately(MovementSystem.Instance.movementVector.magnitude, 0f);

// Update upright
Matrix4x4 l_hmdMatrix = PlayerSetup.Instance.transform.GetMatrix().inverse * (PlayerSetup.Instance._inVr ? PlayerSetup.Instance.vrHeadTracker.transform.GetMatrix() : PlayerSetup.Instance.desktopCameraRig.transform.GetMatrix());
Matrix4x4 l_hmdMatrix = PlayerSetup.Instance.transform.GetMatrix().inverse * (m_isInVR ? 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_avatarScale = (m_avatarScale > 0f) ? (PlayerSetup.Instance._avatar.transform.localScale.y / m_avatarScale) : 0f;
float l_avatarViewHeight = Mathf.Clamp(PlayerSetup.Instance.GetViewPointHeight() * l_avatarScale, 0f, float.MaxValue);
float l_avatarViewHeight = Mathf.Clamp(m_viewPointHeight * l_avatarScale, 0f, float.MaxValue);
m_upright = Mathf.Clamp(((l_avatarViewHeight > 0f) ? (l_currentHeight / l_avatarViewHeight) : 0f), 0f, 1f);
PoseState l_poseState = (m_upright <= m_proneLimit) ? PoseState.Proning : ((m_upright <= m_crouchLimit) ? PoseState.Crouching : PoseState.Standing);

if(m_followHips && (m_avatarHips != null))
if(m_avatarHips != null)
{
Vector4 l_hipsToPlayer = (PlayerSetup.Instance.transform.GetMatrix().inverse * m_avatarHips.GetMatrix()) * ms_pointVector;
m_hipsToPlayer.Set(l_hipsToPlayer.x, 0f, l_hipsToPlayer.z);
Vector4 l_hipsToPoint = (PlayerSetup.Instance.transform.GetMatrix().inverse * m_avatarHips.GetMatrix()) * ms_pointVector;
m_hipsToPlayer.Set(l_hipsToPoint.x, 0f, l_hipsToPoint.z);
}

if(PlayerSetup.Instance._inVr && (m_vrIk != null) && m_vrIk.enabled)
if(m_isInVR && (m_vrIk != null) && m_vrIk.enabled)
{
if(m_poseState != l_poseState)
{
Expand All @@ -164,8 +169,8 @@ void Update()

if(m_poseTransitions)
{
PlayerSetup.Instance.animatorManager.SetAnimatorParameterBool("Crouching", (l_poseState == PoseState.Crouching) && !m_compatibleAvatar && !PlayerSetup.Instance.fullBodyActive);
PlayerSetup.Instance.animatorManager.SetAnimatorParameterBool("Prone", (l_poseState == PoseState.Proning) && !m_compatibleAvatar && !PlayerSetup.Instance.fullBodyActive);
PlayerSetup.Instance.animatorManager.SetAnimatorParameterBool("Crouching", (l_poseState == PoseState.Crouching) && !m_compatibleAvatar && !BodySystem.isCalibratedAsFullBody);
PlayerSetup.Instance.animatorManager.SetAnimatorParameterBool("Prone", (l_poseState == PoseState.Proning) && !m_compatibleAvatar && !BodySystem.isCalibratedAsFullBody);
}
}

Expand Down Expand Up @@ -249,14 +254,17 @@ public void OnAvatarClear()
m_moving = false;
m_hipsToPlayer = Vector3.zero;
m_avatarHips = null;
m_viewPointHeight = 1f;
m_parameters.Clear();
}

public void OnSetupAvatar()
{
m_isInVR = Utils.IsInVR();
m_vrIk = PlayerSetup.Instance._avatar.GetComponent<VRIK>();
m_locomotionLayer = PlayerSetup.Instance._animator.GetLayerIndex("Locomotion/Emotes");
m_avatarHips = PlayerSetup.Instance._animator.GetBoneTransform(HumanBodyBones.Hips);
m_viewPointHeight = PlayerSetup.Instance._avatar.GetComponent<ABI.CCK.Components.CVRAvatar>().viewPosition.y;

// Parse animator parameters
AnimatorControllerParameter[] l_params = PlayerSetup.Instance._animator.parameters;
Expand Down Expand Up @@ -304,13 +312,27 @@ public void OnSetupAvatar()
if(m_customLocomotionOffset)
m_vrIk.solver.locomotion.offset = m_locomotionOffset;

m_vrIk.solver.OnPreUpdate += this.OnIKPreUpdate;
m_vrIk.solver.OnPostUpdate += this.OnIKPostUpdate;
m_vrIk.onPreSolverUpdate.AddListener(this.OnIKPreUpdate);
m_vrIk.onPostSolverUpdate.AddListener(this.OnIKPostUpdate);
}

m_avatarReady = true;
}

public void OnCalibrate()
{
if(m_avatarReady && BodySystem.isCalibratedAsFullBody && BodySystem.enableHipTracking && !BodySystem.enableRightFootTracking && !BodySystem.enableLeftFootTracking && !BodySystem.enableLeftKneeTracking && !BodySystem.enableRightKneeTracking)
{
BodySystem.isCalibratedAsFullBody = false;
BodySystem.TrackingLeftLegEnabled = false;
BodySystem.TrackingRightLegEnabled = false;
BodySystem.TrackingLocomotionEnabled = true;

if(m_vrIk != null)
m_vrIk.solver.spine.maxRootAngle = 25f; // I need to rotate my legs, ffs!
}
}

void OnIKPreUpdate()
{
bool l_legsOverride = false;
Expand All @@ -321,22 +343,17 @@ void OnIKPreUpdate()
if(m_detectEmotes && m_emoteActive)
m_vrIk.solver.IKPositionWeight = 0f;

// Game manages VRIK for desktop itself
if(PlayerSetup.Instance._inVr)
if((m_ikOverrideCrouch && (m_poseState != PoseState.Standing)) || (m_ikOverrideProne && (m_poseState == PoseState.Proning)))
{
if((m_ikOverrideCrouch && (m_poseState != PoseState.Standing)) || (m_ikOverrideProne && (m_poseState == PoseState.Proning)))
{
m_vrIk.solver.locomotion.weight = 0f;
l_legsOverride = true;
}
if(m_ikOverrideFly && MovementSystem.Instance.flying)
{
m_vrIk.solver.locomotion.weight = 0f;
l_legsOverride = true;
}
m_vrIk.solver.locomotion.weight = 0f;
l_legsOverride = true;
}
if(m_ikOverrideFly && MovementSystem.Instance.flying)
{
m_vrIk.solver.locomotion.weight = 0f;
l_legsOverride = true;
}

// But not this
if(m_ikOverrideJump && !m_grounded && !MovementSystem.Instance.flying)
{
m_vrIk.solver.locomotion.weight = 0f;
Expand All @@ -345,8 +362,11 @@ void OnIKPreUpdate()

bool l_solverActive = !Mathf.Approximately(m_vrIk.solver.IKPositionWeight, 0f);

if(l_legsOverride && l_solverActive && m_followHips && !m_moving && PlayerSetup.Instance._inVr)
if(l_legsOverride && l_solverActive && m_followHips && (!m_moving || (m_poseState == PoseState.Proning)) && m_isInVR && !BodySystem.isCalibratedAsFullBody)
{
ABI_RC.Systems.IK.IKSystem.VrikRootController.enabled = false;
PlayerSetup.Instance._avatar.transform.localPosition = m_hipsToPlayer;
}
}

void OnIKPostUpdate()
Expand Down Expand Up @@ -377,7 +397,7 @@ public void SetPoseTransitions(bool p_state)
{
m_poseTransitions = p_state;

if(!m_poseTransitions && m_avatarReady && PlayerSetup.Instance._inVr)
if(!m_poseTransitions && m_avatarReady && m_isInVR)
{
PlayerSetup.Instance.animatorManager.SetAnimatorParameterBool("Crouching", false);
PlayerSetup.Instance.animatorManager.SetAnimatorParameterBool("Prone", false);
Expand All @@ -387,7 +407,7 @@ public void SetAdjustedMovement(bool p_state)
{
m_adjustedMovement = p_state;

if(!m_adjustedMovement && m_avatarReady && PlayerSetup.Instance._inVr)
if(!m_adjustedMovement && m_avatarReady && m_isInVR)
{
MovementSystem.Instance.ChangeCrouch(false);
MovementSystem.Instance.ChangeProne(false);
Expand Down
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.1.8")]
[assembly: AssemblyFileVersion("1.1.8")]
[assembly: AssemblyVersion("1.1.9")]
[assembly: AssemblyFileVersion("1.1.9")]

[assembly: MelonLoader.MelonInfo(typeof(ml_amt.AvatarMotionTweaker), "AvatarMotionTweaker", "1.1.8", "SDraw", "https://github.com/SDraw/ml_mods_cvr")]
[assembly: MelonLoader.MelonInfo(typeof(ml_amt.AvatarMotionTweaker), "AvatarMotionTweaker", "1.1.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)]
3 changes: 3 additions & 0 deletions ml_amt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ Available additional parameters for AAS animator:

Additional avatars tweaks:
* If avatar has child object with name `LocomotionOffset` its local position will be used for offsetting VRIK locomotion mass center.

Additional mod's behaviour:
* Overrides FBT behaviour in 4PT mode (head, hands, hips). Be sure to disable legs and knees tracking in `Settings - IK tab`.
3 changes: 3 additions & 0 deletions ml_amt/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using UnityEngine;
using ABI_RC.Systems.IK;

namespace ml_amt
{
static class Utils
{
public static bool IsInVR() => ((ABI_RC.Core.Savior.CheckVR.Instance != null) && ABI_RC.Core.Savior.CheckVR.Instance.hasVrDeviceLoaded);

// Extensions
public static Matrix4x4 GetMatrix(this Transform p_transform, bool p_pos = true, bool p_rot = true, bool p_scl = false)
{
Expand Down
2 changes: 1 addition & 1 deletion ml_amt/ml_amt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>copy /y "$(TargetPath)" "C:\Games\Steam\common\ChilloutVR\Mods\"</PostBuildEvent>
<PostBuildEvent>copy /y "$(TargetPath)" "D:\Games\Steam\steamapps\common\ChilloutVR\Mods\"</PostBuildEvent>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion ml_amt/ml_amt.csproj.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ReferencePath>C:\Games\Steam\common\ChilloutVR\MelonLoader\;C:\Games\Steam\common\ChilloutVR\ChilloutVR_Data\Managed\</ReferencePath>
<ReferencePath>D:\Games\Steam\steamapps\common\ChilloutVR\MelonLoader\;D:\Games\Steam\steamapps\common\ChilloutVR\ChilloutVR_Data\Managed\</ReferencePath>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion ml_amt/resources/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ function inp_toggle_mod_amt(_obj, _callbackName) {
</div>
</div>
`;
document.getElementById('settings-implementation').appendChild(l_block);
document.getElementById('settings-ik').appendChild(l_block);

// Update sliders in new menu block
let l_sliders = l_block.querySelectorAll('.inp_slider');
Expand Down
Loading

0 comments on commit a06d002

Please sign in to comment.