private static void DrawBezier(float width, Vector2 p1, Vector2 p2, Vector2 t1, Vector2 t2) { bool inverted = p1.x < p2.x; if (inverted) { float center_y = FPMath.CenterOfPoints(p1, p2).y; Vector2 p1_center = new Vector2(p1.x, center_y); Vector2 p2_center = new Vector2(p2.x, center_y); float delta = Mathf.Abs(p1.x - p2.x); float tangent = Mathf.Min(Vector2.Distance(p1, p1_center) * 0.5f, delta); Vector2 p1_tan = new Vector2(p1.x - tangent, p1.y); Vector2 p1c_tan = new Vector2(p1_tan.x, center_y); Vector2 p2_tan = new Vector2(p2.x + tangent, p2.y); Vector2 p2c_tan = new Vector2(p2_tan.x, center_y); Handles.DrawAAPolyLine(width, 2, p1_center, p2_center); Handles.DrawBezier(p1, p1_center, p1_tan, p1c_tan, Handles.color, null, width); Handles.DrawBezier(p2, p2_center, p2_tan, p2c_tan, Handles.color, null, width); } else { Handles.DrawBezier(p1, p2, t1, t2, Handles.color, null, width); } }
void OnGUI() { scroll = EditorGUILayout.BeginScrollView(scroll); if (GUILayout.Button("SAVE")) { GraphEditor.asset.SaveData(); } if (GUILayout.Button("RESET POSITION")) { GraphEditor.scroll = Vector2.zero; } if (GUILayout.Button("GO TO CENTER")) { GraphEditor.scroll = new Vector2(0.0f, 10.0f) + FPMath.CenterOfPoints(GraphEditor.graph.nodes.Select(n => (-n.position - (n.size / 2.0f)) * GraphEditor.zoom + (GraphEditor.window.size / 2.0f) / GraphEditor.zoom).ToList()); } EditorGUILayout.EndScrollView(); }
private static void DrawCircuit(float width, Vector2 p1, Vector2 p2, Vector2 t1, Vector2 t2) { bool inverted = p1.x < p2.x; if (inverted) { float center_y = FPMath.CenterOfPoints(p1, p2).y; float delta = Mathf.Min(Mathf.Abs(p1.x - p2.x) * 0.5f, 40.0f); Vector2 t1_pos = new Vector2(p1.x - delta, p1.y); Vector2 t2_pos = new Vector2(p2.x + delta, p2.y); Vector2 t1_center = new Vector2(t1_pos.x, center_y); Vector2 t2_center = new Vector2(t2_pos.x, center_y); Handles.DrawAAPolyLine(width, 6, p1, t1_pos, t1_center, t2_center, t2_pos, p2); } else { Handles.DrawAAPolyLine(width, 4, p1, t1, t2, p2); } }
public static Vector2 MiddleOfConnection(Vector2 start, Vector2 end) { return(FPMath.CenterOfPoints(start, end)); }
public static Vector2 LerpUnit(Vector2 start, Vector2 end, float t) { float delta = Mathf.Abs(start.x - end.x) * 0.5f; Vector2 right = new Vector2(delta, 0.0f); Vector2 left = new Vector2(-delta, 0.0f); Vector2 p1 = new Vector2(start.x, start.y); Vector2 p2 = new Vector2(end.x, end.y); Vector2 t1 = p1 + left; Vector2 t2 = p2 + right; bool inverted = p1.x < p2.x; float time = (t % 0.333333f) * 3.0f; switch (GraphEditor.connectorType) { case ConnectorType.Bezier: if (inverted) { float center_y = FPMath.CenterOfPoints(p1, p2).y; Vector2 p1_center = new Vector2(p1.x, center_y); Vector2 p2_center = new Vector2(p2.x, center_y); float tangent = Mathf.Min(Vector2.Distance(p1, p1_center) * 0.5f, delta); Vector2 p1_tan = new Vector2(p1.x - tangent, p1.y); Vector2 p1c_tan = new Vector2(p1_tan.x, center_y); Vector2 p2_tan = new Vector2(p2.x + tangent, p2.y); Vector2 p2c_tan = new Vector2(p2_tan.x, center_y); Vector2 l1 = Vector2.zero; Vector2 l2 = Vector2.zero; Vector2 l3 = Vector2.zero; Vector2 l4 = Vector2.zero; Vector2 l5 = Vector2.zero; if (t <= 0.333333f) { l1 = Vector2.Lerp(p1, p1_tan, time); l2 = Vector2.Lerp(p1_tan, p1c_tan, time); l3 = Vector2.Lerp(p1c_tan, p1_center, time); l4 = Vector2.Lerp(l1, l2, time); l5 = Vector2.Lerp(l2, l3, time); } else if (t <= 0.666666f) { return(Vector2.Lerp(p1_center, p2_center, time)); } else { l1 = Vector2.Lerp(p2_center, p2c_tan, time); l2 = Vector2.Lerp(p2c_tan, p2_tan, time); l3 = Vector2.Lerp(p2_tan, p2, time); l4 = Vector2.Lerp(l1, l2, time); l5 = Vector2.Lerp(l2, l3, time); } return(Vector2.Lerp(l4, l5, time)); } else { Vector2 l1 = Vector2.Lerp(p1, t1, t); Vector2 l2 = Vector2.Lerp(t1, t2, t); Vector2 l3 = Vector2.Lerp(t2, p2, t); Vector2 l4 = Vector2.Lerp(l1, l2, t); Vector2 l5 = Vector2.Lerp(l2, l3, t); return(Vector2.Lerp(l4, l5, t)); } case ConnectorType.Line: return(Vector2.Lerp(p1, p2, t)); case ConnectorType.Circuit: if (inverted) { time = (t % 0.2f) * 5.0f; float center_y = FPMath.CenterOfPoints(p1, p2).y; delta = Mathf.Min(Mathf.Abs(p1.x - p2.x) * 0.5f, 40.0f); Vector2 t1_pos = new Vector2(p1.x - delta, p1.y); Vector2 t2_pos = new Vector2(p2.x + delta, p2.y); Vector2 t1_center = new Vector2(t1_pos.x, center_y); Vector2 t2_center = new Vector2(t2_pos.x, center_y); //p1, t1_pos, t1_center, t2_center, t2_pos, p2 if (t <= 0.2f) { return(Vector2.Lerp(p1, t1_pos, time)); } else if (t <= 0.4f) { return(Vector2.Lerp(t1_pos, t1_center, time)); } else if (t <= 0.6f) { return(Vector2.Lerp(t1_center, t2_center, time)); } else if (t <= 0.8f) { return(Vector2.Lerp(t2_center, t2_pos, time)); } else { return(Vector2.Lerp(t2_pos, p2, time)); } } else { if (t <= 0.333333f) { return(Vector2.Lerp(p1, t1, time)); } else if (t <= 0.666666f) { return(Vector2.Lerp(t1, t2, time)); } else { return(Vector2.Lerp(t2, p2, time)); } } default: goto case ConnectorType.Line; } }