QSBDitheringAnimator

This commit is contained in:
JohnCorby 2022-02-18 05:33:48 -08:00
parent ac54a34c30
commit b51d6a8f63
4 changed files with 112 additions and 19 deletions

View File

@ -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;

View File

@ -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<Renderer>(true);
_renderers = new OWRenderer[componentsInChildren.Length];
_ignoreToggleShadows = new bool[componentsInChildren.Length];
for (var i = 0; i < _renderers.Length; i++)
{
_renderers[i] = componentsInChildren[i].GetComponent<OWRenderer>();
if (_renderers[i] == null)
{
_renderers[i] = componentsInChildren[i].gameObject.AddComponent<OWRenderer>();
}
_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;
}
}
}
}

View File

@ -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<PlayerHUDMarker>().Init(player);
REMOTE_Player_Body.GetComponent<PlayerMapMarker>().PlayerName = player.Name;
player._ditheringAnimator = REMOTE_Player_Body.GetComponent<DitheringAnimator>();
// get inactive renderers too
player._ditheringAnimator._renderers = player._ditheringAnimator
.GetComponentsInChildren<Renderer>(true)
.Select(x => x.gameObject.GetAddComponent<OWRenderer>())
.ToArray();
player._ditheringAnimator = REMOTE_Player_Body.GetComponent<QSBDitheringAnimator>();
player.AudioController = REMOTE_Player_Body.transform.Find("REMOTE_Audio_Player").GetComponent<QSBPlayerAudioController>();
/*

View File

@ -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<Renderer>(true)
.Select(x => x.gameObject.GetAddComponent<OWRenderer>())
.ToArray();
}
public override void Start()
{
base.Start();