EditablePathController.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using System.Linq;
  3. using UnityEngine;
  4. namespace UnityEditor.Experimental.Rendering.Universal.Path2D
  5. {
  6. internal class EditablePathController : IEditablePathController
  7. {
  8. private ISnapping<Vector3> m_Snapping = new Snapping();
  9. public IEditablePath editablePath { get; set; }
  10. public IEditablePath closestEditablePath { get { return editablePath; } }
  11. public ISnapping<Vector3> snapping
  12. {
  13. get { return m_Snapping; }
  14. set { m_Snapping = value; }
  15. }
  16. public bool enableSnapping { get; set; }
  17. public void RegisterUndo(string name)
  18. {
  19. if (editablePath.undoObject != null)
  20. editablePath.undoObject.RegisterUndo(name);
  21. }
  22. public void ClearSelection()
  23. {
  24. editablePath.selection.Clear();
  25. }
  26. public void SelectPoint(int index, bool select)
  27. {
  28. editablePath.selection.Select(index, select);
  29. }
  30. public void CreatePoint(int index, Vector3 position)
  31. {
  32. ClearSelection();
  33. if (editablePath.shapeType == ShapeType.Polygon)
  34. {
  35. editablePath.InsertPoint(index + 1, new ControlPoint() { position = position });
  36. }
  37. else if (editablePath.shapeType == ShapeType.Spline)
  38. {
  39. var nextIndex = NextIndex(index);
  40. var currentPoint = editablePath.GetPoint(index);
  41. var nextPoint = editablePath.GetPoint(nextIndex);
  42. float t;
  43. var closestPoint = BezierUtility.ClosestPointOnCurve(
  44. position,
  45. currentPoint.position,
  46. nextPoint.position,
  47. GetRightTangentPosition(index),
  48. GetLeftTangentPosition(nextIndex),
  49. out t);
  50. Vector3 leftStartPosition;
  51. Vector3 leftEndPosition;
  52. Vector3 leftStartTangent;
  53. Vector3 leftEndTangent;
  54. Vector3 rightStartPosition;
  55. Vector3 rightEndPosition;
  56. Vector3 rightStartTangent;
  57. Vector3 rightEndTangent;
  58. BezierUtility.SplitBezier(t, currentPoint.position, nextPoint.position, GetRightTangentPosition(index), GetLeftTangentPosition(nextIndex),
  59. out leftStartPosition, out leftEndPosition, out leftStartTangent, out leftEndTangent,
  60. out rightStartPosition, out rightEndPosition, out rightStartTangent, out rightEndTangent);
  61. var newPointIndex = index + 1;
  62. var newPoint = new ControlPoint()
  63. {
  64. position = closestPoint,
  65. leftTangent = leftEndTangent,
  66. rightTangent = rightStartTangent,
  67. tangentMode = TangentMode.Continuous
  68. };
  69. currentPoint.rightTangent = leftStartTangent;
  70. nextPoint.leftTangent = rightEndTangent;
  71. if (currentPoint.tangentMode == TangentMode.Linear && nextPoint.tangentMode == TangentMode.Linear)
  72. {
  73. newPoint.tangentMode = TangentMode.Linear;
  74. newPoint.localLeftTangent = Vector3.zero;
  75. newPoint.localRightTangent = Vector3.zero;
  76. currentPoint.localRightTangent = Vector3.zero;
  77. nextPoint.localLeftTangent = Vector3.zero;
  78. }
  79. else
  80. {
  81. if (currentPoint.tangentMode == TangentMode.Linear)
  82. currentPoint.tangentMode = TangentMode.Broken;
  83. if (nextPoint.tangentMode == TangentMode.Linear)
  84. nextPoint.tangentMode = TangentMode.Broken;
  85. }
  86. editablePath.SetPoint(index, currentPoint);
  87. editablePath.SetPoint(nextIndex, nextPoint);
  88. editablePath.InsertPoint(newPointIndex, newPoint);
  89. }
  90. }
  91. public void RemoveSelectedPoints()
  92. {
  93. var minPointCount = editablePath.isOpenEnded ? 2 : 3;
  94. if (editablePath.pointCount > minPointCount)
  95. {
  96. var indices = editablePath.selection.elements.OrderByDescending( i => i);
  97. foreach (var index in indices)
  98. if (editablePath.pointCount > minPointCount)
  99. editablePath.RemovePoint(index);
  100. ClearSelection();
  101. }
  102. }
  103. public void MoveSelectedPoints(Vector3 delta)
  104. {
  105. delta = Vector3.ProjectOnPlane(delta, editablePath.forward);
  106. for (var i = 0; i < editablePath.pointCount; ++i)
  107. {
  108. if (editablePath.selection.Contains(i))
  109. {
  110. var controlPoint = editablePath.GetPoint(i);
  111. controlPoint.position += delta;
  112. editablePath.SetPoint(i, controlPoint);
  113. }
  114. }
  115. }
  116. public void MoveEdge(int index, Vector3 delta)
  117. {
  118. if (editablePath.isOpenEnded && index == editablePath.pointCount - 1)
  119. return;
  120. var controlPoint = editablePath.GetPoint(index);
  121. controlPoint.position += delta;
  122. editablePath.SetPoint(index, controlPoint);
  123. controlPoint = NextControlPoint(index);
  124. controlPoint.position += delta;
  125. editablePath.SetPoint(NextIndex(index), controlPoint);
  126. }
  127. public void SetLeftTangent(int index, Vector3 position, bool setToLinear, bool mirror, Vector3 cachedRightTangent)
  128. {
  129. var controlPoint = editablePath.GetPoint(index);
  130. controlPoint.leftTangent = position;
  131. controlPoint.mirrorLeft = false;
  132. if (setToLinear)
  133. {
  134. controlPoint.leftTangent = controlPoint.position;
  135. controlPoint.rightTangent = cachedRightTangent;
  136. }
  137. else if (controlPoint.tangentMode == TangentMode.Continuous || mirror)
  138. {
  139. var magnitude = controlPoint.localRightTangent.magnitude;
  140. if (mirror)
  141. magnitude = controlPoint.localLeftTangent.magnitude;
  142. controlPoint.localRightTangent = magnitude * -controlPoint.localLeftTangent.normalized;
  143. }
  144. editablePath.SetPoint(index, controlPoint);
  145. }
  146. public void SetRightTangent(int index, Vector3 position, bool setToLinear, bool mirror, Vector3 cachedLeftTangent)
  147. {
  148. var controlPoint = editablePath.GetPoint(index);
  149. controlPoint.rightTangent = position;
  150. controlPoint.mirrorLeft = true;
  151. if (setToLinear)
  152. {
  153. controlPoint.rightTangent = controlPoint.position;
  154. controlPoint.leftTangent = cachedLeftTangent;
  155. }
  156. else if (controlPoint.tangentMode == TangentMode.Continuous || mirror)
  157. {
  158. var magnitude = controlPoint.localLeftTangent.magnitude;
  159. if (mirror)
  160. magnitude = controlPoint.localRightTangent.magnitude;
  161. controlPoint.localLeftTangent = magnitude * -controlPoint.localRightTangent.normalized;
  162. }
  163. editablePath.SetPoint(index, controlPoint);
  164. }
  165. public void ClearClosestPath() { }
  166. public void AddClosestPath(float distance) { }
  167. private Vector3 GetLeftTangentPosition(int index)
  168. {
  169. var isLinear = Mathf.Approximately(editablePath.GetPoint(index).localLeftTangent.sqrMagnitude, 0f);
  170. if (isLinear)
  171. {
  172. var position = editablePath.GetPoint(index).position;
  173. var prevPosition = PrevControlPoint(index).position;
  174. return (1f / 3f) * (prevPosition - position) + position;
  175. }
  176. return editablePath.GetPoint(index).leftTangent;
  177. }
  178. private Vector3 GetRightTangentPosition(int index)
  179. {
  180. var isLinear = Mathf.Approximately(editablePath.GetPoint(index).localRightTangent.sqrMagnitude, 0f);
  181. if (isLinear)
  182. {
  183. var position = editablePath.GetPoint(index).position;
  184. var nextPosition = NextControlPoint(index).position;
  185. return (1f / 3f) * (nextPosition - position) + position;
  186. }
  187. return editablePath.GetPoint(index).rightTangent;
  188. }
  189. private int NextIndex(int index)
  190. {
  191. return EditablePathUtility.Mod(index + 1, editablePath.pointCount);
  192. }
  193. private ControlPoint NextControlPoint(int index)
  194. {
  195. return editablePath.GetPoint(NextIndex(index));
  196. }
  197. private int PrevIndex(int index)
  198. {
  199. return EditablePathUtility.Mod(index - 1, editablePath.pointCount);
  200. }
  201. private ControlPoint PrevControlPoint(int index)
  202. {
  203. return editablePath.GetPoint(PrevIndex(index));
  204. }
  205. }
  206. }