mirror of
https://github.com/NotAKidoS/NAK_CVR_Mods.git
synced 2025-09-02 14:29:25 +00:00
i dont rememebr
This commit is contained in:
parent
374ab6c11e
commit
86828a94e2
48 changed files with 1637 additions and 841 deletions
|
@ -1,4 +1,5 @@
|
|||
using ABI_RC.Core.IO;
|
||||
using System.Collections;
|
||||
using ABI_RC.Core.IO;
|
||||
using ABI_RC.Core.Player;
|
||||
using ABI_RC.Core.Player.AvatarTracking;
|
||||
using ABI_RC.Core.UI;
|
||||
|
@ -11,14 +12,29 @@ namespace NAK.AvatarScaleMod.AvatarScaling;
|
|||
|
||||
public class AvatarScaleManager : MonoBehaviour
|
||||
{
|
||||
// Universal Scaling Limits
|
||||
public const float MinHeight = 0.1f;
|
||||
public const float MaxHeight = 10f;
|
||||
|
||||
public static AvatarScaleManager Instance;
|
||||
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/NotAKidOnSteam/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
|
||||
|
@ -27,21 +43,25 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
set
|
||||
{
|
||||
if (value != _settingUniversalScaling && value == false)
|
||||
ResetHeight();
|
||||
ResetTargetHeight();
|
||||
|
||||
_settingUniversalScaling = value;
|
||||
SetTargetHeight(_lastTargetHeight); // immediate height update
|
||||
}
|
||||
}
|
||||
|
||||
public bool Setting_PersistantHeight;
|
||||
public bool Setting_AnimationClipScalingOverride;
|
||||
public bool Setting_PersistentHeight;
|
||||
private float _lastTargetHeight = -1;
|
||||
|
||||
|
||||
#region Unity Methods
|
||||
#endregion
|
||||
|
||||
#region Unity Events
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null)
|
||||
if (Instance != null
|
||||
&& Instance != this)
|
||||
{
|
||||
DestroyImmediate(this);
|
||||
return;
|
||||
|
@ -53,22 +73,82 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
|
||||
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;
|
||||
|
||||
// update local scaler
|
||||
if (_localAvatarScaler != null && _localAvatarScaler.heightNeedsUpdate)
|
||||
{
|
||||
if (_localAvatarScaler.ApplyTargetHeight())
|
||||
AvatarScaleEvents.OnLocalAvatarHeightChanged.Invoke(_localAvatarScaler);
|
||||
}
|
||||
|
||||
// update networked scalers (probably a better way to do this)
|
||||
foreach (var netScaler in _networkedScalers)
|
||||
{
|
||||
if (!netScaler.Value.heightNeedsUpdate) continue;
|
||||
if (netScaler.Value.ApplyTargetHeight())
|
||||
AvatarScaleEvents.OnRemoteAvatarHeightChanged.Invoke(netScaler.Key, netScaler.Value);
|
||||
}
|
||||
}
|
||||
|
||||
// ReSharper disable once IteratorNeverReturns
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
#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);
|
||||
}
|
||||
|
||||
|
@ -81,19 +161,13 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
if (playerSetup._avatar == null)
|
||||
return;
|
||||
|
||||
if (_localAvatarScaler == null)
|
||||
{
|
||||
_localAvatarScaler = playerSetup.gameObject.AddComponent<LocalScaler>();
|
||||
_localAvatarScaler.Initialize();
|
||||
}
|
||||
|
||||
_localAvatarScaler.OnAvatarInstantiated(playerSetup._avatar, playerSetup._initialAvatarHeight,
|
||||
playerSetup.initialScale);
|
||||
|
||||
if (!_settingUniversalScaling)
|
||||
return;
|
||||
|
||||
SetHeight(Setting_PersistantHeight ? _lastTargetHeight : -1f);
|
||||
SetTargetHeight(_lastTargetHeight);
|
||||
}
|
||||
|
||||
public void OnAvatarDestroyed(PlayerSetup playerSetup)
|
||||
|
@ -102,35 +176,34 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
_localAvatarScaler.OnAvatarDestroyed();
|
||||
}
|
||||
|
||||
public void SetHeight(float targetHeight)
|
||||
public void SetTargetHeight(float targetHeight)
|
||||
{
|
||||
_lastTargetHeight = targetHeight; // save for persistent height
|
||||
ModSettings.EntryHiddenAvatarHeight.Value = targetHeight; // save for restart
|
||||
|
||||
if (!_settingUniversalScaling)
|
||||
return;
|
||||
|
||||
if (_localAvatarScaler == null)
|
||||
return;
|
||||
|
||||
_lastTargetHeight = targetHeight;
|
||||
|
||||
_localAvatarScaler.SetTargetHeight(targetHeight);
|
||||
ModNetwork.SendNetworkHeight(targetHeight);
|
||||
|
||||
// immediately update play space scale
|
||||
PlayerSetup.Instance.CheckUpdateAvatarScaleToPlaySpaceRelation();
|
||||
_localAvatarScaler.SetTargetHeight(_lastTargetHeight);
|
||||
_localAvatarScaler.heightNeedsUpdate = true; // only local scaler forces update
|
||||
}
|
||||
|
||||
public void ResetHeight()
|
||||
|
||||
public void ResetTargetHeight()
|
||||
{
|
||||
if (_localAvatarScaler == null)
|
||||
return;
|
||||
|
||||
if (!_localAvatarScaler.IsHeightAdjustedFromInitial())
|
||||
if (!_localAvatarScaler.IsForcingHeight())
|
||||
return;
|
||||
|
||||
// TODO: doesnt work when hitting Reset on slider in BTK UI (is it on main thread?)
|
||||
CohtmlHud.Instance.ViewDropTextImmediate("(Local) AvatarScaleMod", "Avatar Scale Reset!",
|
||||
"Universal Scaling is now disabled.");
|
||||
|
||||
SetHeight(-1f);
|
||||
SetTargetHeight(-1f);
|
||||
}
|
||||
|
||||
public float GetHeight()
|
||||
|
@ -138,10 +211,21 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
if (_localAvatarScaler == null)
|
||||
return PlayerAvatarPoint.defaultAvatarHeight;
|
||||
|
||||
if (!_localAvatarScaler.IsHeightAdjustedFromInitial())
|
||||
if (!_localAvatarScaler.IsForcingHeight())
|
||||
return PlayerSetup.Instance.GetAvatarHeight();
|
||||
|
||||
return _localAvatarScaler.GetHeight();
|
||||
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()
|
||||
|
@ -152,10 +236,10 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
if (_localAvatarScaler == null)
|
||||
return -1f;
|
||||
|
||||
if (!_localAvatarScaler.IsHeightAdjustedFromInitial())
|
||||
if (!_localAvatarScaler.IsForcingHeight())
|
||||
return -1f;
|
||||
|
||||
return _localAvatarScaler.GetHeight();
|
||||
return _localAvatarScaler.GetTargetHeight();
|
||||
}
|
||||
|
||||
public float GetInitialHeight()
|
||||
|
@ -168,17 +252,23 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
|
||||
public bool IsHeightAdjustedFromInitial()
|
||||
{
|
||||
return _localAvatarScaler != null && _localAvatarScaler.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.IsHeightAdjustedFromInitial()) return scaler.GetHeight();
|
||||
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);
|
||||
|
@ -266,4 +356,40 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
}
|
||||
|
||||
#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
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
using ABI_RC.Core;
|
||||
using ABI_RC.Core.Player;
|
||||
using System.Diagnostics;
|
||||
using ABI_RC.Core;
|
||||
using NAK.AvatarScaleMod.AvatarScaling;
|
||||
using NAK.AvatarScaleMod.ScaledComponents;
|
||||
using UnityEngine;
|
||||
|
@ -12,143 +12,201 @@ public class BaseScaler : MonoBehaviour
|
|||
{
|
||||
#region Constants
|
||||
|
||||
public const string ScaleFactorParameterName = "ScaleFactor";
|
||||
public const string ScaleFactorParameterNameLocal = "#ScaleFactor";
|
||||
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
|
||||
|
||||
internal bool _isAvatarInstantiated;
|
||||
internal bool _isHeightAdjustedFromInitial;
|
||||
internal bool _heightNeedsUpdate;
|
||||
// Height update requested
|
||||
public bool heightNeedsUpdate { get; internal set; }
|
||||
|
||||
// Config variables
|
||||
public bool avatarIsHidden { get; set; }
|
||||
public bool useTargetHeight { get; set; }
|
||||
public bool overrideAnimationHeight { get; set; }
|
||||
|
||||
// State variables
|
||||
internal bool _isAvatarInstantiated;
|
||||
internal bool _shouldForceHeight => useTargetHeight || avatarIsHidden; // universal or hidden avatar
|
||||
|
||||
// Avatar info
|
||||
internal Transform _avatarTransform;
|
||||
internal CVRAnimatorManager _animatorManager;
|
||||
|
||||
|
||||
// Initial scaling
|
||||
internal float _initialHeight;
|
||||
internal Vector3 _initialScale;
|
||||
|
||||
// Forced scaling (Universal & Hidden Avatar)
|
||||
internal float _targetHeight = -1;
|
||||
internal Vector3 _targetScale = Vector3.one;
|
||||
internal float _scaleFactor = 1f;
|
||||
|
||||
// detection for animation clip-based scaling
|
||||
internal Vector3 _legacyAnimationScale;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
// 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 = Mathf.Clamp(initialHeight, 0.01f, 100f);
|
||||
_initialScale = initialScale;
|
||||
_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;
|
||||
_heightNeedsUpdate = false;
|
||||
heightNeedsUpdate = false;
|
||||
ClearComponentLists();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public float GetInitialHeight() => _initialHeight;
|
||||
public float GetTargetHeight() => _targetHeight;
|
||||
public float GetAnimatedHeight() => _animatedHeight;
|
||||
public bool IsForcingHeight() => _shouldForceHeight;
|
||||
|
||||
public void SetTargetHeight(float height)
|
||||
{
|
||||
if (_isHeightAdjustedFromInitial
|
||||
&& Math.Abs(height - _targetHeight) < float.Epsilon)
|
||||
return;
|
||||
|
||||
if (height < float.Epsilon)
|
||||
{
|
||||
ResetHeight();
|
||||
ResetTargetHeight();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_isHeightAdjustedFromInitial)
|
||||
_legacyAnimationScale = Vector3.zero;
|
||||
|
||||
_isHeightAdjustedFromInitial = true;
|
||||
|
||||
_targetHeight = Mathf.Clamp(height, AvatarScaleManager.MinHeight, AvatarScaleManager.MaxHeight);
|
||||
_heightNeedsUpdate = true;
|
||||
|
||||
UpdateScaleIfInstantiated();
|
||||
}
|
||||
|
||||
public void ResetHeight()
|
||||
{
|
||||
if (!_isHeightAdjustedFromInitial) return;
|
||||
_isHeightAdjustedFromInitial = false;
|
||||
|
||||
if (Math.Abs(_initialHeight - _targetHeight) < float.Epsilon)
|
||||
return;
|
||||
|
||||
_legacyAnimationScale = Vector3.zero;
|
||||
_scaleFactor = Mathf.Max(_targetHeight / _initialHeight, 0.01f); //safety
|
||||
_targetScale = _initialScale * _scaleFactor;
|
||||
|
||||
_targetHeight = _initialHeight;
|
||||
_heightNeedsUpdate = true;
|
||||
|
||||
UpdateScaleIfInstantiated();
|
||||
InvokeTargetHeightChanged();
|
||||
}
|
||||
|
||||
public float GetHeight() => _targetHeight;
|
||||
public float GetInitialHeight() => _initialHeight;
|
||||
public bool IsHeightAdjustedFromInitial() => _isHeightAdjustedFromInitial;
|
||||
public void ResetTargetHeight()
|
||||
{
|
||||
// if (Math.Abs(_initialHeight - _targetHeight) < float.Epsilon)
|
||||
// return; // no need to change, is close enough
|
||||
|
||||
useTargetHeight = false;
|
||||
|
||||
_targetHeight = _animatedHeight;
|
||||
_targetScale = _animatedScale;
|
||||
_scaleFactor = _animatedScaleFactor;
|
||||
|
||||
InvokeTargetHeightReset();
|
||||
}
|
||||
|
||||
public bool ApplyTargetHeight()
|
||||
{
|
||||
if (!_isAvatarInstantiated || _initialHeight == 0)
|
||||
return false;
|
||||
|
||||
if (_avatarTransform == null)
|
||||
return false;
|
||||
|
||||
heightNeedsUpdate = false;
|
||||
|
||||
ScaleAvatarRoot();
|
||||
UpdateAnimatorParameter();
|
||||
ApplyComponentScaling();
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Private Methods
|
||||
|
||||
internal void ScaleAvatarRoot()
|
||||
private void ScaleAvatarRoot()
|
||||
{
|
||||
if (_avatarTransform == null) return;
|
||||
_avatarTransform.localScale = _targetScale;
|
||||
}
|
||||
|
||||
internal virtual void UpdateAnimatorParameter()
|
||||
|
||||
protected virtual void UpdateAnimatorParameter()
|
||||
{
|
||||
// empty
|
||||
}
|
||||
|
||||
internal void UpdateScaleIfInstantiated()
|
||||
{
|
||||
if (!_isAvatarInstantiated || _initialHeight == 0)
|
||||
return;
|
||||
|
||||
if (_avatarTransform == null)
|
||||
return;
|
||||
|
||||
_scaleFactor = Mathf.Max(_targetHeight / _initialHeight, 0.01f); //safety
|
||||
|
||||
_heightNeedsUpdate = false;
|
||||
_targetScale = _initialScale * _scaleFactor;
|
||||
|
||||
ScaleAvatarRoot();
|
||||
UpdateAnimatorParameter();
|
||||
ApplyComponentScaling();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity Methods
|
||||
|
||||
#region Unity Events
|
||||
|
||||
public virtual void LateUpdate()
|
||||
{
|
||||
if (!_isHeightAdjustedFromInitial)
|
||||
return;
|
||||
if (!_isAvatarInstantiated)
|
||||
return; // no avatar
|
||||
|
||||
if (!_isAvatarInstantiated)
|
||||
return;
|
||||
|
||||
ScaleAvatarRoot(); // override animationclip-based scaling
|
||||
if (!_shouldForceHeight)
|
||||
return; // not universal scaling or hidden avatar
|
||||
|
||||
ScaleAvatarRoot();
|
||||
}
|
||||
|
||||
internal virtual void OnDestroy()
|
||||
|
@ -169,13 +227,13 @@ public class BaseScaler : MonoBehaviour
|
|||
typeof(PositionConstraint),
|
||||
typeof(ScaleConstraint)
|
||||
};
|
||||
|
||||
private readonly List<ScaledLight> _scaledLights = new List<ScaledLight>();
|
||||
private readonly List<ScaledAudioSource> _scaledAudioSources = new List<ScaledAudioSource>();
|
||||
private readonly List<ScaledParentConstraint> _scaledParentConstraints = new List<ScaledParentConstraint>();
|
||||
private readonly List<ScaledPositionConstraint> _scaledPositionConstraints = new List<ScaledPositionConstraint>();
|
||||
private readonly List<ScaledScaleConstraint> _scaledScaleConstraints = new List<ScaledScaleConstraint>();
|
||||
|
||||
|
||||
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();
|
||||
|
@ -184,10 +242,9 @@ public class BaseScaler : MonoBehaviour
|
|||
_scaledPositionConstraints.Clear();
|
||||
_scaledScaleConstraints.Clear();
|
||||
}
|
||||
|
||||
internal async Task FindComponentsOfTypeAsync(Type[] types)
|
||||
|
||||
internal void FindComponentsOfType(Type[] types)
|
||||
{
|
||||
var tasks = new List<Task>();
|
||||
var components = _avatarTransform.gameObject.GetComponentsInChildren<Component>(true);
|
||||
|
||||
foreach (Component component in components)
|
||||
|
@ -195,19 +252,12 @@ public class BaseScaler : MonoBehaviour
|
|||
if (this == null) break;
|
||||
if (component == null) continue;
|
||||
|
||||
tasks.Add(Task.Run(() =>
|
||||
{
|
||||
Type componentType = component.GetType();
|
||||
if (types.Contains(componentType))
|
||||
{
|
||||
AddScaledComponent(componentType, component);
|
||||
}
|
||||
}));
|
||||
Type componentType = component.GetType();
|
||||
if (types.Contains(componentType))
|
||||
AddScaledComponent(componentType, component);
|
||||
}
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
|
||||
|
||||
private void AddScaledComponent(Type type, Component component)
|
||||
{
|
||||
switch (type)
|
||||
|
@ -229,7 +279,7 @@ public class BaseScaler : MonoBehaviour
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void ApplyComponentScaling()
|
||||
{
|
||||
// UpdateLightScales(); // might break dps
|
||||
|
@ -273,6 +323,6 @@ public class BaseScaler : MonoBehaviour
|
|||
foreach (ScaledScaleConstraint scaleConstraint in _scaledScaleConstraints)
|
||||
scaleConstraint.Scale(_scaleFactor);
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
using ABI_RC.Core.Player;
|
||||
using ABI_RC.Core.UI;
|
||||
using ABI.CCK.Components;
|
||||
using NAK.AvatarScaleMod.AvatarScaling;
|
||||
using UnityEngine;
|
||||
|
||||
|
@ -14,31 +13,23 @@ public class LocalScaler : BaseScaler
|
|||
{
|
||||
_animatorManager = GetComponentInParent<PlayerSetup>().animatorManager;
|
||||
|
||||
_heightNeedsUpdate = false;
|
||||
heightNeedsUpdate = false;
|
||||
_isAvatarInstantiated = false;
|
||||
_isHeightAdjustedFromInitial = false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Overrides
|
||||
|
||||
public override async void OnAvatarInstantiated(GameObject avatarObject, float initialHeight, Vector3 initialScale)
|
||||
public override void OnAvatarInstantiated(GameObject avatarObject, float initialHeight, Vector3 initialScale)
|
||||
{
|
||||
if (avatarObject == null)
|
||||
return;
|
||||
|
||||
base.OnAvatarInstantiated(avatarObject, initialHeight, initialScale);
|
||||
await FindComponentsOfTypeAsync(scalableComponentTypes);
|
||||
|
||||
_targetHeight = initialHeight;
|
||||
_scaleFactor = 1f;
|
||||
|
||||
_isHeightAdjustedFromInitial = false;
|
||||
_legacyAnimationScale = Vector3.zero;
|
||||
}
|
||||
|
||||
internal override void UpdateAnimatorParameter()
|
||||
|
||||
protected override void UpdateAnimatorParameter()
|
||||
{
|
||||
if (_animatorManager == null)
|
||||
return;
|
||||
|
@ -49,14 +40,8 @@ public class LocalScaler : BaseScaler
|
|||
|
||||
public override void LateUpdate()
|
||||
{
|
||||
if (!_isHeightAdjustedFromInitial)
|
||||
return;
|
||||
|
||||
if (!_isAvatarInstantiated)
|
||||
return;
|
||||
|
||||
if (!CheckForAnimationScaleChange())
|
||||
ScaleAvatarRoot();
|
||||
base.LateUpdate();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
@ -65,30 +50,43 @@ public class LocalScaler : BaseScaler
|
|||
|
||||
private bool CheckForAnimationScaleChange()
|
||||
{
|
||||
if (_avatarTransform == null) return false;
|
||||
if (_avatarTransform == null)
|
||||
return false;
|
||||
|
||||
//scale matches last recorded animation scale
|
||||
if (_avatarTransform.localScale == _legacyAnimationScale)
|
||||
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 (_avatarTransform.localScale == _targetScale)
|
||||
if (localScale == _targetScale)
|
||||
return false;
|
||||
|
||||
// scale was likely reset or not initiated
|
||||
if (_legacyAnimationScale == Vector3.zero)
|
||||
// this is the first time we've seen the avatar animated scale, record it!
|
||||
if (_animatedScale == Vector3.zero)
|
||||
{
|
||||
_legacyAnimationScale = _avatarTransform.localScale;
|
||||
_animatedScale = localScale;
|
||||
return false;
|
||||
}
|
||||
|
||||
_legacyAnimationScale = _avatarTransform.localScale;
|
||||
// animation scale changed, record it!
|
||||
Vector3 scaleDifference = PlayerSetup.DivideVectors(localScale - _initialScale, _initialScale);
|
||||
_animatedScaleFactor = scaleDifference.y;
|
||||
_animatedHeight = (_initialHeight * _animatedScaleFactor) + _initialHeight;
|
||||
_animatedScale = localScale;
|
||||
InvokeAnimatedHeightChanged();
|
||||
|
||||
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();
|
||||
|
||||
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.");
|
||||
AvatarScaleManager.Instance.ResetHeight(); // disable mod, user used a scale slider
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
using ABI_RC.Core.Player;
|
||||
using System.Diagnostics;
|
||||
using ABI_RC.Core.Player;
|
||||
using NAK.AvatarScaleMod.AvatarScaling;
|
||||
using UnityEngine;
|
||||
|
||||
|
@ -16,28 +17,34 @@ public class NetworkScaler : BaseScaler
|
|||
|
||||
_animatorManager = GetComponentInParent<PuppetMaster>().animatorManager;
|
||||
|
||||
_heightNeedsUpdate = false;
|
||||
heightNeedsUpdate = false;
|
||||
_isAvatarInstantiated = false;
|
||||
_isHeightAdjustedFromInitial = false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Overrides
|
||||
|
||||
public override async void OnAvatarInstantiated(GameObject avatarObject, float initialHeight, Vector3 initialScale)
|
||||
public override void OnAvatarInstantiated(GameObject avatarObject, float initialHeight, Vector3 initialScale)
|
||||
{
|
||||
if (avatarObject == null)
|
||||
return;
|
||||
|
||||
base.OnAvatarInstantiated(avatarObject, initialHeight, initialScale);
|
||||
await FindComponentsOfTypeAsync(scalableComponentTypes);
|
||||
|
||||
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");
|
||||
|
||||
if (_isHeightAdjustedFromInitial && _heightNeedsUpdate)
|
||||
UpdateScaleIfInstantiated();
|
||||
// TODO: why did i do this? height is never set prior to this method being called
|
||||
// if (_isHeightAdjustedFromInitial && heightNeedsUpdate)
|
||||
// UpdateScaleIfInstantiated();
|
||||
}
|
||||
|
||||
internal override void UpdateAnimatorParameter()
|
||||
|
||||
protected override void UpdateAnimatorParameter()
|
||||
{
|
||||
_animatorManager?.SetAnimatorParameter(ScaleFactorParameterNameLocal, _scaleFactor);
|
||||
}
|
||||
|
|
105
AvatarScale/AvatarScaling/Events/AvatarScaleEvents.cs
Normal file
105
AvatarScale/AvatarScaling/Events/AvatarScaleEvents.cs
Normal file
|
@ -0,0 +1,105 @@
|
|||
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
|
||||
}
|
|
@ -48,7 +48,6 @@ public class ScaledLight
|
|||
public void Reset()
|
||||
{
|
||||
Component.range = InitialRange;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue