贝塞尔曲线后续

2020-07-01 10:26:48 浏览数 (1)

有关贝塞尔曲线的定义以及公式已经写在了上一篇文章中,这篇文章主要介绍这个曲线的应用

通过贝塞尔公式结算得到一个路径数组,结合dotween的DoPath做曲线动画

测试代码如下:

代码语言:javascript复制
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);
        }
    }
}
代码语言:javascript复制
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;
    }

}

0 人点赞