基于Bezier的Spline算法,目前的代码不是很满意,效果不够顺滑,勉强可用,先放在这里,慢慢改进。
代码如下:
static public Vector3[] GetSpline(Vector3[] points, int segment)
{
if (points.Length < 4) return null;
if (segment < 1) segment = 1;
segment *= 2;
//
List<Vector3> listPos = new List<Vector3>();
int nodeCount = segment + 1;
int halfCount = nodeCount / 2;
Vector3[,] ps = new Vector3[nodeCount, points.Length - 3];
float segmentInterval = 1f / segment;
for (int i = 0; i < points.Length - 3; i++)
{
for (int j = 0; j < nodeCount; j++)
{
ps[j, i] = Bezier.GetPoint(j * segmentInterval, points[i], points[i + 1], points[i + 2], points[i + 3]);
}
}
for (int j = 0; j < halfCount; j++)
{
listPos.Add(ps[j, 0]);
}
float intervalHalf = 1f / halfCount;
for (int i = 0; i < points.Length - 4; i++)
{
for (int j = halfCount; j < nodeCount; j++)
{
float t = (j - halfCount) * intervalHalf;
t = Mathf.Sin((t - 0.5f) * Mathf.PI) * 0.5f + 0.5f;
Vector3 p1 = ps[j, i];
Vector3 p2 = ps[j - halfCount, i + 1];
Vector3 pos = p1 * (1 - t) + p2 * t;
listPos.Add(pos);
}
}
for (int j = halfCount; j < nodeCount; j++)
{
listPos.Add(ps[j, points.Length - 4]);
}
return listPos.ToArray();
}
里面使用的Bezier类请参看下面的连接:
该博客探讨了一种基于Bezier的Spline算法,作者对其现有代码不满意,认为生成的曲线不够平滑。代码中使用了三点和四点贝塞尔曲线计算节点,并尝试通过Sin函数调整得到更顺滑的效果。作者计划逐步改进这个算法。

1万+

被折叠的 条评论
为什么被折叠?



