Generating road meshes from a spline

I’m building a game where you can edit a network of roads at runtime - basically a city builder. I watched Quill18’s tutorial on creating editable roads which goes into the basics, but I’m stuck taking it further. I’ve used the Interpolate script on the wiki to create an editable Catmull-Rom spline, but I’m unsure of how to turn this into a mesh.

From the interpolate script I’ve got a list of points along the curve. This will end up being at the center of the road, so I can offset these by the road width to get the points at the edge. From this I just need to create the triangles for the mesh, which the Triangulator script appears to do (at least in 2D).

The problem is I’m not sure how to get the points at the edges. The Interpolate script just returns an array of Vector3s - there is no rotation/direction associated with them. I could use math to work out the points perpendicular to this line, however that still isn’t quite what I’m after. I want a road to banked around a curve, so shouldn’t the result from interpolating the spline return the rotation too?

(I’m interested in learning the math behind this, however if there are any plugins that do what I’m after, dynamically building a road network at runtime, I’d be interested in hearing about that too)

yo i had this issue as well.
First off have three splines: left ,middle and right of the track. Input verticies per length of spline update into your vertex array(s).
On start set-up three triangles manually. After which a pattern occurs like so :

The last triangle data of your manual on start triangles will be :
private int TriInd1 = 6;
private int TriInd2 = 9;
private int TriInd3 = 10;

Per ++Length:
AddTriangle(TriInd1+=3, TriInd2+=3, TriInd3+=3);
AddTriangle(TriInd1, TriInd3, TriInd1+1);
AddTriangle(TriInd1+1, TriInd3, TriInd3+1);
AddTriangle(TriInd1+1, TriInd3+1, TriInd1+2);

You can prove this with Paint. exe and many days of headaches. or take my word for it and plug it in :smiley:

P.s Proof: Clip2Net — screen capture tool for Windows, Android, iPad, Mac, Linux

I haven’t got time for a highly detailed answer, but there is an outline. Walk the spline in steps. For each time through the loop you want a current position and the next position. Next you need to know what is consider 'u’p for your road. If it is perfectly flat, it will be Vector3.up, but if you are winding through a terrain, you will need to raycast. With this info, you can generate your two points:

var dir = (Vector3.Cross(up, nextPos - currPos)).normalized;
var point1 = currPos + dir * halfRoadWidth;
var point2 = currPos - dir * halfRoadWidth;

Note if you are winding through a terrain, you may need to adjust the points ‘y’ height to match the terrain.

Note for the last point (which won’t have a next point) on your spline, you can use the ‘dir’ from the previous point, or you can reverse the process and use:

 var dir = (Vector3.Cross(up, currPos - prevPos)).normalized;