Skip to content

Commit

Permalink
Minor code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
SDraw committed Sep 24, 2024
1 parent a2f9c13 commit 4555794
Show file tree
Hide file tree
Showing 35 changed files with 543 additions and 821 deletions.
12 changes: 11 additions & 1 deletion ml_amt/AvatarParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class AvatarParameter
{
public enum ParameterType
{
Moving
Moving,
MovementSpeed,
Velocity
}

readonly ParameterType m_type;
Expand Down Expand Up @@ -44,6 +46,14 @@ public void Update(MotionTweaker p_tweaker)
case ParameterType.Moving:
SetBoolean(p_tweaker.IsMoving());
break;

case ParameterType.MovementSpeed:
SetFloat(p_tweaker.GetMovementSpeed());
break;

case ParameterType.Velocity:
SetFloat(p_tweaker.GetVelocity());
break;
}
}

Expand Down
20 changes: 10 additions & 10 deletions ml_amt/Main.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
using ABI_RC.Core.Player;
using System.Collections;
using System.Collections;
using UnityEngine;

namespace ml_amt
{
public class AvatarMotionTweaker : MelonLoader.MelonMod
{
MotionTweaker m_localTweaker = null;
MotionTweaker m_tweaker = null;

public override void OnInitializeMelon()
{
Settings.Init();
GameEvents.Init(HarmonyInstance);

MelonLoader.MelonCoroutines.Start(WaitForLocalPlayer());
MelonLoader.MelonCoroutines.Start(WaitForRootLogic());
}

IEnumerator WaitForLocalPlayer()
IEnumerator WaitForRootLogic()
{
while(PlayerSetup.Instance == null)
while(ABI_RC.Core.RootLogic.Instance == null)
yield return null;

m_localTweaker = PlayerSetup.Instance.gameObject.AddComponent<MotionTweaker>();
m_tweaker = new GameObject("[AvatarMotionTweaker]").AddComponent<MotionTweaker>();
}

public override void OnDeinitializeMelon()
{
if(m_localTweaker != null)
UnityEngine.Object.Destroy(m_localTweaker);
m_localTweaker = null;
if(m_tweaker != null)
Object.Destroy(m_tweaker.gameObject);
m_tweaker = null;
}
}
}
58 changes: 15 additions & 43 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.Core.Util.AnimatorManager;
using ABI_RC.Systems.IK.SubSystems;
using ABI_RC.Systems.Movement;
using RootMotion.FinalIK;
Expand All @@ -19,24 +20,13 @@ struct IKState
public bool m_bendNormalRight;
}

static readonly Vector4 ms_pointVector = new Vector4(0f, 0f, 0f, 1f);
static readonly int ms_emoteHash = Animator.StringToHash("Emote");

IKState m_ikState;
VRIK m_vrIk = null;
int m_locomotionLayer = 0;
float m_avatarScale = 1f;
Vector3 m_locomotionOffset = Vector3.zero; // Original locomotion offset

bool m_avatarReady = false;
bool m_grounded = false;
bool m_moving = false;

bool m_locomotionOverride = false;
bool m_emoteActive = false;

Vector3 m_massCenter = Vector3.zero;

Transform m_ikLimits = null;

readonly List<AvatarParameter> m_parameters = null;
Expand All @@ -49,6 +39,8 @@ internal MotionTweaker()
// Unity events
void Start()
{
DontDestroyOnLoad(this);

OnCrouchLimitChanged(Settings.CrouchLimit);
OnProneLimitChanged(Settings.ProneLimit);

Expand Down Expand Up @@ -82,18 +74,8 @@ void Update()
{
if(m_avatarReady)
{
m_grounded = BetterBetterCharacterController.Instance.IsGrounded();
m_moving = BetterBetterCharacterController.Instance.IsMoving();

UpdateIKLimits();

m_emoteActive = false;
if(Settings.DetectEmotes && (m_locomotionLayer >= 0))
{
AnimatorStateInfo l_animState = PlayerSetup.Instance._animator.GetCurrentAnimatorStateInfo(m_locomotionLayer);
m_emoteActive = (l_animState.tagHash == ms_emoteHash);
}

foreach(AvatarParameter l_param in m_parameters)
l_param.Update(this);
}
Expand All @@ -103,14 +85,9 @@ void Update()
void OnAvatarClear()
{
m_vrIk = null;
m_locomotionLayer = -1;
m_grounded = false;
m_avatarReady = false;
m_avatarScale = 1f;
m_locomotionOffset = Vector3.zero;
m_emoteActive = false;
m_moving = false;
m_locomotionOverride = false;
m_massCenter = Vector3.zero;
m_ikLimits = null;
m_parameters.Clear();
Expand All @@ -124,11 +101,12 @@ void OnAvatarSetup()
Utils.SetAvatarTPose();

m_vrIk = PlayerSetup.Instance._avatar.GetComponent<VRIK>();
m_locomotionLayer = PlayerSetup.Instance._animator.GetLayerIndex("Locomotion/Emotes");
m_avatarScale = Mathf.Abs(PlayerSetup.Instance._avatar.transform.localScale.y);

// Parse animator parameters
m_parameters.Add(new AvatarParameter(AvatarParameter.ParameterType.Moving, PlayerSetup.Instance.animatorManager));
m_parameters.Add(new AvatarParameter(AvatarParameter.ParameterType.MovementSpeed, PlayerSetup.Instance.animatorManager));
m_parameters.Add(new AvatarParameter(AvatarParameter.ParameterType.Velocity, PlayerSetup.Instance.animatorManager));
m_parameters.RemoveAll(p => !p.IsValid());

// Avatar custom IK limits
Expand All @@ -153,8 +131,8 @@ void OnAvatarSetup()

if((l_foot != null) && (l_toe != null))
{
Vector3 l_footPos = (PlayerSetup.Instance._avatar.transform.GetMatrix().inverse * l_foot.GetMatrix()) * ms_pointVector;
Vector3 l_toePos = (PlayerSetup.Instance._avatar.transform.GetMatrix().inverse * l_toe.GetMatrix()) * ms_pointVector;
Vector3 l_footPos = (PlayerSetup.Instance._avatar.transform.GetMatrix().inverse * l_foot.GetMatrix()).GetPosition();
Vector3 l_toePos = (PlayerSetup.Instance._avatar.transform.GetMatrix().inverse * l_toe.GetMatrix()).GetPosition();
m_massCenter = new Vector3(0f, 0f, l_toePos.z - l_footPos.z);
}
}
Expand Down Expand Up @@ -192,44 +170,32 @@ void OnAvatarReuse()
// IK events
void OnIKPreSolverUpdate()
{
bool l_locomotionOverride = false;

m_ikState.m_weight = m_vrIk.solver.IKPositionWeight;
m_ikState.m_locomotionWeight = m_vrIk.solver.locomotion.weight;
m_ikState.m_plantFeet = m_vrIk.solver.plantFeet;
m_ikState.m_bendNormalLeft = m_vrIk.solver.leftLeg.useAnimatedBendNormal;
m_ikState.m_bendNormalRight = m_vrIk.solver.rightLeg.useAnimatedBendNormal;

if(Settings.DetectEmotes && m_emoteActive)
m_vrIk.solver.IKPositionWeight = 0f;

if(!BodySystem.isCalibratedAsFullBody)
{
if(BetterBetterCharacterController.Instance.AvatarUpright <= BetterBetterCharacterController.Instance.avatarCrouchLimit)
{
m_vrIk.solver.leftLeg.useAnimatedBendNormal = true;
m_vrIk.solver.rightLeg.useAnimatedBendNormal = true;
l_locomotionOverride = true;
}
if(Settings.IKOverrideFly && BetterBetterCharacterController.Instance.IsFlying())
{
m_vrIk.solver.locomotion.weight = 0f;
m_vrIk.solver.leftLeg.useAnimatedBendNormal = true;
m_vrIk.solver.rightLeg.useAnimatedBendNormal = true;
l_locomotionOverride = true;
}
if(Settings.IKOverrideJump && !m_grounded && !BetterBetterCharacterController.Instance.IsFlying())
if(Settings.IKOverrideJump && !BetterBetterCharacterController.Instance.IsGrounded() && !BetterBetterCharacterController.Instance.IsFlying())
{
m_vrIk.solver.locomotion.weight = 0f;
m_vrIk.solver.leftLeg.useAnimatedBendNormal = true;
m_vrIk.solver.rightLeg.useAnimatedBendNormal = true;
l_locomotionOverride = true;
}
}

if(m_locomotionOverride && !l_locomotionOverride)
m_vrIk.solver.Reset();
m_locomotionOverride = l_locomotionOverride;
}

void OnIKPostSolverUpdate()
Expand Down Expand Up @@ -275,6 +241,12 @@ void UpdateIKLimits()
}

// Parameters access
public bool IsMoving() => m_moving;
public bool IsMoving() => BetterBetterCharacterController.Instance.IsMoving();
public float GetMovementSpeed()
{
AvatarAnimatorManager l_animatorManager = PlayerSetup.Instance.animatorManager;
return Mathf.Sqrt(l_animatorManager.MovementX * l_animatorManager.MovementX + l_animatorManager.MovementY * l_animatorManager.MovementY);
}
public float GetVelocity() => BetterBetterCharacterController.Instance.velocity.magnitude;
}
}
2 changes: 1 addition & 1 deletion ml_amt/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[assembly: MelonLoader.MelonInfo(typeof(ml_amt.AvatarMotionTweaker), "AvatarMotionTweaker", "1.4.1", "SDraw", "https://github.com/SDraw/ml_mods_cvr")]
[assembly: MelonLoader.MelonInfo(typeof(ml_amt.AvatarMotionTweaker), "AvatarMotionTweaker", "1.4.2", "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)]
15 changes: 5 additions & 10 deletions ml_amt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,23 @@ This mod adds features for AAS animator and avatar locomotion behaviour.
# Installation
* Install [latest MelonLoader](https://github.com/LavaGang/MelonLoader)
* Get [latest release DLL](../../../releases/latest):
* Put `ml_amt.dll` in `Mods` folder of game
* Put `AvatarMotionTweaker.dll` in `Mods` folder of game

# Usage
Available mod's settings in `Settings - IK - Avatar Motion Tweaker`:
* **Crouch limit:** defines crouch limit; default value - `75`.
* **Prone limit:** defines prone limit; default value - `40`.
* **IK override while flying:** disables legs locomotion/autostep in fly mode; default value - `true`.
* **IK override while jumping:** disables legs locomotion/autostep in jump; default value - `true`.
* **Detect animations emote tag:** disables avatar's IK entirely if current animator state has `Emote` tag; default value - `true`.
* Note: Created as example for [propoused game feature](https://feedback.abinteractive.net/p/disabling-vr-ik-for-emotes-via-animator-state-tag-7b80d963-053a-41c0-86ac-e3d53c61c1e2).
* **Adjusted locomotion mass center:** automatically changes IK locomotion center if avatar has toe bones; default value - `true`.
* Note: Compatible with [DesktopVRIK](https://github.com/NotAKidOnSteam/DesktopVRIK) and [FuckToes](https://github.com/NotAKidOnSteam/FuckToes).

Available additional parameters for AAS animator:
* **`Upright`:** defines linear coefficient between current viewpoint height and avatar's viewpoint height; float, range - [0.0, 1.0].
* Note: Can be set as local-only (not synced) if starts with `#` character.
* Note: Shouldn't be used for transitions between poses in desktop mode. In desktop mode its value is driven by avatar animations. Use `CVR Parameter Stream` for detecting desktop/VR modes and change AAS animator transitions accordingly.
* **`GroundedRaw`:** defines instant grounding state of player instead of delayed default parameter `Grounded`; boolean.
* Note: Can be set as local-only (not synced) if starts with `#` character.
* **`Moving`:** defines movement state of player; boolean.
* Note: Can be set as local-only (not synced) if starts with `#` character.
* **`MovementSpeed`:** length of vector made of default `MovementX` and `MovementY` parameters
* **`Velocity`:** current player's movement velocity in space

Parameters can be set as local-only (not synced) if start with `#` character.

Additional mod's behaviour:
* Overrides and fixes IK behaviour in 4PT mode (head, hands and hips).
* Avatars can have controlled IK crouch and prone limits. For that create `[IKLimits]` GameObject parented to avatar's root. Its local X and Y positions will be used as crouch and prone limits respectively and can be changed via animations. Values should be in range of [0;1].
11 changes: 0 additions & 11 deletions ml_amt/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ enum ModSetting
ProneLimit,
IKOverrideFly,
IKOverrideJump,
DetectEmotes,
MassCenter
};

public static float CrouchLimit { get; private set; } = 0.75f;
public static float ProneLimit { get; private set; } = 0.4f;
public static bool IKOverrideFly { get; private set; } = true;
public static bool IKOverrideJump { get; private set; } = true;
public static bool DetectEmotes { get; private set; } = true;
public static bool MassCenter { get; private set; } = true;

static MelonLoader.MelonPreferences_Category ms_category = null;
Expand All @@ -51,15 +49,13 @@ internal static void Init()
ms_category.CreateEntry(ModSetting.ProneLimit.ToString(), (int)(ProneLimit * 100f)),
ms_category.CreateEntry(ModSetting.IKOverrideFly.ToString(), IKOverrideFly),
ms_category.CreateEntry(ModSetting.IKOverrideJump.ToString(), IKOverrideJump),
ms_category.CreateEntry(ModSetting.DetectEmotes.ToString(), DetectEmotes),
ms_category.CreateEntry(ModSetting.MassCenter.ToString(), MassCenter)
};

CrouchLimit = ((int)ms_entries[(int)ModSetting.CrouchLimit].BoxedValue) * 0.01f;
ProneLimit = ((int)ms_entries[(int)ModSetting.ProneLimit].BoxedValue) * 0.01f;
IKOverrideFly = (bool)ms_entries[(int)ModSetting.IKOverrideFly].BoxedValue;
IKOverrideJump = (bool)ms_entries[(int)ModSetting.IKOverrideJump].BoxedValue;
DetectEmotes = (bool)ms_entries[(int)ModSetting.DetectEmotes].BoxedValue;
MassCenter = (bool)ms_entries[(int)ModSetting.MassCenter].BoxedValue;

MelonLoader.MelonCoroutines.Start(WaitMainMenuUi());
Expand Down Expand Up @@ -142,13 +138,6 @@ static void OnToggleUpdate(string p_name, string p_value)
}
break;

case ModSetting.DetectEmotes:
{
DetectEmotes = l_value;
OnDetectEmotesChanged.Invoke(DetectEmotes);
}
break;

case ModSetting.MassCenter:
{
MassCenter = l_value;
Expand Down
6 changes: 3 additions & 3 deletions ml_amt/ml_amt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Authors>SDraw</Authors>
<Company>None</Company>
<Company>SDraw</Company>
<Product>AvatarMotionTweaker</Product>
<PackageId>AvatarMotionTweaker</PackageId>
<Version>1.4.1</Version>
<Version>1.4.2</Version>
<Platforms>x64</Platforms>
<AssemblyName>ml_amt</AssemblyName>
<AssemblyName>AvatarMotionTweaker</AssemblyName>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand Down
34 changes: 18 additions & 16 deletions ml_bft/FingerSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,25 @@ internal void OnAvatarSetup()
Utils.SetAvatarTPose();
InputHandler.Instance.Rebind(PlayerSetup.Instance.transform.rotation);

// Try to "fix" rotations of fingers
foreach(var l_tuple in ms_rotationFixChains)
if(Settings.FixFingers)
{
ReorientateTowards(
PlayerSetup.Instance._animator.GetBoneTransform(l_tuple.Item1),
PlayerSetup.Instance._animator.GetBoneTransform(l_tuple.Item2),
InputHandler.Instance.GetSourceForBone(l_tuple.Item1, l_tuple.Item3),
InputHandler.Instance.GetSourceForBone(l_tuple.Item2, l_tuple.Item3),
PlaneType.OXZ
);
ReorientateTowards(
PlayerSetup.Instance._animator.GetBoneTransform(l_tuple.Item1),
PlayerSetup.Instance._animator.GetBoneTransform(l_tuple.Item2),
InputHandler.Instance.GetSourceForBone(l_tuple.Item1, l_tuple.Item3),
InputHandler.Instance.GetSourceForBone(l_tuple.Item2, l_tuple.Item3),
PlaneType.OYX
);
foreach(var l_tuple in ms_rotationFixChains)
{
ReorientateTowards(
PlayerSetup.Instance._animator.GetBoneTransform(l_tuple.Item1),
PlayerSetup.Instance._animator.GetBoneTransform(l_tuple.Item2),
InputHandler.Instance.GetSourceForBone(l_tuple.Item1, l_tuple.Item3),
InputHandler.Instance.GetSourceForBone(l_tuple.Item2, l_tuple.Item3),
PlaneType.OXZ
);
ReorientateTowards(
PlayerSetup.Instance._animator.GetBoneTransform(l_tuple.Item1),
PlayerSetup.Instance._animator.GetBoneTransform(l_tuple.Item2),
InputHandler.Instance.GetSourceForBone(l_tuple.Item1, l_tuple.Item3),
InputHandler.Instance.GetSourceForBone(l_tuple.Item2, l_tuple.Item3),
PlaneType.OYX
);
}
}

// Bind hands
Expand Down
Loading

0 comments on commit 4555794

Please sign in to comment.