Math.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using SXR;
  5. namespace Ximmerse.XR.Internal
  6. {
  7. /// <summary>
  8. /// Polyengine mathf methods library.
  9. /// </summary>
  10. internal static class Math
  11. {
  12. /// <summary>
  13. /// Approximately the specified a and b.
  14. /// </summary>
  15. /// <param name="a">The alpha component.</param>
  16. /// <param name="b">The blue component.</param>
  17. public static bool Approximately(Vector4 a, Vector4 b)
  18. {
  19. return Mathf.Approximately(a.x, b.x) && Mathf.Approximately(a.y, b.y) && Mathf.Approximately(a.z, b.z) && Mathf.Approximately(a.w, b.w);
  20. }
  21. /// <summary>
  22. /// Approximately the specified a and b.
  23. /// </summary>
  24. /// <param name="a">The alpha component.</param>
  25. /// <param name="b">The blue component.</param>
  26. public static bool Approximately(Vector3 a, Vector3 b)
  27. {
  28. return Mathf.Approximately(a.x, b.x) && Mathf.Approximately(a.y, b.y) && Mathf.Approximately(a.z, b.z);
  29. }
  30. /// <summary>
  31. /// Approximately the specified a and b.
  32. /// </summary>
  33. /// <param name="a">The alpha component.</param>
  34. /// <param name="b">The blue component.</param>
  35. public static bool Approximately(Vector2 a, Vector2 b)
  36. {
  37. return Mathf.Approximately(a.x, b.x) && Mathf.Approximately(a.y, b.y);
  38. }
  39. /// <summary>
  40. /// Makes the angle a pretty value between [-180 ... 180]
  41. /// </summary>
  42. /// <returns>The angle.</returns>
  43. /// <param name="angle">Angle.</param>
  44. public static float PrettyAngle(this float angle)
  45. {
  46. if (angle >= -180 && angle <= 180)
  47. {
  48. return angle;
  49. }
  50. if (angle <= -180)
  51. {
  52. float mod = Mathf.Repeat(-angle, 360);
  53. if (mod >= 180)
  54. {
  55. return -360 + mod;
  56. }
  57. else
  58. {
  59. return -mod;
  60. }
  61. }
  62. else if (angle > 180 && angle <= 360)
  63. {
  64. return angle - 360;
  65. }
  66. else
  67. {
  68. float mod = Mathf.Repeat(angle, 360);
  69. if (mod >= 180)
  70. {
  71. return -360 + mod;
  72. }
  73. else
  74. {
  75. return mod;
  76. }
  77. }
  78. }
  79. /// <summary>
  80. /// Makes the euler a pretty value between [-180 ... 180]
  81. /// </summary>
  82. /// <returns>The euler.</returns>
  83. /// <param name="euler">Euler.</param>
  84. public static Vector3 PrettyAngle(this Vector3 euler)
  85. {
  86. return new Vector3(PrettyAngle(euler.x),
  87. PrettyAngle(euler.y),
  88. PrettyAngle(euler.z));
  89. }
  90. /// <summary>
  91. /// Makes the euler a pretty value between [-180 ... 180]
  92. /// </summary>
  93. /// <returns>The euler.</returns>
  94. public static Vector3 PrettyAngle(this Quaternion q)
  95. {
  96. return q.eulerAngles.PrettyAngle();
  97. }
  98. /// <summary>
  99. /// 把 rotation 的X和Z轴旋转角度设置为0。
  100. /// Flatten 之后的方向只有水平旋转角度。
  101. /// </summary>
  102. /// <param name="rotation">Rotation.</param>
  103. public static void FlattenXZ(ref Quaternion rotation)
  104. {
  105. var euler = rotation.eulerAngles;
  106. euler.x = 0;
  107. euler.z = 0;
  108. rotation.eulerAngles = euler;
  109. }
  110. /// <summary>
  111. /// 把 rotation 的X和Z轴旋转角度设置为0。
  112. /// Flatten 之后的方向只有水平旋转角度。
  113. /// </summary>
  114. /// <param name="rotation">Rotation.</param>
  115. public static void FlattenXZ(this Quaternion rotation)
  116. {
  117. FlattenXZ(ref rotation);
  118. }
  119. /// <summary>
  120. /// Get the XZ d of dir1 and dir2 (on XZ surface)
  121. /// </summary>
  122. /// <returns>The X.</returns>
  123. /// <param name="dir1">Dir1.</param>
  124. /// <param name="dir2">Dir2.</param>
  125. public static float DistanceXZ(this Vector3 pos1, Vector3 pos2)
  126. {
  127. Vector3 newPos2 = new Vector3(pos2.x, pos1.y, pos2.z);
  128. return Vector3.Distance(pos1, newPos2);
  129. }
  130. /// <summary>
  131. /// Get the XZ DOT of dir1 and dir2 (on XZ surface)
  132. /// </summary>
  133. /// <returns>The X.</returns>
  134. /// <param name="dir1">Dir1.</param>
  135. /// <param name="dir2">Dir2.</param>
  136. public static float DotXZ(this Vector3 dir1, Vector3 dir2)
  137. {
  138. Vector3 newDir1 = new Vector3(dir1.x, dir2.y, dir1.z);
  139. return Vector3.Dot(newDir1, dir2);
  140. }
  141. /// <summary>
  142. /// Get the XZ signed angle of dir1 and dir2 (on XZ surface)
  143. /// </summary>
  144. /// <returns>The X.</returns>
  145. /// <param name="dir1">Dir1.</param>
  146. /// <param name="dir2">Dir2.</param>
  147. public static float SignedAngleXZ(this Vector3 dir1, Vector3 dir2)
  148. {
  149. Vector2 newDir1 = new Vector2(dir1.x, dir1.z);
  150. Vector2 newDir2 = new Vector2(dir2.x, dir2.z);
  151. return SignedAngle(newDir1, newDir2);
  152. }
  153. /// <summary>
  154. /// Steps the input, return a float that is multiple step to stepValue, and not bigger than input.
  155. /// For example, input value = 0.7, step = 0.5, return = 0.5. Input vlaue = 1.2. step = 0.5, return = 1
  156. /// </summary>
  157. /// <param name="value">Value.</param>
  158. /// <param name="step">Step.</param>
  159. public static float FloorStep(float input, float step)
  160. {
  161. int stepCount = Mathf.FloorToInt(input / step);
  162. return stepCount * step;
  163. }
  164. /// <summary>
  165. /// Steps the input, return a float that is multiple step to stepValue, and not smaller than input.
  166. /// For example, input value = 0.7, step = 0.5, return = 1. Input value = 1.2, step = 0.5, return = 1.5
  167. /// </summary>
  168. /// <returns>The step.</returns>
  169. /// <param name="input">Input.</param>
  170. /// <param name="step">Step.</param>
  171. public static float CeilStep(float input, float step)
  172. {
  173. int stepCount = Mathf.CeilToInt(input / step);
  174. return stepCount * step;
  175. }
  176. /// <summary>
  177. /// Get the XZ angle of dir1 and dir2 (on XZ surface)
  178. /// </summary>
  179. /// <returns>The X.</returns>
  180. /// <param name="dir1">Dir1.</param>
  181. /// <param name="dir2">Dir2.</param>
  182. public static float AngleXZ(this Vector3 dir1, Vector3 dir2)
  183. {
  184. Vector2 newDir1 = new Vector3(dir1.x, dir1.z);
  185. Vector2 newDir2 = new Vector3(dir2.x, dir2.z);
  186. return Vector2.Angle(newDir1, newDir2);
  187. }
  188. /// <summary>
  189. /// Rounds the float.
  190. /// </summary>
  191. /// <returns>The float.</returns>
  192. /// <param name="f">F.</param>
  193. /// <param name="digit">Digit.</param>
  194. public static float RoundSingle(this float f, int digit = 2)
  195. {
  196. if (digit <= 0)
  197. {
  198. throw new System.Exception("Digit must GE 0");
  199. }
  200. double d = (double)f;
  201. d = System.Math.Round(d, digit);
  202. float round = (float)d;
  203. return round;
  204. }
  205. /// <summary>
  206. /// Rounds the vector2.
  207. /// </summary>
  208. /// <returns>The float.</returns>
  209. /// <param name="f">F.</param>
  210. /// <param name="digit">Digit.</param>
  211. public static Vector2 RoundVector2(this Vector2 vector2, int digit = 2)
  212. {
  213. if (digit <= 0)
  214. {
  215. throw new System.Exception("Digit must GE 0");
  216. }
  217. float roundX = vector2.x.RoundSingle(digit);
  218. float roundY = vector2.y.RoundSingle(digit);
  219. return new Vector2(roundX, roundY);
  220. }
  221. /// <summary>
  222. /// Rounds the vector3.
  223. /// </summary>
  224. /// <returns>The float.</returns>
  225. /// <param name="f">F.</param>
  226. /// <param name="digit">Digit.</param>
  227. public static Vector3 RoundVector3(this Vector3 vector3, int digit = 2)
  228. {
  229. if (digit <= 0)
  230. {
  231. throw new System.Exception("Digit must GE 0");
  232. }
  233. float roundX = vector3.x.RoundSingle(digit);
  234. float roundY = vector3.y.RoundSingle(digit);
  235. float roundZ = vector3.z.RoundSingle(digit);
  236. return new Vector3(roundX, roundY, roundZ);
  237. }
  238. /// <summary>
  239. /// Rounds the vector4.
  240. /// </summary>
  241. /// <returns>The float.</returns>
  242. /// <param name="f">F.</param>
  243. /// <param name="digit">Digit.</param>
  244. public static Vector4 RoundVector4(this Vector4 vector4, int digit = 2)
  245. {
  246. if (digit <= 0)
  247. {
  248. throw new System.Exception("Digit must GE 0");
  249. }
  250. float roundX = vector4.x.RoundSingle(digit);
  251. float roundY = vector4.y.RoundSingle(digit);
  252. float roundZ = vector4.z.RoundSingle(digit);
  253. float roundW = vector4.w.RoundSingle(digit);
  254. return new Vector4(roundX, roundY, roundZ, roundW);
  255. }
  256. /// <summary>
  257. /// Gets the signed angle of baseDir and dir2
  258. /// </summary>
  259. /// <returns>The angle.</returns>
  260. /// <param name="baseDir">Base dir.</param>
  261. /// <param name="dir2">Dir2.</param>
  262. public static float SignedAngle(Vector3 baseDir, Vector3 dir2)
  263. {
  264. var angle = Vector3.Angle(baseDir, dir2);
  265. return angle * Mathf.Sign(Vector3.Cross(baseDir, dir2).y);
  266. }
  267. public static float SignedAngle(Quaternion qBase, Quaternion qDir)
  268. {
  269. Vector3 v1 = qBase * Vector3.forward;
  270. Vector3 v2 = qDir * Vector3.forward;
  271. return SignedAngle(v1, v2);
  272. }
  273. /// <summary>
  274. /// 返回 dir2 到 baseDir 的带符号角度。
  275. /// 如果在dir2在baseDir右边,返回1.
  276. /// 否则返回-1.
  277. /// 如果方向相同,返回0
  278. /// </summary>
  279. /// <returns>The angle.</returns>
  280. /// <param name="baseDir">Base dir.</param>
  281. /// <param name="dir2">Dir2.</param>
  282. public static float SignedAngle(Vector2 baseDir, Vector2 dir2)
  283. {
  284. Vector3 vB = new Vector3(baseDir.x, 0, baseDir.y);
  285. Vector3 v2 = new Vector3(dir2.x, 0, dir2.y);
  286. var angle = Vector3.Angle(vB, v2);
  287. return angle * Mathf.Sign(Vector3.Cross(vB, v2).y);
  288. }
  289. /// <summary>
  290. /// 计算两个float 的绝对值距离
  291. /// </summary>
  292. /// <returns>The diff.</returns>
  293. /// <param name="a">The alpha component.</param>
  294. /// <param name="b">The blue component.</param>
  295. public static float AbsDiff(float a, float b)
  296. {
  297. if (a == b || Mathf.Approximately(a, b))
  298. {
  299. return 0;
  300. }
  301. if (Mathf.Sign(a) == Mathf.Sign(b))
  302. {
  303. var a1 = Mathf.Abs(a);
  304. var a2 = Mathf.Abs(b);
  305. return Mathf.Abs(a1 - a2);
  306. }
  307. else
  308. {
  309. var bigger = a > b ? a : b;
  310. var smaller = a > b ? b : a;
  311. return bigger - smaller;
  312. }
  313. }
  314. /// <summary>
  315. /// Minimum of two vectors
  316. /// </summary>
  317. /// <param name="v1">V1.</param>
  318. /// <param name="v2">V2.</param>
  319. public static Vector3 Min(Vector3 v1, Vector3 v2)
  320. {
  321. Vector3 ret = new Vector3(Mathf.Min(v1.x, v2.x),
  322. Mathf.Min(v1.y, v2.y),
  323. Mathf.Min(v1.z, v2.z));
  324. return ret;
  325. }
  326. /// <summary>
  327. /// 把 vect.x,y,z 的值 Clamp 在 [0..1]
  328. /// </summary>
  329. /// <param name="vect">Vect.</param>
  330. public static Vector3 Clamp01(Vector3 vect)
  331. {
  332. return new Vector3(Mathf.Clamp01(vect.x),
  333. Mathf.Clamp01(vect.y),
  334. Mathf.Clamp01(vect.z));
  335. }
  336. /// <summary>
  337. /// Maximum of two vectors
  338. /// </summary>
  339. /// <param name="v1">V1.</param>
  340. /// <param name="v2">V2.</param>
  341. public static Vector3 Max(Vector3 v1, Vector3 v2)
  342. {
  343. Vector3 ret = new Vector3(Mathf.Max(v1.x, v2.x),
  344. Mathf.Max(v1.y, v2.y),
  345. Mathf.Max(v1.z, v2.z));
  346. return ret;
  347. }
  348. /// <summary>
  349. /// 对 rotation 做Yaw(水平旋转 N个角度)
  350. /// </summary>
  351. /// <param name="rotation">Rotation.</param>
  352. public static Quaternion YawByAngle(this Quaternion rotation, float yaw)
  353. {
  354. rotation = rotation * Quaternion.Euler(0, yaw, 0);
  355. return rotation;
  356. }
  357. /// <summary>
  358. /// 对 rotation 做 Pitch (以X为轴旋转 N个角度)
  359. /// </summary>
  360. /// <param name="rotation">Rotation.</param>
  361. public static Quaternion PitchByAngle(this Quaternion rotation, float pitch)
  362. {
  363. rotation = rotation * Quaternion.Euler(pitch, 0, 0);
  364. return rotation;
  365. }
  366. /// <summary>
  367. /// Rolls by angle.
  368. /// </summary>
  369. /// <returns>The by angle.</returns>
  370. /// <param name="rotation">Rotation.</param>
  371. /// <param name="roll">Roll.</param>
  372. public static Quaternion RollByAngle(this Quaternion rotation, float roll)
  373. {
  374. rotation = rotation * Quaternion.Euler(0, 0, roll);
  375. return rotation;
  376. }
  377. /// <summary>
  378. /// Clamps the vector2.
  379. /// </summary>
  380. /// <returns>The vector2.</returns>
  381. /// <param name="v2">V2.</param>
  382. /// <param name="minV2">Minimum v2.</param>
  383. /// <param name="maxV2">Max v2.</param>
  384. public static Vector2 ClampVector2(this Vector2 v2, Vector2 minV2, Vector2 maxV2)
  385. {
  386. v2.x = Mathf.Clamp(v2.x, minV2.x, maxV2.x);
  387. v2.y = Mathf.Clamp(v2.y, minV2.y, maxV2.y);
  388. return v2;
  389. }
  390. /// <summary>
  391. /// 给出原数值 single, 和目标值 target, 速度 speed, 令 single 以speed的速度逼近 target。
  392. /// </summary>
  393. /// <param name="single">Single.</param>
  394. /// <param name="target">Target.</param>
  395. /// <param name="speed">Speed.</param>
  396. public static float Approach(this float single, float target, float speed, float deltaTime)
  397. {
  398. if (single == target || Mathf.Approximately(single, target))
  399. {
  400. return target;
  401. }
  402. int dir = target > single ? 1 : -1;//1:正向逼近, -1:负向逼近
  403. float velocity = speed * dir * deltaTime;
  404. single += velocity;
  405. if (single == target || Mathf.Approximately(single, target) ||
  406. (dir == 1 && single > target)//正向超越
  407. || (dir == -1 && single < target)) //负向超越
  408. {
  409. return target;
  410. }
  411. else
  412. {
  413. return single;
  414. }
  415. }
  416. /// <summary>
  417. /// 转换整形数字为小数点后的浮点值: 12345 --> 0.12345
  418. /// </summary>
  419. /// <returns>The fractional float.</returns>
  420. /// <param name="Int">Int.</param>
  421. public static float Int2FractionalFloat(this int Int)
  422. {
  423. if (Int == 0)
  424. return 0;
  425. var absInt = Mathf.Abs(Int);
  426. int seed = 1;
  427. float sign = Mathf.Sign(Int);
  428. for (int i = 1; seed <= int.MaxValue; i++)
  429. {
  430. seed *= 10;
  431. if (absInt < seed)
  432. {
  433. float ret = (((float)absInt) / ((float)seed)) * sign;
  434. return ret;
  435. }
  436. }
  437. //畸大数: 溢出
  438. return -1;
  439. }
  440. /// <summary>
  441. /// Calculate three plane's intersection position.
  442. /// </summary>
  443. /// <returns>The plane intersection.</returns>
  444. /// <param name="p1">P1.</param>
  445. /// <param name="p2">P2.</param>
  446. /// <param name="p3">P3.</param>
  447. public static Vector3 ThreePlaneIntersection(Plane p1, Plane p2, Plane p3)
  448. {
  449. //get the intersection point of 3 planes
  450. return ((-p1.distance * Vector3.Cross(p2.normal, p3.normal)) +
  451. (-p2.distance * Vector3.Cross(p3.normal, p1.normal)) +
  452. (-p3.distance * Vector3.Cross(p1.normal, p2.normal))) /
  453. (Vector3.Dot(p1.normal, Vector3.Cross(p2.normal, p3.normal)));
  454. }
  455. /// <summary>
  456. /// Calculate two line segment's intersection point.
  457. /// Do not calculate the intersection point, faster than another version.
  458. /// </summary>
  459. /// <returns><c>true</c>, if intersection was lined, <c>false</c> otherwise.</returns>
  460. /// <param name="p1">P1 - Line 1 start point</param>
  461. /// <param name="p2">P2 - Line 1 end point</param>
  462. /// <param name="p3">P3 - Line 2 start point</param>
  463. /// <param name="p4">P4 - Line 2 end point</param>
  464. /// <param name="intersection">Intersection.</param>
  465. public static bool LineIntersection(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4)
  466. {
  467. float Ax, Bx, Cx, Ay, By, Cy, d, e, f/*, num,offset*/;
  468. float x1lo, x1hi, y1lo, y1hi;
  469. Ax = p2.x - p1.x;
  470. Bx = p3.x - p4.x;
  471. // X bound box test/
  472. if (Ax < 0)
  473. {
  474. x1lo = p2.x;
  475. x1hi = p1.x;
  476. }
  477. else
  478. {
  479. x1hi = p2.x;
  480. x1lo = p1.x;
  481. }
  482. if (Bx > 0)
  483. {
  484. if (x1hi < p4.x || p3.x < x1lo)
  485. return false;
  486. }
  487. else
  488. {
  489. if (x1hi < p3.x || p4.x < x1lo)
  490. return false;
  491. }
  492. Ay = p2.y - p1.y;
  493. By = p3.y - p4.y;
  494. // Y bound box test//
  495. if (Ay < 0)
  496. {
  497. y1lo = p2.y;
  498. y1hi = p1.y;
  499. }
  500. else
  501. {
  502. y1hi = p2.y;
  503. y1lo = p1.y;
  504. }
  505. if (By > 0)
  506. {
  507. if (y1hi < p4.y || p3.y < y1lo)
  508. return false;
  509. }
  510. else
  511. {
  512. if (y1hi < p3.y || p4.y < y1lo)
  513. return false;
  514. }
  515. Cx = p1.x - p3.x;
  516. Cy = p1.y - p3.y;
  517. d = By * Cx - Bx * Cy; // alpha numerator//
  518. f = Ay * Bx - Ax * By; // both denominator//
  519. // alpha tests//
  520. if (f > 0)
  521. {
  522. if (d < 0 || d > f)
  523. return false;
  524. }
  525. else
  526. {
  527. if (d > 0 || d < f)
  528. return false;
  529. }
  530. e = Ax * Cy - Ay * Cx; // beta numerator//
  531. // beta tests //
  532. if (f > 0)
  533. {
  534. if (e < 0 || e > f)
  535. return false;
  536. }
  537. else
  538. {
  539. if (e > 0 || e < f)
  540. return false;
  541. }
  542. // check if they are parallel
  543. if (f == 0)
  544. return false;
  545. return true;
  546. }
  547. /// <summary>
  548. /// Calculate two line segment's intersection point.
  549. /// </summary>
  550. /// <returns><c>true</c>, if intersection was lined, <c>false</c> otherwise.</returns>
  551. /// <param name="p1">P1 - Line 1 start point</param>
  552. /// <param name="p2">P2 - Line 1 end point</param>
  553. /// <param name="p3">P3 - Line 2 start point</param>
  554. /// <param name="p4">P4 - Line 2 end point</param>
  555. /// <param name="intersection">Intersection.</param>
  556. public static bool LineIntersection(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, ref Vector2 intersection)
  557. {
  558. float Ax, Bx, Cx, Ay, By, Cy, d, e, f, num/*,offset*/;
  559. float x1lo, x1hi, y1lo, y1hi;
  560. Ax = p2.x - p1.x;
  561. Bx = p3.x - p4.x;
  562. // X bound box test/
  563. if (Ax < 0)
  564. {
  565. x1lo = p2.x;
  566. x1hi = p1.x;
  567. }
  568. else
  569. {
  570. x1hi = p2.x;
  571. x1lo = p1.x;
  572. }
  573. if (Bx > 0)
  574. {
  575. if (x1hi < p4.x || p3.x < x1lo)
  576. return false;
  577. }
  578. else
  579. {
  580. if (x1hi < p3.x || p4.x < x1lo)
  581. return false;
  582. }
  583. Ay = p2.y - p1.y;
  584. By = p3.y - p4.y;
  585. // Y bound box test//
  586. if (Ay < 0)
  587. {
  588. y1lo = p2.y;
  589. y1hi = p1.y;
  590. }
  591. else
  592. {
  593. y1hi = p2.y;
  594. y1lo = p1.y;
  595. }
  596. if (By > 0)
  597. {
  598. if (y1hi < p4.y || p3.y < y1lo)
  599. return false;
  600. }
  601. else
  602. {
  603. if (y1hi < p3.y || p4.y < y1lo)
  604. return false;
  605. }
  606. Cx = p1.x - p3.x;
  607. Cy = p1.y - p3.y;
  608. d = By * Cx - Bx * Cy; // alpha numerator//
  609. f = Ay * Bx - Ax * By; // both denominator//
  610. // alpha tests//
  611. if (f > 0)
  612. {
  613. if (d < 0 || d > f)
  614. return false;
  615. }
  616. else
  617. {
  618. if (d > 0 || d < f)
  619. return false;
  620. }
  621. e = Ax * Cy - Ay * Cx; // beta numerator//
  622. // beta tests //
  623. if (f > 0)
  624. {
  625. if (e < 0 || e > f)
  626. return false;
  627. }
  628. else
  629. {
  630. if (e > 0 || e < f)
  631. return false;
  632. }
  633. // check if they are parallel
  634. if (f == 0)
  635. return false;
  636. // compute intersection coordinates //
  637. num = d * Ax; // numerator //
  638. // offset = same_sign(num,f) ? f*0.5f : -f*0.5f; // round direction //
  639. // intersection.x = p1.x + (num+offset) / f;
  640. intersection.x = p1.x + num / f;
  641. num = d * Ay;
  642. // offset = same_sign(num,f) ? f*0.5f : -f*0.5f;
  643. // intersection.y = p1.y + (num+offset) / f;
  644. intersection.y = p1.y + num / f;
  645. return true;
  646. }
  647. /// <summary>
  648. /// Remove roll from euler angle.
  649. /// </summary>
  650. /// <returns>The roll.</returns>
  651. /// <param name="quaternion">Quaternion.</param>
  652. public static Quaternion PitchNYaw(this Quaternion quaternion)
  653. {
  654. Vector3 euler = quaternion.eulerAngles;
  655. return Quaternion.Euler(euler.x, euler.y, 0);
  656. }
  657. /// <summary>
  658. /// Remove yaw and roll from euler angle.
  659. /// </summary>
  660. /// <returns>The roll.</returns>
  661. /// <param name="quaternion">Quaternion.</param>
  662. public static Quaternion Pitch(this Quaternion quaternion)
  663. {
  664. Vector3 euler = quaternion.eulerAngles;
  665. return Quaternion.Euler(euler.x, 0, 0);
  666. }
  667. /// <summary>
  668. /// Remove pitch and roll from euler angle.
  669. /// </summary>
  670. /// <returns>The roll.</returns>
  671. /// <param name="quaternion">Quaternion.</param>
  672. public static Quaternion Yaw(this Quaternion quaternion)
  673. {
  674. Vector3 euler = quaternion.eulerAngles;
  675. return Quaternion.Euler(0, PrettyAngle(euler.y), 0);
  676. }
  677. /// <summary>
  678. /// Remove pitch and yaw from euler angle.
  679. /// </summary>
  680. /// <returns>The roll.</returns>
  681. /// <param name="quaternion">Quaternion.</param>
  682. public static Quaternion Roll(this Quaternion quaternion)
  683. {
  684. Vector3 euler = quaternion.eulerAngles;
  685. return Quaternion.Euler(0, 0, euler.z);
  686. }
  687. /// <summary>
  688. /// Calculate quaternion diff = lhs - rhs
  689. /// </summary>
  690. /// <returns>The iff.</returns>
  691. /// <param name="lhs">Lhs.</param>
  692. /// <param name="rhs">Rhs.</param>
  693. public static Quaternion QDiff(this Quaternion lhs, Quaternion rhs)
  694. {
  695. return Quaternion.Inverse(rhs) * lhs;
  696. }
  697. /// <summary>
  698. /// Calculate quaternion sum : lhs + rhs[0] + rhs[1] + rhs[2] ...
  699. /// </summary>
  700. /// <returns>The plus.</returns>
  701. /// <param name="lhs">Lhs.</param>
  702. /// <param name="rhs">Rhs.</param>
  703. public static Quaternion QSum(this Quaternion lhs, params Quaternion[] rhs)
  704. {
  705. Quaternion qSum = lhs;
  706. for (int i = 0; i < rhs.Length; i++)
  707. {
  708. qSum = qSum * rhs[i];
  709. }
  710. return qSum;
  711. }
  712. /// <summary>
  713. /// yaw angle diff : lhs.yaw - rhs.yaw
  714. /// </summary>
  715. /// <returns>The diff.</returns>
  716. /// <param name="lhs">lhs.</param>
  717. /// <param name="rhs">rhs.</param>
  718. public static float YawDiff(this Quaternion lhs, Quaternion rhs)
  719. {
  720. Quaternion yawlhs = lhs.Yaw();
  721. Quaternion yawrhs = rhs.Yaw();
  722. var qdiff = yawlhs.QDiff(yawrhs);
  723. return PrettyAngle(qdiff.eulerAngles.y);
  724. }
  725. /// <summary>
  726. /// 输出内部归一化参数配置的双目 View Frustum
  727. /// </summary>
  728. /// <param name="near"></param>
  729. /// <param name="far"></param>
  730. /// <param name="left"></param>
  731. /// <param name="right"></param>
  732. /// <param name="top"></param>
  733. /// <param name="bottom"></param>
  734. /// <param name="hFov"></param>
  735. /// <param name="vFov"></param>
  736. internal static void GetViewFrustum(out float near, out float far,
  737. out float left, out float right, out float top, out float bottom, out float hFov, out float vFov)
  738. {
  739. near = ParamLoader.ParamLoaderGetFloat((int)ParamType.Render_Frustum_Near_FLOAT);
  740. far = ParamLoader.ParamLoaderGetFloat((int)ParamType.Render_Frustum_Far_FLOAT);
  741. left = ParamLoader.ParamLoaderGetFloat((int)ParamType.Render_Frustum_Left_FLOAT);
  742. right = ParamLoader.ParamLoaderGetFloat((int)ParamType.Render_Frustum_Right_FLOAT);
  743. top = ParamLoader.ParamLoaderGetFloat((int)ParamType.Render_Frustum_Top_FLOAT);
  744. bottom = ParamLoader.ParamLoaderGetFloat((int)ParamType.Render_Frustum_Bottom_FLOAT);
  745. hFov = ParamLoader.ParamLoaderGetFloat((int)ParamType.Render_EyeBufferFovX_FLOAT);
  746. vFov = ParamLoader.ParamLoaderGetFloat((int)ParamType.Render_EyeBufferFovY_FLOAT);
  747. }
  748. /// <summary>
  749. /// Gets view frustum from ParamLoader
  750. /// </summary>
  751. /// <returns></returns>
  752. internal static Matrix4x4 GetViewFrustum()
  753. {
  754. GetViewFrustum(out float near, out float far,
  755. out float left, out float right, out float top, out float bottom, out float hFov, out float vFov);
  756. return Perspective(left, right, bottom, top, near, far);
  757. }
  758. static internal Matrix4x4 Perspective(float left, float right, float bottom, float top, float near, float far)
  759. {
  760. float x = 2.0F * near / (right - left);
  761. float y = 2.0F * near / (top - bottom);
  762. float a = (right + left) / (right - left);
  763. float b = (top + bottom) / (top - bottom);
  764. float c = -(far + near) / (far - near);
  765. float d = -(2.0F * far * near) / (far - near);
  766. float e = -1.0F;
  767. Matrix4x4 m = new Matrix4x4();
  768. m[0, 0] = x;
  769. m[0, 1] = 0;
  770. m[0, 2] = a;
  771. m[0, 3] = 0;
  772. m[1, 0] = 0;
  773. m[1, 1] = y;
  774. m[1, 2] = b;
  775. m[1, 3] = 0;
  776. m[2, 0] = 0;
  777. m[2, 1] = 0;
  778. m[2, 2] = c;
  779. m[2, 3] = d;
  780. m[3, 0] = 0;
  781. m[3, 1] = 0;
  782. m[3, 2] = e;
  783. m[3, 3] = 0;
  784. return m;
  785. }
  786. }
  787. }