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 Pro builder创建模块化仓库建筑学习教程


下一篇:SIMPLE