From 1910b0e3f75f429fab9ea8d83c4b5e7edcf8bddc Mon Sep 17 00:00:00 2001 From: LiRuoChen <571244399@qq.com> Date: Tue, 28 Nov 2023 09:20:50 +0800 Subject: [PATCH] Emergency backup --- Runtime/CollisionLOD/LodBoxCollider.cs | 42 +- Runtime/CollisionLOD/LodSphereCollider.cs | 37 +- Runtime/CollisionLOD/base/LodColliderBase.cs | 7 +- Runtime/MicroLibrary/MacroMath.cs | 190 ++++++ Runtime/MicroLibrary/Vector3Extend.cs | 657 ++++++++++++++++--- Runtime/Type/Enum/Orientation.cs | 30 - 6 files changed, 849 insertions(+), 114 deletions(-) diff --git a/Runtime/CollisionLOD/LodBoxCollider.cs b/Runtime/CollisionLOD/LodBoxCollider.cs index 84e229a..4dc9cdf 100644 --- a/Runtime/CollisionLOD/LodBoxCollider.cs +++ b/Runtime/CollisionLOD/LodBoxCollider.cs @@ -1,20 +1,19 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; +using UnityEngine.XR; using XericLibrary.Runtime.Type; namespace XericLibrary.Runtime.CollisionLOD { + /// + /// 盒体的AABB碰撞器 + /// public class LodBoxCollider : LodColliderBase { #region 字段属性 - /// - /// 启用轴向 - /// - public Bool3 EnableCollisionAxis = new Bool3(1,0,1); - #endregion #region 生命周期 @@ -25,9 +24,38 @@ namespace XericLibrary.Runtime.CollisionLOD #region 碰撞检测 - private bool CheckCollision() + private static bool CheckCollision(LodBoxCollider box1, LodBoxCollider box2) { - return false; + var box1max = box1.BoundSize.max; + var box1min = box1.BoundSize.min; + var box2max = box2.BoundSize.max; + var box2min = box2.BoundSize.min; + + bool collidX = box1max.x >= box2min.x + && box2max.x >= box1min.x; + bool collidY = box1max.y >= box2min.y + && box2max.y >= box1min.y; + bool collidZ = box1max.z >= box2min.z + && box2max.z >= box1min.z; + + bool enableCollidX = box1.EnableCollisionAxis.x + && box2.EnableCollisionAxis.x; + bool enableCollidY = box1.EnableCollisionAxis.y + && box2.EnableCollisionAxis.y; + bool enableCollidZ = box1.EnableCollisionAxis.z + && box2.EnableCollisionAxis.z; + + if(!(enableCollidX && enableCollidY && enableCollidZ)) + return false; + + bool rescollidX = enableCollidX && collidX + || (!enableCollidX && (collidY || collidZ)); + bool rescollidY = enableCollidY && collidY + || (!enableCollidY && (collidX || collidZ)); + bool rescollidZ = enableCollidZ && collidZ + || (!enableCollidZ && (collidX || collidY)); + + return rescollidX && rescollidY && rescollidZ; } #endregion diff --git a/Runtime/CollisionLOD/LodSphereCollider.cs b/Runtime/CollisionLOD/LodSphereCollider.cs index 2b73b1d..4426ede 100644 --- a/Runtime/CollisionLOD/LodSphereCollider.cs +++ b/Runtime/CollisionLOD/LodSphereCollider.cs @@ -2,11 +2,44 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; +using XericLibrary.Runtime.MacroLibrary; namespace XericLibrary.Runtime.CollisionLOD { public class LodSphereCollider : LodColliderBase { - - } + #region 字段属性 + + /// + /// 半径 + /// + private float radius; + + #endregion + + #region 碰撞检测 + + private static bool CheckCollision(LodSphereCollider sphere1, LodSphereCollider sphere2) + { + return Vector3.Distance(sphere1.BoundSize.center, sphere2.BoundSize.center) + < sphere1.radius + sphere2.radius; + + } + + private static bool CheckCollision(LodSphereCollider sphere, LodBoxCollider box) + { + /* + * 对于任意AABB和圆的最近点的计算: + * 获取圆心C与AABB中心B的偏移量D + * 用AABB的范围去限制这个偏移量 + * 此时矢量总是位于AABB的边缘处 + */ + return (sphere.BoundSize.center - box.BoundSize.center) + .Clamp(box.BoundSize.min, box.BoundSize.max) + .Add(box.BoundSize.center) + .Distance(sphere.BoundSize.center) <= sphere.radius; + } + + #endregion + } } diff --git a/Runtime/CollisionLOD/base/LodColliderBase.cs b/Runtime/CollisionLOD/base/LodColliderBase.cs index 86fb9ee..28be6f1 100644 --- a/Runtime/CollisionLOD/base/LodColliderBase.cs +++ b/Runtime/CollisionLOD/base/LodColliderBase.cs @@ -57,6 +57,11 @@ namespace XericLibrary.Runtime.CollisionLOD /// public Bounds BoundSize; + /// + /// 启用轴向 + /// + public Bool3 EnableCollisionAxis = new Bool3(1,0,1); + /// /// 碰撞器集合 /// @@ -98,7 +103,7 @@ namespace XericLibrary.Runtime.CollisionLOD private void MakeLevelLinearArray(List genList) { genList.Sort(); - int seqIndex = 0; + int seqIndex = 1; foreach(var item in genList) item.SeqLevels = seqIndex++; } diff --git a/Runtime/MicroLibrary/MacroMath.cs b/Runtime/MicroLibrary/MacroMath.cs index 98f331a..43b9e0e 100644 --- a/Runtime/MicroLibrary/MacroMath.cs +++ b/Runtime/MicroLibrary/MacroMath.cs @@ -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)); + /// + /// Interpolates t between a and b to a value between 0 and 1 using a Hermite polynomial. + /// + /// The first value + /// The second value + /// The position value + /// A smoothed value between 0 and 1 + public static float SmoothStep(float a, float b, float t) + { + t = Mathf.Clamp01((t - a) / (b - a)); + return t * t * (3f - 2f * t); + } + + /// + /// Interpolates t between a and b to a value between 0 and 1. + /// + /// The first value + /// The second value + /// The position value + /// Linear value between 0 and 1 + public static float LinearStep(float a, float b, float t) + { + return Mathf.Clamp01((t - a) / (b - a)); + } + + /// + /// Wraps a value between min and max. + /// + /// The value to wrap + /// The minimum value + /// The maximum value + /// + 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; + } + + /// + /// Wraps a value between min and max. + /// + /// The value to wrap + /// The minimum value + /// The maximum value + /// + 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; + } + + /// + /// Wraps a value between min and max. + /// + /// The value to wrap + /// The minimum value + /// The maximum value + /// + 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; + } + + /// + /// Rounds a number based on a mininum difference. + /// + /// The value to round + /// The min difference + /// The rounded value. + 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); + } + + /// + /// Discards the least significant demicals. + /// + /// The value of insignificant decimals + /// Value with significant decimals + 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; + } + } + + /// + /// Clamps and wraps an angle between two values. + /// + /// + /// + /// + /// + 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; + } + + /// + /// 获取最小差值的小数个数 + /// + /// + /// + public static int GetNumberOfDecimalsForMinimumDifference(double minDifference) + { + return Mathf.Clamp(-Mathf.FloorToInt(Mathf.Log10(Mathf.Abs((float)minDifference))), 0, 15); + } + #endregion #region 字节控制 diff --git a/Runtime/MicroLibrary/Vector3Extend.cs b/Runtime/MicroLibrary/Vector3Extend.cs index 4d9c97d..57763d5 100644 --- a/Runtime/MicroLibrary/Vector3Extend.cs +++ b/Runtime/MicroLibrary/Vector3Extend.cs @@ -113,79 +113,7 @@ namespace XericLibrary.Runtime.MacroLibrary #endregion - #region 扩展运算 - - [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 高级运算 + #region 破坏运算 [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3 Min(this Vector3 a, Vector3 b) @@ -256,6 +184,101 @@ namespace XericLibrary.Runtime.MacroLibrary #endregion + #region 扩展运算 + + [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); + + /// + /// Pows each element of the vector. + /// + /// + /// + /// + 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 特殊运算 /// @@ -341,7 +364,493 @@ namespace XericLibrary.Runtime.MacroLibrary #endregion - } + #region 未整理 + + 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. + // + // 参数: + // 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. + // + // 参数: + // 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. + // + // 参数: + // 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. + // + // 参数: + // 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. + // + // 参数: + // 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. + // + // 参数: + // 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. + // + // 参数: + // 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. + // + // 参数: + // 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. + // + // 参数: + // 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. + // + // 参数: + // 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. + // + // 参数: + // 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. + // + // 参数: + // 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. + // + // 参数: + // 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. + // + // 参数: + // 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. + // + // 参数: + // 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. + // + // 参数: + // 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. + // + // 参数: + // 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. + // + // 参数: + // 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. + // + // 参数: + // 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. + // + // 返回结果: + // 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. + // + // 参数: + // 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. + // + // 参数: + // 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. + // + // 参数: + // 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.. + // + // 参数: + // 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 + +} diff --git a/Runtime/Type/Enum/Orientation.cs b/Runtime/Type/Enum/Orientation.cs index b860982..e69de29 100644 --- a/Runtime/Type/Enum/Orientation.cs +++ b/Runtime/Type/Enum/Orientation.cs @@ -1,30 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -namespace XericLibrary.Runtime.Type -{ - /// - /// 矢量方位 - /// - public enum Vector3Orientation - { - Front, - Back, - Up, - Down, - Left, - Right, - } - /// - /// 矢量方位 - /// - public enum Vector2Orientation - { - UpFront, - DownBack, - Left, - Right, - } -}