Cg Programming/Unity/Hermite Curves

This tutorial discusses Hermite curves (more exactly: cubic Hermite curves) and Catmull-Rom splines in Unity. The latter are a special kind of cubic Hermite splines. No shader programming is required since all the code is implemented in C#.

Some splines (for example the quadratic Bézier spline discussed in ) don't go through all control points, i.e. they don't interpolate between them. Hermite splines, on the other hand, can be defined such that they go through all control points. This is a useful feature in many applications, e.g. in animation where it is often important to set specific values for particular key frames and let tools smoothly interpolate further values for inbetweens.



Hermite Curves
A single cubic Hermite curve $$H(t)$$ for $$t$$ from 0 to 1 is defined by a start point $$P_0$$ with tangent $$M_0$$ and an end point $$P_1$$ with tangent $$M_1$$:

$$H(t) = (2t^3-3t^2+1) P_0 + (t^3-2t^2+t) M_0 + (-2t^3+3t^2) P_1 +(t^3-t^2) M_1$$

The curve starts (for $$t = 0$$) at $$P_0$$ in direction of $$M_0$$ and then changes course to direction $$M_1$$ and reaches $$P_1$$ for $$t = 1$$. As the figure illustrates, two Hermite curves can be smoothly attached to each other by choosing the same tangent vector for the corresponding end points.

Curve Script
To implement such a curve in Unity, we can use the Unity component. Apart from setting some parameters, one should set the number of sample points on the curve with the function. Then the sample points have to be computed and set with the function. This is can be implemented this way:

Here we use an index  from 0 to   to count the sample points. From this index  a parameter   from 0 to 1 is computed. The next line computes $$H(t)$$, which is then set with the function.

The rest of the code just sets up the  component and defines public variables that can be used to define the control points and some rendering features of the curve.

To use this script create a C# Script in the Project Window and name it Hermite_Curve, double-click it, copy & paste the code above, save it, create a new empty game object (in the main menu: GameObject > Create Empty) and attach the script (drag the script from the Project Window over the empty game object in the Hierarchy Window).

Then create four more empty game objects (or any other game objects) with different(!) positions that will serve as control points. Select the game object with the script and drag the other game objects into the slots Start, StartTangentPoint (for the end point of a tangent starting in the start point), End, and EndTangentPoint in the Inspector. This should render a Hermite curve from the game object specified as “Start” to the game object specified as “End”.



Catmull-Rom Splines
A cubic Hermite spline consists of a continuous, smooth sequence of cubic Hermite curves. In order to guarantee smoothness, the tangent at the end point of one Hermite curve is the same as the tangent of the start point of the next Hermite curve. In some cases, users provide these tangents (one for each control point), in other cases, however, appropriate tangents have to be computed.

One specific way of computing a tangent vector $$m_k$$ for the k-th control point $$p_k$$ is this:

$$m_k = \frac{p_{k+1} - p_{k-1}}{2}$$

and $$m_k = p_{k+1} - p_{k}$$ for the first point and $$m_k = p_{k} - p_{k-1}$$ for the last point. The resulting cubic Hermite spline is called Catmull-Rom spline.

Spline Script
The following script implements this idea. For the j-th segment, it computes $$P_0$$ as the j-th control point $$p_j$$, $$P_1$$ is set to $$p_{j+1}$$, $$M_0$$ is set to $$(p_{j+1} - p_{j-1})/2$$ (unless it is the tangent of the first control point, in which case it is set to $$p_{j+1} - p_{j}$$) and $$M_1$$ is set to $$(p_{j+2} - p_{j})/2$$ (unless it is the tangent of the last control point, then it is set to $$p_{j+1} - p_{j}$$).

Each individual segment is then just computed as a cubic Hermite curve. The only adjustment is that all but the last segment should not reach $$P_1$$. If they did, the first sample position of the next segment would be at the same position which would be visible in the rendering. The complete script is:

The script should be called Hermite_Spline and works in the same way as the script for Hermite curves except that the user can specify an arbitrary number of control points and doesn't have to specify tangent points.

Summary
In this tutorial, we have looked at:
 * the definition of cubic Hermite curves and Catmull-Rom splines
 * implementations of cubic Hermite curves and Catmull-Rom splines with Unity's LineRenderer component.