/// <summary> /// Get a bezier point at a specific position (0-1) between two spline points /// </summary> /// <param name="p1">Spline point 1</param> /// <param name="p2">Spline point 2</param> /// <param name="u">Position on spline</param> /// <returns>Vector2 point</returns> public Vector2 BezierPoint(OTSplinePoint p1, OTSplinePoint p2, float u) { Vector2 anchor1 = p1.position; Vector2 anchor2 = p2.position; Vector2 control1 = anchor1 + p1.ctrl2; Vector2 control2 = anchor2 + p2.ctrl1; return BezierPoint(anchor1, control1, anchor2, control2, u); }
OTSplinePoint SubdivideSpline(OTSplinePoint p1, OTSplinePoint p2) { OTSplinePoint np = new OTSplinePoint(); // np.position = spline.BezierPoint(p1, p2, 0.5f); Vector2 c1 = p1.position + p1.ctrl2; Vector2 c2 = p2.position + p2.ctrl1; Vector2 pc1 = p1.position + (p1.ctrl2/2); Vector2 pc2 = p2.position + (p2.ctrl1/2); Vector2 cc = c1 + ((c2 - c1) / 2); Vector2 pcc1 = pc1 + ((cc - pc1) / 2); Vector2 pcc2 = pc2 + ((cc - pc2) / 2); np.position = pcc1 + ((pcc2 - pcc1) / 2); np.ctrl1 = pcc1 - np.position; np.ctrl2 = pcc2 - np.position; np.pointType = p1.pointType; p1.ctrl2 = pc1 - p1.position; p2.ctrl1 = pc2 - p2.position; return np; }
void SetSplinePointType(OTSplinePoint.OTSplinePointType type) { for (int p = 0; p < selectedPoints.Count; p++) SetSplinePointType(selectedPoints[p], type); }
void SetSplinePointType(OTSplinePoint point, OTSplinePoint.OTSplinePointType type) { if (point.pointType != type) { if (type == OTSplinePoint.OTSplinePointType.Align) { float d1 = point.ctrl1.magnitude; float d2 = point.ctrl2.magnitude; Vector2 v = point.ctrl1 - point.ctrl2; point.ctrl1 = (v.normalized * d1); point.ctrl2 = (v.normalized * d2) * -1; } point.pointType = type; } }
void SetControlPoint(OTSplinePoint point, int ctrl) { float d1 = point.ctrl1.magnitude; float d2 = point.ctrl2.magnitude; Vector2 v; switch (ctrl) { case 1: v = point.ctrl2 * -1; point.ctrl1 = (v.normalized * d1); break; case 2: v = point.ctrl1 * -1; point.ctrl2 = (v.normalized * d2); break; } }
void RemoveSplinePoint(OTSplinePoint point) { splinePoints.Remove(point); selectedPoints.Remove(point); }
void DrawBezier(OTSplinePoint p1, OTSplinePoint p2) { Vector2 lp = Vector2.zero; Handles.color = spline.lineColor; Vector2 sp = spline.transform.position; //loop through 100 steps of the curve for (float u = 0; u <= 1; u += (float)1 / spline.resolution) { Vector2 pos = spline.BezierPoint(p1,p2,u); if (u == 0) Handles.DrawLine(_p(sp + p1.position), _p(sp + pos)); else Handles.DrawLine(_p(sp + lp), _p(sp + pos)); lp = pos; } //Let the curve end on the second anchorPoint Handles.DrawLine(_p(sp + lp), _p(sp + p2.position)); }
/// <summary> /// Clones a spline point /// </summary> /// <returns>Cloned spline point</returns> public OTSplinePoint Clone() { OTSplinePoint p = new OTSplinePoint(); p.pointType = pointType; p.position = position; p.ctrl1 = ctrl1; p.ctrl2 = ctrl2; return p; }
/// <summary> /// Checks if 2 spline points are equal /// </summary> /// <param name="p1">Point 1</param> /// <param name="p2">Point 2</param> /// <returns>True if equal</returns> public static bool Equals(OTSplinePoint p1, OTSplinePoint p2) { return (Vector2.Equals(p1.position,p2.position) && Vector2.Equals(p1.ctrl1,p2.ctrl1) && Vector2.Equals(p1.ctrl2,p2.ctrl2)); }
new void Awake() { base.Awake(); if (!Application.isPlaying && points.Length==0) { transform.localScale = new Vector3(100, 100, 1); OTSplinePoint point1 = new OTSplinePoint(); point1.pointType = OTSplinePoint.OTSplinePointType.Align; point1.position = new Vector2(-50, 0); point1.ctrl1 = new Vector2(0,-50); point1.ctrl2 = new Vector2(0, 50); OTSplinePoint point2 = new OTSplinePoint(); point2.pointType = OTSplinePoint.OTSplinePointType.Align; point2.position = new Vector2(50, 0); point2.ctrl1 = new Vector2(0, 50); point2.ctrl2 = new Vector2(0, -50); points = new OTSplinePoint[] { point1, point2 }; } }