对数学公式一直不感冒,今天看了个图发现这玩意原来挺简单的
https://blog.csdn.net/xiexian1204/article/details/49592765
暴力法求三阶贝塞尔曲线:
public static Vector3 BezierCurve(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
{
//向量,三线
Vector3 P01 = p1 - p0; //向量0-1
Vector3 P12 = p2 - p1; //向量1-2
Vector3 P23 = p3 - p2; //向量2-3
//根据t拆分向量,加上原始点坐标,为终点【坐标】
Vector3 P01T = P01 * t + p0;
Vector3 P12T = P12 * t + p1;
Vector3 P23T = P23 * t + p2;
//向量,三线变两线
Vector3 P012 = P12T - P01T;
Vector3 P123 = P23T - P12T;
//根据t拆分向量,加上原始点坐标,为终点【坐标】
Vector3 P012T = P012 * t + P01T;
Vector3 P123T = P123 * t + P12T;
//向量,两线变一线
Vector3 P0123 = P123T - P012T;
//根据t拆分向量,加上原始点坐标,为终点【坐标】
Vector3 P0123T = P0123 * t + P012T;
return P0123T;
}
下面有温柔的数学公式:
https://blog.csdn.net/begonia__z/article/details/51199454
public static Vector3 BezierCurve(Vector3 P0, Vector3 P1, Vector3 P2, Vector3 P3, float t)
{
Vector3 B = Vector3.zero;
float t1 = (1 - t) * (1 - t) * (1 - t);
float t2 = (1 - t) * (1 - t) * t;
float t3 = t * t * (1 - t);
float t4 = t * t * t;
B = P0 * t1 + 3 * t2 * P1 + 3 * t3 * P2 + P3 * t4;
//B.y = P0.y*t1 + 3*t2*P1.y + 3*t3*P2.y + P3.y*t4;
//B.z = P0.z*t1 + 3*t2*P1.z + 3*t3*P2.z + P3.z*t4;
return B;
}
其实上面那个化简一下应该和下面的一样,不过下面的效率高还是用下面的吧。
二阶贝塞尔曲线
// p:当前点 t:当前进度(0-1) p0:起点 p1:中间点 p2:终点
p = (1-t)^2 * p0 + 2(1-t)tp1 + t^2p2
/// <summary>
/// 二阶贝塞尔
/// </summary>
/// <param name="t">0-1</param>
/// <param name="p0">起点</param>
/// <param name="p1">中间点</param>
/// <param name="p2">终点</param>
/// <returns></returns>
public Vector3 GetQuadraticCurvePoint(float t, Vector3 p0, Vector3 p1, Vector3 p2)
{
float u = 1 - t; //剩余进度
float tt = t * t; //进度*进度
float uu = u * u; //剩余进度*剩余进度
return (uu * p0) + (2 * u * t * p1) + (tt * p2);
}