2026-05-07 10:14:44 +02:00

92 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace Wing.Utils
{
public class GemetryHelper
{
/// <summary>
/// 重新计算中心点
/// </summary>
/// <param name="raw"></param>
/// <param name="bounds"></param>
/// <returns></returns>
public static List<Vector3> PivotToZero(List<Vector3> raw, out Bounds bounds)
{
Vector3 min = Vector3.one * float.MaxValue;
Vector3 max = Vector3.one * float.MinValue;
for (int i = 0; i < raw.Count; i++)
{
Vector3 pt = raw[i];
min = Vector3.Min(pt, min);
max = Vector3.Max(pt, max);
}
Vector3 center = (max + min) / 2;
List<Vector3> pts = new List<Vector3>();
for (int i = 0; i < raw.Count; i++)
{
pts.Add(raw[i] - center);
}
bounds = new Bounds(center, max - min);
return pts;
}
public static float ValueMod(float value, float modValue)
{
float df = value / modValue;
int sign = df == 0 ? 1 : (int)(df / Mathf.Abs(df));
return ((int)(df + sign * 0.5f)) * modValue;
}
public static Vector3 Vector3Mod(Vector3 value, float modValue)
{
float dx = ValueMod(value.x, modValue);
float dy = ValueMod(value.y, modValue);
float dz = ValueMod(value.z, modValue);
return new Vector3(dx, dy, dz);
}
public static float VectorAngle(Vector2 from, Vector2 to)
{
float angle;
Vector3 cross = Vector3.Cross(from, to);
angle = Vector2.Angle(from, to);
return cross.y > 0 ? angle : -angle;
}
public static float VectorAngle(Vector3 from, Vector3 to)
{
float angle;
Vector3 cross = Vector3.Cross(from, to);
angle = Vector3.Angle(from, to);
return cross.y > 0 ? angle : -angle;
}
public static Vector3 ClosestPointOnLineExt(Vector3 a, Vector3 b, Vector3 point)
{
Vector3 v1 = point - a;
Vector3 v2 = (b - a).normalized;
float t = Vector3.Dot(v2, v1);
Vector3 v3 = v2 * t;
Vector3 closestPoint = a + v3;
return closestPoint;
}
public static Vector3 Mirror(Vector3 a, Vector3 b, Vector3 point)
{
Vector3 closestPoint = ClosestPointOnLineExt(a, b, point);
Vector3 v4 = closestPoint - point;
return v4 * 2 + point;
}
}
}