Unity 使用this关键字进行函数拓展 - Vector3

举报
CoderZ1010 发表于 2022/09/25 04:39:07 2022/09/25
【摘要】 using UnityEngine;using System.Collections.Generic; namespace SK.Framework{ /// <summary> /// 三维向量相关拓展 /// </summary> public static class Vector3...

  
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. namespace SK.Framework
  4. {
  5. /// <summary>
  6. /// 三维向量相关拓展
  7. /// </summary>
  8. public static class Vector3Extension
  9. {
  10. /// <summary>
  11. /// 将xyz值放入一个长度为3的float数组中
  12. /// </summary>
  13. /// <param name="self">三维向量</param>
  14. /// <returns>float数组</returns>
  15. public static float[] ToArray(this Vector3 self)
  16. {
  17. float[] retArray = new float[3];
  18. retArray[0] = self.x;
  19. retArray[1] = self.y;
  20. retArray[2] = self.z;
  21. return retArray;
  22. }
  23. /// <summary>
  24. /// 转四元数
  25. /// </summary>
  26. /// <param name="self">三维向量</param>
  27. /// <returns>四元数</returns>
  28. public static Quaternion ToQuaternion(this Vector3 self)
  29. {
  30. return Quaternion.Euler(self);
  31. }
  32. /// <summary>
  33. /// 获取最小值
  34. /// </summary>
  35. /// <param name="self">三维向量列表</param>
  36. /// <param name="min">最小值</param>
  37. /// <returns>三维向量列表</returns>
  38. public static List<Vector3> GetMin(this List<Vector3> self, out Vector3 min)
  39. {
  40. min = self[0];
  41. for (int i = 1; i < self.Count; i++)
  42. {
  43. min = Vector3.Min(min, self[i]);
  44. }
  45. return self;
  46. }
  47. /// <summary>
  48. /// 获取最大值
  49. /// </summary>
  50. /// <param name="self">三维向量列表</param>
  51. /// <param name="max">最大值</param>
  52. /// <returns>三维向量列表</returns>
  53. public static List<Vector3> GetMax(this List<Vector3> self, out Vector3 max)
  54. {
  55. max = self[0];
  56. for (int i = 1; i < self.Count; i++)
  57. {
  58. max = Vector3.Max(max, self[i]);
  59. }
  60. return self;
  61. }
  62. /// <summary>
  63. /// 计算角度-叉乘
  64. /// </summary>
  65. /// <param name="self">三维向量</param>
  66. /// <param name="target">目标三维向量</param>
  67. /// <returns>角度值</returns>
  68. public static float GetAngle(this Vector3 self, Vector3 target)
  69. {
  70. return Mathf.Acos(Vector3.Dot(self.normalized, target.normalized)) * Mathf.Rad2Deg;
  71. }
  72. /// <summary>
  73. /// 生成多边形Mesh网格
  74. /// </summary>
  75. /// <param name="self">多边形顶点数组</param>
  76. /// <returns>网格</returns>
  77. public static Mesh GenerateMesh(this Vector3[] self)
  78. {
  79. Mesh retMesh = new Mesh();
  80. List<int> triangles = new List<int>();
  81. for (int i = 0; i < self.Length - 1; i++)
  82. {
  83. triangles.Add(i);
  84. triangles.Add(i + 1);
  85. triangles.Add(self.Length - i - 1);
  86. }
  87. retMesh.vertices = self;
  88. retMesh.triangles = triangles.ToArray();
  89. retMesh.RecalculateBounds();
  90. retMesh.RecalculateNormals();
  91. return retMesh;
  92. }
  93. /// <summary>
  94. /// 生成贝塞尔曲线
  95. /// </summary>
  96. /// <param name="self">控制点</param>
  97. /// <param name="startPoint">贝塞尔曲线起点</param>
  98. /// <param name="endPoint">贝塞尔曲线终点</param>
  99. /// <param name="count">贝塞尔曲线点个数</param>
  100. /// <returns>组成贝塞尔曲线的点集合</returns>
  101. public static Vector3[] GenerateBeizer(this Vector3 self, Vector3 startPoint, Vector3 endPoint, int count)
  102. {
  103. Vector3[] retValue = new Vector3[count];
  104. for (int i = 1; i <= count; i++)
  105. {
  106. float t = i / (float)count;
  107. float u = 1 - t;
  108. float tt = Mathf.Pow(t, 2);
  109. float uu = Mathf.Pow(u, 2);
  110. Vector3 point = uu * startPoint;
  111. point += 2 * u * t * self;
  112. point += tt * endPoint;
  113. retValue[i - 1] = point;
  114. }
  115. return retValue;
  116. }
  117. /// <summary>
  118. /// 与目标点的距离
  119. /// </summary>
  120. /// <param name="self">Vector3</param>
  121. /// <param name="target">目标点</param>
  122. /// <returns>距离</returns>
  123. public static float Distance(this Vector3 self, Vector3 target)
  124. {
  125. return Vector3.Distance(self, target);
  126. }
  127. /// <summary>
  128. /// 与目标点的中点
  129. /// </summary>
  130. /// <param name="self">Vector3</param>
  131. /// <param name="target">目标点</param>
  132. /// <returns>中点</returns>
  133. public static Vector3 Half(this Vector3 self, Vector3 target)
  134. {
  135. return (self + target) / 2.0f;
  136. }
  137. /// <summary>
  138. /// 指向目标点的方向
  139. /// </summary>
  140. /// <param name="self">Vector3</param>
  141. /// <param name="target">目标点</param>
  142. /// <returns>方向</returns>
  143. public static Vector3 Direction(this Vector3 self, Vector3 target)
  144. {
  145. return (target - self).normalized;
  146. }
  147. /// <summary>
  148. /// 判断目标点是否在指定区域内
  149. /// </summary>
  150. /// <param name="self">目标点</param>
  151. /// <param name="points">区域各顶点</param>
  152. /// <param name="height">区域高度</param>
  153. /// <returns>是否在区域内</returns>
  154. public static bool IsInRange(this Vector3 self, Vector3[] points, float height)
  155. {
  156. if (self.y > height || self.y < -height) return false;
  157. var comparePoint = (points[0] + points[1]) * 0.5f;
  158. comparePoint += (comparePoint - self).normalized * 10000;
  159. int count = 0;
  160. for (int i = 0; i < points.Length; i++)
  161. {
  162. var a = points[i % points.Length];
  163. var b = points[(i + 1) % points.Length];
  164. var crossA = Mathf.Sign(Vector3.Cross(comparePoint - self, a - self).y);
  165. var crossB = Mathf.Sign(Vector3.Cross(comparePoint - self, b - self).y);
  166. if (Mathf.Approximately(crossA, crossB)) continue;
  167. var crossC = Mathf.Sign(Vector3.Cross(b - a, self - a).y);
  168. var crossD = Mathf.Sign(Vector3.Cross(b - a, comparePoint - a).y);
  169. if (Mathf.Approximately(crossC, crossD)) continue;
  170. count++;
  171. }
  172. return count % 2 == 1;
  173. }
  174. /// <summary>
  175. /// 获取点坐标数组
  176. /// </summary>
  177. /// <param name="self">点列表</param>
  178. /// <returns>点坐标数组</returns>
  179. public static Vector3[] GetPositions(this List<Transform> self)
  180. {
  181. Vector3[] retArray = new Vector3[self.Count];
  182. for (int i = 0; i < self.Count; i++)
  183. {
  184. retArray[i] = self[i].position;
  185. }
  186. return retArray;
  187. }
  188. /// <summary>
  189. /// 获取点坐标数组
  190. /// </summary>
  191. /// <param name="self">点数组</param>
  192. /// <returns>点坐标数组</returns>
  193. public static Vector3[] GetPositions(this Transform[] self)
  194. {
  195. Vector3[] retArray = new Vector3[self.Length];
  196. for (int i = 0; i < self.Length; i++)
  197. {
  198. retArray[i] = self[i].position;
  199. }
  200. return retArray;
  201. }
  202. }
  203. }

文章来源: coderz.blog.csdn.net,作者:CoderZ1010,版权归原作者所有,如需转载,请联系作者。

原文链接:coderz.blog.csdn.net/article/details/116755802

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。