Guest User

Untitled

a guest
Sep 11th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. [CustomEditor(typeof(PathCreator))]
  2. public class PathEditor : Editor {
  3.  
  4. PathCreator creator;
  5. Path path;
  6. Path subPath;
  7. float curveLengthValue = 1.0f;
  8.  
  9.  
  10. void OnEnable()
  11. {
  12. creator = (PathCreator)target;
  13. if (creator.path == null)
  14. {
  15. creator.CreatePath();
  16. }
  17. path = creator.path;
  18. subPath = new Path(creator.transform.position);
  19.  
  20. }
  21.  
  22. public override void OnInspectorGUI()
  23. {
  24. base.OnInspectorGUI();
  25. EditorGUI.BeginChangeCheck();
  26.  
  27. bool continuousControlPoints = GUILayout.Toggle(path.isContinuous, "Set Continuous Control Points");
  28. if (continuousControlPoints != path.isContinuous)
  29. {
  30. Undo.RecordObject(creator, "Toggle set continuous controls");
  31. path.isContinuous = continuousControlPoints;
  32. }
  33.  
  34. bool symmetricControlPoints = GUILayout.Toggle(path.isSymmetric, "Symmetric Control Points");
  35. if (symmetricControlPoints != path.isSymmetric)
  36. {
  37. Undo.RecordObject(creator, "Toggle set symmetric controls");
  38. path.isSymmetric = symmetricControlPoints;
  39. }
  40.  
  41.  
  42. // HERE IS WHERE I HANDLE THE BEZIER SIZE SLIDER
  43. if (symmetricControlPoints)
  44. {
  45. curveLengthValue = EditorGUILayout.Slider("Curve Length", creator.curveSizeValue, 0.0f, 1.0f);
  46.  
  47. if (!Mathf.Approximately(creator.curveSizeValue, curveLengthValue))
  48. {
  49. subPath = PathIntervalFromTo(path, 0.5f - 0.5f * curveLengthValue, 0.5f + 0.5f * curveLengthValue, 0); //I'm not sure how to set the subPath so it draws the LineRenderer and responds to symmetry and continuity bools above
  50.  
  51. creator.curveSizeValue = curveLengthValue;
  52. }
  53. }
  54.  
  55. if (EditorGUI.EndChangeCheck())
  56. {
  57. SceneView.RepaintAll();
  58. }
  59. }
  60.  
  61. static Path IntervalFromTo(Path curve, float start, float end, int segIndex)
  62. {
  63.  
  64. float oneMinusStart = 1f - start;
  65. float oneMinusEnd = 1f - end;
  66. float scale = (end - start) / 3f;
  67.  
  68. var a =
  69. oneMinusStart * oneMinusStart * oneMinusStart * curve.GetPointsInSegment(segIndex)[0] +
  70. 3f * oneMinusStart * oneMinusStart * start * curve.GetPointsInSegment(segIndex)[1] +
  71. 3f * oneMinusStart * start * start * curve.GetPointsInSegment(segIndex)[2] +
  72. start * start * start * curve.GetPointsInSegment(segIndex)[3];
  73.  
  74. var b = a + (3f * oneMinusStart * oneMinusStart * (curve.GetPointsInSegment(segIndex)[1] - curve.GetPointsInSegment(segIndex)[0]) +
  75. 6 * start * oneMinusStart * (curve.GetPointsInSegment(segIndex)[2] - curve.GetPointsInSegment(segIndex)[1]) +
  76. 3 * start * start * (curve.GetPointsInSegment(segIndex)[3] - curve.GetPointsInSegment(segIndex)[2])) * scale;
  77.  
  78. var d = oneMinusEnd * oneMinusEnd * oneMinusEnd * curve.GetPointsInSegment(segIndex)[0] +
  79. 3f * oneMinusEnd * oneMinusEnd * end * curve.GetPointsInSegment(segIndex)[1] +
  80. 3f * oneMinusEnd * end * end * curve.GetPointsInSegment(segIndex)[2] +
  81. end * end * end * curve[3];
  82.  
  83. var c = d - (3f * oneMinusEnd * oneMinusEnd * (curve.GetPointsInSegment(segIndex)[1] - curve.GetPointsInSegment(segIndex)[0]) +
  84. 6 * end * oneMinusEnd * (curve.GetPointsInSegment(segIndex)[2] - curve.GetPointsInSegment(segIndex)[1]) +
  85. 3 * end * end * (curve.GetPointsInSegment(segIndex)[3] - curve.GetPointsInSegment(segIndex)[2])) * scale;
  86.  
  87. return curve;
  88. }
  89.  
  90.  
  91. void OnSceneGUI()
  92. {
  93. Handles.matrix = creator.transform.localToWorldMatrix;
  94. handleRotation = Tools.pivotRotation == PivotRotation.Local ? creator.transform.rotation : Quaternion.identity;
  95.  
  96. Input();
  97. Draw();
  98. }
  99.  
  100. void Input()
  101. {
  102. Event guiEvent = Event.current;
  103. Vector2 mousePos = HandleUtility.GUIPointToWorldRay(guiEvent.mousePosition).origin;
  104.  
  105. if (guiEvent.type == EventType.MouseDown && guiEvent.button == 0 && guiEvent.shift)
  106. {
  107. Undo.RecordObject(creator, "Add segment");
  108. path.AddSegment(mousePos);
  109. }
  110. }
  111.  
  112. void Draw()
  113. {
  114.  
  115. for (int i = 0; i < path.NumSegments; i++)
  116. {
  117. Vector2[] points = path.GetPointsInSegment(i);
  118. Handles.color = Color.black;
  119. Handles.DrawLine(points[1], points[0]);
  120. Handles.DrawLine(points[2], points[3]);
  121. Handles.DrawBezier(points[0], points[3], points[1], points[2], Color.green, null, 2);
  122. }
  123.  
  124. creator.DrawBezierCurve(); // HANDLES DRAWING OF LINERENDERER FOR BEZIER CURVE
  125. Handles.color = Color.red;
  126. for (int i = 0; i < path.NumPoints; i++)
  127. {
  128. Vector2 newPos = Handles.FreeMoveHandle(path[i], Quaternion.identity, .1f, Vector2.zero, Handles.CylinderHandleCap);
  129. if (path[i] != newPos)
  130. {
  131. Undo.RecordObject(creator, "Move point");
  132. path.MovePoint(i, newPos);
  133. }
  134. }
  135. }
  136.  
  137.  
  138. }
Add Comment
Please, Sign In to add comment