Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [CustomEditor(typeof(PathCreator))]
- public class PathEditor : Editor {
- PathCreator creator;
- Path path;
- Path subPath;
- float curveLengthValue = 1.0f;
- void OnEnable()
- {
- creator = (PathCreator)target;
- if (creator.path == null)
- {
- creator.CreatePath();
- }
- path = creator.path;
- subPath = new Path(creator.transform.position);
- }
- public override void OnInspectorGUI()
- {
- base.OnInspectorGUI();
- EditorGUI.BeginChangeCheck();
- bool continuousControlPoints = GUILayout.Toggle(path.isContinuous, "Set Continuous Control Points");
- if (continuousControlPoints != path.isContinuous)
- {
- Undo.RecordObject(creator, "Toggle set continuous controls");
- path.isContinuous = continuousControlPoints;
- }
- bool symmetricControlPoints = GUILayout.Toggle(path.isSymmetric, "Symmetric Control Points");
- if (symmetricControlPoints != path.isSymmetric)
- {
- Undo.RecordObject(creator, "Toggle set symmetric controls");
- path.isSymmetric = symmetricControlPoints;
- }
- // HERE IS WHERE I HANDLE THE BEZIER SIZE SLIDER
- if (symmetricControlPoints)
- {
- curveLengthValue = EditorGUILayout.Slider("Curve Length", creator.curveSizeValue, 0.0f, 1.0f);
- if (!Mathf.Approximately(creator.curveSizeValue, curveLengthValue))
- {
- 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
- creator.curveSizeValue = curveLengthValue;
- }
- }
- if (EditorGUI.EndChangeCheck())
- {
- SceneView.RepaintAll();
- }
- }
- static Path IntervalFromTo(Path curve, float start, float end, int segIndex)
- {
- float oneMinusStart = 1f - start;
- float oneMinusEnd = 1f - end;
- float scale = (end - start) / 3f;
- var a =
- oneMinusStart * oneMinusStart * oneMinusStart * curve.GetPointsInSegment(segIndex)[0] +
- 3f * oneMinusStart * oneMinusStart * start * curve.GetPointsInSegment(segIndex)[1] +
- 3f * oneMinusStart * start * start * curve.GetPointsInSegment(segIndex)[2] +
- start * start * start * curve.GetPointsInSegment(segIndex)[3];
- var b = a + (3f * oneMinusStart * oneMinusStart * (curve.GetPointsInSegment(segIndex)[1] - curve.GetPointsInSegment(segIndex)[0]) +
- 6 * start * oneMinusStart * (curve.GetPointsInSegment(segIndex)[2] - curve.GetPointsInSegment(segIndex)[1]) +
- 3 * start * start * (curve.GetPointsInSegment(segIndex)[3] - curve.GetPointsInSegment(segIndex)[2])) * scale;
- var d = oneMinusEnd * oneMinusEnd * oneMinusEnd * curve.GetPointsInSegment(segIndex)[0] +
- 3f * oneMinusEnd * oneMinusEnd * end * curve.GetPointsInSegment(segIndex)[1] +
- 3f * oneMinusEnd * end * end * curve.GetPointsInSegment(segIndex)[2] +
- end * end * end * curve[3];
- var c = d - (3f * oneMinusEnd * oneMinusEnd * (curve.GetPointsInSegment(segIndex)[1] - curve.GetPointsInSegment(segIndex)[0]) +
- 6 * end * oneMinusEnd * (curve.GetPointsInSegment(segIndex)[2] - curve.GetPointsInSegment(segIndex)[1]) +
- 3 * end * end * (curve.GetPointsInSegment(segIndex)[3] - curve.GetPointsInSegment(segIndex)[2])) * scale;
- return curve;
- }
- void OnSceneGUI()
- {
- Handles.matrix = creator.transform.localToWorldMatrix;
- handleRotation = Tools.pivotRotation == PivotRotation.Local ? creator.transform.rotation : Quaternion.identity;
- Input();
- Draw();
- }
- void Input()
- {
- Event guiEvent = Event.current;
- Vector2 mousePos = HandleUtility.GUIPointToWorldRay(guiEvent.mousePosition).origin;
- if (guiEvent.type == EventType.MouseDown && guiEvent.button == 0 && guiEvent.shift)
- {
- Undo.RecordObject(creator, "Add segment");
- path.AddSegment(mousePos);
- }
- }
- void Draw()
- {
- for (int i = 0; i < path.NumSegments; i++)
- {
- Vector2[] points = path.GetPointsInSegment(i);
- Handles.color = Color.black;
- Handles.DrawLine(points[1], points[0]);
- Handles.DrawLine(points[2], points[3]);
- Handles.DrawBezier(points[0], points[3], points[1], points[2], Color.green, null, 2);
- }
- creator.DrawBezierCurve(); // HANDLES DRAWING OF LINERENDERER FOR BEZIER CURVE
- Handles.color = Color.red;
- for (int i = 0; i < path.NumPoints; i++)
- {
- Vector2 newPos = Handles.FreeMoveHandle(path[i], Quaternion.identity, .1f, Vector2.zero, Handles.CylinderHandleCap);
- if (path[i] != newPos)
- {
- Undo.RecordObject(creator, "Move point");
- path.MovePoint(i, newPos);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment