mirror of
https://github.com/NotAKidoS/NAK_CVR_Mods.git
synced 2025-09-02 22:39:22 +00:00
Move many mods to Deprecated folder, fix spelling
This commit is contained in:
parent
5e822cec8d
commit
0042590aa6
539 changed files with 7475 additions and 3120 deletions
397
.Deprecated/AvatarScaleMod/AvatarScaling/AvatarScaleManager.cs
Normal file
397
.Deprecated/AvatarScaleMod/AvatarScaling/AvatarScaleManager.cs
Normal file
|
@ -0,0 +1,397 @@
|
|||
using System.Collections;
|
||||
using ABI_RC.Core.IO;
|
||||
using ABI_RC.Core.Player;
|
||||
using ABI_RC.Core.Player.AvatarTracking;
|
||||
using ABI_RC.Core.UI;
|
||||
using ABI_RC.Systems.GameEventSystem;
|
||||
using NAK.AvatarScaleMod.Components;
|
||||
using NAK.AvatarScaleMod.Networking;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NAK.AvatarScaleMod.AvatarScaling;
|
||||
|
||||
public class AvatarScaleManager : MonoBehaviour
|
||||
{
|
||||
public static AvatarScaleManager Instance { get; private set; }
|
||||
|
||||
private LocalScaler _localAvatarScaler;
|
||||
private Dictionary<string, NetworkScaler> _networkedScalers;
|
||||
|
||||
private Coroutine _heightUpdateCoroutine;
|
||||
private readonly YieldInstruction _heightUpdateYield = new WaitForEndOfFrame();
|
||||
|
||||
#region Universal Scaling Limits
|
||||
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
// To match AvatarScaleTool: https://github.com/NotAKidoS/AvatarScaleTool/tree/main
|
||||
public const float DefaultMinHeight = 0.25f;
|
||||
public const float DefaultMaxHeight = 2.50f;
|
||||
// ReSharper restore MemberCanBePrivate.Global
|
||||
|
||||
// Universal Scaling Limits
|
||||
public static float MinHeight { get; private set; } = DefaultMinHeight;
|
||||
public static float MaxHeight { get; private set; } = DefaultMaxHeight;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Settings
|
||||
|
||||
private bool _settingUniversalScaling;
|
||||
public bool Setting_UniversalScaling
|
||||
{
|
||||
get => _settingUniversalScaling;
|
||||
set
|
||||
{
|
||||
if (_settingUniversalScaling == value)
|
||||
return;
|
||||
|
||||
_settingUniversalScaling = value;
|
||||
|
||||
if (_localAvatarScaler != null)
|
||||
_localAvatarScaler.UseTargetHeight = value;
|
||||
}
|
||||
}
|
||||
|
||||
private bool _settingAnimationClipScalingOverride;
|
||||
public bool Setting_AnimationClipScalingOverride
|
||||
{
|
||||
get => _settingAnimationClipScalingOverride;
|
||||
set
|
||||
{
|
||||
if (_settingAnimationClipScalingOverride == value)
|
||||
return;
|
||||
|
||||
_settingAnimationClipScalingOverride = value;
|
||||
|
||||
if (_localAvatarScaler != null)
|
||||
_localAvatarScaler.overrideAnimationHeight = value;
|
||||
}
|
||||
}
|
||||
|
||||
private bool _settingPersistentHeight;
|
||||
public bool Setting_PersistentHeight
|
||||
{
|
||||
get => _settingPersistentHeight;
|
||||
set
|
||||
{
|
||||
if (_settingPersistentHeight == value)
|
||||
return;
|
||||
|
||||
_settingPersistentHeight = value;
|
||||
|
||||
// if (_localAvatarScaler != null)
|
||||
// _localAvatarScaler.persistHeight = value;
|
||||
}
|
||||
}
|
||||
|
||||
private float _lastTargetHeight = -1f;
|
||||
public float LastTargetHeight
|
||||
{
|
||||
get => _lastTargetHeight;
|
||||
set
|
||||
{
|
||||
_lastTargetHeight = value;
|
||||
ModSettings.EntryHiddenAvatarHeight.Value = _lastTargetHeight;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity Events
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null
|
||||
&& Instance != this)
|
||||
{
|
||||
DestroyImmediate(this);
|
||||
return;
|
||||
}
|
||||
|
||||
Instance = this;
|
||||
_networkedScalers = new Dictionary<string, NetworkScaler>();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_localAvatarScaler = PlayerSetup.Instance.gameObject.AddComponent<LocalScaler>();
|
||||
_localAvatarScaler.Initialize();
|
||||
|
||||
_settingUniversalScaling = ModSettings.EntryUseUniversalScaling.Value;
|
||||
Setting_AnimationClipScalingOverride = ModSettings.EntryAnimationScalingOverride.Value;
|
||||
Setting_PersistentHeight = ModSettings.EntryPersistentHeight.Value;
|
||||
_lastTargetHeight = ModSettings.EntryPersistThroughRestart.Value
|
||||
? ModSettings.EntryHiddenAvatarHeight.Value : -1f; // -1f is default
|
||||
|
||||
// listen for events
|
||||
_localAvatarScaler.OnAnimatedHeightOverride += OnAnimationHeightOverride;
|
||||
|
||||
CVRGameEventSystem.Instance.OnConnected.AddListener(OnInstanceConnected);
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (_heightUpdateCoroutine != null) StopCoroutine(_heightUpdateCoroutine);
|
||||
_heightUpdateCoroutine = StartCoroutine(HeightUpdateCoroutine());
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (_heightUpdateCoroutine != null) StopCoroutine(_heightUpdateCoroutine);
|
||||
_heightUpdateCoroutine = null;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
_heightUpdateCoroutine = null;
|
||||
CVRGameEventSystem.Instance.OnConnected.RemoveListener(OnInstanceConnected);
|
||||
|
||||
if (_localAvatarScaler != null) Destroy(_localAvatarScaler);
|
||||
_localAvatarScaler = null;
|
||||
|
||||
foreach (NetworkScaler scaler in _networkedScalers.Values) Destroy(scaler);
|
||||
_networkedScalers.Clear();
|
||||
|
||||
if (Instance == this)
|
||||
Instance = null;
|
||||
}
|
||||
|
||||
// only update the height per scaler once per frame, to prevent spam & jitter
|
||||
// this is to ensure that the height is also set at correct time during frame, no matter when it is called
|
||||
private IEnumerator HeightUpdateCoroutine()
|
||||
{
|
||||
while (enabled) yield return _heightUpdateYield;
|
||||
// ReSharper disable once IteratorNeverReturns
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Game Events
|
||||
|
||||
public void OnInstanceConnected(string instanceId)
|
||||
{
|
||||
// TODO: need to know if this causes issues when in a reconnection loop
|
||||
SchedulerSystem.AddJob(ModNetwork.RequestHeightSync, 2f, 1f, 1);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Local Methods
|
||||
|
||||
public void OnAvatarInstantiated(PlayerSetup playerSetup)
|
||||
{
|
||||
if (playerSetup._avatar == null)
|
||||
return;
|
||||
|
||||
_localAvatarScaler.OnAvatarInstantiated(playerSetup._avatar, playerSetup._initialAvatarHeight,
|
||||
playerSetup.initialScale);
|
||||
|
||||
if (!_settingUniversalScaling)
|
||||
return;
|
||||
|
||||
SetTargetHeight(_lastTargetHeight);
|
||||
}
|
||||
|
||||
public void OnAvatarDestroyed(PlayerSetup playerSetup)
|
||||
{
|
||||
if (_localAvatarScaler != null)
|
||||
_localAvatarScaler.OnAvatarDestroyed();
|
||||
}
|
||||
|
||||
public void SetTargetHeight(float targetHeight)
|
||||
{
|
||||
_lastTargetHeight = targetHeight; // save for persistent height
|
||||
|
||||
if (_localAvatarScaler == null)
|
||||
return;
|
||||
|
||||
_localAvatarScaler.TargetHeight = _lastTargetHeight;
|
||||
}
|
||||
|
||||
public float GetHeight()
|
||||
{
|
||||
if (_localAvatarScaler == null)
|
||||
return PlayerAvatarPoint.DefaultAvatarHeight;
|
||||
|
||||
if (!_localAvatarScaler.IsForcingHeight())
|
||||
return PlayerSetup.Instance.GetAvatarHeight();
|
||||
|
||||
return _localAvatarScaler.GetTargetHeight();
|
||||
}
|
||||
|
||||
public float GetAnimationClipHeight()
|
||||
{
|
||||
if (_localAvatarScaler == null)
|
||||
return PlayerAvatarPoint.DefaultAvatarHeight;
|
||||
|
||||
if (!_localAvatarScaler.IsForcingHeight())
|
||||
return PlayerSetup.Instance.GetAvatarHeight();
|
||||
|
||||
return _localAvatarScaler.GetAnimatedHeight();
|
||||
}
|
||||
|
||||
public float GetHeightForNetwork()
|
||||
{
|
||||
if (!_settingUniversalScaling)
|
||||
return -1f;
|
||||
|
||||
if (_localAvatarScaler == null)
|
||||
return -1f;
|
||||
|
||||
if (!_localAvatarScaler.IsForcingHeight())
|
||||
return -1f;
|
||||
|
||||
return _localAvatarScaler.GetTargetHeight();
|
||||
}
|
||||
|
||||
public float GetInitialHeight()
|
||||
{
|
||||
if (_localAvatarScaler == null)
|
||||
return -1f;
|
||||
|
||||
return _localAvatarScaler.GetInitialHeight();
|
||||
}
|
||||
|
||||
public bool IsHeightAdjustedFromInitial()
|
||||
{
|
||||
return _localAvatarScaler != null && _localAvatarScaler.IsForcingHeight();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Network Methods
|
||||
|
||||
public bool DoesNetworkHeightScalerExist(string playerId)
|
||||
=> _networkedScalers.ContainsKey(playerId);
|
||||
|
||||
public int GetNetworkHeightScalerCount()
|
||||
=> _networkedScalers.Count;
|
||||
|
||||
public float GetNetworkHeight(string playerId)
|
||||
{
|
||||
if (_networkedScalers.TryGetValue(playerId, out NetworkScaler scaler))
|
||||
if (scaler.IsForcingHeight()) return scaler.GetTargetHeight();
|
||||
|
||||
//doesn't have mod or has no custom height, get from player avatar directly
|
||||
CVRPlayerEntity playerEntity = CVRPlayerManager.Instance.NetworkPlayers.Find((players) => players.Uuid == playerId);
|
||||
if (playerEntity != null && playerEntity.PuppetMaster != null)
|
||||
return playerEntity.PuppetMaster.netIkController.GetRemoteHeight();
|
||||
|
||||
// player is invalid???
|
||||
return -1f;
|
||||
}
|
||||
|
||||
// we will create a Universal Scaler for only users that send a height update
|
||||
// this is sent at a rate of 10s locally!
|
||||
|
||||
internal void OnNetworkHeightUpdateReceived(string playerId, float targetHeight)
|
||||
{
|
||||
if (_networkedScalers.TryGetValue(playerId, out NetworkScaler scaler))
|
||||
scaler.TargetHeight = targetHeight;
|
||||
else
|
||||
SetupHeightScalerForNetwork(playerId, targetHeight);
|
||||
}
|
||||
|
||||
internal void OnNetworkAvatarInstantiated(PuppetMaster puppetMaster)
|
||||
{
|
||||
var playerId = puppetMaster._playerDescriptor.ownerId;
|
||||
if (_networkedScalers.TryGetValue(playerId, out NetworkScaler scaler))
|
||||
scaler.OnAvatarInstantiated(puppetMaster.avatarObject, puppetMaster.netIkController._initialHeight,
|
||||
puppetMaster.netIkController._initialScale);
|
||||
}
|
||||
|
||||
internal void OnNetworkAvatarDestroyed(PuppetMaster puppetMaster)
|
||||
{
|
||||
// on disconnect
|
||||
if (puppetMaster == null || puppetMaster._playerDescriptor == null)
|
||||
return;
|
||||
|
||||
var playerId = puppetMaster._playerDescriptor.ownerId;
|
||||
if (_networkedScalers.TryGetValue(playerId, out NetworkScaler scaler))
|
||||
scaler.OnAvatarDestroyed();
|
||||
}
|
||||
|
||||
internal void RemoveNetworkHeightScaler(string playerId)
|
||||
{
|
||||
if (_networkedScalers.ContainsKey(playerId))
|
||||
{
|
||||
AvatarScaleMod.Logger.Msg(
|
||||
$"Removed user height scaler! This is hopefully due to a disconnect or block. : {playerId}");
|
||||
|
||||
_networkedScalers.Remove(playerId);
|
||||
return;
|
||||
}
|
||||
|
||||
AvatarScaleMod.Logger.Msg(
|
||||
$"Failed to remove a user height scaler! This shouldn't happen. : {playerId}");
|
||||
}
|
||||
|
||||
private void SetupHeightScalerForNetwork(string playerId, float targetHeight)
|
||||
{
|
||||
CVRPlayerEntity playerEntity =
|
||||
CVRPlayerManager.Instance.NetworkPlayers.Find(players => players.Uuid == playerId);
|
||||
|
||||
PuppetMaster puppetMaster = playerEntity?.PuppetMaster;
|
||||
|
||||
if (playerEntity == null || puppetMaster == null)
|
||||
{
|
||||
AvatarScaleMod.Logger.Error(
|
||||
$"Attempted to set up height scaler for user which does not exist! : {playerId}");
|
||||
return;
|
||||
}
|
||||
|
||||
AvatarScaleMod.Logger.Msg(
|
||||
$"Setting up new height scaler for user which has sent a height update! : {playerId}");
|
||||
|
||||
if (_networkedScalers.ContainsKey(playerId))
|
||||
_networkedScalers.Remove(playerId); // ??
|
||||
|
||||
NetworkScaler scaler = puppetMaster.gameObject.AddComponent<NetworkScaler>();
|
||||
scaler.Initialize(playerId);
|
||||
|
||||
scaler.OnAvatarInstantiated(puppetMaster.avatarObject, puppetMaster.netIkController._initialHeight,
|
||||
puppetMaster.netIkController._initialScale);
|
||||
|
||||
_networkedScalers[playerId] = scaler;
|
||||
|
||||
scaler.TargetHeight = targetHeight;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Manager Methods
|
||||
|
||||
// sometimes fun to play with via UE
|
||||
public void SetUniversalScalingLimit(float min, float max)
|
||||
{
|
||||
const float HardCodedMinLimit = 0.01f;
|
||||
const float HardCodedMaxLimit = 100f;
|
||||
|
||||
MinHeight = Mathf.Clamp(min, HardCodedMinLimit, HardCodedMaxLimit);
|
||||
MaxHeight = Mathf.Clamp(max, HardCodedMinLimit, HardCodedMaxLimit);
|
||||
|
||||
AvatarScaleMod.Logger.Msg($"Universal Scaling Limits changed: {min} - {max}");
|
||||
AvatarScaleMod.Logger.Warning("This will not network to other users unless they also have the same limits set!");
|
||||
}
|
||||
|
||||
public void ResetUniversalScalingLimit()
|
||||
{
|
||||
MinHeight = DefaultMinHeight;
|
||||
MaxHeight = DefaultMaxHeight;
|
||||
|
||||
AvatarScaleMod.Logger.Msg("Universal Scaling Limits reset to default!");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Event Listeners
|
||||
|
||||
private static void OnAnimationHeightOverride(BaseScaler scaler)
|
||||
{
|
||||
AvatarScaleMod.Logger.Msg("AnimationClip-based avatar scaling detected. Disabling Universal Scaling.");
|
||||
CohtmlHud.Instance.ViewDropTextImmediate("(Local) AvatarScaleMod", "Avatar Scale Changed!",
|
||||
"Universal Scaling is now disabled in favor of built-in avatar scaling.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
|
@ -0,0 +1,367 @@
|
|||
using System.Diagnostics;
|
||||
using ABI_RC.Core;
|
||||
using ABI_RC.Core.Util.AnimatorManager;
|
||||
using NAK.AvatarScaleMod.AvatarScaling;
|
||||
using NAK.AvatarScaleMod.ScaledComponents;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Animations;
|
||||
|
||||
namespace NAK.AvatarScaleMod.Components;
|
||||
|
||||
[DefaultExecutionOrder(-99999)] // before playersetup/puppetmaster but after animator
|
||||
public class BaseScaler : MonoBehaviour
|
||||
{
|
||||
#region Constants
|
||||
|
||||
protected const string ScaleFactorParameterName = "ScaleFactor";
|
||||
protected const string ScaleFactorParameterNameLocal = "#" + ScaleFactorParameterName;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
// OnAnimatedHeightChanged
|
||||
public delegate void AnimatedHeightChangedDelegate(BaseScaler scaler);
|
||||
public event AnimatedHeightChangedDelegate OnAnimatedHeightChanged;
|
||||
|
||||
// OnAnimatedHeightOverride
|
||||
public delegate void AnimatedHeightOverrideDelegate(BaseScaler scaler);
|
||||
public event AnimatedHeightOverrideDelegate OnAnimatedHeightOverride;
|
||||
|
||||
// OnTargetHeightChanged
|
||||
public delegate void TargetHeightChangedDelegate(BaseScaler scaler);
|
||||
public event TargetHeightChangedDelegate OnTargetHeightChanged;
|
||||
|
||||
// OnHeightReset
|
||||
public delegate void HeightResetDelegate(BaseScaler scaler);
|
||||
public event HeightResetDelegate OnTargetHeightReset;
|
||||
|
||||
// ------------------------------------------------
|
||||
|
||||
protected void InvokeAnimatedHeightChanged()
|
||||
=> OnAnimatedHeightChanged?.Invoke(this);
|
||||
|
||||
protected void InvokeAnimatedHeightOverride()
|
||||
=> OnAnimatedHeightOverride?.Invoke(this);
|
||||
|
||||
protected void InvokeTargetHeightChanged()
|
||||
=> OnTargetHeightChanged?.Invoke(this);
|
||||
|
||||
protected void InvokeTargetHeightReset()
|
||||
=> OnTargetHeightReset?.Invoke(this);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Variables
|
||||
|
||||
private float _targetHeight;
|
||||
public float TargetHeight
|
||||
{
|
||||
get => _targetHeight;
|
||||
set
|
||||
{
|
||||
if (value < float.Epsilon)
|
||||
{
|
||||
// reset to animated height
|
||||
_targetHeight = _animatedHeight;
|
||||
_targetScale = _animatedScale;
|
||||
_scaleFactor = _animatedScaleFactor;
|
||||
_targetHeightChanged = true;
|
||||
return;
|
||||
}
|
||||
|
||||
_targetHeight = Mathf.Clamp(value, AvatarScaleManager.MinHeight, AvatarScaleManager.MaxHeight);
|
||||
_scaleFactor = Mathf.Max(_targetHeight / _initialHeight, 0.01f); //safety
|
||||
_targetScale = _initialScale * _scaleFactor;
|
||||
_targetHeightChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
protected bool _useTargetHeight;
|
||||
public bool UseTargetHeight
|
||||
{
|
||||
get => _useTargetHeight;
|
||||
set
|
||||
{
|
||||
if (_useTargetHeight == value)
|
||||
return;
|
||||
|
||||
_useTargetHeight = value;
|
||||
_targetHeightChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Config variables
|
||||
public bool avatarIsHidden { get; set; }
|
||||
public bool overrideAnimationHeight { get; set; }
|
||||
|
||||
// State variables
|
||||
internal bool _isAvatarInstantiated;
|
||||
private bool _shouldForceHeight => _useTargetHeight || avatarIsHidden;
|
||||
private bool _targetHeightChanged;
|
||||
|
||||
// Avatar info
|
||||
internal Transform _avatarTransform;
|
||||
internal CVRAnimatorManager _animatorManager;
|
||||
|
||||
// Initial scaling
|
||||
internal float _initialHeight;
|
||||
internal Vector3 _initialScale;
|
||||
|
||||
// Forced scaling (Universal & Hidden Avatar)
|
||||
internal Vector3 _targetScale = Vector3.one;
|
||||
internal float _scaleFactor = 1f;
|
||||
|
||||
// AnimationClip-based scaling (Local Avatar)
|
||||
internal float _animatedHeight;
|
||||
internal Vector3 _animatedScale;
|
||||
internal float _animatedScaleFactor = 1f;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Avatar Events
|
||||
|
||||
public virtual void OnAvatarInstantiated(GameObject avatarObject, float initialHeight, Vector3 initialScale)
|
||||
{
|
||||
if (_isAvatarInstantiated) return;
|
||||
_isAvatarInstantiated = true;
|
||||
|
||||
_initialHeight = _animatedHeight = Mathf.Clamp(initialHeight, 0.01f, 100f);
|
||||
_initialScale = _animatedScale = initialScale;
|
||||
_animatedScaleFactor = 1f;
|
||||
|
||||
if (!_shouldForceHeight) // not universal or hidden avatar
|
||||
{
|
||||
_targetHeight = _initialHeight;
|
||||
_targetScale = _initialScale;
|
||||
_scaleFactor = 1f;
|
||||
}
|
||||
|
||||
_avatarTransform = avatarObject.transform;
|
||||
|
||||
Stopwatch stopwatch = new();
|
||||
stopwatch.Start();
|
||||
|
||||
FindComponentsOfType(scalableComponentTypes);
|
||||
|
||||
stopwatch.Stop();
|
||||
if (ModSettings.Debug_ComponentSearchTime.Value)
|
||||
AvatarScaleMod.Logger.Msg($"({typeof(LocalScaler)}) Component search time for {avatarObject}: {stopwatch.ElapsedMilliseconds}ms");
|
||||
}
|
||||
|
||||
public void OnAvatarDestroyed()
|
||||
{
|
||||
if (!_isAvatarInstantiated) return;
|
||||
_isAvatarInstantiated = false;
|
||||
|
||||
_avatarTransform = null;
|
||||
ClearComponentLists();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public float GetInitialHeight() => _initialHeight;
|
||||
public float GetTargetHeight() => _targetHeight;
|
||||
public float GetAnimatedHeight() => _animatedHeight;
|
||||
public bool IsForcingHeight() => _shouldForceHeight;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
public bool ApplyTargetHeight()
|
||||
{
|
||||
if (!_isAvatarInstantiated || _initialHeight == 0)
|
||||
return false;
|
||||
|
||||
if (_avatarTransform == null)
|
||||
return false;
|
||||
|
||||
_targetHeightChanged = false;
|
||||
|
||||
ScaleAvatarRoot();
|
||||
UpdateAnimatorParameter();
|
||||
ApplyComponentScaling();
|
||||
|
||||
InvokeTargetHeightChanged();
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void ResetTargetHeight()
|
||||
{
|
||||
if (!_isAvatarInstantiated)
|
||||
return;
|
||||
|
||||
if (_avatarTransform == null)
|
||||
return;
|
||||
|
||||
_targetHeight = _initialHeight;
|
||||
_targetScale = _initialScale;
|
||||
_scaleFactor = 1f;
|
||||
_targetHeightChanged = false;
|
||||
|
||||
ScaleAvatarRoot();
|
||||
UpdateAnimatorParameter();
|
||||
ApplyComponentScaling();
|
||||
|
||||
InvokeTargetHeightReset();
|
||||
}
|
||||
|
||||
private void ScaleAvatarRoot()
|
||||
{
|
||||
if (_avatarTransform == null) return;
|
||||
_avatarTransform.localScale = _targetScale;
|
||||
}
|
||||
|
||||
protected virtual void UpdateAnimatorParameter()
|
||||
{
|
||||
// empty
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity Events
|
||||
|
||||
public virtual void LateUpdate()
|
||||
{
|
||||
if (!_isAvatarInstantiated)
|
||||
return; // no avatar
|
||||
|
||||
if (!_shouldForceHeight)
|
||||
return; // using built-in scaling
|
||||
|
||||
// called on state change
|
||||
if (_targetHeightChanged)
|
||||
{
|
||||
if (_useTargetHeight)
|
||||
ApplyTargetHeight();
|
||||
else
|
||||
ResetTargetHeight();
|
||||
_targetHeightChanged = false;
|
||||
InvokeTargetHeightChanged();
|
||||
}
|
||||
|
||||
// called constantly when forcing change
|
||||
if (_shouldForceHeight)
|
||||
ScaleAvatarRoot();
|
||||
}
|
||||
|
||||
internal virtual void OnDestroy()
|
||||
{
|
||||
ClearComponentLists();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Component Scaling
|
||||
|
||||
internal static readonly Type[] scalableComponentTypes =
|
||||
{
|
||||
typeof(Light),
|
||||
typeof(AudioSource),
|
||||
typeof(ParticleSystem),
|
||||
typeof(ParentConstraint),
|
||||
typeof(PositionConstraint),
|
||||
typeof(ScaleConstraint)
|
||||
};
|
||||
|
||||
private readonly List<ScaledLight> _scaledLights = new();
|
||||
private readonly List<ScaledAudioSource> _scaledAudioSources = new();
|
||||
private readonly List<ScaledParentConstraint> _scaledParentConstraints = new();
|
||||
private readonly List<ScaledPositionConstraint> _scaledPositionConstraints = new();
|
||||
private readonly List<ScaledScaleConstraint> _scaledScaleConstraints = new();
|
||||
|
||||
private void ClearComponentLists()
|
||||
{
|
||||
_scaledLights.Clear();
|
||||
_scaledAudioSources.Clear();
|
||||
_scaledParentConstraints.Clear();
|
||||
_scaledPositionConstraints.Clear();
|
||||
_scaledScaleConstraints.Clear();
|
||||
}
|
||||
|
||||
internal void FindComponentsOfType(Type[] types)
|
||||
{
|
||||
var components = _avatarTransform.gameObject.GetComponentsInChildren<Component>(true);
|
||||
|
||||
foreach (Component component in components)
|
||||
{
|
||||
if (this == null) break;
|
||||
if (component == null) continue;
|
||||
|
||||
Type componentType = component.GetType();
|
||||
if (types.Contains(componentType))
|
||||
AddScaledComponent(componentType, component);
|
||||
}
|
||||
}
|
||||
|
||||
private void AddScaledComponent(Type type, Component component)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case not null when type == typeof(AudioSource):
|
||||
_scaledAudioSources.Add(new ScaledAudioSource((AudioSource)component));
|
||||
break;
|
||||
case not null when type == typeof(Light):
|
||||
_scaledLights.Add(new ScaledLight((Light)component));
|
||||
break;
|
||||
case not null when type == typeof(ParentConstraint):
|
||||
_scaledParentConstraints.Add(new ScaledParentConstraint((ParentConstraint)component));
|
||||
break;
|
||||
case not null when type == typeof(PositionConstraint):
|
||||
_scaledPositionConstraints.Add(new ScaledPositionConstraint((PositionConstraint)component));
|
||||
break;
|
||||
case not null when type == typeof(ScaleConstraint):
|
||||
_scaledScaleConstraints.Add(new ScaledScaleConstraint((ScaleConstraint)component));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyComponentScaling()
|
||||
{
|
||||
// UpdateLightScales(); // might break dps
|
||||
UpdateAudioSourceScales();
|
||||
UpdateParentConstraintScales();
|
||||
UpdatePositionConstraintScales();
|
||||
UpdateScaleConstraintScales();
|
||||
}
|
||||
|
||||
private void UpdateLightScales()
|
||||
{
|
||||
// Update range of each light component
|
||||
foreach (ScaledLight light in _scaledLights)
|
||||
light.Scale(_scaleFactor);
|
||||
}
|
||||
|
||||
private void UpdateAudioSourceScales()
|
||||
{
|
||||
// Update min and max distance of each audio source component
|
||||
foreach (ScaledAudioSource audioSource in _scaledAudioSources)
|
||||
audioSource.Scale(_scaleFactor);
|
||||
}
|
||||
|
||||
private void UpdateParentConstraintScales()
|
||||
{
|
||||
// Update translationAtRest and translationOffsets of each parent constraint component
|
||||
foreach (ScaledParentConstraint parentConstraint in _scaledParentConstraints)
|
||||
parentConstraint.Scale(_scaleFactor);
|
||||
}
|
||||
|
||||
private void UpdatePositionConstraintScales()
|
||||
{
|
||||
// Update translationAtRest and translationOffset of each position constraint component
|
||||
foreach (ScaledPositionConstraint positionConstraint in _scaledPositionConstraints)
|
||||
positionConstraint.Scale(_scaleFactor);
|
||||
}
|
||||
|
||||
private void UpdateScaleConstraintScales()
|
||||
{
|
||||
// Update scaleAtRest and scaleOffset of each scale constraint component
|
||||
foreach (ScaledScaleConstraint scaleConstraint in _scaledScaleConstraints)
|
||||
scaleConstraint.Scale(_scaleFactor);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
using ABI_RC.Core;
|
||||
using ABI_RC.Core.Player;
|
||||
using ABI_RC.Core.UI;
|
||||
using NAK.AvatarScaleMod.AvatarScaling;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NAK.AvatarScaleMod.Components;
|
||||
|
||||
public class LocalScaler : BaseScaler
|
||||
{
|
||||
#region Public Methods
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_animatorManager = GetComponentInParent<PlayerSetup>().animatorManager;
|
||||
_isAvatarInstantiated = false;
|
||||
|
||||
// listen for events
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Overrides
|
||||
|
||||
public override void OnAvatarInstantiated(GameObject avatarObject, float initialHeight, Vector3 initialScale)
|
||||
{
|
||||
if (avatarObject == null)
|
||||
return;
|
||||
|
||||
base.OnAvatarInstantiated(avatarObject, initialHeight, initialScale);
|
||||
}
|
||||
|
||||
protected override void UpdateAnimatorParameter()
|
||||
{
|
||||
if (_animatorManager == null)
|
||||
return;
|
||||
|
||||
_animatorManager.SetParameter(ScaleFactorParameterName, _scaleFactor);
|
||||
_animatorManager.SetParameter(ScaleFactorParameterNameLocal, _scaleFactor);
|
||||
}
|
||||
|
||||
public override void LateUpdate()
|
||||
{
|
||||
if (!CheckForAnimationScaleChange())
|
||||
base.LateUpdate();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private bool CheckForAnimationScaleChange()
|
||||
{
|
||||
if (_avatarTransform == null)
|
||||
return false;
|
||||
|
||||
Vector3 localScale = _avatarTransform.localScale;
|
||||
|
||||
// scale matches last recorded animation scale
|
||||
if (localScale == _animatedScale)
|
||||
return false;
|
||||
|
||||
// avatar may not have scale animation, check if it isn't equal to targetScale
|
||||
if (localScale == _targetScale)
|
||||
return false;
|
||||
|
||||
// this is the first time we've seen the avatar animated scale, record it!
|
||||
if (_animatedScale == Vector3.zero)
|
||||
{
|
||||
_animatedScale = localScale;
|
||||
return false;
|
||||
}
|
||||
|
||||
// animation scale changed, record it!
|
||||
Vector3 scaleDifference = CVRTools.DivideVectors(localScale - _initialScale, _initialScale);
|
||||
_animatedScaleFactor = scaleDifference.y;
|
||||
_animatedHeight = (_initialHeight * _animatedScaleFactor) + _initialHeight;
|
||||
_animatedScale = localScale;
|
||||
|
||||
if (overrideAnimationHeight
|
||||
|| !_useTargetHeight)
|
||||
return false; // user has disabled animation height override or is not using universal scaling
|
||||
|
||||
// animation scale changed and now will override universal scaling
|
||||
ResetTargetHeight();
|
||||
InvokeAnimatedHeightOverride();
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
using System.Diagnostics;
|
||||
using ABI_RC.Core.Player;
|
||||
using NAK.AvatarScaleMod.AvatarScaling;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NAK.AvatarScaleMod.Components;
|
||||
|
||||
public class NetworkScaler : BaseScaler
|
||||
{
|
||||
private string playerGuid;
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public void Initialize(string playerId)
|
||||
{
|
||||
playerGuid = playerId;
|
||||
|
||||
_animatorManager = GetComponentInParent<PuppetMaster>().animatorManager;
|
||||
|
||||
_isAvatarInstantiated = false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Overrides
|
||||
|
||||
public override void OnAvatarInstantiated(GameObject avatarObject, float initialHeight, Vector3 initialScale)
|
||||
{
|
||||
if (avatarObject == null)
|
||||
return;
|
||||
|
||||
base.OnAvatarInstantiated(avatarObject, initialHeight, initialScale);
|
||||
|
||||
Stopwatch stopwatch = new();
|
||||
stopwatch.Start();
|
||||
FindComponentsOfType(scalableComponentTypes);
|
||||
stopwatch.Stop();
|
||||
if (ModSettings.Debug_ComponentSearchTime.Value)
|
||||
AvatarScaleMod.Logger.Msg($"({typeof(NetworkScaler)}) Component search time for {avatarObject}: {stopwatch.ElapsedMilliseconds}ms");
|
||||
|
||||
// TODO: why did i do this? height is never set prior to this method being called
|
||||
// if (_isHeightAdjustedFromInitial && heightNeedsUpdate)
|
||||
// UpdateScaleIfInstantiated();
|
||||
}
|
||||
|
||||
protected override void UpdateAnimatorParameter()
|
||||
{
|
||||
_animatorManager?.SetParameter(ScaleFactorParameterNameLocal, _scaleFactor);
|
||||
}
|
||||
|
||||
internal override void OnDestroy()
|
||||
{
|
||||
AvatarScaleManager.Instance.RemoveNetworkHeightScaler(playerGuid);
|
||||
base.OnDestroy();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
|
@ -0,0 +1,102 @@
|
|||
using NAK.AvatarScaleMod.Components;
|
||||
|
||||
namespace NAK.AvatarScaleMod.AvatarScaling;
|
||||
|
||||
public static class AvatarScaleEvents
|
||||
{
|
||||
#region Local Avatar Scaling Events
|
||||
|
||||
/// <summary>
|
||||
/// Invoked when the local avatar's height changes for any reason.
|
||||
/// </summary>
|
||||
public static readonly AvatarScaleEvent<LocalScaler> OnLocalAvatarHeightChanged = new();
|
||||
|
||||
/// <summary>
|
||||
/// Invoked when the local avatar's animated height changes.
|
||||
/// </summary>
|
||||
public static readonly AvatarScaleEvent<LocalScaler> OnLocalAvatarAnimatedHeightChanged = new();
|
||||
|
||||
/// <summary>
|
||||
/// Invoked when the local avatar's target height changes.
|
||||
/// </summary>
|
||||
public static readonly AvatarScaleEvent<LocalScaler> OnLocalAvatarTargetHeightChanged = new();
|
||||
|
||||
/// <summary>
|
||||
/// Invoked when the local avatar's height is reset.
|
||||
/// </summary>
|
||||
public static readonly AvatarScaleEvent<LocalScaler> OnLocalAvatarHeightReset = new();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Avatar Scaling Events
|
||||
|
||||
/// <summary>
|
||||
/// Invoked when a remote avatar's height changes.
|
||||
/// </summary>
|
||||
public static readonly AvatarScaleEvent<string, NetworkScaler> OnRemoteAvatarHeightChanged = new();
|
||||
|
||||
/// <summary>
|
||||
/// Invoked when a remote avatar's height is reset.
|
||||
/// </summary>
|
||||
public static readonly AvatarScaleEvent<string, NetworkScaler> OnRemoteAvatarHeightReset = new();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Event Classes
|
||||
|
||||
public class AvatarScaleEvent<T>
|
||||
{
|
||||
private Action<T> _listener = arg => { };
|
||||
|
||||
public void AddListener(Action<T> listener) => _listener += listener;
|
||||
public void RemoveListener(Action<T> listener) => _listener -= listener;
|
||||
|
||||
public void Invoke(T arg)
|
||||
{
|
||||
var invokeList = _listener.GetInvocationList();
|
||||
foreach (Delegate method in invokeList)
|
||||
{
|
||||
if (method is not Action<T> action)
|
||||
continue;
|
||||
|
||||
try
|
||||
{
|
||||
action(arg);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
AvatarScaleMod.Logger.Error($"Unable to invoke listener, an exception was thrown and not handled: {e}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class AvatarScaleEvent<T1, T2>
|
||||
{
|
||||
private Action<T1, T2> _listener = (arg1, arg2) => { };
|
||||
|
||||
public void AddListener(Action<T1, T2> listener) => _listener += listener;
|
||||
public void RemoveListener(Action<T1, T2> listener) => _listener -= listener;
|
||||
|
||||
public void Invoke(T1 arg1, T2 arg2)
|
||||
{
|
||||
var invokeList = _listener.GetInvocationList();
|
||||
foreach (Delegate method in invokeList)
|
||||
{
|
||||
if (method is not Action<T1, T2> action)
|
||||
continue;
|
||||
|
||||
try
|
||||
{
|
||||
action(arg1, arg2);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
AvatarScaleMod.Logger.Error($"Unable to invoke listener, an exception was thrown and not handled: {e}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
132
.Deprecated/AvatarScaleMod/AvatarScaling/ScaledComponents.cs
Normal file
132
.Deprecated/AvatarScaleMod/AvatarScaling/ScaledComponents.cs
Normal file
|
@ -0,0 +1,132 @@
|
|||
using UnityEngine;
|
||||
using UnityEngine.Animations;
|
||||
|
||||
namespace NAK.AvatarScaleMod.ScaledComponents;
|
||||
|
||||
public class ScaledAudioSource
|
||||
{
|
||||
private readonly AudioSource Component;
|
||||
private readonly float InitialMinDistance;
|
||||
private readonly float InitialMaxDistance;
|
||||
|
||||
public ScaledAudioSource(AudioSource component)
|
||||
{
|
||||
Component = component;
|
||||
InitialMinDistance = component.minDistance;
|
||||
InitialMaxDistance = component.maxDistance;
|
||||
}
|
||||
|
||||
public void Scale(float scaleFactor)
|
||||
{
|
||||
Component.minDistance = InitialMinDistance * scaleFactor;
|
||||
Component.maxDistance = InitialMaxDistance * scaleFactor;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
Component.minDistance = InitialMinDistance;
|
||||
Component.maxDistance = InitialMaxDistance;
|
||||
}
|
||||
}
|
||||
|
||||
public class ScaledLight
|
||||
{
|
||||
private readonly Light Component;
|
||||
private readonly float InitialRange;
|
||||
|
||||
public ScaledLight(Light component)
|
||||
{
|
||||
Component = component;
|
||||
InitialRange = component.range;
|
||||
}
|
||||
|
||||
public void Scale(float scaleFactor)
|
||||
{
|
||||
Component.range = InitialRange * scaleFactor;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
Component.range = InitialRange;
|
||||
}
|
||||
}
|
||||
|
||||
public class ScaledPositionConstraint
|
||||
{
|
||||
private readonly PositionConstraint Component;
|
||||
private readonly Vector3 InitialTranslationAtRest;
|
||||
private readonly Vector3 InitialTranslationOffset;
|
||||
|
||||
public ScaledPositionConstraint(PositionConstraint component)
|
||||
{
|
||||
Component = component;
|
||||
InitialTranslationAtRest = component.translationAtRest;
|
||||
InitialTranslationOffset = component.translationOffset;
|
||||
}
|
||||
|
||||
public void Scale(float scaleFactor)
|
||||
{
|
||||
Component.translationAtRest = InitialTranslationAtRest * scaleFactor;
|
||||
Component.translationOffset = InitialTranslationOffset * scaleFactor;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
Component.translationAtRest = InitialTranslationAtRest;
|
||||
Component.translationOffset = InitialTranslationOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public class ScaledParentConstraint
|
||||
{
|
||||
private readonly ParentConstraint Component;
|
||||
private readonly Vector3 InitialTranslationAtRest;
|
||||
private readonly List<Vector3> InitialTranslationOffsets;
|
||||
|
||||
public ScaledParentConstraint(ParentConstraint component)
|
||||
{
|
||||
Component = component;
|
||||
InitialTranslationAtRest = component.translationAtRest;
|
||||
InitialTranslationOffsets = component.translationOffsets.ToList();
|
||||
}
|
||||
|
||||
public void Scale(float scaleFactor)
|
||||
{
|
||||
Component.translationAtRest = InitialTranslationAtRest * scaleFactor;
|
||||
for (int i = 0; i < InitialTranslationOffsets.Count; i++)
|
||||
Component.translationOffsets[i] = InitialTranslationOffsets[i] * scaleFactor;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
Component.translationAtRest = InitialTranslationAtRest;
|
||||
for (int i = 0; i < InitialTranslationOffsets.Count; i++)
|
||||
Component.translationOffsets[i] = InitialTranslationOffsets[i];
|
||||
}
|
||||
}
|
||||
|
||||
public class ScaledScaleConstraint
|
||||
{
|
||||
private readonly ScaleConstraint Component;
|
||||
private readonly Vector3 InitialScaleAtRest;
|
||||
private readonly Vector3 InitialScaleOffset;
|
||||
|
||||
public ScaledScaleConstraint(ScaleConstraint component)
|
||||
{
|
||||
Component = component;
|
||||
InitialScaleAtRest = component.scaleAtRest;
|
||||
InitialScaleOffset = component.scaleOffset;
|
||||
}
|
||||
|
||||
public void Scale(float scaleFactor)
|
||||
{
|
||||
Component.scaleAtRest = InitialScaleAtRest * scaleFactor;
|
||||
// Component.scaleOffset = InitialScaleOffset * scaleFactor;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
Component.scaleAtRest = InitialScaleAtRest;
|
||||
Component.scaleOffset = InitialScaleOffset;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue