mirror of
https://github.com/NotAKidoS/NAK_CVR_Mods.git
synced 2025-09-02 14:29:25 +00:00
[SmoothRay] Fixes for 2023r171
This commit is contained in:
parent
a44354502f
commit
d67c45023f
5 changed files with 110 additions and 77 deletions
|
@ -23,6 +23,8 @@
|
|||
**/
|
||||
|
||||
using ABI_RC.Core.InteractionSystem;
|
||||
using ABI_RC.Core.Savior;
|
||||
using MelonLoader;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using Valve.VR;
|
||||
|
@ -31,118 +33,146 @@ namespace NAK.SmoothRay;
|
|||
|
||||
public class SmoothRayer : MonoBehaviour
|
||||
{
|
||||
#region Variables
|
||||
|
||||
public ControllerRay ray;
|
||||
|
||||
//settings
|
||||
bool isEnabled;
|
||||
bool menuOnly;
|
||||
float positionSmoothingValue;
|
||||
float rotationSmoothingValue;
|
||||
float smallMovementThresholdAngle;
|
||||
private bool _isEnabled;
|
||||
private bool _menuOnly;
|
||||
private float _positionSmoothingValue;
|
||||
private float _rotationSmoothingValue;
|
||||
private float _smallMovementThresholdAngle;
|
||||
|
||||
//internal
|
||||
Vector3 smoothedPosition = Vector3.zero;
|
||||
Quaternion smoothedRotation = Quaternion.identity;
|
||||
float angleVelocitySnap = 1f;
|
||||
private Vector3 _smoothedPosition = Vector3.zero;
|
||||
private Quaternion _smoothedRotation = Quaternion.identity;
|
||||
private float _angleVelocitySnap = 1f;
|
||||
|
||||
//native & trackedcontrollerfix stuff
|
||||
SteamVR_Behaviour_Pose pose;
|
||||
SteamVR_TrackedObject tracked;
|
||||
SteamVR_Events.Action newPosesAction = null;
|
||||
|
||||
void Start()
|
||||
private SteamVR_Behaviour_Pose _behaviourPose;
|
||||
private SteamVR_TrackedObject _trackedObject;
|
||||
private SteamVR_Events.Action _newPosesAction;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity Methods
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// native CVR
|
||||
pose = GetComponent<SteamVR_Behaviour_Pose>();
|
||||
if (pose != null)
|
||||
pose.onTransformUpdatedEvent += OnTransformUpdated;
|
||||
// Native ChilloutVR - OpenVR
|
||||
if (TryGetComponent(out _behaviourPose))
|
||||
UpdateTransformUpdatedEvent(true);
|
||||
|
||||
// trackedcontrollerfix support
|
||||
tracked = GetComponent<SteamVR_TrackedObject>();
|
||||
if (tracked != null)
|
||||
// TrackedControllerFix support - OpenVR
|
||||
if (TryGetComponent(out _trackedObject))
|
||||
{
|
||||
newPosesAction = SteamVR_Events.NewPosesAppliedAction(new UnityAction(OnAppliedPoses));
|
||||
newPosesAction.enabled = true;
|
||||
_newPosesAction = SteamVR_Events.NewPosesAppliedAction(new UnityAction(OnAppliedPoses));
|
||||
UpdatePosesAction(true);
|
||||
}
|
||||
|
||||
foreach (var setting in SmoothRay.Category.Entries)
|
||||
{
|
||||
foreach (MelonPreferences_Entry setting in SmoothRay.Category.Entries)
|
||||
setting.OnEntryValueChangedUntyped.Subscribe(OnUpdateSettings);
|
||||
}
|
||||
|
||||
OnUpdateSettings(null, null);
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
smoothedPosition = transform.localPosition;
|
||||
smoothedRotation = transform.localRotation;
|
||||
_smoothedPosition = transform.localPosition;
|
||||
_smoothedRotation = transform.localRotation;
|
||||
|
||||
// desktopvrswitch support, start handles this for normal use
|
||||
if (pose != null)
|
||||
pose.onTransformUpdatedEvent += OnTransformUpdated;
|
||||
if (tracked != null && newPosesAction != null)
|
||||
newPosesAction.enabled = true;
|
||||
UpdateTransformUpdatedEvent(true);
|
||||
UpdatePosesAction(true);
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
private void OnDisable()
|
||||
{
|
||||
smoothedPosition = transform.localPosition;
|
||||
smoothedRotation = transform.localRotation;
|
||||
|
||||
_smoothedPosition = transform.localPosition;
|
||||
_smoothedRotation = transform.localRotation;
|
||||
|
||||
// desktopvrswitch support, normal use wont run this
|
||||
if (pose != null)
|
||||
pose.onTransformUpdatedEvent -= OnTransformUpdated;
|
||||
if (tracked != null && newPosesAction != null)
|
||||
newPosesAction.enabled = false;
|
||||
UpdateTransformUpdatedEvent(false);
|
||||
UpdatePosesAction(false);
|
||||
}
|
||||
|
||||
void OnUpdateSettings(object arg1, object arg2)
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private void UpdatePosesAction(bool enable)
|
||||
{
|
||||
isEnabled = SmoothRay.EntryEnabled.Value;
|
||||
menuOnly = SmoothRay.EntryMenuOnly.Value;
|
||||
smallMovementThresholdAngle = SmoothRay.EntrySmallMovementThresholdAngle.Value;
|
||||
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 = SmoothRay.EntryEnabled.Value;
|
||||
_menuOnly = SmoothRay.EntryMenuOnly.Value;
|
||||
_smallMovementThresholdAngle = SmoothRay.EntrySmallMovementThresholdAngle.Value;
|
||||
// dont let value hit 0, itll freeze controllers
|
||||
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);
|
||||
_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);
|
||||
}
|
||||
|
||||
void OnAppliedPoses() => SmoothTransform();
|
||||
void OnTransformUpdated(SteamVR_Behaviour_Pose pose, SteamVR_Input_Sources inputSource) => SmoothTransform();
|
||||
|
||||
void SmoothTransform()
|
||||
private void OnAppliedPoses() => SmoothTransform();
|
||||
|
||||
private void OnTransformUpdated(SteamVR_Behaviour_Pose pose, SteamVR_Input_Sources inputSource) => 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;
|
||||
}
|
||||
|
||||
var angDiff = Quaternion.Angle(smoothedRotation, transform.localRotation);
|
||||
angleVelocitySnap = Mathf.Min(angleVelocitySnap + angDiff, 90f);
|
||||
var angDiff = Quaternion.Angle(_smoothedRotation, transform.localRotation);
|
||||
_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)
|
||||
angleVelocitySnap -= Mathf.Max(0.4f, angleVelocitySnap / 1.7f);
|
||||
if (_angleVelocitySnap > 0.1f)
|
||||
_angleVelocitySnap -= Mathf.Max(0.4f, _angleVelocitySnap / 1.7f);
|
||||
|
||||
if (positionSmoothingValue < 20f)
|
||||
if (_positionSmoothingValue < 20f)
|
||||
{
|
||||
smoothedPosition = Vector3.Lerp(smoothedPosition, transform.localPosition, positionSmoothingValue * Time.deltaTime * snapMulti);
|
||||
transform.localPosition = smoothedPosition;
|
||||
_smoothedPosition = Vector3.Lerp(_smoothedPosition, transform.localPosition, _positionSmoothingValue * Time.deltaTime * snapMulti);
|
||||
transform.localPosition = _smoothedPosition;
|
||||
}
|
||||
|
||||
if (rotationSmoothingValue < 20f)
|
||||
if (_rotationSmoothingValue < 20f)
|
||||
{
|
||||
smoothedRotation = Quaternion.Lerp(smoothedRotation, transform.localRotation, rotationSmoothingValue * Time.deltaTime * snapMulti);
|
||||
transform.localRotation = smoothedRotation;
|
||||
_smoothedRotation = Quaternion.Lerp(_smoothedRotation, transform.localRotation, _rotationSmoothingValue * Time.deltaTime * snapMulti);
|
||||
transform.localRotation = _smoothedRotation;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
smoothedPosition = transform.localPosition;
|
||||
smoothedRotation = transform.localRotation;
|
||||
_smoothedPosition = transform.localPosition;
|
||||
_smoothedRotation = transform.localRotation;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue