Skip to content

Commit

Permalink
Tracking modes
Browse files Browse the repository at this point in the history
  • Loading branch information
SDraw committed Jun 9, 2022
1 parent 6b8d7a7 commit 04fd9b3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 45 deletions.
88 changes: 58 additions & 30 deletions ml_lme_cvr/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace ml_lme_cvr
public class LeapMotionExtension : MelonLoader.MelonMod
{
static readonly Quaternion ms_hmdRotationFix = new Quaternion(0f, 0.7071068f, 0.7071068f, 0f);
static readonly Quaternion ms_screentopRotationFix = new Quaternion(0f, 0f, -1f, 0f);

static LeapMotionExtension ms_instance = null;

Expand All @@ -31,7 +32,7 @@ public override void OnApplicationStart()
Settings.DesktopOffsetChange += this.OnSettingsDesktopOffsetChange;
Settings.FingersOnlyChange += this.OnSettingsFingersOptionChange;
Settings.ModelVisibilityChange += this.OnSettingsModelVisibilityChange;
Settings.HmdModeChange += this.OnSettingsHmdModeChange;
Settings.TrackingModeChange += this.OnSettingsTrackingModeChange;
Settings.RootAngleChange += this.OnSettingsRootAngleChange;
Settings.HeadAttachChange += this.OnSettingsHeadAttachChange;
Settings.HeadOffsetChange += this.OnSettingsHeadOffsetChange;
Expand Down Expand Up @@ -95,7 +96,7 @@ System.Collections.IEnumerator CreateTrackingObjects()
OnSettingsEnableChange();
OnSettingsFingersOptionChange();
OnSettingsModelVisibilityChange();
OnSettingsHmdModeChange();
OnSettingsTrackingModeChange();
OnSettingsHeadAttachChange(); // Includes offsets and parenting
}

Expand All @@ -119,7 +120,7 @@ public override void OnUpdate()
{
Vector3 l_pos = m_gesturesData.m_handsPositons[i];
Quaternion l_rot = m_gesturesData.m_handsRotations[i];
ReorientateLeapToUnity(ref l_pos, ref l_rot, Settings.HmdMode);
ReorientateLeapToUnity(ref l_pos, ref l_rot, Settings.TrackingMode);
m_leapHands[i].transform.localPosition = l_pos;
m_leapHands[i].transform.localRotation = l_rot;
}
Expand All @@ -138,11 +139,7 @@ void OnSettingsEnableChange()
if(Settings.Enabled)
{
m_leapController.StartConnection();
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_SCREENTOP, null);
if(Settings.HmdMode)
m_leapController.SetPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD, null);
else
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD, null);
UpdateDeviceTrackingMode();
}
else
m_leapController.StopConnection();
Expand Down Expand Up @@ -174,19 +171,26 @@ void OnSettingsModelVisibilityChange()
m_leapControllerModel.SetActive(Settings.ModelVisibility);
}

void OnSettingsHmdModeChange()
void OnSettingsTrackingModeChange()
{
if(m_leapController != null)
{
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_SCREENTOP, null);
if(Settings.HmdMode)
m_leapController.SetPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD, null);
else
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD, null);
}
if(Settings.Enabled && (m_leapController != null))
UpdateDeviceTrackingMode();

if(m_leapControllerModel != null)
m_leapControllerModel.transform.localRotation = (Settings.HmdMode ? Quaternion.Euler(270f, 180f, 0f) : Quaternion.identity);
{
switch(Settings.TrackingMode)
{
case Settings.LeapTrackingMode.Screentop:
m_leapControllerModel.transform.localRotation = Quaternion.Euler(0f, 0f, 180f);
break;
case Settings.LeapTrackingMode.Desktop:
m_leapControllerModel.transform.localRotation = Quaternion.identity;
break;
case Settings.LeapTrackingMode.HMD:
m_leapControllerModel.transform.localRotation = Quaternion.Euler(270f, 180f, 0f);
break;
}
}
}

void OnSettingsRootAngleChange()
Expand Down Expand Up @@ -245,17 +249,28 @@ void OnSettingsHeadOffsetChange()
}
}

// Leap events
void OnLeapDeviceInitialized(object p_sender, Leap.DeviceEventArgs p_args)
// Internal utility
void UpdateDeviceTrackingMode()
{
if(Settings.Enabled && (m_leapController != null))
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_SCREENTOP, null);
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD, null);

switch(Settings.TrackingMode)
{
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_SCREENTOP, null);
if(Settings.HmdMode)
case Settings.LeapTrackingMode.Screentop:
m_leapController.SetPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_SCREENTOP, null);
break;
case Settings.LeapTrackingMode.HMD:
m_leapController.SetPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD, null);
else
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD, null);
break;
}
}

// Leap events
void OnLeapDeviceInitialized(object p_sender, Leap.DeviceEventArgs p_args)
{
if(Settings.Enabled && (m_leapController != null))
UpdateDeviceTrackingMode();

if(CohtmlHud.Instance != null)
CohtmlHud.Instance.ViewDropText("Leap Motion Extension", "Device initialized");
Expand Down Expand Up @@ -305,18 +320,31 @@ void OnLocalPlayerAvatarSetup(Animator p_animator, IndexIK p_indexIK)
OnSettingsHeadAttachChange();
}

static void ReorientateLeapToUnity(ref Vector3 p_pos, ref Quaternion p_rot, bool p_hmd)
// Utilities
static void ReorientateLeapToUnity(ref Vector3 p_pos, ref Quaternion p_rot, Settings.LeapTrackingMode p_mode)
{
p_pos *= 0.001f;
p_pos.z *= -1f;
p_rot.x *= -1f;
p_rot.y *= -1f;

if(p_hmd)
switch(p_mode)
{
p_pos.x *= -1f;
Utils.Swap(ref p_pos.y, ref p_pos.z);
p_rot = (ms_hmdRotationFix * p_rot);
case Settings.LeapTrackingMode.Screentop:
{
p_pos.x *= -1f;
p_pos.y *= -1f;
p_rot = (ms_screentopRotationFix * p_rot);
}
break;

case Settings.LeapTrackingMode.HMD:
{
p_pos.x *= -1f;
Utils.Swap(ref p_pos.y, ref p_pos.z);
p_rot = (ms_hmdRotationFix * p_rot);
}
break;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions ml_lme_cvr/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Reflection;

[assembly: AssemblyTitle("LeapMotionExtension")]
[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_lme_cvr.LeapMotionExtension), "LeapMotionExtension", "1.0.6", "SDraw", "https://github.com/SDraw/ml_mods_cvr")]
[assembly: MelonLoader.MelonInfo(typeof(ml_lme_cvr.LeapMotionExtension), "LeapMotionExtension", "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)]
2 changes: 1 addition & 1 deletion ml_lme_cvr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This mod allows you to use your Leap Motion controller for hands and fingers vis
## Settings
Available mod's settings in `Settings - Implementation - Leap Motion Tracking`:
* **Enable tracking:** enable hands tracking from Leap Motion data, disabled by default.
* **HMD mode:** force Leap Motion to use head-mounted orientation mode, disabled by default.
* **Tracking mode:** set Leap Motion tracking mode, available values: `Screentop`, `Desktop` (by default), `HMD`.
* **Desktop offset X/Y/Z:** offset position for body attachment, (0, -45, 30) by default.
* **Attach to head:** attach hands transformation to head instead of body, disabled by default.
* **Head offset X/Y/Z:** offset position for head attachment (`Attach to head` is **`true`**), (0, -30, 15) by default.
Expand Down
29 changes: 18 additions & 11 deletions ml_lme_cvr/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ namespace ml_lme_cvr
{
static class Settings
{
public enum LeapTrackingMode
{
Screentop = 0,
Desktop,
HMD
}

public static readonly string[] ms_defaultSettings =
{
"InteractionLeapMotionTracking",
Expand All @@ -14,7 +21,7 @@ static class Settings
"InteractionLeapMotionTrackingDesktopZ",
"InteractionLeapMotionTrackingFingersOnly",
"InteractionLeapMotionTrackingModel",
"InteractionLeapMotionTrackingHmd",
"InteractionLeapMotionTrackingMode",
"InteractionLeapMotionTrackingAngle",
"InteractionLeapMotionTrackingHead",
"InteractionLeapMotionTrackingHeadX",
Expand All @@ -26,7 +33,7 @@ static class Settings
static Vector3 ms_desktopOffset = new Vector3(0f, -0.45f, 0.3f);
static bool ms_fingersOnly = false;
static bool ms_modelVisibility = false;
static bool ms_hmdMode = false;
static LeapTrackingMode ms_trackingMode = LeapTrackingMode.Desktop;
static float ms_rootAngle = 0f;
static bool ms_headAttach = false;
static Vector3 ms_headOffset = new Vector3(0f, -0.3f, 0.15f);
Expand All @@ -37,7 +44,7 @@ static class Settings
static public event Action DesktopOffsetChange;
static public event Action FingersOnlyChange;
static public event Action ModelVisibilityChange;
static public event Action HmdModeChange;
static public event Action TrackingModeChange;
static public event Action RootAngleChange;
static public event Action HeadAttachChange;
static public event Action HeadOffsetChange;
Expand All @@ -64,7 +71,7 @@ static void BeforeSettingsLoad(ref CVRSettings __instance)
l_settings.Add(new CVRSettingsInt(ms_defaultSettings[3], 30));
l_settings.Add(new CVRSettingsBool(ms_defaultSettings[4], false));
l_settings.Add(new CVRSettingsBool(ms_defaultSettings[5], false));
l_settings.Add(new CVRSettingsBool(ms_defaultSettings[6], false));
l_settings.Add(new CVRSettingsInt(ms_defaultSettings[6], 1));
l_settings.Add(new CVRSettingsInt(ms_defaultSettings[7], 0));
l_settings.Add(new CVRSettingsBool(ms_defaultSettings[8], false));
l_settings.Add(new CVRSettingsInt(ms_defaultSettings[9], 0));
Expand Down Expand Up @@ -120,13 +127,13 @@ static void BeforeSettingsLoad(ref CVRSettings __instance)
}
});

// HMD mode
__instance.settingBoolChanged.AddListener((name, value) =>
// Tracking mode
__instance.settingIntChanged.AddListener((name, value) =>
{
if(name == ms_defaultSettings[6])
{
ms_hmdMode = value;
HmdModeChange?.Invoke();
ms_trackingMode = (LeapTrackingMode)value;
TrackingModeChange?.Invoke();
}
});

Expand Down Expand Up @@ -182,7 +189,7 @@ static public void Reload()
) * 0.01f;
ms_fingersOnly = MetaPort.Instance.settings.GetSettingsBool(ms_defaultSettings[4]);
ms_modelVisibility = MetaPort.Instance.settings.GetSettingsBool(ms_defaultSettings[5]);
ms_hmdMode = MetaPort.Instance.settings.GetSettingsBool(ms_defaultSettings[6]);
ms_trackingMode = (LeapTrackingMode)MetaPort.Instance.settings.GetSettingInt(ms_defaultSettings[6]);
ms_rootAngle = MetaPort.Instance.settings.GetSettingInt(ms_defaultSettings[7]);
ms_headAttach = MetaPort.Instance.settings.GetSettingsBool(ms_defaultSettings[8]);
ms_headOffset = new Vector3(
Expand Down Expand Up @@ -212,9 +219,9 @@ public static bool ModelVisibility
get => ms_modelVisibility;
}

public static bool HmdMode
public static LeapTrackingMode TrackingMode
{
get => ms_hmdMode;
get => ms_trackingMode;
}

public static float RootAngle
Expand Down

0 comments on commit 04fd9b3

Please sign in to comment.