Easy spline is a tool for drawing multi-purpose 3D spline in unity. This tool is mostly aimed at the creation of spline-based procedural geometry which can be used to create 3D models like tracks, roller coasters, walking paths… They also allow for creating spline paths, which can be used to create colliders, to move enemies/players over more interesting paths, or to create animations.

Features:

  • Easy to learn spline drawing tool (based on photoshop and Cinema 4D), adapted for artists.
  • Spline meshes which can be generated in real-time.
  • Preset shapes to create turns, loopings… in 1 click.
  • Twist prevention at vertical splines.

Goal

I had been using a lot of splines to draw objects in Photoshop and found them very useful and wanted to start using them in other programs as well. But when I searched for spline tools for Unity, they all had the same problems: they twisted at a vertical slope, were made to work but were not aimed at usability and thus were very hard to use and they missed a lot of features found in other programs. That’s why I wanted to created a spline tool where usability was the main target. On top of that I also wanted to fix the vertical twisting issue in my program.

Generating Meshes

To generate a spline mesh, a base mesh is required, this mesh can be any shape or size, but should start at the origin of the z-axis.

  • To prepare the mesh, the mesh is sliced into sections where each point with the same z value is grouped together.
  • Then on the spline, a point is searched with the same distance as the z value.
  • Then the position, up vector and binormal vector are calculated for this point, the x value is multiplied with the binormal and the y value is multiplied with the up vector.
  • The point is then offset by the calculated position.
  • When all points are calculated, they are connected using triangles for a mesh.
  • Then the same steps are repeated with an offset until the whole spline is converted.

Usability

First of al I wanted to make sure that the tool was really easy to use, by studying the spline tools of both photoshop and Cinema 4D I created a tool where it was easy to add, remove and move points and control points. By letting artists test my tool, it was easy to adapt it to their liking

When this part was finished, I also decided to take a look at games like rollercoaster tycoon, in those types of games they also have presets for turns, looping’s and other shapes, which I also added to my spline tool.

Solved Issues

The main issue I encountered was the twisting of the spline mesh at a vertical sections, in these parts, the tangent and the up vector are pointed in the same direction, which makes it impossible to determine the angle of the binormal which is necessary for correct rotation.
First I started by using the up vector of the previous point, but the track could make a vertical turn of 90 degrees, which still gave the same problem.
Then I added intermediate points where I calculated the up vector again. These intermediate points helped to fix the twisting issues and can be seen in the demo video.

The second Issue I had was the stretching of meshes, when a spline is drawn, depending on the size of the control points, the mesh might be stretched more on the first half compared to the second half.
By dividing each Bezier curve of the spline into smaller line segments, I was able to calculate the distances between points almost perfectly (exact enough to be invisible) which was a lot cheaper than calculating the real spline distance. Then I was able to lay out the spline mesh over the spline with correct distances.

The final big issue I had was optimizing the code. When I did this, I had to use a lot of time in the profiler and was able to remove a lot of normalize calls which were the culprit of most of my slow code.
If I would create this tool again now, I would use compute shaders to make it as fast as possible as the generation of splines is very easy to do parallel.

Web demo showing the tool in real-time (might take a little while to load).