[SmoothRay] Fixes for 2023r171

This commit is contained in:
NotAKidoS 2023-07-28 22:29:24 -05:00
parent a44354502f
commit d67c45023f
5 changed files with 110 additions and 77 deletions

View file

@ -3,11 +3,11 @@ using HarmonyLib;
namespace NAK.SmoothRay.HarmonyPatches; namespace NAK.SmoothRay.HarmonyPatches;
class PlayerSetupPatches internal class PlayerSetupPatches
{ {
[HarmonyPostfix] [HarmonyPostfix]
[HarmonyPatch(typeof(PlayerSetup), nameof(PlayerSetup.Start))] [HarmonyPatch(typeof(PlayerSetup), nameof(PlayerSetup.Start))]
static void Post_PlayerSetup_Start(ref PlayerSetup __instance) private static void Post_PlayerSetup_Start(ref PlayerSetup __instance)
{ {
__instance.vrLeftHandTracker.gameObject.AddComponent<SmoothRayer>().ray = __instance.leftRay; __instance.vrLeftHandTracker.gameObject.AddComponent<SmoothRayer>().ray = __instance.leftRay;
__instance.vrRightHandTracker.gameObject.AddComponent<SmoothRayer>().ray = __instance.rightRay; __instance.vrRightHandTracker.gameObject.AddComponent<SmoothRayer>().ray = __instance.rightRay;

View file

@ -36,7 +36,7 @@ public class SmoothRay : MelonMod
ApplyPatches(typeof(HarmonyPatches.PlayerSetupPatches)); ApplyPatches(typeof(HarmonyPatches.PlayerSetupPatches));
} }
void ApplyPatches(Type type) private void ApplyPatches(Type type)
{ {
try try
{ {

View file

@ -20,11 +20,14 @@ using System.Reflection;
[assembly: MelonGame("Alpha Blend Interactive", "ChilloutVR")] [assembly: MelonGame("Alpha Blend Interactive", "ChilloutVR")]
[assembly: MelonPlatform(MelonPlatformAttribute.CompatiblePlatforms.WINDOWS_X64)] [assembly: MelonPlatform(MelonPlatformAttribute.CompatiblePlatforms.WINDOWS_X64)]
[assembly: MelonPlatformDomain(MelonPlatformDomainAttribute.CompatibleDomains.MONO)] [assembly: MelonPlatformDomain(MelonPlatformDomainAttribute.CompatibleDomains.MONO)]
[assembly: MelonColor(255, 220, 130, 5)]
[assembly: MelonAuthorColor(255, 158, 21, 32)]
[assembly: HarmonyDontPatchAll] [assembly: HarmonyDontPatchAll]
namespace NAK.SmoothRay.Properties; namespace NAK.SmoothRay.Properties;
internal static class AssemblyInfoParams internal static class AssemblyInfoParams
{ {
public const string Version = "1.0.1"; public const string Version = "1.0.2";
public const string Author = "NotAKidoS"; public const string Author = "NotAKidoS";
} }

View file

@ -23,6 +23,8 @@
**/ **/
using ABI_RC.Core.InteractionSystem; using ABI_RC.Core.InteractionSystem;
using ABI_RC.Core.Savior;
using MelonLoader;
using UnityEngine; using UnityEngine;
using UnityEngine.Events; using UnityEngine.Events;
using Valve.VR; using Valve.VR;
@ -31,118 +33,146 @@ namespace NAK.SmoothRay;
public class SmoothRayer : MonoBehaviour public class SmoothRayer : MonoBehaviour
{ {
#region Variables
public ControllerRay ray; public ControllerRay ray;
//settings //settings
bool isEnabled; private bool _isEnabled;
bool menuOnly; private bool _menuOnly;
float positionSmoothingValue; private float _positionSmoothingValue;
float rotationSmoothingValue; private float _rotationSmoothingValue;
float smallMovementThresholdAngle; private float _smallMovementThresholdAngle;
//internal //internal
Vector3 smoothedPosition = Vector3.zero; private Vector3 _smoothedPosition = Vector3.zero;
Quaternion smoothedRotation = Quaternion.identity; private Quaternion _smoothedRotation = Quaternion.identity;
float angleVelocitySnap = 1f; private float _angleVelocitySnap = 1f;
//native & trackedcontrollerfix stuff //native & trackedcontrollerfix stuff
SteamVR_Behaviour_Pose pose; private SteamVR_Behaviour_Pose _behaviourPose;
SteamVR_TrackedObject tracked; private SteamVR_TrackedObject _trackedObject;
SteamVR_Events.Action newPosesAction = null; private SteamVR_Events.Action _newPosesAction;
void Start() #endregion
#region Unity Methods
private void Start()
{ {
// native CVR // Native ChilloutVR - OpenVR
pose = GetComponent<SteamVR_Behaviour_Pose>(); if (TryGetComponent(out _behaviourPose))
if (pose != null) UpdateTransformUpdatedEvent(true);
pose.onTransformUpdatedEvent += OnTransformUpdated;
// trackedcontrollerfix support // TrackedControllerFix support - OpenVR
tracked = GetComponent<SteamVR_TrackedObject>(); if (TryGetComponent(out _trackedObject))
if (tracked != null)
{ {
newPosesAction = SteamVR_Events.NewPosesAppliedAction(new UnityAction(OnAppliedPoses)); _newPosesAction = SteamVR_Events.NewPosesAppliedAction(new UnityAction(OnAppliedPoses));
newPosesAction.enabled = true; UpdatePosesAction(true);
} }
foreach (var setting in SmoothRay.Category.Entries) foreach (MelonPreferences_Entry setting in SmoothRay.Category.Entries)
{
setting.OnEntryValueChangedUntyped.Subscribe(OnUpdateSettings); setting.OnEntryValueChangedUntyped.Subscribe(OnUpdateSettings);
}
OnUpdateSettings(null, null); OnUpdateSettings(null, null);
} }
void OnEnable() private void OnEnable()
{ {
smoothedPosition = transform.localPosition; _smoothedPosition = transform.localPosition;
smoothedRotation = transform.localRotation; _smoothedRotation = transform.localRotation;
// desktopvrswitch support, start handles this for normal use // desktopvrswitch support, start handles this for normal use
if (pose != null) UpdateTransformUpdatedEvent(true);
pose.onTransformUpdatedEvent += OnTransformUpdated; UpdatePosesAction(true);
if (tracked != null && newPosesAction != null)
newPosesAction.enabled = true;
} }
void OnDisable() private void OnDisable()
{ {
smoothedPosition = transform.localPosition; _smoothedPosition = transform.localPosition;
smoothedRotation = transform.localRotation; _smoothedRotation = transform.localRotation;
// desktopvrswitch support, normal use wont run this // desktopvrswitch support, normal use wont run this
if (pose != null) UpdateTransformUpdatedEvent(false);
pose.onTransformUpdatedEvent -= OnTransformUpdated; UpdatePosesAction(false);
if (tracked != null && newPosesAction != null)
newPosesAction.enabled = false;
} }
void OnUpdateSettings(object arg1, object arg2) #endregion
#region Private Methods
private void UpdatePosesAction(bool enable)
{ {
isEnabled = SmoothRay.EntryEnabled.Value; if (enable && CheckVR.Instance.forceOpenXr)
menuOnly = SmoothRay.EntryMenuOnly.Value; return;
smallMovementThresholdAngle = SmoothRay.EntrySmallMovementThresholdAngle.Value;
if (_trackedObject != null && _newPosesAction != null)
_newPosesAction.enabled = enable;
}
private void UpdateTransformUpdatedEvent(bool enable)
{
if (enable && CheckVR.Instance.forceOpenXr)
return;
if (_behaviourPose == null)
return;
if (enable)
_behaviourPose.onTransformUpdatedEvent += OnTransformUpdated;
else
_behaviourPose.onTransformUpdatedEvent -= OnTransformUpdated;
}
private void OnUpdateSettings(object arg1, object arg2)
{
_isEnabled = SmoothRay.EntryEnabled.Value;
_menuOnly = SmoothRay.EntryMenuOnly.Value;
_smallMovementThresholdAngle = SmoothRay.EntrySmallMovementThresholdAngle.Value;
// dont let value hit 0, itll freeze controllers // dont let value hit 0, itll freeze controllers
positionSmoothingValue = Math.Max(20f - Mathf.Clamp(SmoothRay.EntryPositionSmoothing.Value, 0f, 20f), 0.1f); _positionSmoothingValue = Math.Max(20f - Mathf.Clamp(SmoothRay.EntryPositionSmoothing.Value, 0f, 20f), 0.1f);
rotationSmoothingValue = Math.Max(20f - Mathf.Clamp(SmoothRay.EntryRotationSmoothing.Value, 0f, 20f), 0.1f); _rotationSmoothingValue = Math.Max(20f - Mathf.Clamp(SmoothRay.EntryRotationSmoothing.Value, 0f, 20f), 0.1f);
} }
void OnAppliedPoses() => SmoothTransform(); private void OnAppliedPoses() => SmoothTransform();
void OnTransformUpdated(SteamVR_Behaviour_Pose pose, SteamVR_Input_Sources inputSource) => SmoothTransform();
private void OnTransformUpdated(SteamVR_Behaviour_Pose pose, SteamVR_Input_Sources inputSource) => SmoothTransform();
void SmoothTransform()
private void SmoothTransform()
{ {
if (isEnabled && ray.lineRenderer != null && ray.lineRenderer.enabled) if (_isEnabled && ray.lineRenderer != null && ray.lineRenderer.enabled)
{ {
if (menuOnly && (!ray.uiActive || (ray.hitTransform != ViewManager.Instance.transform && ray.hitTransform != CVR_MenuManager.Instance.quickMenu.transform))) if (_menuOnly && (!ray.uiActive || (ray.hitTransform != ViewManager.Instance.transform && ray.hitTransform != CVR_MenuManager.Instance.quickMenu.transform)))
{ {
return; return;
} }
var angDiff = Quaternion.Angle(smoothedRotation, transform.localRotation); var angDiff = Quaternion.Angle(_smoothedRotation, transform.localRotation);
angleVelocitySnap = Mathf.Min(angleVelocitySnap + angDiff, 90f); _angleVelocitySnap = Mathf.Min(_angleVelocitySnap + angDiff, 90f);
var snapMulti = Mathf.Clamp(angleVelocitySnap / smallMovementThresholdAngle, 0.1f, 2.5f); var snapMulti = Mathf.Clamp(_angleVelocitySnap / _smallMovementThresholdAngle, 0.1f, 2.5f);
if (angleVelocitySnap > 0.1f) if (_angleVelocitySnap > 0.1f)
angleVelocitySnap -= Mathf.Max(0.4f, angleVelocitySnap / 1.7f); _angleVelocitySnap -= Mathf.Max(0.4f, _angleVelocitySnap / 1.7f);
if (positionSmoothingValue < 20f) if (_positionSmoothingValue < 20f)
{ {
smoothedPosition = Vector3.Lerp(smoothedPosition, transform.localPosition, positionSmoothingValue * Time.deltaTime * snapMulti); _smoothedPosition = Vector3.Lerp(_smoothedPosition, transform.localPosition, _positionSmoothingValue * Time.deltaTime * snapMulti);
transform.localPosition = smoothedPosition; transform.localPosition = _smoothedPosition;
} }
if (rotationSmoothingValue < 20f) if (_rotationSmoothingValue < 20f)
{ {
smoothedRotation = Quaternion.Lerp(smoothedRotation, transform.localRotation, rotationSmoothingValue * Time.deltaTime * snapMulti); _smoothedRotation = Quaternion.Lerp(_smoothedRotation, transform.localRotation, _rotationSmoothingValue * Time.deltaTime * snapMulti);
transform.localRotation = smoothedRotation; transform.localRotation = _smoothedRotation;
} }
} }
else else
{ {
smoothedPosition = transform.localPosition; _smoothedPosition = transform.localPosition;
smoothedRotation = transform.localRotation; _smoothedRotation = transform.localRotation;
} }
} }
#endregion
} }

View file

@ -1,12 +1,12 @@
{ {
"_id": -1, "_id": 162,
"name": "SmoothRay", "name": "SmoothRay",
"modversion": "1.0.1", "modversion": "1.0.2",
"gameversion": "2022r170p1", "gameversion": "2023r171",
"loaderversion": "0.6.1", "loaderversion": "0.6.1",
"modtype": "Mod", "modtype": "Mod",
"author": "NotAKidoS", "author": "NotAKidoS",
"description": "Smoothes your controller while using the Quick and Main menus to make it easier to navigate.\nThis is a CVR adaptation of a Beat Saber mod: [BeatSaber_SmoothedController](https://github.com/kinsi55/BeatSaber_SmoothedController)\n\nSupport for TrackedControllerFix & DesktopVRSwitch.", "description": "Smoothes your controller while using the Quick and Main menus to make it easier to navigate.\nThis is a CVR adaptation of a Beat Saber mod: [BeatSaber_SmoothedController](https://github.com/kinsi55/BeatSaber_SmoothedController)\n\nSupport for TrackedControllerFix & DesktopVRSwitch.\n**Only supports OpenVR, not OpenXR.**",
"searchtags": [ "searchtags": [
"vr", "vr",
"ray", "ray",
@ -16,8 +16,8 @@
"requirements": [ "requirements": [
"None" "None"
], ],
"downloadlink": "https://github.com/NotAKidOnSteam/NAK_CVR_Mods/releases/download/r9/SmoothRay.dll", "downloadlink": "https://github.com/NotAKidOnSteam/NAK_CVR_Mods/releases/download/r16/SmoothRay.dll",
"sourcelink": "https://github.com/NotAKidOnSteam/NAK_CVR_Mods/tree/main/SmoothRay/", "sourcelink": "https://github.com/NotAKidOnSteam/NAK_CVR_Mods/tree/main/SmoothRay/",
"changelog": "Initial CVRMG Release", "changelog": "- Fixes for 2023r171.\n- Prevented from initializing when launching with OpenXR.",
"embedcolor": "dc8105" "embedcolor": "#dc8105"
} }