一.原理
贝塞尔曲线在起始点和终止点锁定的情况下做均匀移动,
这意味着:
1. 在平面内选3个不同线的点并且依次用线段连接。如下所示
2. 在AB和BC线段上找出点D和点E,使得 AD/AB = BE/BC
3. 连接DE,在DE上寻找点F,F点需要满足:DF/DE = AD/AB = BE/BC
4. 当D点从A到B、E点从B到C、F点从D到E时,F点走过的就是贝塞尔曲线
5. 以上是二阶贝塞尔曲线,更高的也是同样的原理
二. 公式和运用
公式:
代码:
using UnityEditor;
using UnityEngine;
public class BezierCurve : MonoBehaviour
{
public Vector3[] points;
public void Reset()
{
points = new Vector3[] {new Vector3(1f, 0f, 0f),
new Vector3(2f, 0f, 0f),
new Vector3(3f, 0f, 0f),
new Vector3(4f, 0f, 0f)
};
}
public Vector3 GetPoint_TwoPower(float t)
{
//父节点位置会变,所以在世界坐标下计算
return transform.TransformPoint(BezierHelper.GetPoint_TwoPower(points[0], points[1], points[2], t));
}
public Vector3 GetPoint_ThreePower(float t)
{
//父节点位置会变,所以在世界坐标下计算
return transform.TransformPoint(BezierHelper.GetPoint_ThreePower(points[0], points[1], points[2], points[3], t));
}
public Vector3 GetVelocity_TwoPower(float t)
{
//return transform.TransformPoint(Bezier.GetFirstDerivative(points[0], points[1], points[2], t)) - transform.position;
return transform.TransformDirection(BezierHelper.GetVelocity_TwoPower(points[0], points[1], points[2], t));
}
public Vector3 GetVelocity_ThreePower(float t)
{
//return transform.TransformPoint(Bezier.GetFirstDerivative(points[0], points[1], points[2], t)) - transform.position;
return transform.TransformDirection(BezierHelper.GetVelocity_ThreePower(points[0], points[1], points[2], points[3], t));
}
public Vector3 GetDirection(float t)
{
return GetVelocity_ThreePower(t).normalized; //长度变小点
}
/// <summary>
/// 贝塞尔曲线
/// </summary>
public static class BezierHelper
{
/// <summary>
/// 二次方公式
/// </summary>
/// <param name="p0"></param>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <param name="t"></param>
/// <returns></returns>
public static Vector3 GetPoint_TwoPower(Vector3 p0, Vector3 p1, Vector3 p2, float t)
{
return (1 - t) * (1 - t) * p0 + 2 * t * (1 - t) * p1 + t * t * p2; //公式代替Lerp
//return Vector3.Lerp(Vector3.Lerp(p0, p1, t), Vector3.Lerp(p1, p2, t), t); //这个也可以
}
/// <summary>
/// 获取二次贝塞尔曲线的切线。也就是速度、也就是对公式求导。
/// </summary>
/// <param name="p0"></param>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <param name="t"></param>
/// <returns></returns>
public static Vector3 GetVelocity_TwoPower(Vector3 p0, Vector3 p1, Vector3 p2, float t)
{
return
2f * (1f - t) * (p1 - p0) +
2f * t * (p2 - p1);
}
/// <summary>
/// 三次方公式
/// </summary>
/// <param name="p0"></param>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <param name="t"></param>
/// <returns></returns>
public static Vector3 GetPoint_ThreePower(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
{
t = Mathf.Clamp01(t);
float oneMinusT = 1f - t;
return
oneMinusT * oneMinusT * oneMinusT * p0 +
3f * oneMinusT * oneMinusT * t * p1 +
3f * oneMinusT * t * t * p2 +
t * t * t * p3;
}
/// <summary>
/// 获取三次贝塞尔曲线的切线。也就是速度、也就是对公式求导。
/// </summary>
/// <param name="p0"></param>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <param name="t"></param>
/// <returns></returns>
public static Vector3 GetVelocity_ThreePower(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
{
t = Mathf.Clamp01(t);
float oneMinusT = 1f - t;
return
3f * oneMinusT * oneMinusT * (p1 - p0) +
6f * oneMinusT * t * (p2 - p1) +
3f * t * t * (p3 - p2);
}
}
}
[CustomEditor(typeof(BezierCurve))]
public class BerzierCurveEditor : Editor
{
BezierCurve curve;
Transform handleTransform;
Quaternion handleRotation;
private const int lineSteps = 20;
private void OnSceneGUI()
{
curve = target as BezierCurve;
handleTransform = curve.transform;
handleRotation = Tools.pivotRotation == PivotRotation.Local ? handleTransform.rotation : Quaternion.identity; //处理Local和Global的切换时
//处理Scene上的点
Vector3 p0 = ShowPoint(0);
Vector3 p1 = ShowPoint(1);
Vector3 p2 = ShowPoint(2);
Vector3 p3 = ShowPoint(3);
//画第一个点
Vector3 lineStart = curve.GetPoint_TwoPower(0f);
Handles.color = Color.gray;
Handles.DrawLine(lineStart, lineStart + curve.GetDirection(0f));
//lineSteps越大,曲线越平滑
for (int i = 1; i <= lineSteps; i++)
{
//画点
Vector3 lineEnd = curve.GetPoint_ThreePower(i / (float)lineSteps);
Handles.color = Color.red;
Handles.DrawLine(lineStart, lineEnd);
//画切线
Handles.color = Color.gray;
Handles.DrawLine(lineEnd, lineEnd + curve.GetDirection(i / (float)lineSteps));
lineStart = lineEnd;
}
}
private Vector3 ShowPoint(int index)
{
Vector3 point = handleTransform.TransformPoint(curve.points[index]); //世界坐标下的点
EditorGUI.BeginChangeCheck(); //检查在代码块内更改了的任何控件。
point = Handles.PositionHandle(point, handleRotation); //处理点的移动
if (EditorGUI.EndChangeCheck()) //如果有改动
{
Undo.RecordObject(curve, "Move Point");//在撤消菜单中可见
//EditorUtility.SetDirty(curve);
curve.points[index] = handleTransform.InverseTransformPoint(point); //本地坐标的点
}
return point;
}
}