点数组平滑插值

//helper array for curved paths, includes control points for waypoint array
Vector3[] points;

//taken and modified from
//http://code.google.com/p/hotween/source/browse/trunk/Holoville/HOTween/Core/Path.cs
//draws the full path
void DrawCurvedLine(int curvedSmoothIndex)
{
    Gizmos.color = curvedWayLineColor;

    Transform[] waypoints = GetWayPoints();

    if (waypoints.Length < 2) return;

    points = new Vector3[waypoints.Length + 2];

    for (int i = 0; i < waypoints.Length; i++)
    {
        points[i + 1] = waypoints[i].position;
    }

    points[0] = points[1];
    points[points.Length - 1] = points[points.Length - 2];

    Gizmos.color = curvedWayLineColor;
    Vector3[] drawPs;
    Vector3 currPt;

    // Store draw points.
    int subdivisions = points.Length * curvedSmoothIndex;
    drawPs = new Vector3[subdivisions + 1];
    for (int i = 0; i <= subdivisions; ++i)
    {
        float pm = i / (float)subdivisions;
        currPt = GetPoint(pm);
        drawPs[i] = currPt;
    }

    // Draw path.
    Vector3 prevPt = drawPs[0];
    for (int i = 1; i < drawPs.Length; ++i)
    {
        currPt = drawPs[i];
        Gizmos.DrawLine(currPt, prevPt);
        prevPt = currPt;
    }
}


//taken from
//http://code.google.com/p/hotween/source/browse/trunk/Holoville/HOTween/Core/Path.cs
// Catmull-Rom spline
// Gets the point on the curve at the given percentage (0 to 1).
// t: The percentage (0 to 1) at which to get the point.
private Vector3 GetPoint(float t)
{
    int numSections = points.Length - 3;
    int tSec = (int)System.Math.Floor(t * numSections);
    int currPt = numSections - 1;
    if (currPt > tSec)
    {
        currPt = tSec;
    }
    float u = t * numSections - currPt;

    Vector3 a = points[currPt];
    Vector3 b = points[currPt + 1];
    Vector3 c = points[currPt + 2];
    Vector3 d = points[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
               );
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

iningwei

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

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

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

打赏作者

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

抵扣说明:

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

余额充值