首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >贝塞尔曲线后续

贝塞尔曲线后续

作者头像
bering
发布2020-07-01 10:26:48
9720
发布2020-07-01 10:26:48
举报

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

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

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-06-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档