一.原理
貝塞爾曲線在起始點和終止點鎖定的情況下做均勻移動,
這意味著:
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;
}
}