mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-16 13:30:10 +00:00
219 lines
7.3 KiB
C#
219 lines
7.3 KiB
C#
/******************************************/
|
||
/* */
|
||
/* Copyright (c) 2020 monitor1394 */
|
||
/* https://github.com/monitor1394 */
|
||
/* */
|
||
/******************************************/
|
||
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
namespace XUGL
|
||
{
|
||
public static class UGLHelper
|
||
{
|
||
public static bool IsValueEqualsColor(Color32 color1, Color32 color2)
|
||
{
|
||
return color1.a == color2.a &&
|
||
color1.b == color2.b &&
|
||
color1.g == color2.g &&
|
||
color1.r == color2.r;
|
||
}
|
||
|
||
public static bool IsValueEqualsColor(Color color1, Color color2)
|
||
{
|
||
return color1.a == color2.a &&
|
||
color1.b == color2.b &&
|
||
color1.g == color2.g &&
|
||
color1.r == color2.r;
|
||
}
|
||
|
||
public static bool IsValueEqualsString(string str1, string str2)
|
||
{
|
||
if (str1 == null && str2 == null) return true;
|
||
else if (str1 != null && str2 != null) return str1.Equals(str2);
|
||
else return false;
|
||
}
|
||
|
||
public static bool IsValueEqualsVector2(Vector2 v1, Vector2 v2)
|
||
{
|
||
return v1.x == v2.x && v1.y == v2.y;
|
||
}
|
||
|
||
public static bool IsValueEqualsVector3(Vector3 v1, Vector3 v2)
|
||
{
|
||
return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z;
|
||
}
|
||
|
||
public static bool IsValueEqualsList<T>(List<T> list1, List<T> list2)
|
||
{
|
||
if (list1 == null || list2 == null) return false;
|
||
if (list1.Count != list2.Count) return false;
|
||
for (int i = 0; i < list1.Count; i++)
|
||
{
|
||
if (list1[i] == null && list2[i] == null) { }
|
||
else
|
||
{
|
||
if (list1[i] != null)
|
||
{
|
||
if (!list1[i].Equals(list2[i])) return false;
|
||
}
|
||
else
|
||
{
|
||
if (!list2[i].Equals(list1[i])) return false;
|
||
}
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public static bool IsClearColor(Color32 color)
|
||
{
|
||
return color.a == 0 && color.b == 0 && color.g == 0 && color.r == 0;
|
||
}
|
||
|
||
public static bool IsClearColor(Color color)
|
||
{
|
||
return color.a == 0 && color.b == 0 && color.g == 0 && color.r == 0;
|
||
}
|
||
|
||
public static bool IsZeroVector(Vector3 pos)
|
||
{
|
||
return pos.x == 0 && pos.y == 0 && pos.z == 0;
|
||
}
|
||
|
||
public static Vector3 RotateRound(Vector3 position, Vector3 center, Vector3 axis, float angle)
|
||
{
|
||
Vector3 point = Quaternion.AngleAxis(angle, axis) * (position - center);
|
||
Vector3 resultVec3 = center + point;
|
||
return resultVec3;
|
||
}
|
||
|
||
public static void GetBezierList(ref List<Vector3> posList, Vector3 sp, Vector3 ep,
|
||
Vector3 lsp, Vector3 nep, float smoothness = 2f, float k = 2.0f)
|
||
{
|
||
float dist = Mathf.Abs(sp.x - ep.x);
|
||
Vector3 cp1, cp2;
|
||
var dir = (ep - sp).normalized;
|
||
var diff = dist / k;
|
||
if (lsp == sp)
|
||
{
|
||
cp1 = sp + dist / k * dir * 1;
|
||
cp1.y = sp.y;
|
||
cp1 = sp;
|
||
}
|
||
else
|
||
{
|
||
cp1 = sp + (ep - lsp).normalized * diff;
|
||
}
|
||
if (nep == ep) cp2 = ep;
|
||
else cp2 = ep - (nep - sp).normalized * diff;
|
||
dist = Vector3.Distance(sp, ep);
|
||
int segment = (int)(dist / (smoothness <= 0 ? 2f : smoothness));
|
||
if (segment < 1) segment = (int)(dist / 0.5f);
|
||
if (segment < 4) segment = 4;
|
||
GetBezierList2(ref posList, sp, ep, segment, cp1, cp2);
|
||
}
|
||
|
||
public static void GetBezierListVertical(ref List<Vector3> posList, Vector3 sp, Vector3 ep,
|
||
float smoothness = 2f, float k = 2.0f)
|
||
{
|
||
Vector3 dir = (ep - sp).normalized;
|
||
float dist = Vector3.Distance(sp, ep);
|
||
Vector3 cp1 = sp + dist / k * dir * 1;
|
||
Vector3 cp2 = sp + dist / k * dir * (k - 1);
|
||
cp1.x = sp.x;
|
||
cp2.x = ep.x;
|
||
int segment = (int)(dist / (smoothness <= 0 ? 2f : smoothness));
|
||
GetBezierList2(ref posList, sp, ep, segment, cp1, cp2);
|
||
}
|
||
|
||
public static List<Vector3> GetBezierList(Vector3 sp, Vector3 ep, int segment, Vector3 cp)
|
||
{
|
||
List<Vector3> list = new List<Vector3>();
|
||
for (int i = 0; i < segment; i++)
|
||
{
|
||
list.Add(GetBezier(i / (float)segment, sp, cp, ep));
|
||
}
|
||
list.Add(ep);
|
||
return list;
|
||
}
|
||
|
||
public static void GetBezierList2(ref List<Vector3> posList, Vector3 sp, Vector3 ep, int segment, Vector3 cp,
|
||
Vector3 cp2)
|
||
{
|
||
posList.Clear();
|
||
if (posList.Capacity < segment + 1)
|
||
{
|
||
posList.Capacity = segment + 1;
|
||
}
|
||
for (int i = 0; i < segment; i++)
|
||
{
|
||
posList.Add((GetBezier2(i / (float)segment, sp, cp, cp2, ep)));
|
||
}
|
||
posList.Add(ep);
|
||
}
|
||
|
||
public static Vector3 GetBezier(float t, Vector3 sp, Vector3 cp, Vector3 ep)
|
||
{
|
||
Vector3 aa = sp + (cp - sp) * t;
|
||
Vector3 bb = cp + (ep - cp) * t;
|
||
return aa + (bb - aa) * t;
|
||
}
|
||
|
||
public static Vector3 GetBezier2(float t, Vector3 sp, Vector3 p1, Vector3 p2, Vector3 ep)
|
||
{
|
||
t = Mathf.Clamp01(t);
|
||
var oneMinusT = 1f - t;
|
||
return oneMinusT * oneMinusT * oneMinusT * sp +
|
||
3f * oneMinusT * oneMinusT * t * p1 +
|
||
3f * oneMinusT * t * t * p2 +
|
||
t * t * t * ep;
|
||
}
|
||
|
||
public static Vector3 GetDire(float angle, bool isDegree = false)
|
||
{
|
||
angle = isDegree ? angle * Mathf.Deg2Rad : angle;
|
||
return new Vector3(Mathf.Sin(angle), Mathf.Cos(angle));
|
||
}
|
||
|
||
public static Vector3 GetVertialDire(Vector3 dire)
|
||
{
|
||
if (dire.x == 0)
|
||
{
|
||
return new Vector3(-1, 0, 0);
|
||
}
|
||
if (dire.y == 0)
|
||
{
|
||
return new Vector3(0, -1, 0);
|
||
}
|
||
else
|
||
{
|
||
return new Vector3(-dire.y / dire.x, 1, 0).normalized;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获得0-360的角度(12点钟方向为0度)
|
||
/// </summary>
|
||
/// <param name="from"></param>
|
||
/// <param name="to"></param>
|
||
/// <returns></returns>
|
||
public static float GetAngle360(Vector2 from, Vector2 to)
|
||
{
|
||
float angle;
|
||
|
||
Vector3 cross = Vector3.Cross(from, to);
|
||
angle = Vector2.Angle(from, to);
|
||
angle = cross.z > 0 ? -angle : angle;
|
||
angle = (angle + 360) % 360;
|
||
return angle;
|
||
}
|
||
|
||
public static Vector3 GetPos(Vector3 center, float radius, float angle, bool isDegree = false)
|
||
{
|
||
angle = isDegree ? angle * Mathf.Deg2Rad : angle;
|
||
return new Vector3(center.x + radius * Mathf.Sin(angle), center.y + radius * Mathf.Cos(angle));
|
||
}
|
||
}
|
||
} |