之前经常需要在 Unity 里画贝塞尔曲线与 Catmull-Rom 曲线,当时特意封装了一个工具类,以方便使用。

二阶贝塞尔曲线

二阶贝塞尔曲线由起点、终点,再加一个控制点构成。曲线上任意一点的坐标与当前进度的关系如公式:

代码

public class BezierCurveTool
{
    /// <summary>
    /// 获取一条贝塞尔曲线(二阶)by azimiao.com
    /// </summary>
    /// <param name="startPoint">开始点</param>
    /// <param name="endPoint">结束点</param>
    /// <param name="middlePoint">中间控制点</param>
    /// <param name="nultiple">曲线采样次数</param>
    public static void GetWayPoints(Vector3 startPoint, Vector3 endPoint, Vector3 middlePoint, ref List<Vector3> result,int nultiple = 10)
    {
        if (result == null) result = new List<Vector3>();
        result.Clear();

        float dis1 = Vector3.Distance(startPoint, middlePoint);
        float dis2 = Vector3.Distance(middlePoint, endPoint);
        Vector3 directStart2Middle = Vector3.Normalize(middlePoint - startPoint);
        Vector3 directMiddle2End = Vector3.Normalize(endPoint - middlePoint);


        for (int i = 0; i < nultiple; i++)
        {//azimiao.com
            float tempPercent = (float)i / nultiple;

            Vector3 point1 = startPoint + directStart2Middle * dis1 * tempPercent;

            Vector3 point2 = middlePoint + directMiddle2End * dis2 * tempPercent;

            Vector3 linePoint = point1 + Vector3.Normalize(point2 - point1) * dis2 * tempPercent;

            result.Add(linePoint);
        }

        result.Add(endPoint);
    }
}

Catmull-Rom 曲线

Catmull-Rom曲线即经过所有给定点的曲线(不含起始点与结束点),为了让其经过起始点与结束点,可以在列表前后各多加一个点作为起点与终点。

代码

public class CatmulCurveTool
{
    //代码结构来自于:https://blog.csdn.net/qq_33520289/article/details/106234291
    //根据提供的路径获取平滑路径
    public static void GetWayPoints(Vector3[] points, int amountRadio, ref List<Vector3> wayPoints)
    {
        if (points == null || points.Length <= 1) { Debug.Log("points is empty!"); return; }

        if (wayPoints == null) wayPoints = new List<Vector3>();
        wayPoints.Clear();

        Vector3[] vector3s = PathControlPointGenerator(points);

        Vector3 prevPt = Interp(vector3s, 0);
        int SmoothAmount = (points.Length - 1) * amountRadio;
        for (int i = 1; i <= SmoothAmount; i++)
        {
            float pm = (float)i / SmoothAmount;
            Vector3 currPt = Interp(vector3s, pm);
            wayPoints.Add(currPt);
            prevPt = currPt;
        }
    }

    //Gizmos平滑的绘制提供的路径
    public static void DrawPathHelper(Vector3[] path, Color color)
    {
        Vector3[] vector3s = PathControlPointGenerator(path);

        Vector3 prevPt = Interp(vector3s, 0);
        Gizmos.color = color;
        int SmoothAmount = path.Length * 20;
        for (int i = 1; i <= SmoothAmount; i++)
        {
            float pm = (float)i / SmoothAmount;
            Vector3 currPt = Interp(vector3s, pm);
            Gizmos.DrawLine(currPt, prevPt);
            prevPt = currPt;
        }
    }

    //计算路径的长度
    public static float PathLength(Vector3[] path)
    {
        float pathLength = 0;

        Vector3[] vector3s = PathControlPointGenerator(path);

        Vector3 prevPt = Interp(vector3s, 0);
        int SmoothAmount = path.Length * 20;
        for (int i = 1; i <= SmoothAmount; i++)
        {
            float pm = (float)i / SmoothAmount;
            Vector3 currPt = Interp(vector3s, pm);
            pathLength += Vector3.Distance(prevPt, currPt);
            prevPt = currPt;
        }

        return pathLength;
    }

    //生成曲线控制点,path.length>=2(为路径添加首尾点,便于绘制Cutmull-Rom曲线)
    private static Vector3[] PathControlPointGenerator(Vector3[] path)
    {
        Vector3[] suppliedPath;
        Vector3[] vector3s;

        suppliedPath = path;

        int offset = 2;
        vector3s = new Vector3[suppliedPath.Length + offset];
        Array.Copy(suppliedPath, 0, vector3s, 1, suppliedPath.Length);

        //计算第一个控制点和最后一个控制点位置
        vector3s[0] = vector3s[1] + (vector3s[1] - vector3s[2]);
        vector3s[vector3s.Length - 1] = vector3s[vector3s.Length - 2] + (vector3s[vector3s.Length - 2] - vector3s[vector3s.Length - 3]);

        //首位点重合时,形成闭合的Catmull-Rom曲线
        if (vector3s[1] == vector3s[vector3s.Length - 2])
        {
            Vector3[] tmpLoopSpline = new Vector3[vector3s.Length];
            Array.Copy(vector3s, tmpLoopSpline, vector3s.Length);
            tmpLoopSpline[0] = tmpLoopSpline[tmpLoopSpline.Length - 3];
            tmpLoopSpline[tmpLoopSpline.Length - 1] = tmpLoopSpline[2];
            vector3s = new Vector3[tmpLoopSpline.Length];
            Array.Copy(tmpLoopSpline, vector3s, tmpLoopSpline.Length);
        }

        return (vector3s);
    }

    //Catmull-Rom曲线 参考:https://blog.csdn.net/u012154588/article/details/98977717
    private static Vector3 Interp(Vector3[] pts, float t)
    {
        int numSections = pts.Length - 3;
        int currPt = Mathf.Min(Mathf.FloorToInt(t * (float)numSections), numSections - 1);
        float u = t * (float)numSections - (float)currPt;

        Vector3 a = pts[currPt];
        Vector3 b = pts[currPt + 1];
        Vector3 c = pts[currPt + 2];
        Vector3 d = pts[currPt + 3];

        return .5f * (
            (-a + 3f * b - 3f * c + d) * (u * u * u) +
            (2f * a - 5f * b + 4f * c - d) * (u * u) +
            (-a + c) * u +
            2f * b
        );
    }
}

其他

封装的两个工具类用了好久了,在路径动画、插值运动等需求中非常好用。在实际使用时,结合 DoTween 的执行队列等工具,可以创造出非常特别的动画效果。

DoTween 的使用请参考文章:使用DoTween在Unity中制作队列(Sequence)动画

梓喵出没博客(azimiao.com)版权所有,转载请注明链接:https://www.azimiao.com/7722.html
欢迎加入梓喵出没博客交流群:313732000

我来吐槽

*

*