In this repository, I have implemented a tool for creating and editing splines. The scripts give easy access to points on curve and the ability to get the position, tangent, normal and binormal at the desired point of the curve.
There are 2 ways:
- import
Splines.unitypackage
via Assets-Import Package - clone/download this repository and move the
Assets/Scripts/Curves
folder to your Unity project's Assets folder
The Curve
class implements 3 matrices for different curve types. You can switch between types in real time using curves redactor.
- Bézier
- B-spline
- Cardinal
The redactor allows you to see the curve, normals, tangents and binormals. You can also change the location of control points and reset them.
Redactor is automatically connected to a GameObject that has a Spline
component.
You can see a simple example of how to use it in the Curve Animator file. This script animates object along spline. Curve information can be accesed using P()
method of Spline
class.
spline.P(
t,
out Vector3 vertex,
out Vector3 tangent,
out Vector3 normal,
out Vector3 binormal,
bool inWorld = false
);
where:
t
- position of point on curve [0 -> spline.GetMaxPointInd()]
inWorld
- get point information (vertex, tangent, normal, binormal) in world space. Default is local space.
You can attach Mesh Generator
script to GameObject for procedural plane mesh generation. The mesh will be generated with all necessary information (normals, tangents, uvs) as well as with rounded ends. You can control width of the mesh and generation step. Note that this works better with 2D normals setting turned on.
Mesh Generator
script allows you to animate mesh generation. You can specify animation time and call it with AnimateMeshGeneration()
method of MeshGenerator
class.
You have two options to choose from:
- 3D normals. They are calculated using Frenet normal algorithm (a rather naive approach), so they are not well suited for mesh generation.
- 2D normals. They lock one coordinate, but allow you to rotate normals. These normals are more uniform, so they are better for generating meshes (roads for example).
Thank you for reading this 😊!