diff --git a/QSB/Player/PlayerInfo.cs b/QSB/Player/PlayerInfo.cs index ad935997..d8ba4201 100644 --- a/QSB/Player/PlayerInfo.cs +++ b/QSB/Player/PlayerInfo.cs @@ -8,6 +8,7 @@ using QSB.ItemSync.WorldObjects.Items; using QSB.Messaging; using QSB.Player.Messages; using QSB.Player.TransformSync; +using QSB.PlayerBodySetup.Remote; using QSB.QuantumSync.WorldObjects; using QSB.RoastingSync; using QSB.Tools; @@ -40,7 +41,7 @@ namespace QSB.Player public bool IsInEyeShuttle { get; set; } public IQSBQuantumObject EntangledObject { get; set; } public QSBPlayerAudioController AudioController { get; set; } - internal DitheringAnimator _ditheringAnimator; + internal QSBDitheringAnimator _ditheringAnimator; public bool IsLocalPlayer => TransformSync.isLocalPlayer; diff --git a/QSB/PlayerBodySetup/Remote/QSBDitheringAnimator.cs b/QSB/PlayerBodySetup/Remote/QSBDitheringAnimator.cs new file mode 100644 index 00000000..fac2c79d --- /dev/null +++ b/QSB/PlayerBodySetup/Remote/QSBDitheringAnimator.cs @@ -0,0 +1,107 @@ +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; + + 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; + } + } + + private void Start() => enabled = false; + + public void SetVisibleImmediate(bool visible) + { + if (_visible != visible) + { + _visible = visible; + _visibleFraction = _visible ? 1f : 0f; + UpdateDithering(); + UpdateShadowCasting(); + } + } + + public void SetVisible(bool visible, float fadeRate) + { + if (_visible != visible) + { + _visible = visible; + _fadeRate = fadeRate; + if (!_visible) + { + UpdateShadowCasting(); + } + + enabled = true; + } + } + + private void Update() + { + var target = _visible ? 1f : 0f; + _visibleFraction = Mathf.MoveTowards(_visibleFraction, target, _fadeRate * Time.deltaTime); + if (OWMath.ApproxEquals(_visibleFraction, target)) + { + _visibleFraction = target; + enabled = false; + if (_visible) + { + UpdateShadowCasting(); + } + } + + UpdateDithering(); + } + + private void UpdateDithering() + { + foreach (var renderer 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]) + { + continue; + } + + _renderers[i].GetRenderer().shadowCastingMode = _visible ? ShadowCastingMode.On : ShadowCastingMode.Off; + } + } + } +} diff --git a/QSB/PlayerBodySetup/Remote/RemotePlayerCreation.cs b/QSB/PlayerBodySetup/Remote/RemotePlayerCreation.cs index 2bf77df4..5d2ca639 100644 --- a/QSB/PlayerBodySetup/Remote/RemotePlayerCreation.cs +++ b/QSB/PlayerBodySetup/Remote/RemotePlayerCreation.cs @@ -3,7 +3,6 @@ using QSB.Player; using QSB.RoastingSync; using QSB.Tools; using QSB.Utility; -using System.Linq; using UnityEngine; namespace QSB.PlayerBodySetup.Remote @@ -59,12 +58,7 @@ namespace QSB.PlayerBodySetup.Remote REMOTE_Player_Body.GetComponent().Init(player); REMOTE_Player_Body.GetComponent().PlayerName = player.Name; - player._ditheringAnimator = REMOTE_Player_Body.GetComponent(); - // get inactive renderers too - player._ditheringAnimator._renderers = player._ditheringAnimator - .GetComponentsInChildren(true) - .Select(x => x.gameObject.GetAddComponent()) - .ToArray(); + player._ditheringAnimator = REMOTE_Player_Body.GetComponent(); player.AudioController = REMOTE_Player_Body.transform.Find("REMOTE_Audio_Player").GetComponent(); /* diff --git a/QSB/Tools/QSBTool.cs b/QSB/Tools/QSBTool.cs index 68ddc177..a881715a 100644 --- a/QSB/Tools/QSBTool.cs +++ b/QSB/Tools/QSBTool.cs @@ -1,6 +1,6 @@ using QSB.Player; +using QSB.PlayerBodySetup.Remote; using QSB.Utility; -using System.Linq; using UnityEngine; namespace QSB.Tools @@ -11,7 +11,7 @@ namespace QSB.Tools public ToolType Type { get; set; } public GameObject ToolGameObject { get; set; } [SerializeField] - private DitheringAnimator _ditheringAnimator; + private QSBDitheringAnimator _ditheringAnimator; public DampedSpringQuat MoveSpring { @@ -39,15 +39,6 @@ namespace QSB.Tools protected bool _isDitheringOut; - private void Awake() - { - // get inactive renderers too - _ditheringAnimator._renderers = _ditheringAnimator - .GetComponentsInChildren(true) - .Select(x => x.gameObject.GetAddComponent()) - .ToArray(); - } - public override void Start() { base.Start();