貝塞爾曲線後續

有關貝塞爾曲線的定義以及公式已經寫在了上一篇文章中,這篇文章主要介紹這個曲線的應用

通過貝塞爾公式結算得到一個路徑數組,結合dotween的DoPath做曲線動畫
在這裏插入圖片描述
測試代碼如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Vproject : MonoBehaviour
{
    public Transform start;
    public Transform end;
    public float ef = 1;
    public int vertCount = 3;
    public int pointCount = 10;     //曲線上點的個數
    private Vector3[] linePointList;
    void Start()
    {
        List<Vector3> newP = new List<Vector3>();
    }

    // Update is called once per frame
    void Update()
    {

    }
    public void OnDrawGizmos()
    {
        Vector3 center = (start.position + end.position) / 2;
        Vector3 centerProject = Vector3.Project(center, start.position - end.position);
        transform.position= Vector3.MoveTowards(center, centerProject, ef);
        Debug.DrawLine(center, centerProject,Color.yellow);
        Debug.DrawLine(start.position,end.position,Color.red);
        linePointList = BezierUtils.GetBeizerPointList(start.position, transform.position, end.position, vertCount);
        for (int i = 0; i < linePointList.Length - 1; i++)
        {
            Debug.DrawLine(linePointList[i], linePointList[i + 1], Color.yellow);
        }
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class BezierUtils
{
    /// <summary>
    /// 線性
    /// </summary>
    /// <param name="p0">起點</param>
    /// <param name="p1">終點</param>
    /// <param name="t">【0-1】</param>
    /// <returns></returns>
    public static Vector3 BezierPoint(Vector3 p0, Vector3 p1, float t)
    {
        return (1 - t) * p0 + t * p1;
    }

    /// <summary>
    /// 二階曲線
    /// </summary>
    /// <param name="p0"></param>
    /// <param name="p1"></param>
    /// <param name="p2"></param>
    /// <param name="t"></param>
    /// <returns></returns>
    public static Vector3 BezierPoint(Vector3 p0, Vector3 p1, Vector3 p2, float t)
    {
        Vector3 p0p1 = (1 - t) * p0 + t * p1;
        Vector3 p1p2 = (1 - t) * p1 + t * p2;
        Vector3 result = (1 - t) * p0p1 + t * p1p2;
        return result;
    }

    /// <summary>
    /// 三階曲線
    /// </summary>
    /// <param name="p0"></param>
    /// <param name="p1"></param>
    /// <param name="p2"></param>
    /// <param name="p3"></param>
    /// <param name="t"></param>
    /// <returns></returns>
    public static Vector3 BezierPoint(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
    {
        Vector3 result;
        Vector3 p0p1 = (1 - t) * p0 + t * p1;
        Vector3 p1p2 = (1 - t) * p1 + t * p2;
        Vector3 p2p3 = (1 - t) * p2 + t * p3;
        Vector3 p0p1p2 = (1 - t) * p0p1 + t * p1p2;
        Vector3 p1p2p3 = (1 - t) * p1p2 + t * p2p3;
        result = (1 - t) * p0p1p2 + t * p1p2p3;
        return result;
    }

    /// <summary>
    /// 多階曲線  (可以遞歸 有多組線性組合)
    /// </summary>
    /// <param name="t"></param>
    /// <param name="p"></param>
    /// <returns></returns>
    public static Vector3 BezierPoint(float t, List<Vector3> p)
    {
        if (p.Count < 2)
            return p[0];
        List<Vector3> newP = new List<Vector3>();
        for (int i = 0; i < p.Count - 1; i++)
        {
            Vector3 p0p1 = (1 - t) * p[i] + t * p[i + 1];
            newP.Add(p0p1);
        }
        return BezierPoint(t, newP);
    }

    /// <summary>
    /// 獲取存儲貝塞爾曲線點的數組(二階)
    /// </summary>
    /// <param name="startPoint">起始點</param>
    /// <param name="controlPoint">控制點</param>
    /// <param name="endPoint">目標點</param>
    /// <param name="segmentNum">採樣點的數量</param>
    /// <returns>存儲貝塞爾曲線點的數組</returns>
    public static Vector3[] GetBeizerPointList(Vector3 startPoint, Vector3 controlPoint, Vector3 endPoint, int segmentNum)
    {
        Vector3[] path = new Vector3[segmentNum];
        for (int i = 1; i <= segmentNum; i++)
        {
            float t = i / (float)segmentNum;
            Vector3 pixel = BezierPoint(startPoint, controlPoint, endPoint, t);
            path[i - 1] = pixel;
        }
        return path;
    }

    /// <summary>
    /// 獲取存儲貝塞爾曲線點的數組(多階)
    /// </summary>
    /// <param name="segmentNum">採樣點的數量</param>
    /// <param name="p">控制點集合</param>
    /// <returns></returns>
    public static Vector3[] GetBeizerPointList(int segmentNum, List<Vector3> p)
    {
        Vector3[] path = new Vector3[segmentNum];
        for (int i = 1; i <= segmentNum; i++)
        {
            float t = i / (float)segmentNum;
            Vector3 pixel = BezierPoint(t, p);
            path[i - 1] = pixel;
        }
        return path;
    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章