Move Spline Motion
Today I'm going to talk about my favourite motion. Move Spline Motion! It also is the motion that I have spent the most time building, but the rewards are amazing. The concept behind this is very simple: we have an ISpline interface, that when implemented you can apply it to the motion.
public interface ISpline
{
public Vector3 GetPoint(float t);
}
FlowEnt comes with a few splines pre-implemented, but you can also create your own, as long as you implement this interface. Here are the existing ones: Bezier Curve, BSpline, Catmull-Rom, Cubic Spline, and Linear Spline. Others will come (Lagrange is on my to-do list).
Now, by default, splines don't come normalised, which means that if you have a shorter and a longer segment, the animation will spend the same amount of time on both, when it should proportionally split them. Have no worries though, FlowEnt has a solution for that. Here comes the Normalised Spline! The best thing about this spline is that it can be applied to any AbstractSpline!
These are some examples straight from FlowEnt's demo!
new Tween(18f).For(Character).MoveTo(new BSpline(SplinePoints).Normalise()).Start()
new Tween(Time)
.SetEasing(Easing.EaseInOutSine)
.For(Wrapper)
.MoveTo(new CatmullRomSpline(SplinePoints).Normalise())
.Start();
As for the motion? Couldn't me more simple!
public class MoveSplineMotion : AbstractSplineMotion<Transform>
{
[Serializable]
public class Builder : AbstractSplineBuilder
{
public override ITweenMotion Build()
=> new MoveSplineMotion(item, GetSpline());
}
public MoveSplineMotion(Transform item, ISpline spline) : base(item, spline)
{
}
public override void OnUpdate(float t)
{
item.position = spline.GetPoint(t);
}
}
For those who scrolled all the way to the bottom, I have some treat for you. ISpline has a really neat extension method that allows you to visualise the spline in the editor using gizmos. PS: gizmos are editor only so make sure you wrap this call in a UNITY_EDITOR condition!
Here's the method:
public static void DrawGizmo(this ISpline spline, Color color = default, float width = 1f, float step = 0.001f)
{
Vector3[] points = new Vector3[Mathf.CeilToInt(1f / step) + 2];
float t = 0f;
int i = 0;
if (color == default)
{
color = Color.white;
}
for (; t <= 1f; t += step, i++)
{
points[i] = spline.GetPoint(t);
}
for (; i < points.Length; i++)
{
points[i] = spline.GetPoint(1f);
}
Color initialColour = Handles.color;
Handles.color = color;
Handles.DrawAAPolyLine(width, points);
Handles.color = initialColour;
}
And it's simple usage:
new BSpline(SplinePoints).DrawGizmo(Color.blue, 2f);
This is how it looks
Comments
Post a Comment