您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Unity貝塞爾曲線的實(shí)現(xiàn)方法”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
一階貝塞爾曲線就是一條線,我們很容易根據(jù) t 求出 t 點(diǎn)的位置。
P(t)=P0+(P1-P0)*t =(1-t)*P0+tP1 ; t[ 0,1] ,且其等同于線性插值。
取平面內(nèi)三個(gè)不共線的點(diǎn),AB:AC=CD:CE,這個(gè)時(shí)候BD又是一條直線,可以按照一階的貝塞爾方程來(lái)進(jìn)行線性插值了。
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; }
三階貝塞爾曲線和二階其實(shí)是同一個(gè)道理,都可以按照一階的貝塞爾方程來(lái)進(jìn)行線性插值。這里就直接上公式了。
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貝塞爾曲線的實(shí)現(xiàn)方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。