Emergency backup
This commit is contained in:
@@ -170,6 +170,196 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
public static int MinPositive(int a, int b)
|
||||
=> Math.Min(Math.Max(0, a), Math.Max(0, b));
|
||||
|
||||
/// <summary>
|
||||
/// Interpolates t between a and b to a value between 0 and 1 using a Hermite polynomial.
|
||||
/// </summary>
|
||||
/// <param name="a">The first value</param>
|
||||
/// <param name="b">The second value</param>
|
||||
/// <param name="t">The position value</param>
|
||||
/// <returns>A smoothed value between 0 and 1</returns>
|
||||
public static float SmoothStep(float a, float b, float t)
|
||||
{
|
||||
t = Mathf.Clamp01((t - a) / (b - a));
|
||||
return t * t * (3f - 2f * t);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interpolates t between a and b to a value between 0 and 1.
|
||||
/// </summary>
|
||||
/// <param name="a">The first value</param>
|
||||
/// <param name="b">The second value</param>
|
||||
/// <param name="t">The position value</param>
|
||||
/// <returns>Linear value between 0 and 1</returns>
|
||||
public static float LinearStep(float a, float b, float t)
|
||||
{
|
||||
return Mathf.Clamp01((t - a) / (b - a));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps a value between min and max.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap</param>
|
||||
/// <param name="min">The minimum value</param>
|
||||
/// <param name="max">The maximum value</param>
|
||||
/// <returns></returns>
|
||||
public static double Wrap(double value, double min, double max)
|
||||
{
|
||||
double num = max - min;
|
||||
num = ((num < 0.0) ? (0.0 - num) : num);
|
||||
if(value < min)
|
||||
{
|
||||
return value + num * Math.Ceiling(Math.Abs(value / num));
|
||||
}
|
||||
|
||||
if(value >= max)
|
||||
{
|
||||
return value - num * Math.Floor(Math.Abs(value / num));
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps a value between min and max.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap</param>
|
||||
/// <param name="min">The minimum value</param>
|
||||
/// <param name="max">The maximum value</param>
|
||||
/// <returns></returns>
|
||||
public static float Wrap(float value, float min, float max)
|
||||
{
|
||||
float num = max - min;
|
||||
num = (((double)num < 0.0) ? (0f - num) : num);
|
||||
if(value < min)
|
||||
{
|
||||
return value + num * (float)Math.Ceiling(Math.Abs(value / num));
|
||||
}
|
||||
|
||||
if(value >= max)
|
||||
{
|
||||
return value - num * (float)Math.Floor(Math.Abs(value / num));
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps a value between min and max.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap</param>
|
||||
/// <param name="min">The minimum value</param>
|
||||
/// <param name="max">The maximum value</param>
|
||||
/// <returns></returns>
|
||||
public static int Wrap(int value, int min, int max)
|
||||
{
|
||||
int num = max - min;
|
||||
num = ((num < 0) ? (-num) : num);
|
||||
if(value < min)
|
||||
{
|
||||
return value + num * (Math.Abs(value / num) + 1);
|
||||
}
|
||||
|
||||
if(value >= max)
|
||||
{
|
||||
return value - num * Math.Abs(value / num);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rounds a number based on a mininum difference.
|
||||
/// </summary>
|
||||
/// <param name="valueToRound">The value to round</param>
|
||||
/// <param name="minDifference">The min difference</param>
|
||||
/// <returns>The rounded value.</returns>
|
||||
public static double RoundBasedOnMinimumDifference(double valueToRound, double minDifference)
|
||||
{
|
||||
if(minDifference == 0.0)
|
||||
{
|
||||
return DiscardLeastSignificantDecimal(valueToRound);
|
||||
}
|
||||
|
||||
return (float)Math.Round(valueToRound, MacroMath.GetNumberOfDecimalsForMinimumDifference(minDifference), MidpointRounding.AwayFromZero);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Discards the least significant demicals.
|
||||
/// </summary>
|
||||
/// <param name="v">The value of insignificant decimals</param>
|
||||
/// <returns>Value with significant decimals</returns>
|
||||
public static double DiscardLeastSignificantDecimal(double v)
|
||||
{
|
||||
int digits = Math.Max(0, (int)(5.0 - Math.Log10(Math.Abs(v))));
|
||||
try
|
||||
{
|
||||
return Math.Round(v, digits);
|
||||
}
|
||||
catch(ArgumentOutOfRangeException)
|
||||
{
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clamps and wraps an angle between two values.
|
||||
/// </summary>
|
||||
/// <param name="angle"></param>
|
||||
/// <param name="min"></param>
|
||||
/// <param name="max"></param>
|
||||
/// <returns></returns>
|
||||
public static float ClampWrapAngle(float angle, float min, float max)
|
||||
{
|
||||
float num = 360f;
|
||||
float num2 = min;
|
||||
float num3 = max;
|
||||
float num4 = angle;
|
||||
if(num2 < 0f)
|
||||
{
|
||||
num2 = num2 % num + num;
|
||||
}
|
||||
|
||||
if(num3 < 0f)
|
||||
{
|
||||
num3 = num3 % num + num;
|
||||
}
|
||||
|
||||
if(num4 < 0f)
|
||||
{
|
||||
num4 = num4 % num + num;
|
||||
}
|
||||
|
||||
float num5 = (float)(int)(Math.Abs(min - max) / num) * num;
|
||||
num3 += num5;
|
||||
num4 += num5;
|
||||
if(min > max)
|
||||
{
|
||||
num3 += num;
|
||||
}
|
||||
|
||||
if(num4 < num2)
|
||||
{
|
||||
num4 = num2;
|
||||
}
|
||||
|
||||
if(num4 > num3)
|
||||
{
|
||||
num4 = num3;
|
||||
}
|
||||
|
||||
return num4;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20><>ȡ<EFBFBD><C8A1>С<EFBFBD><D0A1>ֵ<EFBFBD><D6B5>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
/// </summary>
|
||||
/// <param name="minDifference"></param>
|
||||
/// <returns></returns>
|
||||
public static int GetNumberOfDecimalsForMinimumDifference(double minDifference)
|
||||
{
|
||||
return Mathf.Clamp(-Mathf.FloorToInt(Mathf.Log10(Mathf.Abs((float)minDifference))), 0, 15);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region <EFBFBD>ֽڿ<EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
@@ -113,79 +113,7 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
|
||||
#endregion
|
||||
|
||||
#region <EFBFBD><EFBFBD>չ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Xonly(this Vector3 a)
|
||||
=> new Vector3(
|
||||
a.x,
|
||||
0f,
|
||||
0f
|
||||
);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Yonly(this Vector3 a)
|
||||
=> new Vector3(
|
||||
0f,
|
||||
a.y,
|
||||
0f
|
||||
);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Zonly(this Vector3 a)
|
||||
=> new Vector3(
|
||||
0f,
|
||||
0f,
|
||||
a.z
|
||||
);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Mode(this Vector3 a, Vector3 b)
|
||||
{
|
||||
return new Vector3(a.x % b.x, a.y % b.y, a.z % b.z);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Mode(this Vector3 a, float b)
|
||||
{
|
||||
return new Vector3(a.x % b, a.y % b, a.z % b);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Sign(this Vector3 vec)
|
||||
=> new Vector3(
|
||||
Math.Sign(vec.x),
|
||||
Math.Sign(vec.y),
|
||||
Math.Sign(vec.z)
|
||||
);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3Int CeilToInt(this Vector3 vec)
|
||||
=> new Vector3Int((int)Mathf.Ceil(vec.x), (int)Mathf.Ceil(vec.y), (int)Mathf.Ceil(vec.z));
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Ceil(this Vector3 vec)
|
||||
=> new Vector3(Mathf.Ceil(vec.x), Mathf.Ceil(vec.y), Mathf.Ceil(vec.z));
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3Int RoundToInt(this Vector3 vec)
|
||||
=> new Vector3Int((int)Mathf.Round(vec.x), (int)Mathf.Round(vec.y), (int)Mathf.Round(vec.z));
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Round(this Vector3 vec)
|
||||
=> new Vector3(Mathf.Round(vec.x), Mathf.Round(vec.y), Mathf.Round(vec.z));
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3Int FloorToInt(this Vector3 vec)
|
||||
=> new Vector3Int((int)vec.x, (int)vec.y, (int)vec.z);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Floor(this Vector3 vec)
|
||||
=> new Vector3((int)vec.x, (int)vec.y, (int)vec.z);
|
||||
|
||||
#endregion
|
||||
|
||||
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
#region <EFBFBD>ƻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Min(this Vector3 a, Vector3 b)
|
||||
@@ -256,6 +184,101 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
|
||||
#endregion
|
||||
|
||||
#region <EFBFBD><EFBFBD>չ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Xonly(this Vector3 a)
|
||||
=> new Vector3(
|
||||
a.x,
|
||||
0f,
|
||||
0f
|
||||
);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Yonly(this Vector3 a)
|
||||
=> new Vector3(
|
||||
0f,
|
||||
a.y,
|
||||
0f
|
||||
);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Zonly(this Vector3 a)
|
||||
=> new Vector3(
|
||||
0f,
|
||||
0f,
|
||||
a.z
|
||||
);
|
||||
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Mode(this Vector3 a, Vector3 b)
|
||||
{
|
||||
return new Vector3(a.x % b.x, a.y % b.y, a.z % b.z);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Mode(this Vector3 a, float b)
|
||||
{
|
||||
return new Vector3(a.x % b, a.y % b, a.z % b);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Sign(this Vector3 vec)
|
||||
=> new Vector3(
|
||||
Math.Sign(vec.x),
|
||||
Math.Sign(vec.y),
|
||||
Math.Sign(vec.z)
|
||||
);
|
||||
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3Int CeilToInt(this Vector3 vec)
|
||||
=> new Vector3Int((int)Mathf.Ceil(vec.x), (int)Mathf.Ceil(vec.y), (int)Mathf.Ceil(vec.z));
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Ceil(this Vector3 vec)
|
||||
=> new Vector3(Mathf.Ceil(vec.x), Mathf.Ceil(vec.y), Mathf.Ceil(vec.z));
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3Int RoundToInt(this Vector3 vec)
|
||||
=> new Vector3Int((int)Mathf.Round(vec.x), (int)Mathf.Round(vec.y), (int)Mathf.Round(vec.z));
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Round(this Vector3 vec)
|
||||
=> new Vector3(Mathf.Round(vec.x), Mathf.Round(vec.y), Mathf.Round(vec.z));
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3Int FloorToInt(this Vector3 vec)
|
||||
=> new Vector3Int((int)vec.x, (int)vec.y, (int)vec.z);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Floor(this Vector3 vec)
|
||||
=> new Vector3((int)vec.x, (int)vec.y, (int)vec.z);
|
||||
|
||||
/// <summary>
|
||||
/// Pows each element of the vector.
|
||||
/// </summary>
|
||||
/// <param name="v"></param>
|
||||
/// <param name="p"></param>
|
||||
/// <returns></returns>
|
||||
public static Vector3 Pow(this Vector3 v, float p)
|
||||
{
|
||||
v.x = Mathf.Pow(v.x, p);
|
||||
v.y = Mathf.Pow(v.y, p);
|
||||
v.z = Mathf.Pow(v.z, p);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static float Distance(this Vector3 a, Vector3 b)
|
||||
=> Vector3.Distance(a, b);
|
||||
|
||||
#endregion
|
||||
|
||||
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
/// <summary>
|
||||
@@ -341,7 +364,493 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
#region δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
private const float ZERO_TOLERANCE = 1E-06f;
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Distance from a point to a line.
|
||||
public static float PointDistanceToLine(Vector3 point, Vector3 a, Vector3 b)
|
||||
{
|
||||
return Mathf.Abs((b.x - a.x) * (a.y - point.y) - (a.x - point.x) * (b.y - a.y)) / Mathf.Sqrt(Mathf.Pow(b.x - a.x, 2f) + Mathf.Pow(b.y - a.y, 2f));
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Returns a smooth value between start and end based on t.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// start:
|
||||
// First point.
|
||||
//
|
||||
// end:
|
||||
// Second point.
|
||||
//
|
||||
// t:
|
||||
// Position between 0 and 1.
|
||||
public static float Hermite(float start, float end, float t)
|
||||
{
|
||||
return Mathf.Lerp(start, end, t * t * (3f - 2f * t));
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Returns a smooth value between start and end based on t.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// start:
|
||||
// First point.
|
||||
//
|
||||
// end:
|
||||
// Second point.
|
||||
//
|
||||
// t:
|
||||
// Position between 0 and 1.
|
||||
//
|
||||
// count:
|
||||
// Number of interpolations to make.
|
||||
public static float StackHermite(float start, float end, float t, int count)
|
||||
{
|
||||
for(int i = 0; i < count; i++)
|
||||
{
|
||||
t = Hermite(start, end, t);
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Returns the fractional of the value.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// value:
|
||||
// The value to get the fractional of.
|
||||
public static float Fract(float value)
|
||||
{
|
||||
return value - (float)Math.Truncate(value);
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Returns the fractional of the value.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// value:
|
||||
// The value to get the fractional of.
|
||||
public static Vector2 Fract(Vector2 value)
|
||||
{
|
||||
return new Vector3(Fract(value.x), Fract(value.y));
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Returns the fractional of the value.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// value:
|
||||
// The value to get the fractional of.
|
||||
public static Vector3 Fract(Vector3 value)
|
||||
{
|
||||
return new Vector3(Fract(value.x), Fract(value.y), Fract(value.z));
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Returns a value based on t, that bounces faster and faster.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// t:
|
||||
// The value to bounce.
|
||||
public static float BounceEaseInFastOut(float t)
|
||||
{
|
||||
return Mathf.Cos(t * t * (float)Math.PI * 2f) * -0.5f + 0.5f;
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Returns a smooth value between 0 and 1 based on t.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// t:
|
||||
// Position between 0 and 1.
|
||||
public static float Hermite01(float t)
|
||||
{
|
||||
return Mathf.Lerp(0f, 1f, t * t * (3f - 2f * t));
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Returns a smooth value between 0 and 1 based on t.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// t:
|
||||
// Position between 0 and 1.
|
||||
//
|
||||
// count:
|
||||
// Number of interpolations to make.
|
||||
public static float StackHermite01(float t, int count)
|
||||
{
|
||||
for(int i = 0; i < count; i++)
|
||||
{
|
||||
t = Hermite01(t);
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Returns an unclamped linear interpolation of two vectors.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// from:
|
||||
// The first vector.
|
||||
//
|
||||
// to:
|
||||
// The second vector.
|
||||
//
|
||||
// amount:
|
||||
// The interpolation factor.
|
||||
public static Vector3 LerpUnclamped(Vector3 from, Vector3 to, float amount)
|
||||
{
|
||||
return from + (to - from) * amount;
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Returns an unclamped linear interpolation of two vectors.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// from:
|
||||
// The first vector.
|
||||
//
|
||||
// to:
|
||||
// The second vector.
|
||||
//
|
||||
// amount:
|
||||
// The interpolation factor.
|
||||
public static Vector2 LerpUnclamped(Vector2 from, Vector2 to, float amount)
|
||||
{
|
||||
return from + (to - from) * amount;
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Returns a value that bounces between 0 and 1 based on value.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// value:
|
||||
// The value to bounce.
|
||||
public static float Bounce(float value)
|
||||
{
|
||||
return Mathf.Abs(Mathf.Sin(value % 1f * (float)Math.PI));
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Returns a value that eases in elasticly.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// value:
|
||||
// The value to ease in elasticly.
|
||||
//
|
||||
// amplitude:
|
||||
// The amplitude.
|
||||
//
|
||||
// length:
|
||||
// The length.
|
||||
public static float EaseInElastic(float value, float amplitude = 0.25f, float length = 0.6f)
|
||||
{
|
||||
value = Mathf.Clamp01(value);
|
||||
float num = Mathf.Clamp01(value * 7.5f);
|
||||
float num2 = 1f - num * num * (3f - 2f * num);
|
||||
float num3 = Mathf.Pow(1f - Mathf.Sin(Mathf.Min(value * (1f - length), 0.5f) * (float)Math.PI), 2f);
|
||||
float num4 = Mathf.Cos((float)Math.PI + value * 23f) * amplitude + num2 * (0f - (1f - amplitude));
|
||||
return 1f + num4 * num3;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Returns a value that eases out elasticly.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// value:
|
||||
// The value to ease out elasticly.
|
||||
//
|
||||
// amplitude:
|
||||
// The amplitude.
|
||||
//
|
||||
// length:
|
||||
// The length.
|
||||
public static float EaseOutElastic(float value, float amplitude = 0.25f, float length = 0.6f)
|
||||
{
|
||||
return 1f - EaseInElastic(1f - value, amplitude, length);
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Returns a smooth value betweeen that peaks at t=0.5 and then comes back down
|
||||
// again.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// t:
|
||||
// A value between 0 and 1.
|
||||
public static float EaseInOut(float t)
|
||||
{
|
||||
t = 1f - Mathf.Abs(Mathf.Clamp01(t) * 2f - 1f);
|
||||
t = t * t * (3f - 2f * t);
|
||||
return t;
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Clamps the value of a Vector3.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// value:
|
||||
// The vector to clamp.
|
||||
//
|
||||
// min:
|
||||
// The min value.
|
||||
//
|
||||
// max:
|
||||
// The max value.
|
||||
public static Vector3 Clamp(this Vector3 value, Vector3 min, Vector3 max)
|
||||
{
|
||||
return new Vector3(Mathf.Clamp(value.x, min.x, max.x), Mathf.Clamp(value.y, min.y, max.y), Mathf.Clamp(value.z, min.z, max.z));
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Clamps the value of a Vector2.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// value:
|
||||
// The vector to clamp.
|
||||
//
|
||||
// min:
|
||||
// The min value.
|
||||
//
|
||||
// max:
|
||||
// The max value.
|
||||
public static Vector2 Clamp(this Vector2 value, Vector2 min, Vector2 max)
|
||||
{
|
||||
return new Vector2(Mathf.Clamp(value.x, min.x, max.x), Mathf.Clamp(value.y, min.y, max.y));
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Computes a hash for a byte array.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// data:
|
||||
// The byte array.
|
||||
public static int ComputeByteArrayHash(byte[] data)
|
||||
{
|
||||
int num = -2128831035;
|
||||
for(int i = 0; i < data.Length; i++)
|
||||
{
|
||||
num = (num ^ data[i]) * 16777619;
|
||||
}
|
||||
|
||||
num += num << 13;
|
||||
num ^= num >> 7;
|
||||
num += num << 3;
|
||||
num ^= num >> 17;
|
||||
return num + (num << 5);
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Gives a smooth path between a collection of points.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// path:
|
||||
// The collection of point.
|
||||
//
|
||||
// t:
|
||||
// The current position in the path. 0 is at the start of the path, 1 is at the
|
||||
// end of the path.
|
||||
public static Vector3 InterpolatePoints(Vector3[] path, float t)
|
||||
{
|
||||
t = Mathf.Clamp01(t * (1f - 1f / (float)path.Length));
|
||||
int b = path.Length - 1;
|
||||
int num = Mathf.FloorToInt(t * (float)path.Length);
|
||||
float num2 = t * (float)path.Length - (float)num;
|
||||
Vector3 vector = path[Mathf.Max(0, --num)];
|
||||
Vector3 vector2 = path[Mathf.Min(num + 1, b)];
|
||||
Vector3 vector3 = path[Mathf.Min(num + 2, b)];
|
||||
Vector3 vector4 = path[Mathf.Min(num + 3, b)];
|
||||
return 0.5f * ((-vector + 3f * vector2 - 3f * vector3 + vector4) * (num2 * num2 * num2) + (2f * vector - 5f * vector2 + 4f * vector3 - vector4) * (num2 * num2) + (-vector + vector3) * num2 + 2f * vector2);
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Checks if two given lines intersect with one another and returns the intersection
|
||||
// point (if any) in an out parameter. Source: http://stackoverflow.com/questions/3746274/line-intersection-with-aabb-rectangle.
|
||||
// Edited to implement Cohen-Sutherland type pruning for efficiency.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// a1:
|
||||
// Starting point of line a.
|
||||
//
|
||||
// a2:
|
||||
// Ending point of line a.
|
||||
//
|
||||
// b1:
|
||||
// Starting point of line b.
|
||||
//
|
||||
// b2:
|
||||
// Ending point of line b.
|
||||
//
|
||||
// intersection:
|
||||
// The out parameter which contains the intersection point if there was any.
|
||||
//
|
||||
// <20><><EFBFBD>ؽ<EFBFBD><D8BD><EFBFBD>:
|
||||
// True if the two lines intersect, otherwise false.
|
||||
public static bool LineIntersectsLine(Vector2 a1, Vector2 a2, Vector2 b1, Vector2 b2, out Vector2 intersection)
|
||||
{
|
||||
intersection = Vector2.zero;
|
||||
Vector2 vector = new Vector2((b1.x < b2.x) ? b1.x : b2.x, (b1.y > b2.y) ? b1.y : b2.y);
|
||||
Vector2 vector2 = new Vector2((b1.x > b2.x) ? b1.x : b2.x, (b1.y < b2.y) ? b1.y : b2.y);
|
||||
if((a1.x < vector.x && a2.x < vector.x) || (a1.y > vector.y && a2.y > vector.y) || (a1.x > vector2.x && a2.x > vector2.x) || (a1.y < vector2.y && a2.y < vector2.y))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector2 vector3 = a2 - a1;
|
||||
Vector2 vector4 = b2 - b1;
|
||||
float num = vector3.x * vector4.y - vector3.y * vector4.x;
|
||||
if(num == 0f)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector2 vector5 = b1 - a1;
|
||||
float num2 = (vector5.x * vector4.y - vector5.y * vector4.x) / num;
|
||||
if(num2 < 0f || num2 > 1f)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
float num3 = (vector5.x * vector3.y - vector5.y * vector3.x) / num;
|
||||
if(num3 < 0f || num3 > 1f)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
intersection = a1 + num2 * vector3;
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Returns the collision point between two infinite lines.
|
||||
public static Vector2 InfiniteLineIntersect(Vector2 ps1, Vector2 pe1, Vector2 ps2, Vector2 pe2)
|
||||
{
|
||||
float num = pe1.y - ps1.y;
|
||||
float num2 = ps1.x - pe1.x;
|
||||
float num3 = num * ps1.x + num2 * ps1.y;
|
||||
float num4 = pe2.y - ps2.y;
|
||||
float num5 = ps2.x - pe2.x;
|
||||
float num6 = num4 * ps2.x + num5 * ps2.y;
|
||||
float num7 = num * num5 - num4 * num2;
|
||||
if(num7 == 0f)
|
||||
{
|
||||
throw new Exception("Lines are parallel");
|
||||
}
|
||||
|
||||
return new Vector2((num5 * num3 - num2 * num6) / num7, (num * num6 - num4 * num3) / num7);
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Distance from line to plane.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// planeOrigin:
|
||||
// Position of the plane.
|
||||
//
|
||||
// planeNormal:
|
||||
// Surface normal of the plane.
|
||||
//
|
||||
// lineOrigin:
|
||||
// Origin of the line.
|
||||
//
|
||||
// lineDirectionNormalized:
|
||||
// Line direction normal.
|
||||
public static float LineDistToPlane(Vector3 planeOrigin, Vector3 planeNormal, Vector3 lineOrigin, Vector3 lineDirectionNormalized)
|
||||
{
|
||||
return Vector3.Dot(lineDirectionNormalized, planeNormal) * Vector3.Distance(planeOrigin, lineOrigin);
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Distance from ray to plane.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// ray:
|
||||
// The ray.
|
||||
//
|
||||
// plane:
|
||||
// The plane.
|
||||
public static float RayDistToPlane(Ray ray, Plane plane)
|
||||
{
|
||||
float num = Vector3.Dot(plane.normal, ray.direction);
|
||||
if(Mathf.Abs(num) < 1E-06f)
|
||||
{
|
||||
return 0f;
|
||||
}
|
||||
|
||||
float num2 = Vector3.Dot(plane.normal, ray.origin);
|
||||
return (0f - plane.distance - num2) / num;
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Rotates a Vector2 by an angle.
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// point:
|
||||
// The point to rotate.
|
||||
//
|
||||
// degrees:
|
||||
// The angle to rotate.
|
||||
public static Vector2 RotatePoint(Vector2 point, float degrees)
|
||||
{
|
||||
float f = degrees * ((float)Math.PI / 180f);
|
||||
float num = Mathf.Cos(f);
|
||||
float num2 = Mathf.Sin(f);
|
||||
return new Vector2(num * point.x - num2 * point.y, num2 * point.x + num * point.y);
|
||||
}
|
||||
|
||||
//
|
||||
// ժҪ:
|
||||
// Rotates a Vector2 around a point by an angle..
|
||||
//
|
||||
// <20><><EFBFBD><EFBFBD>:
|
||||
// point:
|
||||
// The point to rotate.
|
||||
//
|
||||
// around:
|
||||
// The point to rotate around.
|
||||
//
|
||||
// degrees:
|
||||
// The angle to rotate.
|
||||
public static Vector2 RotatePoint(Vector2 point, Vector2 around, float degrees)
|
||||
{
|
||||
float f = degrees * ((float)Math.PI / 180f);
|
||||
float num = Mathf.Cos(f);
|
||||
float num2 = Mathf.Sin(f);
|
||||
return new Vector2(num * (point.x - around.x) - num2 * (point.y - around.y) + around.x, num2 * (point.x - around.x) + num * (point.y - around.y) + around.y);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user