/// <summary> /// 获取样条曲线 /// </summary> /// <param name="points">基准点</param> /// <returns>样条曲线</returns> public static List <Vector3> FetchCurves(int ratio, params Vector3[] points) { if (points == null || points.Length == 0) { return(null); } List <SplineCurves> splines = new List <SplineCurves>(); SplineCurves current, pre; foreach (var point in points) { if (splines.Count == 0) { current = new SplineCurves(ratio); current.AddJoint(null, point); splines.Add(current); } else { current = new SplineCurves(ratio); pre = splines[splines.Count - 1]; current.AddJoint(pre, point); splines.Add(current); } } List <Vector3> curves = new List <Vector3>(); foreach (SplineCurves spline in splines) { if (spline.First) { continue; } foreach (Vector3 point in spline.points) { if (curves.Contains(point)) { continue; } curves.Add(point); } } return(curves); }
/// <summary> /// 添加关节。将新控制点添加到控制点列表中,并更新前面的样条曲线。 /// </summary> /// <param name="spline">上一条样条曲线</param> /// <param name="point">当前点</param> public void AddJoint(SplineCurves spline, Vector3 point) { //前一根样条曲线不为null,所以更新前一根样条曲线的控制点列表,同时更新当前样条曲线的控制点列表。 if (spline != null) { //前一根样条曲线是第1根样条曲线,更新它的Pk+1和Pk+2点 if (spline.First) { origin = spline.origin; start = spline.start; end = point; destination = point; GenerateSamplePoint(); return; } //前一根样条曲线不是第1根样条曲线,仅更新它的Pk+2点 else { spline.destination = point; spline.GenerateSamplePoint(); origin = spline.start; start = spline.end; end = point; destination = point; GenerateSamplePoint(); } } //前一根样条曲线spline为null,说明控制点列表中只有一个点,所以4个控制点样同。 else { origin = point; start = point; end = point; destination = point; First = true; } }