I am trying to generate a Catmull-Rom curve from a list of points in Unity. As I do not want to store points in between points of the curve I opted to use a solution that could calculate a position in a Catmull-Rom curve based on time. There are a few examples of this, here and here, however, neither shows how to implement alpha.
The reason I want to implement alpha is so I can have the ability to change between a centripetal, chordal, and uniform Catmull-Rom curve.
private Vector3 GetCatmullRomPosition(float t, Vector3 p0, Vector3 p1,
Vector3 p2, Vector3 p3, float alpha)
{
Vector3 a = 2f * p1;
Vector3 b = p2 - p0;
Vector3 c = 2f * p0 - 5f * p1 + 4f * p2 - p3;
Vector3 d = -p0 + 3f * p1 - 3f * p2 + p3;
return 0.5f * (a + (b * t) + (c * t * t) + (d * t * t * t));
}
To anyone who comes here, the answer actually comes from the maths in one of the links from the original question. It is an answer from an SO question Catmull-rom curve with no cusps and no self-intersections . So credits to cfh.
The code posted in my original question is a great way to calculate points in a uniform Catmull-Rom spline, however, it did not factor in alpha. Therefore, it could not be used for chordal, or centripetal, (or anything in between) Catmull-Rom splines. The code below does take into account alpha and therefore supports both chordal and centripetal Catmull-Rom splines.
Without further ado, here is the code ported into C# for Unity.