Hermite插值

时间:2017-07-18 22:11:25

标签: c# unity3d spline hermite

我试图使用Hermite样条插入4点之间。但是我的样条似乎总是从第二个点开始,只插入到第3个点。我已经尝试了几个不同的计算,并得到了相同的结果。

有谁能让我对此有所了解?这是我的代码。

public ControlPoint Get(float t)
{
    //speed multiplyer
    //t = t * 10;

    return new ControlPoint(
        new Vector3(Hermite(points[0].pos.x,  points[1].pos.x, points[2].pos.x, points[3].pos.x, t)
        , Hermite(points[0].pos.y, points[1].pos.y, points[2].pos.y, points[3].pos.y, t)
        , Hermite(points[0].pos.z, points[1].pos.z, points[2].pos.z, points[3].pos.z, t)
        ),
        new Quaternion(Hermite(points[0].rot.x, points[1].rot.x, points[2].rot.x, points[3].rot.x, t)
        , Hermite(points[0].rot.y, points[1].rot.y, points[2].rot.y, points[3].rot.y, t)
        , Hermite(points[0].rot.z, points[1].rot.z, points[2].rot.z, points[3].rot.z, t)
        , Hermite(points[0].rot.w, points[1].rot.w, points[2].rot.w, points[3].rot.w, t)
        )
        );
}

float Hermite(
    float y0, float y1,
    float y2, float y3,
    float mu,
    float tension = 0,
    float bias = 0)
{
    float m0, m1, mu2, mu3;
    float a0, a1, a2, a3;

    mu2 = mu * mu;
    mu3 = mu2 * mu;
    m0 = (y1 - y0) * (1 + bias) * (1 - tension) / 2;
    m0 += (y2 - y1) * (1 - bias) * (1 - tension) / 2;
    m1 = (y2 - y1) * (1 + bias) * (1 - tension) / 2;
    m1 += (y3 - y2) * (1 - bias) * (1 - tension) / 2;
    a0 = 2 * mu3 - 3 * mu2 + 1;
    a1 = mu3 - 2 * mu2 + mu;
    a2 = mu3 - mu2;
    a3 = -2 * mu3 + 3 * mu2;

    return (a0 * y1 + a1 * m0 + a2 * m1 + a3 * y2);
}

1 个答案:

答案 0 :(得分:0)

在任何想象中,我都不是Hermite Splines的专家,但从我所看到的是,预期的行为是在第二点和第三点之间进行插值。在我看来,你只是在每个坐标中硬编码到你的Get函数,所以当Hermite Spline是一个函数时,你只得到一个插值是有意义的。将第二个和第三个点视为要在其间插入的两个点,第一个和第四个点只是有助于创建更好的曲线。

由于看起来你只有四个点,要在第一个和第二个点之间插值,以及第三个和第四个点,请尝试重复你的第一个坐标和最后一个坐标。

//Interpolate between 1st and 2nd points' x coord Hermite(points[0].pos.x, points[0].pos.x, points[1].pos.x, points[2].pos.x);

//Interpolate between 3rd and 4th points' x coord Hermite(points[2].pos.x, points[3].pos.x, points[4].pos.x, points[4].pos.x);

要在第一个和第二个点之间进行插值points[0]重复两次,因为没有points[-1]。对于第三点和第四点之间的插值,重复points[4],因为没有points[5]

重申一下,除非您只需要一次插值,否则不要在坐标中进行硬编码。您必须修改Get功能并调用它几次以调整您想要的行为。看看Snea如何在DrawGraph函数中实现Hermite Spline,它帮助我更好地理解Hermite Spline行为:Cubic Hermite Spline behaving strangely

相关问题