From a6712259039be24cdff732a93e17b23a6b5539f8 Mon Sep 17 00:00:00 2001 From: Nicolas Gnyra Date: Thu, 3 Oct 2024 18:11:00 -0400 Subject: [PATCH] Hide avatar in spectator FPFC --- Source/CustomAvatar/Rendering/MainCamera.cs | 65 ++++++++++--------- .../CustomAvatar/Rendering/SpectatorCamera.cs | 24 +++++++ 2 files changed, 60 insertions(+), 29 deletions(-) diff --git a/Source/CustomAvatar/Rendering/MainCamera.cs b/Source/CustomAvatar/Rendering/MainCamera.cs index 02a199e6..991f5df5 100644 --- a/Source/CustomAvatar/Rendering/MainCamera.cs +++ b/Source/CustomAvatar/Rendering/MainCamera.cs @@ -35,14 +35,16 @@ internal class MainCamera : MonoBehaviour private ActivePlayerSpaceManager _activePlayerSpaceManager; private ActiveOriginManager _activeOriginManager; private ActiveCameraManager _activeCameraManager; - private IFPFCSettings _fpfcSettings; - private BeatSaberUtilities _beatSaberUtilities; private Transform _playerSpace; private Transform _origin; private Camera _camera; private TrackedPoseDriver _trackedPoseDriver; + protected IFPFCSettings fpfcSettings { get; private set; } + + protected BeatSaberUtilities beatSaberUtilities { get; private set; } + protected virtual (Transform playerSpace, Transform origin) GetPlayerSpaceAndOrigin() { VRCenterAdjust center = transform.GetComponentInParent(); @@ -58,6 +60,23 @@ protected virtual (Transform playerSpace, Transform origin) GetPlayerSpaceAndOri } } + protected virtual int GetCameraMask(int mask) + { + mask |= AvatarLayers.kAlwaysVisibleMask; + + // FPFC basically ends up being a 3rd person camera + if (fpfcSettings.Enabled || !beatSaberUtilities.hasFocus) + { + mask |= AvatarLayers.kOnlyInThirdPersonMask; + } + else + { + mask &= ~AvatarLayers.kOnlyInThirdPersonMask; + } + + return mask; + } + protected void Awake() { _camera = GetComponent(); @@ -72,17 +91,17 @@ protected void OnEnable() _settings.cameraNearClipPlane.changed += OnCameraNearClipPlaneChanged; } - if (_fpfcSettings != null) + if (fpfcSettings != null) { - _fpfcSettings.Changed -= OnFpfcSettingsChanged; - _fpfcSettings.Changed += OnFpfcSettingsChanged; + fpfcSettings.Changed -= OnFpfcSettingsChanged; + fpfcSettings.Changed += OnFpfcSettingsChanged; } - if (_beatSaberUtilities != null) + if (beatSaberUtilities != null) { - _beatSaberUtilities.focusChanged -= OnFocusChanged; - _beatSaberUtilities.focusChanged += OnFocusChanged; - OnFocusChanged(_beatSaberUtilities.hasFocus); + beatSaberUtilities.focusChanged -= OnFocusChanged; + beatSaberUtilities.focusChanged += OnFocusChanged; + OnFocusChanged(beatSaberUtilities.hasFocus); } UpdateCameraMask(); @@ -104,8 +123,8 @@ private void Construct( _activePlayerSpaceManager = activePlayerSpaceManager; _activeOriginManager = activeOriginManager; _activeCameraManager = activeCameraManager; - _fpfcSettings = fpfcSettings; - _beatSaberUtilities = beatSaberUtilities; + this.fpfcSettings = fpfcSettings; + this.beatSaberUtilities = beatSaberUtilities; } protected void Start() @@ -128,14 +147,14 @@ protected void OnDisable() _settings.cameraNearClipPlane.changed -= OnCameraNearClipPlaneChanged; } - if (_fpfcSettings != null) + if (fpfcSettings != null) { - _fpfcSettings.Changed -= OnFpfcSettingsChanged; + fpfcSettings.Changed -= OnFpfcSettingsChanged; } - if (_beatSaberUtilities != null) + if (beatSaberUtilities != null) { - _beatSaberUtilities.focusChanged -= OnFocusChanged; + beatSaberUtilities.focusChanged -= OnFocusChanged; } } @@ -169,26 +188,14 @@ private void OnFocusChanged(bool hasFocus) private void UpdateCameraMask() { - if (_logger == null || _settings == null || _fpfcSettings == null) + if (_logger == null || _settings == null || fpfcSettings == null) { return; } _logger.LogTrace($"Setting avatar culling mask and near clip plane on '{_camera.name}'"); - int mask = _camera.cullingMask | AvatarLayers.kAlwaysVisibleMask; - - // FPFC basically ends up being a 3rd person camera - if (_fpfcSettings.Enabled || !_beatSaberUtilities.hasFocus) - { - mask |= AvatarLayers.kOnlyInThirdPersonMask; - } - else - { - mask &= ~AvatarLayers.kOnlyInThirdPersonMask; - } - - _camera.cullingMask = mask; + _camera.cullingMask = GetCameraMask(_camera.cullingMask); _camera.nearClipPlane = _settings.cameraNearClipPlane; } diff --git a/Source/CustomAvatar/Rendering/SpectatorCamera.cs b/Source/CustomAvatar/Rendering/SpectatorCamera.cs index 7c1524d6..d3ccdd73 100644 --- a/Source/CustomAvatar/Rendering/SpectatorCamera.cs +++ b/Source/CustomAvatar/Rendering/SpectatorCamera.cs @@ -14,6 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . +using CustomAvatar.Avatar; using UnityEngine; namespace CustomAvatar.Rendering @@ -29,5 +30,28 @@ protected override (Transform playerSpace, Transform origin) GetPlayerSpaceAndOr { return (playerSpace, origin); } + + protected override int GetCameraMask(int mask) + { + if (fpfcSettings.Enabled) + { + mask &= ~AvatarLayers.kAllLayersMask; + } + else + { + mask |= AvatarLayers.kAlwaysVisibleMask; + } + + if (beatSaberUtilities.hasFocus) + { + mask &= ~AvatarLayers.kOnlyInThirdPersonMask; + } + else + { + mask |= AvatarLayers.kOnlyInThirdPersonMask; + } + + return mask; + } } }