本文主要是介绍Unity中的曲线插值CatmullRom,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
链接:Unity中的曲线插值CatmullRom
ps:博客从typecho换成jekyll后,文章复制到简书来因为图片链接原因变得麻烦了。- -!
正文
之前写了个插件,有个需要曲线插值的功能。给定一些点的位置,物体成一条平滑曲线依次通过这些点。
Bezier曲线是在Unity里比较常用的,但是不适合这里的需求。因为Bezier无法通过所有的点,它需要有另外的点来构造切线。如下图:
图1

查了资料发现CatmullRom曲线可以平滑的通过所有点,满足需求,如下图:
图2

可以看这里的介绍。
在Unity中C#脚本的实现:
/// <summary>
/// Catmull-Rom 曲线插值
/// </summary>
/// <param name="p0"></param>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <param name="p3"></param>
/// <param name="t">0-1</param>
/// <returns></returns>
public static Vector3 CatmullRomPoint(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
{return p1 + (0.5f * (p2 - p0) * t) + 0.5f * (2f * p0 - 5f * p1 + 4f * p2 - p3) * t * t +0.5f * (-p0 + 3f * p1 - 3f * p2 + p3) * t * t * t;
}
需要注意的是,接受参数p0-p3四个点后,所得的曲线为p1和p2之间的。所以要得到多个点构造的曲线,首位部分需要特殊处理下,比如:CatmullRomPoint(p0,p0,p1,p2,t);
这样可以得到p0和p1之间的曲线。
在Unity中绘制出来:

这篇关于Unity中的曲线插值CatmullRom的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!