Skip to content

Commit

Permalink
Pose detection and animator transitions for regular avatars
Browse files Browse the repository at this point in the history
  • Loading branch information
SDraw committed Sep 14, 2022
1 parent 0cab0fb commit 6b67471
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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.2 | Yes | Working |
| Avatar Motion Tweaker | ml_amt | 1.0.7 | On review | Working |
| Avatar Motion Tweaker | ml_amt | 1.0.8 | On review | Working |
| Desktop Reticle Switch | ml_drs | 1.0.0 | Yes | Working |
| Four Point Tracking | ml_fpt | 1.0.4 | Yes | Working |
| Leap Motion Extension | ml_lme | 1.1.8 | Yes | Working |
Expand Down
14 changes: 14 additions & 0 deletions ml_amt/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public override void OnApplicationStart()
Settings.Init();
Settings.IKOverrideChange += this.OnIKOverrideChange;
Settings.CrouchLimitChange += this.OnCrouchLimitChange;
Settings.DetectPoseChange += this.OnDetectPoseChange;
Settings.ProneLimitChange += this.OnProneLimitChange;

HarmonyInstance.Patch(
typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.ClearAvatar)),
Expand All @@ -38,6 +40,8 @@ System.Collections.IEnumerator WaitForLocalPlayer()
m_localTweaker = PlayerSetup.Instance.gameObject.AddComponent<MotionTweaker>();
m_localTweaker.SetIKOverride(Settings.IKOverride);
m_localTweaker.SetCrouchLimit(Settings.CrouchLimit);
m_localTweaker.SetDetectPose(Settings.DetectPose);
m_localTweaker.SetProneLimit(Settings.ProneLimit);
}


Expand All @@ -51,6 +55,16 @@ void OnCrouchLimitChange(float p_value)
if(m_localTweaker != null)
m_localTweaker.SetCrouchLimit(p_value);
}
void OnDetectPoseChange(bool p_state)
{
if(m_localTweaker != null)
m_localTweaker.SetDetectPose(p_state);
}
void OnProneLimitChange(float p_value)
{
if(m_localTweaker != null)
m_localTweaker.SetProneLimit(p_value);
}

static void OnAvatarClear_Postfix() => ms_instance?.OnAvatarClear();
void OnAvatarClear()
Expand Down
86 changes: 68 additions & 18 deletions ml_amt/MotionTweaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ enum PoseState
float m_locomotionWeight = 1f; // Original weight

bool m_avatarReady = false;
bool m_compatibleAvatar = false;

bool m_ikOverride = true;
float m_currentUpright = 1f;
PoseState m_poseState = PoseState.Standing;

float m_crouchLimit = 0.65f;
bool m_customCrouchLimit = false;
float m_proneLimit = 0.3f; // Unused

bool m_detectPose = true;
float m_proneLimit = 0.3f;
bool m_customProneLimit = false;

bool m_customLocomotionOffset = false;
Vector3 m_locomotionOffset = Vector3.zero;
Expand All @@ -70,10 +74,39 @@ void Update()
m_currentUpright = Mathf.Clamp((((l_currentHeight > 0f) && (l_avatarViewHeight > 0f)) ? (l_currentHeight / l_avatarViewHeight) : 0f), 0f, 1f);
PoseState l_poseState = (m_currentUpright <= m_proneLimit) ? PoseState.Proning : ((m_currentUpright <= m_crouchLimit) ? PoseState.Crouching : PoseState.Standing);

if(m_ikOverride && (m_vrIk != null) && m_vrIk.enabled)
if((m_vrIk != null) && m_vrIk.enabled)
{
if((m_poseState != l_poseState) && (l_poseState == PoseState.Standing))
if(m_ikOverride && (m_poseState != l_poseState) && (l_poseState == PoseState.Standing))
ms_rootVelocity.SetValue(m_vrIk.solver, Vector3.zero);

if(m_detectPose && !m_compatibleAvatar && !PlayerSetup.Instance.fullBodyActive)
{
switch(l_poseState)
{
case PoseState.Standing:
{
PlayerSetup.Instance._movementSystem.ChangeCrouch(false);
PlayerSetup.Instance._movementSystem.ChangeProne(false);
PlayerSetup.Instance.animatorManager.SetAnimatorParameterBool("Crouching", false); // Forced to stop transitioning to standing locomotion
PlayerSetup.Instance.animatorManager.SetAnimatorParameterBool("Prone", false); // Forced to stop transitioning to standing locomotion
}
break;

case PoseState.Crouching:
PlayerSetup.Instance._movementSystem.ChangeCrouch(true);
PlayerSetup.Instance.animatorManager.SetAnimatorParameterBool("Crouching", true);
PlayerSetup.Instance.animatorManager.SetAnimatorParameterBool("Prone", false);
break;

case PoseState.Proning:
{
PlayerSetup.Instance._movementSystem.ChangeProne(true);
PlayerSetup.Instance.animatorManager.SetAnimatorParameterBool("Crouching", false);
PlayerSetup.Instance.animatorManager.SetAnimatorParameterBool("Prone", true);
}
break;
}
}
}

m_poseState = l_poseState;
Expand Down Expand Up @@ -106,10 +139,12 @@ void Update()
public void OnAvatarClear()
{
m_avatarReady = false;
m_compatibleAvatar = false;
m_vrIk = null;
m_poseState = PoseState.Standing;
m_parameters.Clear();
m_customCrouchLimit = false;
m_customProneLimit = false;
m_customLocomotionOffset = false;
m_locomotionOffset = Vector3.zero;
}
Expand Down Expand Up @@ -143,10 +178,16 @@ public void OnSetupAvatarGeneral()
}
}

m_compatibleAvatar = m_parameters.Exists(p => p.m_name.Contains("Upright"));

Transform l_customTransform = PlayerSetup.Instance._avatar.transform.Find("CrouchLimit");
m_customCrouchLimit = (l_customTransform != null);
m_crouchLimit = m_customCrouchLimit ? Mathf.Clamp(l_customTransform.localPosition.y, 0f, 1f) : Settings.CrouchLimit;

l_customTransform = PlayerSetup.Instance._avatar.transform.Find("ProneLimit");
m_customProneLimit = (l_customTransform != null);
m_proneLimit = m_customProneLimit ? Mathf.Clamp(l_customTransform.localPosition.y, 0f, 1f) : Settings.ProneLimit;

l_customTransform = PlayerSetup.Instance._avatar.transform.Find("LocomotionOffset");
m_customLocomotionOffset = (l_customTransform != null);
m_locomotionOffset = m_customLocomotionOffset ? l_customTransform.localPosition : Vector3.zero;
Expand All @@ -164,37 +205,46 @@ public void OnSetupAvatarGeneral()
m_avatarReady = true;
}

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;
}

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)
public void SetDetectPose(bool p_state)
{
m_proneLimit = Mathf.Clamp(p_value, 0f, 1f);
}
m_detectPose = p_state;

void OnIKPreUpdate()
{
if(m_ikOverride)
if(!m_detectPose && m_avatarReady && !m_compatibleAvatar && PlayerSetup.Instance._inVr)
{
m_locomotionWeight = m_vrIk.solver.locomotion.weight;
if(m_poseState != PoseState.Standing)
m_vrIk.solver.locomotion.weight = 0f;
PlayerSetup.Instance._movementSystem.ChangeCrouch(false);
PlayerSetup.Instance.animatorManager.SetAnimatorParameterBool("Crouching", false);
PlayerSetup.Instance._movementSystem.ChangeProne(false);
PlayerSetup.Instance.animatorManager.SetAnimatorParameterBool("Prone", false);
}
}

void OnIKPostUpdate()
public void SetProneLimit(float p_value)
{
if(m_ikOverride)
m_vrIk.solver.locomotion.weight = m_locomotionWeight;
m_proneLimit = Mathf.Clamp(p_value, 0f, 1f);
}
}
}
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.7")]
[assembly: AssemblyFileVersion("1.0.7")]
[assembly: AssemblyVersion("1.0.8")]
[assembly: AssemblyFileVersion("1.0.8")]

[assembly: MelonLoader.MelonInfo(typeof(ml_amt.AvatarMotionTweaker), "AvatarMotionTweaker", "1.0.7", "SDraw", "https://github.com/SDraw/ml_mods_cvr")]
[assembly: MelonLoader.MelonInfo(typeof(ml_amt.AvatarMotionTweaker), "AvatarMotionTweaker", "1.0.8", "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)]
20 changes: 13 additions & 7 deletions ml_amt/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Avatar Motion Tweaker
This mod adds `Upright` parameter for usage in AAS animator and allows disabling legs autostep upon reaching specific `Upright` value.
This mod adds features for AAS animator and avatar locomotion behaviour.

![](.github/img_01.png)

Expand All @@ -10,17 +10,24 @@ This mod adds `Upright` parameter for usage in AAS animator and allows disabling

# Usage
Available mod's settings in `Settings - Implementation - Avatar Motion Tweaker`:
* **Legs locomotion upright limit:** defines upright limit of legs autostep. If HMD tracking goes below set limit, legs autostep is disabled. Default value - 65.
* Limit can be overrided by avatar. For this avatar has to have child gameobject with name `CrouchLimit` and its Y-axis location will be used as limit, should be in range [0.0, 1.0].
* **IK locomotion override:** disables legs locomotion/autostep upon HMD reaching height of `CrouchLimit`; default value - `true`.
* **Crouch limit:** defines first limit; default value - `65`.
* Note: Can be overrided by avatar. For this avatar has to have child gameobject with name `CrouchLimit`, its Y-axis location will be used as limit, should be in range [0.0, 1.0].
* **Detect pose (regular avatars):** forces regular avatars' animations to transit to crouching/proning animation states; default value - `true`.
* Note: Avatar is considered as regular if its animator doesn't have `Upright` parameter.
* **Prone limit (regular avatars):** defines second limit; default value - `30`.
* Note: Can be overrided by avatar. For this avatar has to have child gameobject with name `ProneLimit`, its Y-axis location will be used as limit, should be in range [0.0, 1.0].
* Note: Has no effect for mod compatible avatars.

Available additional parameters for AAS animator:
* **`Upright`:** defines linear coefficient between current viewpoint height and avatar's viewpoint height. Range - [0.0,1.0] (0.0 - floor, 1.0 - full standing).
* Note: can be set as local-only (not synced) if starts with `#` character.
* Note: Can be set as local-only (not synced) if starts with `#` character.
* Note: Defining this parameter in AAS animator will consider avatar as compatible with mod.

Additional avatars tweaks:
* If avatar has child object with name `LocomotionOffset` its local position will be used for offsetting VRIK locomotion center.
## Example of usage in AAS animator for mixed desktop and VR

## Advanced usage in AAS animator for mixed desktop and VR
* To differentiate between desktop and VR players use `CVR Parameter Stream` component on avatar's root gameobject. As example, `InVR` and `InFBT` are boolean typed animator parameters:
![](.github/img_02.png)
* Add additional transitions between standing, crouching and proning blend trees:
Expand All @@ -38,5 +45,4 @@ Additional avatars tweaks:
![](.github/img_08.png)

# Notes
* Sometimes after restoring legs autostep avatar's torso shakes, currently investigating solution.
* Usage of `Upright` parameter for transition between poses (standing/crouching/proning) in desktop mode is useless, because in this case your animations are updating value of `Upright` parameter, not the other way around.
40 changes: 35 additions & 5 deletions ml_amt/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,23 @@ static class Settings
enum ModSetting
{
IKOverride = 0,
CrouchLimit
CrouchLimit,
DetectPose,
ProneLimit
};

static bool ms_ikOverride = true;
static float ms_crouchLimit = 0.65f;
static bool ms_detectPose = true;
static float ms_proneLimit = 0.3f;

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;
static public event Action<bool> DetectPoseChange;
static public event Action<float> ProneLimitChange;

public static void Init()
{
Expand All @@ -29,6 +35,8 @@ public static void Init()
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));
ms_entries.Add(ms_category.CreateEntry(ModSetting.DetectPose.ToString(), true));
ms_entries.Add(ms_category.CreateEntry(ModSetting.ProneLimit.ToString(), 30));

Load();

Expand Down Expand Up @@ -61,6 +69,8 @@ static void Load()
{
ms_ikOverride = (bool)ms_entries[(int)ModSetting.IKOverride].BoxedValue;
ms_crouchLimit = ((int)ms_entries[(int)ModSetting.CrouchLimit].BoxedValue) * 0.01f;
ms_detectPose = (bool)ms_entries[(int)ModSetting.DetectPose].BoxedValue;
ms_proneLimit = ((int)ms_entries[(int)ModSetting.ProneLimit].BoxedValue) * 0.01f;
}

static void OnSliderUpdate(string p_name, string p_value)
Expand All @@ -73,8 +83,13 @@ static void OnSliderUpdate(string p_name, string p_value)
{
ms_crouchLimit = int.Parse(p_value) * 0.01f;
CrouchLimitChange?.Invoke(ms_crouchLimit);
}
break;
} break;

case ModSetting.ProneLimit:
{
ms_proneLimit = int.Parse(p_value) * 0.01f;
ProneLimitChange?.Invoke(ms_proneLimit);
} break;
}

ms_entries[(int)l_setting].BoxedValue = int.Parse(p_value);
Expand All @@ -91,8 +106,13 @@ static void OnToggleUpdate(string p_name, string p_value)
{
ms_ikOverride = bool.Parse(p_value);
IKOverrideChange?.Invoke(ms_ikOverride);
}
break;
} break;

case ModSetting.DetectPose:
{
ms_detectPose = bool.Parse(p_value);
DetectPoseChange?.Invoke(ms_detectPose);
} break;
}

ms_entries[(int)l_setting].BoxedValue = bool.Parse(p_value);
Expand All @@ -108,5 +128,15 @@ public static bool IKOverride
{
get => ms_ikOverride;
}

public static bool DetectPose
{
get => ms_detectPose;
}

public static float ProneLimit
{
get => ms_proneLimit;
}
}
}
18 changes: 16 additions & 2 deletions ml_amt/resources/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,32 @@ function inp_toggle_mod_amt(_obj, _callbackName) {
</div>
<div class ="row-wrapper">
<div class ="option-caption">IK override: </div>
<div class ="option-caption">IK locomotion 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-caption">Crouch limit: </div>
<div class ="option-input">
<div id="CrouchLimit" class ="inp_slider no-scroll" data-min="0" data-max="100" data-current="65"></div>
</div>
</div>
<div class ="row-wrapper">
<div class ="option-caption">Detect pose (regular avatars): </div>
<div class ="option-input">
<div id="DetectPose" class ="inp_toggle no-scroll" data-current="true"></div>
</div>
</div>
<div class ="row-wrapper">
<div class ="option-caption">Prone limit (regular avatars): </div>
<div class ="option-input">
<div id="ProneLimit" class ="inp_slider no-scroll" data-min="0" data-max="100" data-current="30"></div>
</div>
</div>
`;
document.getElementById('settings-implementation').appendChild(l_block);

Expand Down

0 comments on commit 6b67471

Please sign in to comment.