You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
287 lines
8.1 KiB
C#
287 lines
8.1 KiB
C#
2 weeks ago
|
using System;
|
||
|
using System.Runtime.CompilerServices;
|
||
|
using System.Text;
|
||
|
using UnityEngine;
|
||
|
|
||
|
namespace Fusion.Addons.SimpleKCC
|
||
|
{
|
||
|
internal static class KCCUtility
|
||
|
{
|
||
|
private static readonly float[] _sortPriorities = new float[64];
|
||
|
|
||
|
public static void ClampLookRotationAngles(ref float pitch, ref float yaw)
|
||
|
{
|
||
|
pitch = Mathf.Clamp(pitch, -90f, 90f);
|
||
|
while (yaw > 180f)
|
||
|
{
|
||
|
yaw -= 360f;
|
||
|
}
|
||
|
|
||
|
while (yaw < -180f)
|
||
|
{
|
||
|
yaw += 360f;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void ClampLookRotationAngles(ref float pitch, ref float yaw, float minPitch, float maxPitch)
|
||
|
{
|
||
|
if (minPitch < -90f)
|
||
|
{
|
||
|
minPitch = -90f;
|
||
|
}
|
||
|
|
||
|
if (maxPitch > 90f)
|
||
|
{
|
||
|
maxPitch = 90f;
|
||
|
}
|
||
|
|
||
|
if (maxPitch < minPitch)
|
||
|
{
|
||
|
maxPitch = minPitch;
|
||
|
}
|
||
|
|
||
|
pitch = Mathf.Clamp(pitch, minPitch, maxPitch);
|
||
|
while (yaw > 180f)
|
||
|
{
|
||
|
yaw -= 360f;
|
||
|
}
|
||
|
|
||
|
while (yaw < -180f)
|
||
|
{
|
||
|
yaw += 360f;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static Vector2 ClampLookRotationAngles(Vector2 lookRotation, float minPitch, float maxPitch)
|
||
|
{
|
||
|
if (minPitch < -90f)
|
||
|
{
|
||
|
minPitch = -90f;
|
||
|
}
|
||
|
|
||
|
if (maxPitch > 90f)
|
||
|
{
|
||
|
maxPitch = 90f;
|
||
|
}
|
||
|
|
||
|
if (maxPitch < minPitch)
|
||
|
{
|
||
|
maxPitch = minPitch;
|
||
|
}
|
||
|
|
||
|
lookRotation.x = Mathf.Clamp(lookRotation.x, minPitch, maxPitch);
|
||
|
while (lookRotation.y > 180f)
|
||
|
{
|
||
|
lookRotation.y -= 360f;
|
||
|
}
|
||
|
|
||
|
while (lookRotation.y < -180f)
|
||
|
{
|
||
|
lookRotation.y += 360f;
|
||
|
}
|
||
|
|
||
|
return lookRotation;
|
||
|
}
|
||
|
|
||
|
public static void GetClampedLookRotationAngles(Quaternion lookRotation, out float pitch, out float yaw)
|
||
|
{
|
||
|
Vector3 eulerAngles = lookRotation.eulerAngles;
|
||
|
if (eulerAngles.x > 180f)
|
||
|
{
|
||
|
eulerAngles.x -= 360f;
|
||
|
}
|
||
|
|
||
|
if (eulerAngles.y > 180f)
|
||
|
{
|
||
|
eulerAngles.y -= 360f;
|
||
|
}
|
||
|
|
||
|
pitch = Mathf.Clamp(eulerAngles.x, -90f, 90f);
|
||
|
yaw = Mathf.Clamp(eulerAngles.y, -180f, 180f);
|
||
|
}
|
||
|
|
||
|
public static Vector2 GetClampedEulerLookRotation(Quaternion lookRotation)
|
||
|
{
|
||
|
Vector2 result = lookRotation.eulerAngles;
|
||
|
if (result.x > 180f)
|
||
|
{
|
||
|
result.x -= 360f;
|
||
|
}
|
||
|
|
||
|
if (result.y > 180f)
|
||
|
{
|
||
|
result.y -= 360f;
|
||
|
}
|
||
|
|
||
|
result.x = Mathf.Clamp(result.x, -90f, 90f);
|
||
|
result.y = Mathf.Clamp(result.y, -180f, 180f);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
public static Vector2 GetClampedEulerLookRotation(Vector3 direction)
|
||
|
{
|
||
|
return GetClampedEulerLookRotation(Quaternion.LookRotation(direction));
|
||
|
}
|
||
|
|
||
|
public static Vector2 GetClampedEulerLookRotation(Vector2 lookRotation, Vector2 lookRotationDelta, float minPitch, float maxPitch)
|
||
|
{
|
||
|
return ClampLookRotationAngles(lookRotation + lookRotationDelta, minPitch, maxPitch);
|
||
|
}
|
||
|
|
||
|
public static Vector2 GetClampedEulerLookRotationDelta(Vector2 lookRotation, Vector2 lookRotationDelta, float minPitch, float maxPitch)
|
||
|
{
|
||
|
lookRotationDelta.x = Mathf.Clamp(lookRotation.x + lookRotationDelta.x, minPitch, maxPitch) - lookRotation.x;
|
||
|
return lookRotationDelta;
|
||
|
}
|
||
|
|
||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||
|
public static float InterpolateRange(float from, float to, float min, float max, float alpha)
|
||
|
{
|
||
|
float num = max - min;
|
||
|
if (num <= 0f)
|
||
|
{
|
||
|
throw new ArgumentException("max must be greater than min!");
|
||
|
}
|
||
|
|
||
|
if (from < min)
|
||
|
{
|
||
|
from = min;
|
||
|
}
|
||
|
else if (from > max)
|
||
|
{
|
||
|
from = max;
|
||
|
}
|
||
|
|
||
|
if (to < min)
|
||
|
{
|
||
|
to = min;
|
||
|
}
|
||
|
else if (to > max)
|
||
|
{
|
||
|
to = max;
|
||
|
}
|
||
|
|
||
|
if (from == to)
|
||
|
{
|
||
|
return from;
|
||
|
}
|
||
|
|
||
|
float num2 = num * 0.5f;
|
||
|
float num3;
|
||
|
if (from < to)
|
||
|
{
|
||
|
if (to - from <= num2)
|
||
|
{
|
||
|
num3 = Mathf.Lerp(from, to, alpha);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
num3 = Mathf.Lerp(from + num, to, alpha);
|
||
|
if (num3 > max)
|
||
|
{
|
||
|
num3 -= num;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else if (from - to <= num2)
|
||
|
{
|
||
|
num3 = Mathf.Lerp(from, to, alpha);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
num3 = Mathf.Lerp(from - num, to, alpha);
|
||
|
if (num3 <= min)
|
||
|
{
|
||
|
num3 += num;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return num3;
|
||
|
}
|
||
|
|
||
|
public static void LogInfo(SimulationBehaviour behaviour, params object[] messages)
|
||
|
{
|
||
|
Log(behaviour, null, EKCCLogType.Info, messages);
|
||
|
}
|
||
|
|
||
|
public static void LogWarning(SimulationBehaviour behaviour, params object[] messages)
|
||
|
{
|
||
|
Log(behaviour, null, EKCCLogType.Warning, messages);
|
||
|
}
|
||
|
|
||
|
public static void LogError(SimulationBehaviour behaviour, params object[] messages)
|
||
|
{
|
||
|
Log(behaviour, null, EKCCLogType.Error, messages);
|
||
|
}
|
||
|
|
||
|
public static void Log(SimulationBehaviour behaviour, string logGroup, EKCCLogType logType, params object[] messages)
|
||
|
{
|
||
|
StringBuilder stringBuilder = new StringBuilder();
|
||
|
NetworkRunner runner = behaviour.Runner;
|
||
|
if (Time.frameCount % 2 == 0)
|
||
|
{
|
||
|
stringBuilder.Append($"<color=#19A7CE>[{Time.frameCount}]</color>");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
stringBuilder.Append($"<color=#FC3C3C>[{Time.frameCount}]</color>");
|
||
|
}
|
||
|
|
||
|
if (runner != null)
|
||
|
{
|
||
|
bool num = runner.Stage != (SimulationStages)0;
|
||
|
bool isForward = runner.IsForward;
|
||
|
if (num)
|
||
|
{
|
||
|
if (isForward)
|
||
|
{
|
||
|
stringBuilder.Append($"<color=#FFFF00>[{runner.Tick.Raw}]</color>");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
stringBuilder.Append($"<color=#FF0000>[{runner.Tick.Raw}]</color>");
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
stringBuilder.Append($"<color=#00FF00>[{runner.Tick.Raw}]</color>");
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
stringBuilder.Append("[--]");
|
||
|
}
|
||
|
|
||
|
if (!string.IsNullOrEmpty(logGroup))
|
||
|
{
|
||
|
stringBuilder.Append("[" + logGroup + "]");
|
||
|
}
|
||
|
|
||
|
stringBuilder.Append("[" + behaviour.name + "]");
|
||
|
foreach (object obj in messages)
|
||
|
{
|
||
|
if (obj != null)
|
||
|
{
|
||
|
stringBuilder.Append(" ");
|
||
|
stringBuilder.Append(obj);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
switch (logType)
|
||
|
{
|
||
|
case EKCCLogType.Info:
|
||
|
Debug.Log(stringBuilder.ToString(), behaviour);
|
||
|
break;
|
||
|
case EKCCLogType.Warning:
|
||
|
Debug.LogWarning(stringBuilder.ToString(), behaviour);
|
||
|
break;
|
||
|
case EKCCLogType.Error:
|
||
|
Debug.LogError(stringBuilder.ToString(), behaviour);
|
||
|
break;
|
||
|
default:
|
||
|
throw new ArgumentOutOfRangeException("logType", logType, null);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|