BezierUtility.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using UnityEngine;
  2. namespace UnityEditor.Experimental.Rendering.Universal.Path2D
  3. {
  4. internal static class BezierUtility
  5. {
  6. static Vector3[] s_TempPoints = new Vector3[3];
  7. public static Vector3 BezierPoint(Vector3 startPosition, Vector3 startTangent, Vector3 endTangent, Vector3 endPosition, float t)
  8. {
  9. float s = 1.0f - t;
  10. return startPosition * s * s * s + startTangent * s * s * t * 3.0f + endTangent * s * t * t * 3.0f + endPosition * t * t * t;
  11. }
  12. public static Vector3 ClosestPointOnCurve(Vector3 point, Vector3 startPosition, Vector3 endPosition, Vector3 startTangent, Vector3 endTangent, out float t)
  13. {
  14. Vector3 startToEnd = endPosition - startPosition;
  15. Vector3 startToTangent = (startTangent - startPosition);
  16. Vector3 endToTangent = (endTangent - endPosition);
  17. float sqrError = 0.001f;
  18. if (Colinear(startToTangent, startToEnd, sqrError) && Colinear(endToTangent, startToEnd, sqrError))
  19. return ClosestPointToSegment(point, startPosition, endPosition, out t);
  20. Vector3 leftStartPosition;
  21. Vector3 leftEndPosition;
  22. Vector3 leftStartTangent;
  23. Vector3 leftEndTangent;
  24. Vector3 rightStartPosition;
  25. Vector3 rightEndPosition;
  26. Vector3 rightStartTangent;
  27. Vector3 rightEndTangent;
  28. float leftStartT = 0f;
  29. float leftEndT = 0.5f;
  30. float rightStartT = 0.5f;
  31. float rightEndT = 1f;
  32. SplitBezier(0.5f, startPosition, endPosition, startTangent, endTangent,
  33. out leftStartPosition, out leftEndPosition, out leftStartTangent, out leftEndTangent,
  34. out rightStartPosition, out rightEndPosition, out rightStartTangent, out rightEndTangent);
  35. Vector3 pointLeft = ClosestPointOnCurveIterative(point, leftStartPosition, leftEndPosition, leftStartTangent, leftEndTangent, sqrError, ref leftStartT, ref leftEndT);
  36. Vector3 pointRight = ClosestPointOnCurveIterative(point, rightStartPosition, rightEndPosition, rightStartTangent, rightEndTangent, sqrError, ref rightStartT, ref rightEndT);
  37. if ((point - pointLeft).sqrMagnitude < (point - pointRight).sqrMagnitude)
  38. {
  39. t = leftStartT;
  40. return pointLeft;
  41. }
  42. t = rightStartT;
  43. return pointRight;
  44. }
  45. public static Vector3 ClosestPointOnCurveFast(Vector3 point, Vector3 startPosition, Vector3 endPosition, Vector3 startTangent, Vector3 endTangent, out float t)
  46. {
  47. float sqrError = 0.001f;
  48. float startT = 0f;
  49. float endT = 1f;
  50. Vector3 closestPoint = ClosestPointOnCurveIterative(point, startPosition, endPosition, startTangent, endTangent, sqrError, ref startT, ref endT);
  51. t = startT;
  52. return closestPoint;
  53. }
  54. private static Vector3 ClosestPointOnCurveIterative(Vector3 point, Vector3 startPosition, Vector3 endPosition, Vector3 startTangent, Vector3 endTangent, float sqrError, ref float startT, ref float endT)
  55. {
  56. while ((startPosition - endPosition).sqrMagnitude > sqrError)
  57. {
  58. Vector3 startToEnd = endPosition - startPosition;
  59. Vector3 startToTangent = (startTangent - startPosition);
  60. Vector3 endToTangent = (endTangent - endPosition);
  61. if (Colinear(startToTangent, startToEnd, sqrError) && Colinear(endToTangent, startToEnd, sqrError))
  62. {
  63. float t;
  64. Vector3 closestPoint = ClosestPointToSegment(point, startPosition, endPosition, out t);
  65. t *= (endT - startT);
  66. startT += t;
  67. endT -= t;
  68. return closestPoint;
  69. }
  70. Vector3 leftStartPosition;
  71. Vector3 leftEndPosition;
  72. Vector3 leftStartTangent;
  73. Vector3 leftEndTangent;
  74. Vector3 rightStartPosition;
  75. Vector3 rightEndPosition;
  76. Vector3 rightStartTangent;
  77. Vector3 rightEndTangent;
  78. SplitBezier(0.5f, startPosition, endPosition, startTangent, endTangent,
  79. out leftStartPosition, out leftEndPosition, out leftStartTangent, out leftEndTangent,
  80. out rightStartPosition, out rightEndPosition, out rightStartTangent, out rightEndTangent);
  81. s_TempPoints[0] = leftStartPosition;
  82. s_TempPoints[1] = leftStartTangent;
  83. s_TempPoints[2] = leftEndTangent;
  84. float sqrDistanceLeft = SqrDistanceToPolyLine(point, s_TempPoints);
  85. s_TempPoints[0] = rightEndPosition;
  86. s_TempPoints[1] = rightEndTangent;
  87. s_TempPoints[2] = rightStartTangent;
  88. float sqrDistanceRight = SqrDistanceToPolyLine(point, s_TempPoints);
  89. if (sqrDistanceLeft < sqrDistanceRight)
  90. {
  91. startPosition = leftStartPosition;
  92. endPosition = leftEndPosition;
  93. startTangent = leftStartTangent;
  94. endTangent = leftEndTangent;
  95. endT -= (endT - startT) * 0.5f;
  96. }
  97. else
  98. {
  99. startPosition = rightStartPosition;
  100. endPosition = rightEndPosition;
  101. startTangent = rightStartTangent;
  102. endTangent = rightEndTangent;
  103. startT += (endT - startT) * 0.5f;
  104. }
  105. }
  106. return endPosition;
  107. }
  108. public static void SplitBezier(float t, Vector3 startPosition, Vector3 endPosition, Vector3 startRightTangent, Vector3 endLeftTangent,
  109. out Vector3 leftStartPosition, out Vector3 leftEndPosition, out Vector3 leftStartTangent, out Vector3 leftEndTangent,
  110. out Vector3 rightStartPosition, out Vector3 rightEndPosition, out Vector3 rightStartTangent, out Vector3 rightEndTangent)
  111. {
  112. Vector3 tangent0 = (startRightTangent - startPosition);
  113. Vector3 tangent1 = (endLeftTangent - endPosition);
  114. Vector3 tangentEdge = (endLeftTangent - startRightTangent);
  115. Vector3 tangentPoint0 = startPosition + tangent0 * t;
  116. Vector3 tangentPoint1 = endPosition + tangent1 * (1f - t);
  117. Vector3 tangentEdgePoint = startRightTangent + tangentEdge * t;
  118. Vector3 newTangent0 = tangentPoint0 + (tangentEdgePoint - tangentPoint0) * t;
  119. Vector3 newTangent1 = tangentPoint1 + (tangentEdgePoint - tangentPoint1) * (1f - t);
  120. Vector3 newTangentEdge = newTangent1 - newTangent0;
  121. Vector3 bezierPoint = newTangent0 + newTangentEdge * t;
  122. leftStartPosition = startPosition;
  123. leftEndPosition = bezierPoint;
  124. leftStartTangent = tangentPoint0;
  125. leftEndTangent = newTangent0;
  126. rightStartPosition = bezierPoint;
  127. rightEndPosition = endPosition;
  128. rightStartTangent = newTangent1;
  129. rightEndTangent = tangentPoint1;
  130. }
  131. private static Vector3 ClosestPointToSegment(Vector3 point, Vector3 segmentStart, Vector3 segmentEnd, out float t)
  132. {
  133. Vector3 relativePoint = point - segmentStart;
  134. Vector3 segment = (segmentEnd - segmentStart);
  135. Vector3 segmentDirection = segment.normalized;
  136. float length = segment.magnitude;
  137. float dot = Vector3.Dot(relativePoint, segmentDirection);
  138. if (dot <= 0f)
  139. dot = 0f;
  140. else if (dot >= length)
  141. dot = length;
  142. t = dot / length;
  143. return segmentStart + segment * t;
  144. }
  145. private static float SqrDistanceToPolyLine(Vector3 point, Vector3[] points)
  146. {
  147. float minDistance = float.MaxValue;
  148. for (int i = 0; i < points.Length - 1; ++i)
  149. {
  150. float distance = SqrDistanceToSegment(point, points[i], points[i + 1]);
  151. if (distance < minDistance)
  152. minDistance = distance;
  153. }
  154. return minDistance;
  155. }
  156. private static float SqrDistanceToSegment(Vector3 point, Vector3 segmentStart, Vector3 segmentEnd)
  157. {
  158. Vector3 relativePoint = point - segmentStart;
  159. Vector3 segment = (segmentEnd - segmentStart);
  160. Vector3 segmentDirection = segment.normalized;
  161. float length = segment.magnitude;
  162. float dot = Vector3.Dot(relativePoint, segmentDirection);
  163. if (dot <= 0f)
  164. return (point - segmentStart).sqrMagnitude;
  165. else if (dot >= length)
  166. return (point - segmentEnd).sqrMagnitude;
  167. return Vector3.Cross(relativePoint, segmentDirection).sqrMagnitude;
  168. }
  169. private static bool Colinear(Vector3 v1, Vector3 v2, float error = 0.0001f)
  170. {
  171. return 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) < error;
  172. }
  173. }
  174. }