SmootherRay: expanded and enhanced

This commit is contained in:
NotAKidoS 2024-09-18 19:53:25 -05:00
parent 59cec7e7d3
commit 70ade663bc
9 changed files with 57 additions and 53 deletions

91
SmootherRay/Main.cs Normal file
View file

@ -0,0 +1,91 @@
using System;
using ABI_RC.Core.InteractionSystem;
using ABI_RC.Core.Player;
using HarmonyLib;
using MelonLoader;
namespace NAK.SmootherRay;
// ChilloutVR adaptation of:
// https://github.com/kinsi55/BeatSaber_SmoothedController
// https://github.com/kinsi55/BeatSaber_SmoothedController/blob/master/LICENSE
public class SmootherRayMod : MelonMod
{
internal static MelonLogger.Instance Logger;
#region Melon Preferences
public static readonly MelonPreferences_Category Category =
MelonPreferences.CreateCategory(nameof(SmootherRayMod));
public static readonly MelonPreferences_Entry<bool> EntryEnabled =
Category.CreateEntry("Enable Smoothing", true,
description: "Enable or disable smoothing.");
public static readonly MelonPreferences_Entry<bool> EntryMenuOnly =
Category.CreateEntry("Menu Only", false,
description: "Only use smoothing on Main Menu and Quick Menu. This will be fine for most users, but it may be desired on pickups & Unity UI elements too. When off it is best paired with WhereAmIPointing.");
public static readonly MelonPreferences_Entry<float> EntryPositionSmoothing =
Category.CreateEntry("Position Smoothing (3f)", 3f,
description: "How much to smooth position changes by. Use the slider to adjust the position smoothing factor. Range: 0 to 20.");
public static readonly MelonPreferences_Entry<float> EntryRotationSmoothing =
Category.CreateEntry("Rotation Smoothing (12f)", 12f,
description: "How much to smooth rotation changes by. Use the slider to adjust the rotation smoothing factor. Range: 0 to 20.");
public static readonly MelonPreferences_Entry<float> EntrySmallMovementThresholdAngle =
Category.CreateEntry("Small Angle Threshold (6f)", 6f,
description: "Angle difference to consider a 'small' movement. The less shaky your hands are, the lower you probably want to set this. This is probably the primary value you want to tweak. Use the slider to adjust the threshold angle. Range: 4 to 15.");
#endregion Melon Preferences
#region Melon Events
public override void OnInitializeMelon()
{
Logger = LoggerInstance;
ApplyPatches(typeof(PlayerSetup_Patches));
ApplyPatches(typeof(ControllerRay_Patches));
}
private void ApplyPatches(Type type)
{
try
{
HarmonyInstance.PatchAll(type);
}
catch (Exception e)
{
LoggerInstance.Msg($"Failed while patching {type.Name}!");
LoggerInstance.Error(e);
}
}
#endregion Melon Events
#region Harmony Patches
internal static class PlayerSetup_Patches
{
[HarmonyPostfix]
[HarmonyPatch(typeof(PlayerSetup), nameof(PlayerSetup.Start))]
private static void Postfix_PlayerSetup_Start(ref PlayerSetup __instance)
{
__instance.vrLeftHandTracker.gameObject.AddComponent<SmootherRayer>().ray = __instance.vrRayLeft;
__instance.vrRightHandTracker.gameObject.AddComponent<SmootherRayer>().ray = __instance.vrRayRight;
}
}
internal static class ControllerRay_Patches
{
// SmootherRay
[HarmonyPrefix]
[HarmonyPatch(typeof(ControllerRay), nameof(ControllerRay.SmoothRay))]
private static bool Prefix_ControllerRay_SmoothRay(ref ControllerRay __instance)
=> !EntryEnabled.Value; // SmootherRay method enforces identity local pos when disabled, so we skip it
}
#endregion Harmony Patches
}

View file

@ -0,0 +1,33 @@
using MelonLoader;
using NAK.SmootherRay.Properties;
using System.Reflection;
[assembly: AssemblyVersion(AssemblyInfoParams.Version)]
[assembly: AssemblyFileVersion(AssemblyInfoParams.Version)]
[assembly: AssemblyInformationalVersion(AssemblyInfoParams.Version)]
[assembly: AssemblyTitle(nameof(NAK.SmootherRay))]
[assembly: AssemblyCompany(AssemblyInfoParams.Author)]
[assembly: AssemblyProduct(nameof(NAK.SmootherRay))]
[assembly: MelonInfo(
typeof(NAK.SmootherRay.SmootherRayMod),
nameof(NAK.SmootherRay),
AssemblyInfoParams.Version,
AssemblyInfoParams.Author,
downloadLink: "https://github.com/NotAKidoS/NAK_CVR_Mods/tree/main/SmootherRay"
)]
[assembly: MelonGame("Alpha Blend Interactive", "ChilloutVR")]
[assembly: MelonPlatform(MelonPlatformAttribute.CompatiblePlatforms.WINDOWS_X64)]
[assembly: MelonPlatformDomain(MelonPlatformDomainAttribute.CompatibleDomains.MONO)]
[assembly: MelonColor(255, 246, 25, 99)] // red-pink
[assembly: MelonAuthorColor(255, 158, 21, 32)] // red
[assembly: HarmonyDontPatchAll]
namespace NAK.SmootherRay.Properties;
internal static class AssemblyInfoParams
{
public const string Version = "1.0.5";
public const string Author = "NotAKidoS";
}

17
SmootherRay/README.md Normal file
View file

@ -0,0 +1,17 @@
# SmoothRay
Smoothes your controller while using the Quick and Main menus to make it easier to navigate.
This is a CVR adaptation of a Beat Saber mod: [BeatSaber_SmoothedController](https://github.com/kinsi55/BeatSaber_SmoothedController)
**Only supports OpenVR, not OpenXR.**
---
Here is the block of text where I tell you this mod is not affiliated or endorsed by ABI.
https://documentation.abinteractive.net/official/legal/tos/#7-modding-our-games
> This mod is an independent creation and is not affiliated with, supported by or approved by Alpha Blend Interactive.
> Use of this mod is done so at the user's own risk and the creator cannot be held responsible for any issues arising from its use.
> To the best of my knowledge, I have adhered to the Modding Guidelines established by Alpha Blend Interactive.

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RootNamespace>SmoothRay</RootNamespace>
</PropertyGroup>
</Project>

View file

@ -0,0 +1,234 @@
/**
MIT License
Copyright (c) 2021 Kinsi, NotAKidoS
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
using ABI_RC.Core;
using ABI_RC.Core.InteractionSystem;
using ABI_RC.Core.Savior;
using ABI_RC.Systems.InputManagement;
using MelonLoader;
using UnityEngine;
using Valve.VR;
namespace NAK.SmootherRay;
public class SmootherRayer : MonoBehaviour
{
#region Variables
public ControllerRay ray;
//settings
private bool _isEnabled;
private bool _menuOnly;
private float _positionSmoothingValue;
private float _rotationSmoothingValue;
private float _smallMovementThresholdAngle;
//internal
private Vector3 _smoothedPosition = Vector3.zero;
private Quaternion _smoothedRotation = Quaternion.identity;
private float _angleVelocitySnap = 1f;
//native & trackedcontrollerfix stuff
private SteamVR_Behaviour_Pose _behaviourPose;
private SteamVR_TrackedObject _trackedObject;
private SteamVR_Events.Action _newPosesAction;
#endregion
#region Unity Methods
private void Start()
{
// Native ChilloutVR - OpenVR
if (TryGetComponent(out _behaviourPose))
UpdateTransformUpdatedEvent(true);
// TrackedControllerFix support - OpenVR
if (TryGetComponent(out _trackedObject))
{
_newPosesAction = SteamVR_Events.NewPosesAppliedAction(OnAppliedPoses);
UpdatePosesAction(true);
}
foreach (MelonPreferences_Entry setting in SmootherRayMod.Category.Entries)
setting.OnEntryValueChangedUntyped.Subscribe(OnUpdateSettings);
MetaPort.Instance.settings.settingBoolChanged.AddListener(OnSettingsBoolChanged);
OnUpdateSettings(null, null);
}
private void OnEnable()
{
Transform controller = transform;
_smoothedPosition = controller.localPosition;
_smoothedRotation = controller.localRotation;
// desktopvrswitch support, start handles this for normal use
UpdateTransformUpdatedEvent(true);
UpdatePosesAction(true);
}
private void OnDisable()
{
Transform controller = transform;
_smoothedPosition = controller.localPosition;
_smoothedRotation = controller.localRotation;
// desktopvrswitch support, normal use wont run this
UpdateTransformUpdatedEvent(false);
UpdatePosesAction(false);
}
#endregion
#region Private Methods
private void UpdatePosesAction(bool enable)
{
if (enable && CheckVR.Instance.forceOpenXr)
return;
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 = SmootherRayMod.EntryEnabled.Value;
_menuOnly = SmootherRayMod.EntryMenuOnly.Value;
_smallMovementThresholdAngle = SmootherRayMod.EntrySmallMovementThresholdAngle.Value;
// dont let value hit 0, itll freeze controllers
_positionSmoothingValue = Mathf.Max(20f - Mathf.Clamp(SmootherRayMod.EntryPositionSmoothing.Value, 0f, 20f), 0.1f);
_rotationSmoothingValue = Mathf.Max(20f - Mathf.Clamp(SmootherRayMod.EntryRotationSmoothing.Value, 0f, 20f), 0.1f);
if (!_isEnabled)
return; // only care about setting being enabled
ray._enableSmoothRay = false; // ensure built-in smoothing is disabled
if (MetaPort.Instance.settings.GetSettingsBool("ControlSmoothRaycast"))
return; // disable saved setting once
SmootherRayMod.Logger.Msg("Built-in SmootherRay setting found to be enabled. Disabling built-in SmootherRay implementation in favor of modded implementation.");
MetaPort.Instance.settings.SetSettingsBool("ControlSmoothRaycast", false);
ViewManager.SetGameSettingBool("ControlSmoothRaycast", false);
// ^ did you know the game doesn't even use this method native...
}
private void OnSettingsBoolChanged(string key, bool value)
{
if (key != "ControlSmoothRaycast")
return; // only care about SmoothRaycast setting
if (!value)
return; // only care about setting being enabled
_isEnabled = false; // ensure modded SmootherRay is disabled
if (!SmootherRayMod.EntryEnabled.Value) return; // disable saved setting once
SmootherRayMod.Logger.Msg("Modded SmootherRay found to be enabled. Disabling modded SmootherRay implementation in favor of built-in implementation.");
SmootherRayMod.EntryEnabled.Value = false;
}
private void OnAppliedPoses()
{
SmoothTransform();
}
private void OnTransformUpdated(SteamVR_Behaviour_Pose pose, SteamVR_Input_Sources inputSource)
{
SmoothTransform();
}
private void SmoothTransform()
{
Transform controller = transform;
if (!CanSmoothRay())
{
_smoothedPosition = controller.localPosition;
_smoothedRotation = controller.localRotation;
}
else
{
var angDiff = Quaternion.Angle(_smoothedRotation, controller.localRotation);
_angleVelocitySnap = Mathf.Min(_angleVelocitySnap + angDiff, 90f);
var snapMulti = Mathf.Clamp(_angleVelocitySnap / _smallMovementThresholdAngle, 0.1f, 2.5f);
if (_angleVelocitySnap > 0.1f)
_angleVelocitySnap -= Mathf.Max(0.4f, _angleVelocitySnap / 1.7f);
if (_positionSmoothingValue < 20f)
{
_smoothedPosition = Vector3.Lerp(_smoothedPosition, controller.localPosition,
_positionSmoothingValue * Time.deltaTime * snapMulti);
controller.localPosition = _smoothedPosition;
}
if (_rotationSmoothingValue < 20f)
{
_smoothedRotation = Quaternion.Lerp(_smoothedRotation, controller.localRotation,
_rotationSmoothingValue * Time.deltaTime * snapMulti);
controller.localRotation = _smoothedRotation;
}
}
}
private bool CanSmoothRay()
{
bool canSmoothRay = _isEnabled && ray.lineRenderer != null && ray.lineRenderer.enabled;
if (_menuOnly)
{
switch (ray.hand)
{
case CVRHand.Left when !CVRInputManager.Instance.leftControllerPointingMenu:
case CVRHand.Right when !CVRInputManager.Instance.rightControllerPointingMenu:
canSmoothRay = false;
break;
}
}
return canSmoothRay;
}
#endregion Private Methods
}

23
SmootherRay/format.json Normal file
View file

@ -0,0 +1,23 @@
{
"_id": 162,
"name": "SmootherRay",
"modversion": "1.0.5",
"gameversion": "2024r176",
"loaderversion": "0.6.1",
"modtype": "Mod",
"author": "NotAKidoS",
"description": "Smoothes your controller while the raycast lines are visible.\nThis is a CVR adaptation of a Beat Saber mod: [BeatSaber_SmoothedController](https://github.com/kinsi55/BeatSaber_SmoothedController)\n\n- An option is provided to only smooth when aiming at menus.\n- Smoothing characteristics are completely configurable, but the defaults are basically perfect.\n- Pairs well with the [WhereAmIPointing](https://discord.com/channels/1001388809184870441/1002058238545641542/1282798820073406556) mod.\n\n**Only supports OpenVR, not OpenXR.**\n\n-# NOTE: This disables the shitty built-in Smooth Ray setting when the mod is enabled. Compare both & you'll see why.",
"searchtags": [
"vr",
"ray",
"controller",
"smoothing"
],
"requirements": [
"None"
],
"downloadlink": "https://github.com/NotAKidoS/NAK_CVR_Mods/releases/download/r40/SmootherRay.dll",
"sourcelink": "https://github.com/NotAKidoS/NAK_CVR_Mods/tree/main/SmootherRay/",
"changelog": "- Fixed for 2024r176.",
"embedcolor": "#f61963"
}