Math basics in Unity - Bezier curve

I. Introduction 

A Bezier curve is composed of a set of defined control points P0 to Pn, n=1 is linear, n=2 is quadratic... The first and last control points are called the starting point and the end point, and the middle The control points are generally not located on the curve. 
Obtaining the point between two points is through linear interpolation (Mathef.Lerp), 0 <= t <= 1


2: Bezier curve formula

——Linear formula: Given points P0 and P1, a linear Bezier curve is just a straight line between two points. This line is given by

1


——Second-order Bezier curve: The path of the quadratic Bezier curve is deduced from the function B(t) formula of the given points P0, P1, P2: linearly obtained from (P0, P1), (P1, P2) respectively The results P0' and P1' obtained by the formula are then brought into the linear formula, and the result is the quadratic formula
P0 and P1: P1
1.1.1
and P2: P0
1.2.2
, P1 and P2. The quadratic formula:
1.2.3
simplified result
1.2.4


——Third-order Bezier curve: The four points P0, P1, P2, and P3 define a cubic Bezier curve on the plane or in three-dimensional space. The curve starts from P0 and goes to P1, and from the direction of P2 to P3. You generally won't go through P1 or P2; these two points are just there to provide direction. The distance between P0 and P1 determines "how long" the curve goes in the direction of P2 before turning towards P3.
Its formula is
1.3.1 


Three: Convert formulas to codes

using UnityEngine;

/// <summary>
/// 贝塞尔工具类
/// </summary>
public static class BezierUtils
{
    /// <summary>
    /// 线性贝塞尔曲线
    /// </summary>
    public static Vector3 BezierCurve(Vector3 p0, Vector3 p1, float t)
    {
        Vector3 B = Vector3.zero;
        B = (1 - t) * p0 + t * p1;
        return B;
    }

    /// <summary>
    /// 二阶贝塞尔曲线
    /// </summary>
    public static Vector3 BezierCurve(Vector3 p0, Vector3 p1, Vector3 p2, float t)
    {
        Vector3 B = Vector3.zero;
        float t1 = (1 - t) * (1 - t);
        float t2 = 2 * t * (1 - t);
        float t3 = t * t;
        B = t1 * p0 + t2 * p1 + t3 * p2;
        return B;
    }

    /// <summary>
    /// 三阶贝塞尔曲线
    /// </summary>
    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 = 3 * t * (1 - t) * (1 - t);
        float t3 = 3 * t * t * (1 - t);
        float t4 = t * t * t;
        B = t1 * p0 + t2 * p1 + t3 * p2 + t4 * p3;
        return B;
    }
}

 Four: Draw a curve

  

using System.Collections.Generic;
using UnityEngine;

public class BezierTest : MonoBehaviour
{
    public int m_CurveDensity;//曲线密度
    public bool m_IsSecondOrderBezier;//是否为二阶贝塞尔曲线,否则为三阶贝塞尔曲线

    private List<Transform> m_ControlPointList = new List<Transform>();//所有的控制点(控制点作为挂载此脚本的游戏物体的子物体)

    public void OnDrawGizmos()
    {
        //添加控制点
        m_ControlPointList.Clear();
        foreach (Transform trans in transform)
        {
            m_ControlPointList.Add(trans);
        }

        List<Vector3> pointList = new List<Vector3>();//曲线上的所有点
        if (m_IsSecondOrderBezier)
        {
            if (m_ControlPointList.Count < 3)
            {
                return;
            }
            //获取曲线上的所有点
            for (int i = 0; i < m_ControlPointList.Count - 2; i += 2)
            {
                Vector3 p0 = m_ControlPointList[i].position;
                Vector3 p1 = m_ControlPointList[i + 1].position;
                Vector3 p2 = m_ControlPointList[i + 2].position;
                for (int j = 0; j <= m_CurveDensity; j++)
                {
                    float t = j * 1f / m_CurveDensity;
                    Vector3 point = BezierUtils.BezierCurve(p0, p1, p2, t);
                    pointList.Add(point);
                }
            }
        }
        else
        {
            if (m_ControlPointList.Count < 4)
            {
                return;
            }
            //获取曲线上的所有点
            for (int i = 0; i < m_ControlPointList.Count - 3; i += 3)
            {
                Vector3 p0 = m_ControlPointList[i].position;
                Vector3 p1 = m_ControlPointList[i + 1].position;
                Vector3 p2 = m_ControlPointList[i + 2].position;
                Vector3 p3 = m_ControlPointList[i + 3].position;
                for (int j = 0; j <= m_CurveDensity; j++)
                {
                    float t = j * 1f / m_CurveDensity;
                    Vector3 point = BezierUtils.BezierCurve(p0, p1, p2, p3, t);
                    pointList.Add(point);
                }
            }
        }

        //绘制所有点
        foreach (var point in pointList)
        {
            Gizmos.DrawSphere(point, 0.1f);
        }
        //绘制控制点连线
        Gizmos.color = Color.red;
        for (int i = 0; i < m_ControlPointList.Count - 1; i++)
        {
            Gizmos.DrawLine(m_ControlPointList[i].position, m_ControlPointList[i + 1].position);
        }
    }
}

Guess you like

Origin blog.csdn.net/LLLLL__/article/details/132456142