您好,登錄后才能下訂單哦!
本篇內容介紹了“Unity貝塞爾曲線的實現方法”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
一階貝塞爾曲線就是一條線,我們很容易根據 t 求出 t 點的位置。
P(t)=P0+(P1-P0)*t =(1-t)*P0+tP1 ; t[ 0,1] ,且其等同于線性插值。
取平面內三個不共線的點,AB:AC=CD:CE,這個時候BD又是一條直線,可以按照一階的貝塞爾方程來進行線性插值了。
P(B)=(1-t)*P0+tP1 ;
P(D)=(1-t)P1+tP2 ;
P(t)=(1-t)*P(B)+tP(D)
=(1-t)*((1-t)*P0+tP1)+t((1-t)P1+tP2 )
=(1-t)² *P0+2t*(1-t)*P1+t²*P2 ;t[0,1];
代碼:
public LineRenderer line_b; public LineRenderer line_a; public LineRenderer line_c; public Transform start; public Transform end; public Transform c; void Start() { } void Update() { line_a.SetPosition(0, start.position); line_a.SetPosition(1, c.position); line_c.SetPosition(0, end.position); line_c.SetPosition(1, c.position); // float distance = Vector3.Distance(start.position, end.position); Vector3 controlPoint = c.position; //start.position + (start.position+ c.position).normalized * distance / 1.6f; Vector3[] bcList = GetBeizerPathPointList(start.position, controlPoint, end.position, 50); line_b.positionCount = bcList.Length + 1; line_b.SetPosition(0, start.position); for (int i = 0; i < bcList.Length; i++) { Vector3 v = bcList[i]; line_b.SetPosition(i + 1, v); } } public static Vector3[] GetBeizerPathPointList(Vector3 startPoint, Vector3 controlPoint, Vector3 endPoint, int pointNum) { Vector3[] BeizerPathPointList = new Vector3[pointNum]; for (int i = 1; i <= pointNum; i++) { float t = i / (float)pointNum; Vector3 point = GetBeizerPathPoint(t, startPoint, controlPoint, endPoint); BeizerPathPointList[i - 1] = point; } return BeizerPathPointList; } //貝塞爾曲線二次方公式 private static Vector3 GetBeizerPathPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2) { return (1 - t) * (1 - t) * p0 + 2 * t * (1 - t) * p1 + t * t * p2; }
三階貝塞爾曲線和二階其實是同一個道理,都可以按照一階的貝塞爾方程來進行線性插值。這里就直接上公式了。
P(t)=P0*(1-t)³ +3P1*t*(1-t)²+3P2*t²*(1-t)+P3*t³ ; t[0,1];
代碼
public Transform start; public Transform end; public Transform c0; public Transform c1; public LineRenderer line_b; public LineRenderer line_a; public LineRenderer line_c; public LineRenderer line_d; void Start() { } // Update is called once per frame void Update() { line_a.SetPosition(0, start.position); line_a.SetPosition(1, c0.position); line_c.SetPosition(0, c1.position); line_c.SetPosition(1, c0.position); line_d.SetPosition(0, c1.position); line_d.SetPosition(1, end.position); Vector3[] bcList = GetBeizerPathPointList(start.position, c0.position,c1.position, end.position, 50); line_b.positionCount = bcList.Length + 1; line_b.SetPosition(0, start.position); for (int i = 0; i < bcList.Length; i++) { Vector3 v = bcList[i]; line_b.SetPosition(i + 1, v); } } public static Vector3[] GetBeizerPathPointList(Vector3 startPoint, Vector3 controlPoint0, Vector3 controlPoint1, Vector3 endPoint, int pointNum) { Vector3[] BeizerPathPointList = new Vector3[pointNum]; for (int i = 1; i <= pointNum; i++) { float t = i / (float)pointNum; Vector3 point = GetBeizerPathPoint(t, startPoint, controlPoint0, controlPoint1, endPoint); BeizerPathPointList[i - 1] = point; } return BeizerPathPointList; } //貝塞爾曲線三次方公式 private static Vector3 GetBeizerPathPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2,Vector3 p3) { return (1 - t) * (1 - t) * (1 - t) * p0 + 3 * p1 * t * (1 - t) * (1 - t) + 3 * p2 * t * t * (1 - t) + p3 * t * t * t; }
“Unity貝塞爾曲線的實現方法”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。