From b91041502ae70b6bd2af6e6b5374e93e39f14ac1 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 24 Feb 2022 23:13:31 -0800 Subject: [PATCH] update QSBDitheringAnimator --- QSB/Player/PlayerInfo.cs | 17 ++- .../Remote/QSBDitheringAnimator.cs | 108 ++++++------------ QSB/Tools/QSBTool.cs | 13 ++- 3 files changed, 52 insertions(+), 86 deletions(-) diff --git a/QSB/Player/PlayerInfo.cs b/QSB/Player/PlayerInfo.cs index 2d21c0e3..1cef4ecf 100644 --- a/QSB/Player/PlayerInfo.cs +++ b/QSB/Player/PlayerInfo.cs @@ -34,7 +34,7 @@ public class PlayerInfo public ClientState State { get; set; } public EyeState EyeState { get; set; } public bool IsDead { get; set; } - public bool Visible => IsLocalPlayer || !_ditheringAnimator || _ditheringAnimator._visible; + public bool Visible => IsLocalPlayer || _ditheringAnimator == null || _ditheringAnimator.FullyVisible; public bool IsReady { get; set; } public bool IsInMoon { get; set; } public bool IsInShrine { get; set; } @@ -67,6 +67,7 @@ public class PlayerInfo _camera = value; } } + private OWCamera _camera; public GameObject CameraBody { get; set; } @@ -92,6 +93,7 @@ public class PlayerInfo _body = value; } } + private GameObject _body; public GameObject RoastingStick { get; set; } @@ -104,6 +106,7 @@ public class PlayerInfo public QSBTool Translator => GetToolByType(ToolType.Translator); public QSBProbeLauncherTool ProbeLauncher => (QSBProbeLauncherTool)GetToolByType(ToolType.ProbeLauncher); private Transform _handPivot; + public Transform HandPivot { get @@ -130,6 +133,7 @@ public class PlayerInfo return _handPivot; } } + public Transform ItemSocket => HandPivot.Find("REMOTE_ItemSocket"); public Transform ScrollSocket => HandPivot.Find("REMOTE_ScrollSocket"); public Transform SharedStoneSocket => HandPivot.Find("REMOTE_SharedStoneSocket"); @@ -295,13 +299,6 @@ public class PlayerInfo return; } - if (seconds == 0) - { - _ditheringAnimator.SetVisibleImmediate(visible); - } - else - { - _ditheringAnimator.SetVisible(visible, 1 / seconds); - } + _ditheringAnimator.SetVisible(visible, seconds); } -} \ No newline at end of file +} diff --git a/QSB/PlayerBodySetup/Remote/QSBDitheringAnimator.cs b/QSB/PlayerBodySetup/Remote/QSBDitheringAnimator.cs index d0d3b8a3..09885855 100644 --- a/QSB/PlayerBodySetup/Remote/QSBDitheringAnimator.cs +++ b/QSB/PlayerBodySetup/Remote/QSBDitheringAnimator.cs @@ -1,106 +1,74 @@ -using UnityEngine; +using System.Linq; +using UnityEngine; using UnityEngine.Rendering; namespace QSB.PlayerBodySetup.Remote; public class QSBDitheringAnimator : MonoBehaviour { - [SerializeField] - private bool _toggleShadowCasting = true; - public bool _visible { get; private set; } = true; - public float _visibleFraction { get; private set; } = 1f; - private float _fadeRate = 1f; - public OWRenderer[] _renderers { get; private set; } - private bool[] _ignoreToggleShadows; + public bool FullyVisible => !enabled && OWMath.ApproxEquals(_visibleFraction, 1); + public bool FullyInvisible => !enabled && OWMath.ApproxEquals(_visibleFraction, 0); + + private float _visibleTarget = 1; + private float _visibleFraction = 1; + private float _fadeRate; + private (OWRenderer Renderer, bool UpdateShadows)[] _renderers; private void Awake() { - var componentsInChildren = GetComponentsInChildren(true); - _renderers = new OWRenderer[componentsInChildren.Length]; - _ignoreToggleShadows = new bool[componentsInChildren.Length]; - for (var i = 0; i < _renderers.Length; i++) - { - _renderers[i] = componentsInChildren[i].GetComponent(); - if (_renderers[i] == null) - { - _renderers[i] = componentsInChildren[i].gameObject.AddComponent(); - } - - _ignoreToggleShadows[i] = componentsInChildren[i].shadowCastingMode == ShadowCastingMode.Off; - } + _renderers = GetComponentsInChildren(true) + .Select(x => (x.gameObject.GetAddComponent(), x.shadowCastingMode != ShadowCastingMode.Off)) + .ToArray(); + enabled = false; } - private void Start() => enabled = false; - - public void SetVisibleImmediate(bool visible) + public void SetVisible(bool visible, float seconds = 0) { - if (_visible != visible) + var visibleTarget = visible ? 1 : 0; + if (OWMath.ApproxEquals(visibleTarget, _visibleTarget)) { - _visible = visible; - _visibleFraction = _visible ? 1f : 0f; - UpdateDithering(); - UpdateShadowCasting(); + return; } - } - public void SetVisible(bool visible, float fadeRate) - { - if (_visible != visible) + _visibleTarget = visibleTarget; + if (seconds != 0) { - _visible = visible; - _fadeRate = fadeRate; - if (!_visible) - { - UpdateShadowCasting(); - } - + _fadeRate = 1 / seconds; enabled = true; } + else + { + _visibleFraction = _visibleTarget; + UpdateRenderers(); + } } private void Update() { - var target = _visible ? 1f : 0f; - _visibleFraction = Mathf.MoveTowards(_visibleFraction, target, _fadeRate * Time.deltaTime); - if (OWMath.ApproxEquals(_visibleFraction, target)) + _visibleFraction = Mathf.MoveTowards(_visibleFraction, _visibleTarget, _fadeRate * Time.deltaTime); + if (OWMath.ApproxEquals(_visibleFraction, _visibleTarget)) { - _visibleFraction = target; + _visibleFraction = _visibleTarget; enabled = false; - if (_visible) - { - UpdateShadowCasting(); - } } - UpdateDithering(); + UpdateRenderers(); } - private void UpdateDithering() + private void UpdateRenderers() { - foreach (var renderer in _renderers) + foreach (var (renderer, updateShadows) in _renderers) { - if (renderer != null) - { - renderer.SetDitherFade(1f - _visibleFraction); - } - } - } - - private void UpdateShadowCasting() - { - if (!_toggleShadowCasting) - { - return; - } - - for (var i = 0; i < _renderers.Length; i++) - { - if (_ignoreToggleShadows[i]) + if (renderer == null) { continue; } - _renderers[i].GetRenderer().shadowCastingMode = _visible ? ShadowCastingMode.On : ShadowCastingMode.Off; + renderer.SetDitherFade(1 - _visibleFraction); + if (updateShadows) + { + renderer._renderer.shadowCastingMode = FullyVisible ? ShadowCastingMode.On : ShadowCastingMode.Off; + } } } -} \ No newline at end of file +} diff --git a/QSB/Tools/QSBTool.cs b/QSB/Tools/QSBTool.cs index 3bfc0f16..e7a08ff1 100644 --- a/QSB/Tools/QSBTool.cs +++ b/QSB/Tools/QSBTool.cs @@ -43,6 +43,7 @@ public class QSBTool : PlayerTool { base.Start(); ToolGameObject?.SetActive(false); + _ditheringAnimator.SetVisible(false); } public virtual void OnEnable() => ToolGameObject?.SetActive(true); @@ -70,10 +71,10 @@ public class QSBTool : PlayerTool { base.EquipTool(); - if (_ditheringAnimator != null && _ditheringAnimator._renderers != null) + if (_ditheringAnimator != null) { ToolGameObject?.SetActive(true); - _ditheringAnimator.SetVisible(true, 5f); + _ditheringAnimator.SetVisible(true, .2f); } Player.AudioController.PlayEquipTool(); @@ -83,11 +84,11 @@ public class QSBTool : PlayerTool { base.UnequipTool(); - if (_ditheringAnimator != null && _ditheringAnimator._renderers != null) + if (_ditheringAnimator != null) { _isDitheringOut = true; - _ditheringAnimator.SetVisible(false, 5f); - Delay.RunWhen(() => _ditheringAnimator._visibleFraction == 0, FinishDitherOut); + _ditheringAnimator.SetVisible(false, .2f); + Delay.RunWhen(() => _ditheringAnimator.FullyInvisible, FinishDitherOut); } Player.AudioController.PlayUnequipTool(); @@ -98,4 +99,4 @@ public class QSBTool : PlayerTool ToolGameObject?.SetActive(false); _isDitheringOut = false; } -} \ No newline at end of file +}