本篇内容介绍了“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。