留个档,Unity自定义曲线,Unity画曲线,贝塞尔曲线

网上有很多贝塞尔曲线实现原理,这篇给自己留个档,好用就行。

using UnityEngine;

public class BezierCurve: MonoBehaviour
{
    /// <summary>
    /// 曲线上的点数量;
    /// </summary>
    [SerializeField] int m_nPointCount = 50;
    [SerializeField] Transform[] m_arrTransDocks = null;

    /// <summary>
    /// 计算结果;
    /// </summary>
    private Vector3[] m_arrResultPoints = null;

    public Vector3[] MathPathPoints()
    {
        int nCount = m_arrTransDocks.Length;
        Vector3[] m_arrDockPoints = new Vector3[nCount];
        for (int i = 0; i < nCount; i++)
        {
            m_arrDockPoints[i] = m_arrTransDocks[i].position;
        }

        m_arrResultPoints = new Vector3[m_nPointCount];
        for (int i = 0; i < m_nPointCount; i++)
        {
            Vector3[] temp1 = m_arrDockPoints;
            for (int j = temp1.Length - 1; j > 0; j--)
            {
                Vector3[] temp2 = new Vector3[j];
                for (int k = 0; k < j; k++)
                {
                    temp2[k] = Vector3.Lerp(temp1[k], temp1[k + 1], i / (float)m_nPointCount);
                }
                temp1 = temp2;
            }
            m_arrResultPoints[i] = temp1[0];
        }
        return m_arrResultPoints;
    }

#if UNITY_EDITOR
    void OnDrawGizmos()
    {
        MathPathPoints();
        if (m_arrResultPoints != null && m_arrResultPoints.Length > 0)
        {
            Gizmos.color = Color.yellow;
            for (int i = 0; i < m_arrResultPoints.Length - 1; i++)
            {
                Gizmos.DrawLine(m_arrResultPoints[i], m_arrResultPoints[i + 1]);
            }
        }
    }
#endif
}

程序学无止尽。
欢迎大家沟通,有啥不明确的,或者不对的,也可以和我私聊
我的QQ 334524067 神一般的狄狄

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值