Unity 使用this关键字进行函数拓展 - Vector3
【摘要】
using UnityEngine;using System.Collections.Generic; namespace SK.Framework{ /// <summary> /// 三维向量相关拓展 /// </summary> public static class Vector3...
-
using UnityEngine;
-
using System.Collections.Generic;
-
-
namespace SK.Framework
-
{
-
/// <summary>
-
/// 三维向量相关拓展
-
/// </summary>
-
public static class Vector3Extension
-
{
-
/// <summary>
-
/// 将xyz值放入一个长度为3的float数组中
-
/// </summary>
-
/// <param name="self">三维向量</param>
-
/// <returns>float数组</returns>
-
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;
-
}
-
/// <summary>
-
/// 转四元数
-
/// </summary>
-
/// <param name="self">三维向量</param>
-
/// <returns>四元数</returns>
-
public static Quaternion ToQuaternion(this Vector3 self)
-
{
-
return Quaternion.Euler(self);
-
}
-
/// <summary>
-
/// 获取最小值
-
/// </summary>
-
/// <param name="self">三维向量列表</param>
-
/// <param name="min">最小值</param>
-
/// <returns>三维向量列表</returns>
-
public static List<Vector3> GetMin(this List<Vector3> self, out Vector3 min)
-
{
-
min = self[0];
-
for (int i = 1; i < self.Count; i++)
-
{
-
min = Vector3.Min(min, self[i]);
-
}
-
return self;
-
}
-
/// <summary>
-
/// 获取最大值
-
/// </summary>
-
/// <param name="self">三维向量列表</param>
-
/// <param name="max">最大值</param>
-
/// <returns>三维向量列表</returns>
-
public static List<Vector3> GetMax(this List<Vector3> self, out Vector3 max)
-
{
-
max = self[0];
-
for (int i = 1; i < self.Count; i++)
-
{
-
max = Vector3.Max(max, self[i]);
-
}
-
return self;
-
}
-
/// <summary>
-
/// 计算角度-叉乘
-
/// </summary>
-
/// <param name="self">三维向量</param>
-
/// <param name="target">目标三维向量</param>
-
/// <returns>角度值</returns>
-
public static float GetAngle(this Vector3 self, Vector3 target)
-
{
-
return Mathf.Acos(Vector3.Dot(self.normalized, target.normalized)) * Mathf.Rad2Deg;
-
}
-
/// <summary>
-
/// 生成多边形Mesh网格
-
/// </summary>
-
/// <param name="self">多边形顶点数组</param>
-
/// <returns>网格</returns>
-
public static Mesh GenerateMesh(this Vector3[] self)
-
{
-
Mesh retMesh = new Mesh();
-
List<int> triangles = new List<int>();
-
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;
-
}
-
/// <summary>
-
/// 生成贝塞尔曲线
-
/// </summary>
-
/// <param name="self">控制点</param>
-
/// <param name="startPoint">贝塞尔曲线起点</param>
-
/// <param name="endPoint">贝塞尔曲线终点</param>
-
/// <param name="count">贝塞尔曲线点个数</param>
-
/// <returns>组成贝塞尔曲线的点集合</returns>
-
public static Vector3[] GenerateBeizer(this Vector3 self, Vector3 startPoint, Vector3 endPoint, int count)
-
{
-
Vector3[] retValue = new Vector3[count];
-
for (int i = 1; i <= count; i++)
-
{
-
float t = i / (float)count;
-
float u = 1 - t;
-
float tt = Mathf.Pow(t, 2);
-
float uu = Mathf.Pow(u, 2);
-
Vector3 point = uu * startPoint;
-
point += 2 * u * t * self;
-
point += tt * endPoint;
-
retValue[i - 1] = point;
-
}
-
return retValue;
-
}
-
/// <summary>
-
/// 与目标点的距离
-
/// </summary>
-
/// <param name="self">Vector3</param>
-
/// <param name="target">目标点</param>
-
/// <returns>距离</returns>
-
public static float Distance(this Vector3 self, Vector3 target)
-
{
-
return Vector3.Distance(self, target);
-
}
-
/// <summary>
-
/// 与目标点的中点
-
/// </summary>
-
/// <param name="self">Vector3</param>
-
/// <param name="target">目标点</param>
-
/// <returns>中点</returns>
-
public static Vector3 Half(this Vector3 self, Vector3 target)
-
{
-
return (self + target) / 2.0f;
-
}
-
/// <summary>
-
/// 指向目标点的方向
-
/// </summary>
-
/// <param name="self">Vector3</param>
-
/// <param name="target">目标点</param>
-
/// <returns>方向</returns>
-
public static Vector3 Direction(this Vector3 self, Vector3 target)
-
{
-
return (target - self).normalized;
-
}
-
/// <summary>
-
/// 判断目标点是否在指定区域内
-
/// </summary>
-
/// <param name="self">目标点</param>
-
/// <param name="points">区域各顶点</param>
-
/// <param name="height">区域高度</param>
-
/// <returns>是否在区域内</returns>
-
public static bool IsInRange(this Vector3 self, Vector3[] points, float height)
-
{
-
if (self.y > 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;
-
}
-
/// <summary>
-
/// 获取点坐标数组
-
/// </summary>
-
/// <param name="self">点列表</param>
-
/// <returns>点坐标数组</returns>
-
public static Vector3[] GetPositions(this List<Transform> self)
-
{
-
Vector3[] retArray = new Vector3[self.Count];
-
for (int i = 0; i < self.Count; i++)
-
{
-
retArray[i] = self[i].position;
-
}
-
return retArray;
-
}
-
/// <summary>
-
/// 获取点坐标数组
-
/// </summary>
-
/// <param name="self">点数组</param>
-
/// <returns>点坐标数组</returns>
-
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;
-
}
-
}
-
}
文章来源: coderz.blog.csdn.net,作者:CoderZ1010,版权归原作者所有,如需转载,请联系作者。
原文链接:coderz.blog.csdn.net/article/details/116755802
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)