Skip to content

Commit

Permalink
New mod: AdditionalAvatarParameters
Browse files Browse the repository at this point in the history
  • Loading branch information
SDraw committed Aug 5, 2022
1 parent ba56c11 commit df96194
Show file tree
Hide file tree
Showing 16 changed files with 610 additions and 46 deletions.
57 changes: 57 additions & 0 deletions ml_aap/Main.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using ABI_RC.Core.Player;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ml_aap
{
public class AdditionalAvatarParameters : MelonLoader.MelonMod
{
static AdditionalAvatarParameters ms_instance = null;

ParametersHandler m_localHandler = null;

public override void OnApplicationStart()
{
ms_instance = this;

HarmonyInstance.Patch(
typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.ClearAvatar)),
null,
new HarmonyLib.HarmonyMethod(typeof(AdditionalAvatarParameters).GetMethod(nameof(OnLocalAvatarClear_Postfix), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static))
);

HarmonyInstance.Patch(
typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.SetupAvatar)),
null,
new HarmonyLib.HarmonyMethod(typeof(AdditionalAvatarParameters).GetMethod(nameof(OnLocalAvatarSetup_Postfix), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static))
);

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

System.Collections.IEnumerator WaitForLocalPlayer()
{
while(PlayerSetup.Instance == null)
yield return null;

m_localHandler = PlayerSetup.Instance.gameObject.AddComponent<ParametersHandler>();
}

static void OnLocalAvatarClear_Postfix() => ms_instance?.OnLocalAvatarClear();
void OnLocalAvatarClear()
{
if(m_localHandler != null)
m_localHandler.OnAvatarClear();
}

static void OnLocalAvatarSetup_Postfix() => ms_instance?.OnLocalAvatarSetup();
void OnLocalAvatarSetup()
{
if(m_localHandler != null)
m_localHandler.OnAvatarSetup();
}
}
}
229 changes: 229 additions & 0 deletions ml_aap/ParametersHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
using ABI_RC.Core;
using ABI_RC.Core.Player;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace ml_aap
{
class ParametersHandler : MonoBehaviour
{
enum AdditionalParameter
{
Upright,
Viseme,
Voice,
Muted,
InVR,
InHmd,
InFBT,
Zoom
}
enum AdditionalParameterSync
{
Local,
Synced
}

struct AdditionalParameterInfo
{
public AdditionalParameter m_type;
public AdditionalParameterSync m_sync;
public string m_name;
public int m_hash; // For local only
}

static readonly Vector4 ms_pointVector = new Vector4(0f, 0f, 0f, 1f);
static readonly System.Reflection.FieldInfo ms_visemeWeights = typeof(CVRVisemeController).GetField("visemeWeights", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

readonly List<AdditionalParameterInfo> m_parameters = null;
bool m_active = false;

CVRVisemeController m_visemeController = null;

public ParametersHandler()
{
m_parameters = new List<AdditionalParameterInfo>();
}

void Update()
{
if(m_active)
{
foreach(AdditionalParameterInfo l_param in m_parameters)
{
switch(l_param.m_type)
{
case AdditionalParameter.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.GetAvatarScale().y, 0f, float.MaxValue);
float l_currentUpright = Mathf.Clamp((((l_currentHeight > 0f) && (l_avatarViewHeight > 0f)) ? (l_currentHeight / l_avatarViewHeight) : 0f), 0f, 1f);

switch(l_param.m_sync)
{
case AdditionalParameterSync.Local:
PlayerSetup.Instance._animator.SetFloat(l_param.m_hash, l_currentUpright);
break;
case AdditionalParameterSync.Synced:
PlayerSetup.Instance.changeAnimatorParam(l_param.m_name, l_currentUpright);
break;
}
}
break;

case AdditionalParameter.Viseme:
{
float[] l_weights = (float[])ms_visemeWeights?.GetValue(m_visemeController);
if(l_weights != null)
{
int l_index = 0;
float l_maxWeight = 0f;

for(int i = 0; i < l_weights.Length; i++)
{
if(l_maxWeight < l_weights[i])
{
l_maxWeight = l_weights[i];
l_index = i;
}
}

switch(l_param.m_sync)
{
case AdditionalParameterSync.Local:
PlayerSetup.Instance._animator.SetInteger(l_param.m_hash, l_index);
break;
case AdditionalParameterSync.Synced:
PlayerSetup.Instance.changeAnimatorParam(l_param.m_name, l_index);
break;
}
}
}
break;

case AdditionalParameter.Voice:
{
switch(l_param.m_sync)
{
case AdditionalParameterSync.Local:
PlayerSetup.Instance._animator.SetFloat(l_param.m_hash, m_visemeController.visemeLoudness);
break;
case AdditionalParameterSync.Synced:
PlayerSetup.Instance.changeAnimatorParam(l_param.m_name, m_visemeController.visemeLoudness);
break;
}
}
break;

case AdditionalParameter.InVR:
{
switch(l_param.m_sync)
{
case AdditionalParameterSync.Local:
PlayerSetup.Instance._animator.SetBool(l_param.m_hash, PlayerSetup.Instance._inVr);
break;
case AdditionalParameterSync.Synced:
PlayerSetup.Instance.changeAnimatorParam(l_param.m_name, PlayerSetup.Instance._inVr ? 1f : 0f);
break;
}
}
break;

case AdditionalParameter.InHmd:
{
switch(l_param.m_sync)
{
case AdditionalParameterSync.Local:
PlayerSetup.Instance._animator.SetBool(l_param.m_hash, PlayerSetup.Instance._trackerManager.headsetOnHead);
break;
case AdditionalParameterSync.Synced:
PlayerSetup.Instance.changeAnimatorParam(l_param.m_name, PlayerSetup.Instance._trackerManager.headsetOnHead ? 1f : 0f);
break;
}
} break;

case AdditionalParameter.InFBT:
{
switch(l_param.m_sync)
{
case AdditionalParameterSync.Local:
PlayerSetup.Instance._animator.SetBool(l_param.m_hash, PlayerSetup.Instance.fullBodyActive);
break;
case AdditionalParameterSync.Synced:
PlayerSetup.Instance.changeAnimatorParam(l_param.m_name, PlayerSetup.Instance.fullBodyActive ? 1f : 0f);
break;
}
} break;

case AdditionalParameter.Muted:
{
switch(l_param.m_sync)
{
case AdditionalParameterSync.Local:
PlayerSetup.Instance._animator.SetBool(l_param.m_hash, RootLogic.Instance.comms.IsMuted);
break;
case AdditionalParameterSync.Synced:
PlayerSetup.Instance.changeAnimatorParam(l_param.m_name, RootLogic.Instance.comms.IsMuted ? 1f : 0f);
break;
}
}
break;

case AdditionalParameter.Zoom:
{
switch(l_param.m_sync)
{
case AdditionalParameterSync.Local:
PlayerSetup.Instance._animator.SetFloat(l_param.m_hash, CVR_DesktopCameraController.currentZoomProgress);
break;
case AdditionalParameterSync.Synced:
PlayerSetup.Instance.changeAnimatorParam(l_param.m_name, CVR_DesktopCameraController.currentZoomProgress);
break;
}
} break;
}
}
}
}

public void OnAvatarClear()
{
m_parameters.Clear();
m_active = false;
m_visemeController = null;
}

public void OnAvatarSetup()
{
m_visemeController = PlayerSetup.Instance._animator.GetComponent<CVRVisemeController>();

AnimatorControllerParameter[] l_params = PlayerSetup.Instance._animator.parameters;
AdditionalParameter[] l_enumParams = (AdditionalParameter[])Enum.GetValues(typeof(AdditionalParameter));

foreach(var l_param in l_params)
{
foreach(var l_enumParam in l_enumParams)
{
if(l_param.name.Contains(l_enumParam.ToString()) && (m_parameters.FindIndex(p => p.m_type == l_enumParam) == -1))
{
bool l_local = (l_param.name[0] == '#');

m_parameters.Add(new AdditionalParameterInfo
{
m_type = l_enumParam,
m_sync = (l_local ? AdditionalParameterSync.Local : AdditionalParameterSync.Synced),
m_name = l_param.name,
m_hash = (l_local ? l_param.nameHash : 0)
});

break;
}
}
}

m_active = (m_parameters.Count > 0);
}
}
}
10 changes: 10 additions & 0 deletions ml_aap/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Reflection;

[assembly: AssemblyTitle("AdditionalAvatarParameters")]
[assembly: AssemblyVersion("1.0.0")]
[assembly: AssemblyFileVersion("1.0.0")]

[assembly: MelonLoader.MelonInfo(typeof(ml_aap.AdditionalAvatarParameters), "AdditionalAvatarParameters", "1.0.0", "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)]
24 changes: 24 additions & 0 deletions ml_aap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Additional Avatar Parameters
This mod adds additional paramaters for usage in avatar's animator.

# Installation
* Install [latest MelonLoader](https://github.com/LavaGang/MelonLoader)
* Get [latest release DLL](../../../releases/latest):
* Put `ml_aap.dll` in `Mods` folder of game

# Usage
List of new available parameters:
| Name | Type | Note |
|------|------|------|
| Upright | float | Proportion value between avatar's viewpoint height and floor, ranged in [0,1] |
| Viseme | int | Most active viseme index, ranged in [0,14], doesn't update in offline rooms |
| Voice | float | Voice level, ranged in [0,1], doesn't update in offline rooms |
| Muted | bool | Indicates if microphone is muted or unmuted |
| InVR | bool | Indicates if player is in VR |
| InHmd | bool | Indicates if players' headset is on head, can vary between different VR headsets |
| InFBT | bool | Indicates if player is in full body tracking mode |
| Zoom | float | Zoom level of camera, ranged in [0,1], desktop only |

# Notes
* All new parameters use additional sync data besides listed in avatar's advanced settings.
* If character `#` is added at start of parameter's name it will be interpreted as local-only, won't be synced over network and won't use additional sync data.
13 changes: 13 additions & 0 deletions ml_aap/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using UnityEngine;

namespace ml_aap
{
static class Utils
{
// Extensions
public static Matrix4x4 GetMatrix(this Transform p_transform, bool p_pos = true, bool p_rot = true, bool p_scl = false)
{
return Matrix4x4.TRS(p_pos ? p_transform.position : Vector3.zero, p_rot ? p_transform.rotation : Quaternion.identity, p_scl ? p_transform.localScale : Vector3.one);
}
}
}
Loading

0 comments on commit df96194

Please sign in to comment.