贝塞尔曲线后续

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

通过贝塞尔公式结算得到一个路径数组,结合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;
    }

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值