Flexible bezier curve generator can adapt to many situations

Currently javascript, this is a generalized bezier curve point generator.

As a demo, I’ve replaced the use of Curve3.CreateCubicBezierCurve() with Bezier.CreateCubicBezierCurve(). But there is much more behind the scenes. This Bezier class can generate points on a 2d curve, 3d curve and can handle Bezier curves of order 2, 3, 4, and higher. It can emit each “point” as a number array, Vector2, Vector3, Vector3 with XZ only, Vector4, or literally any object for which a conversion function is supplied.

Input is treated similarly: use the default array of number arrays, or supply a conversion function that converts each element of an input object array into a number array.

After performance and functional testing, it should (might?) be able to replace other uses of the variety of bezier curve functions throughout Babylon.

The core algorithm is split into two parts:

  1. a constructor that accepts the bezier order (how many points define a single bezier curve segment, where 3 defines quadratic and 4 defines cubic, but not limited to only those values) and functions for emit and consume.
  2. a JavaScript generator that emits the specified number of points (tesselation value) along the calculated bezier curve, and an option to skip the first point (for use when chaining several bezier segments together).

The generator can be used to generate an array of points with […genPoints()] but can also be accessed with generator functions that can minimize the need to create and sling around temporary arrays of points.

If there is interest, I can work on formalizing the “template” to produce catmull-rom, hermite, B-splines, Kochanek–Bartels spline, and potentially other splines as well.

Centralizing the spline calculation means it can be used in other areas of Babylon code without requiring the customization/rewriting of the code for each use. Need 3d bezier curve defined in array of arrays for CreateLineSystem? Need 2d beziermcurve output in Vector3 XZ plane for extruding? Need to just replace Curve3.CreateCubicBezierCurve () or Curve3.CreateQuadraticBezierCurve()? Bezier can do it.

Thoughts?

5 Likes

Generalized spline uses a basis matrix with several built in. The playground shows a Catmull-Rom spline with varying tension parameter (0->1 in 0.25 steps). It’s still a little clunky, but shows the basic intent.

Also implements Cubic Bezier and (not yet working) Cubic B-Spline. I’ve incorporated my general Matrix class so theoretically it will work in 1d, 2d, or 3d as well as higher order splines. Specifying emit and consume functions allow it to take in and spit out whatever objects are preferred, with Vector3 object given as an example.

A default function for generating the “t” matrix (nominally [1, t, t * t, t * t * t]) can be changed for parameterizing the spline.