using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; //[ExecuteInEditMode] public class PathManager : MonoBehaviour { public GameObject rootPoint; public Transform trans; public LineRenderer lineRenderer; private static PathManager Instance; // Use this for initialization public static PathManager GetInstance() { return Instance; } public BezierPathModel BezierPath; private void Awake() { Instance = this; } void Start() { if (!lineRenderer) { lineRenderer = gameObject.AddComponent<LineRenderer>(); } else { lineRenderer = gameObject.GetComponent<LineRenderer>(); } InitPath(); DrawCurve(BezierPath.PathTrans); //trans.DOPath(BezierPath.PathTrans,10); // trans.DOPath(BezierPath.PathTrans, 15, PathType.Linear, PathMode.Full3D); // rootPoint.DOPath(BezierPath.PathTrans, 15, PathType.Linear, PathMode.Full3D); } public void Update() { } void DrawCurve(List<Vector3> pos) { lineRenderer.positionCount = pos.Count; for (int i = 0; i < pos.Count; i++) { // float t = i / (float)pos.Count; // int nodeIndex = 0; // Debug.Log("i"+i); Pixel = Vector3 POS [I]; // lineRenderer.positionCount = I; lineRenderer.SetPosition (I, Pixel); } } public void InitPath () { BezierPath.PathTrans = the Calculate (BezierPath.ControlTrans, BezierPath.segmentNum); } public List <Vector3> the Calculate (List <the Transform> POSS, int Precision) { // dimension coordinate data (two-dimensional coordinates, three-dimensional coordinates ...) shaft int dimersion = 2 ; // Bezier control points (order ) int Number = poss.Count; //Control points is not less than 2, at least two-dimensional coordinate system IF (Number < 2 || dimersion < 2 ) return null ; List <Vector3> Result = new new List <Vector3> (); // calculate Pascal's triangle int [] = mi The new new int [Number]; mi The [ 0 ] = mi The [ . 1 ] = . 1 ; for ( int I = . 3 ; I <= Number; I ++ ) { int [] T = new new int [I - . 1 ]; for (int j = 0; j < t.Length; j++) { t[j] = mi[j]; } mi[0] = mi[i - 1] = 1; for (int j = 0; j < i - 2; j++) { mi[j + 1] = t[j] + t[j + 1]; } } //计算坐标点 for (int i = 0; i < precision; i++) { float t = (float)i / precision; Vector3 temp_ = new Vector3(0, 0, 0); for (int k = 0; k < number; k++) { temp_.x += Mathf.Pow(1 - t, number - k - 1) * poss[k].position.x * Mathf.Pow(t, k) * mi[k]; temp_.y += Mathf.Pow(1 - t, number - k - 1) * poss[k].position.y * Mathf.Pow(t, k) * mi[k]; temp_.z += Mathf.Pow(1 - t, number - k - 1) * poss[k].position.z * Mathf.Pow(t, k) * mi[k]; } result.Add(temp_); } return result; } public List<Vector3> ReturnPath(int id) { return BezierPath.PathTrans; } } [System.Serializable] public class BezierPathModel { public List<Transform> ControlTrans; public int segmentNum; public List<Vector3> PathTrans; }