Procedural Track Generation in Unity3D

This post is basically how to generate a procedural/dynamic road or a  track at run time in Unity3D.


Prerequisite: Check out this link

Final Result:

This mesh is actually an extrusion of the following 2D surface -

In 3D, this is how it looks -

Explanation of terms -
Block - 1,2,..n - The entire track is built of a sequence of building blocks. This is the repeating entity in the track

Points - P1, P2.. Pn - Points that define the track (input for this generator)

Sequence - S1, S2,.. Sn - The track can be thought of these 8 sequences of vertices (4 are shown above). Each sequence originates from the corners of the flat 2D surface above

Lets call this Track-Generator, hmmm.. let's see.. GUS ( I'm sure AlterVik would have given it a better name... eviiiiiiiil).

Gus needs an Array of Vector3, which is the list of Points described above. GUS generates the track along these points. This means that when GUS gets hungry, you create a spline (line) in your fav 3D editor and export the points and feed it to GUS.

How GUS works - 
First of all GUS assumes this is not a loop (To loop, GUS expects you to mutate it and create GEORGE, GEORGE LOOGUS). Secondly, GUS expects at least 2 points. Anyway...

1. Gus calculates the number of blocks required as ( # of Points -1 ) and calls it "n" [ n = #Points -1 ]

2. Gus knows there are 8 Sequences of vertices in track as calls it "S"[ S = 8]

3. Gus then identifies number of vertices required as 18 + (n-1) x 8
[ 16 for 1st block, +8 for each after ]

Now GUS loops S=8 times (for each Sequence required) -
   For this Sequence
       GUS finds out offset [width (w) and height (h)] of the sequence from the center
        For each Block
           GUS finds out in which direction the track is moving [ P(n+1) - P(n)]
           GUS finds out Left Direction from this [ See Note at the end ]
           GUS adds point [ P + Left * w + Up * h]

Now it's time for GUS to identify the Triangles...
GUS knows there are n x 16 Tris, hence n x 16  x 3 points to define all the Tris.

Like before, GUS loops S=8 times (for each Sequence required) -
  For this Sequence
     For each Block
        Green Tri's points - (i)th and (i+1)th vertex from Sequence S, (i)th from S + 1
        Red Tri's points - (i+1)th  vertex from Sequence S, (i)th and (i+1)th from S + 1

That's it. I haven't explained the complete workflow as given in 1st fig, you can refer the code below and this is in no way optimized and bug free... so basically WTF License.

1. While calculating the direction in which the Track is being generated, GUS assumes that the Road is not banked. Reason being, GUS only has a line as direction, there can be infinite "Up" vectors. Hence, GUS assumes "Up" to be global "Up" axis. Based on this, GUS does Cross-product of "Up" and "Forward" to get "Left".

To have banking, points given to GUS should also have associated Left or Up direction data. In short, GUS should be mutated into BOGUS - to calculate direction based on BOth these data.

2. GUS no like UV mapping for now.

Save this link.
Add the script to a game object and set the width and height parameters.

If it helped you in some way, leave a comment so that I know someone out there is actually reading all this x_X


  1. Interesting read. I don't use Unity atm but enjoy the pseudocode and concepts. What are you planning to use this for? A virtual roller-coaster, perhaps?

  2. @JW Glad you enjoyed :)

    I was actually planning to use this for a racing game. But the problem with this is -
    1. Tough to have forks in the road
    2. Won't be avail in the game editor

  3. This comment has been removed by the author.

  4. We were having a look at some tools, and found one called Road/Path tool, which also allows generating tracks - though far from perfect.

    An overview of pros and cons of the tool compared with EasyRoads3D (one where you can create nice roads, but not procedural) can be found in our blog post:

    I also came across a tool called Road Network generator, but didn't play around with it - it seems a bit limited though for now:

    In the past, we also had a look at Urban Pad, a City generator, which is now discontinued. However, the founder of the company is looking into reviving it. The tool generated a mesh outside of Unity, including buildings and streets. We made a simple demo with programmer art to try it out:


Note: Only a member of this blog may post a comment.