using UnityEngine;
using System.Collections.Generic;
namespace SK.Framework
{
///
/// 三维向量相关拓展
///
public static class Vector3Extension
{
///
/// 将xyz值放入一个长度为3的float数组中
///
/// 三维向量
/// float数组
public static float[] ToArray(this Vector3 self)
{
float[] retArray = new float[3];
retArray[0] = self.x;
retArray[1] = self.y;
retArray[2] = self.z;
return retArray;
}
///
/// 转四元数
///
/// 三维向量
/// 四元数
public static Quaternion ToQuaternion(this Vector3 self)
{
return Quaternion.Euler(self);
}
///
/// 获取最小值
///
/// 三维向量列表
/// 最小值
/// 三维向量列表
public static List GetMin(this List self, out Vector3 min)
{
min = self[0];
for (int i = 1; i < self.Count; i++)
{
min = Vector3.Min(min, self[i]);
}
return self;
}
///
/// 获取最大值
///
/// 三维向量列表
/// 最大值
/// 三维向量列表
public static List GetMax(this List self, out Vector3 max)
{
max = self[0];
for (int i = 1; i < self.Count; i++)
{
max = Vector3.Max(max, self[i]);
}
return self;
}
///
/// 计算角度-叉乘
///
/// 三维向量
/// 目标三维向量
/// 角度值
public static float GetAngle(this Vector3 self, Vector3 target)
{
return Mathf.Acos(Vector3.Dot(self.normalized, target.normalized)) * Mathf.Rad2Deg;
}
///
/// 生成多边形Mesh网格
///
/// 多边形顶点数组
/// 网格
public static Mesh GenerateMesh(this Vector3[] self)
{
Mesh retMesh = new Mesh();
List triangles = new List();
for (int i = 0; i < self.Length - 1; i++)
{
triangles.Add(i);
triangles.Add(i + 1);
triangles.Add(self.Length - i - 1);
}
retMesh.vertices = self;
retMesh.triangles = triangles.ToArray();
retMesh.RecalculateBounds();
retMesh.RecalculateNormals();
return retMesh;
}
///
/// 生成贝塞尔曲线
///
/// 控制点
/// 贝塞尔曲线起点
/// 贝塞尔曲线终点
/// 贝塞尔曲线点个数
/// 组成贝塞尔曲线的点集合
public static Vector3[] GenerateBeizer(this Vector3 self, Vector3 startPoint, Vector3 endPoint, int count)
{
Vector3[] retValue = new Vector3[count];
for (int i = 1; i height || self.y < -height) return false;
var comparePoint = (points[0] + points[1]) * 0.5f;
comparePoint += (comparePoint - self).normalized * 10000;
int count = 0;
for (int i = 0; i < points.Length; i++)
{
var a = points[i % points.Length];
var b = points[(i + 1) % points.Length];
var crossA = Mathf.Sign(Vector3.Cross(comparePoint - self, a - self).y);
var crossB = Mathf.Sign(Vector3.Cross(comparePoint - self, b - self).y);
if (Mathf.Approximately(crossA, crossB)) continue;
var crossC = Mathf.Sign(Vector3.Cross(b - a, self - a).y);
var crossD = Mathf.Sign(Vector3.Cross(b - a, comparePoint - a).y);
if (Mathf.Approximately(crossC, crossD)) continue;
count++;
}
return count % 2 == 1;
}
///
/// 获取点坐标数组
///
/// 点列表
/// 点坐标数组
public static Vector3[] GetPositions(this List self)
{
Vector3[] retArray = new Vector3[self.Count];
for (int i = 0; i < self.Count; i++)
{
retArray[i] = self[i].position;
}
return retArray;
}
///
/// 获取点坐标数组
///
/// 点数组
/// 点坐标数组
public static Vector3[] GetPositions(this Transform[] self)
{
Vector3[] retArray = new Vector3[self.Length];
for (int i = 0; i < self.Length; i++)
{
retArray[i] = self[i].position;
}
return retArray;
}
}
}
Unity 使用this关键字进行函数拓展 - Vector3
关注
打赏