暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SplineUtility.cs 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using Unity.Collections;
  3. using System.Runtime.InteropServices;
  4. using Unity.Collections.LowLevel.Unsafe;
  5. namespace UnityEngine.U2D
  6. {
  7. /// <summary>
  8. /// Utility functions for Spline.
  9. /// </summary>
  10. public class SplineUtility
  11. {
  12. /// <summary>
  13. /// Calculate angle between direction vectors.
  14. /// </summary>
  15. /// <param name="start">Start Vector</param>
  16. /// <param name="end">End Vector</param>
  17. /// <returns>Angle</returns>
  18. public static float SlopeAngle(Vector2 start, Vector2 end)
  19. {
  20. Vector2 dir = start - end;
  21. dir.Normalize();
  22. Vector2 dvup = new Vector2(0, 1f);
  23. Vector2 dvrt = new Vector2(1f, 0);
  24. float dr = Vector2.Dot(dir, dvrt);
  25. float du = Vector2.Dot(dir, dvup);
  26. float cu = Mathf.Acos(du);
  27. float sn = dr >= 0 ? 1.0f : -1.0f;
  28. float an = cu * Mathf.Rad2Deg * sn;
  29. // Adjust angles when direction is parallel to Up Axis.
  30. an = (du != 1f) ? an : 0;
  31. an = (du != -1f) ? an : -180f;
  32. return an;
  33. }
  34. /// <summary>
  35. /// Calculate Left and Right Tangents for the given Control Point.
  36. /// </summary>
  37. /// <param name="point">Position of current point</param>
  38. /// <param name="prevPoint">Position of previous point.</param>
  39. /// <param name="nextPoint">Position of next point.</param>
  40. /// <param name="forward">Forward vector.</param>
  41. /// <param name="scale">Scale.</param>
  42. /// <param name="rightTangent">Right Tangent (out Value)</param>
  43. /// <param name="leftTangent">Left Tangent (out Value)</param>
  44. public static void CalculateTangents(Vector3 point, Vector3 prevPoint, Vector3 nextPoint, Vector3 forward, float scale, out Vector3 rightTangent, out Vector3 leftTangent)
  45. {
  46. Vector3 v1 = (prevPoint - point).normalized;
  47. Vector3 v2 = (nextPoint - point).normalized;
  48. Vector3 v3 = v1 + v2;
  49. Vector3 cross = forward;
  50. if (prevPoint != nextPoint)
  51. {
  52. bool colinear = Mathf.Abs(v1.x * v2.y - v1.y * v2.x + v1.x * v2.z - v1.z * v2.x + v1.y * v2.z - v1.z * v2.y) < 0.01f;
  53. if (colinear)
  54. {
  55. rightTangent = v2 * scale;
  56. leftTangent = v1 * scale;
  57. return;
  58. }
  59. cross = Vector3.Cross(v1, v2);
  60. }
  61. rightTangent = Vector3.Cross(cross, v3).normalized * scale;
  62. leftTangent = -rightTangent;
  63. }
  64. internal static int NextIndex(int index, int pointCount)
  65. {
  66. return Mod(index + 1, pointCount);
  67. }
  68. internal static int PreviousIndex(int index, int pointCount)
  69. {
  70. return Mod(index - 1, pointCount);
  71. }
  72. private static int Mod(int x, int m)
  73. {
  74. int r = x % m;
  75. return r < 0 ? r + m : r;
  76. }
  77. }
  78. // Copy utility.
  79. internal class SpriteShapeCopyUtility<T> where T : struct
  80. {
  81. internal static void Copy(NativeSlice<T> dst, T[] src, int length)
  82. {
  83. NativeSlice<T> dstSet = new NativeSlice<T>(dst, 0, length);
  84. dstSet.CopyFrom(src);
  85. }
  86. internal static void Copy(T[] dst, NativeSlice<T> src, int length)
  87. {
  88. NativeSlice<T> dstSet = new NativeSlice<T>(src, 0, length);
  89. dstSet.CopyTo(dst);
  90. }
  91. }
  92. }