Unity 贝塞尔曲线 优化画线平滑

贝塞尔曲线 优化画线平滑

代码调用下面这个方法,传入自己所要平滑的曲线的点的List列表,BezierCheck()会计算出每2个点之间需要补充多少个贝塞尔补间的点。

    private static Vector3 pos1;
    private static Vector3 pos2;
    private static Vector3 pos3;
    private static Vector3 new_pos1;
    private static Vector3 new_pos2;
    private static Vector3 new_pos3;
    #region 贝塞尔曲线优化检测
    /// <summary>
    /// 贝塞尔曲线优化检测
    /// </summary>
    public static void BezierCheck(List<Vector3> bezierList)
    {
        if (bezierList.Count > 2)
        {
            pos1 = bezierList[bezierList.Count - 3];
            pos2 = bezierList[bezierList.Count - 2];
            pos3 = bezierList[bezierList.Count - 1];

            float count = (pos3 - pos1).magnitude / 0.25f;

            if (count < 1)
            {
                return;
            }

            float pice = 1f / count;

            new_pos1 = Bezier(pos1, pos2, pos3, 0f);
            new_pos2 = Bezier(pos1, pos2, pos3, pice);
            new_pos3 = Bezier(pos1, pos2, pos3, 2 * pice);

            bezierList[bezierList.Count - 1] = new_pos3;
            bezierList[bezierList.Count - 2] = new_pos2;
            bezierList[bezierList.Count - 3] = new_pos1;

            for (float i = 3f; i * pice < 1; i++)
            {
                bezierList.Add(Bezier(pos1, pos2, pos3, i * pice));
            }

        }
    }
    #endregion

上面那个方法里会调用到下面这个方法,这个方法的意义是计算出补间的贝塞尔点的坐标

    private static Vector3 p0p1;
    private static Vector3 p1p2;
    private static Vector3 result;
    #region 三点求贝塞尔曲线
    /// <summary>
    ///三点求贝塞尔曲线
    /// </summary>
    /// <returns>The bezier.</returns>
    /// <param name="p0">P0.</param>
    /// <param name="p1">P1.</param>
    /// <param name="p2">P2.</param>
    /// <param name="t">T.</param>
    public static Vector3 Bezier(Vector3 p0, Vector3 p1, Vector3 p2, float t)
    {
        p0p1 = (1 - t) * p0 + t * p1;
        p1p2 = (1 - t) * p1 + t * p2;
        result = (1 - t) * p0p1 + t * p1p2;
        return result;
    }
    #endregion

相关文章

实现Unity AssetBundle资源加载管理器 AssetBundle是实现资源...
Unity3D 使用LineRenderer绘制尾迹与虚线 1.添加LineRendere...
Unity 添加新建Lua脚本选项 最近学习Unity的XLua热更新框架的...
挂载脚本时文件名和类名的关联方式 写过Unity脚本的人应该都...
Unity单例基类的实现方式 游戏开发的过程中我们经常会将各种...
这篇文章主要介绍了Unity游戏开发中外观模式是什么意思,具有...