unity实战篇 Catmull-Rom曲线工具类

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class PathHelper {
 
    //根据提供的路径获取平滑路径
    public static void GetWayPoints (Vector3[] points, int amountRadio, ref List<Vector3> wayPoints) {
        if (points == null || points.Length <= 1) { Debug.Log ("points is empty!"); return; }
 
        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
        );
    }
 
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

其子昱舟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值