Track Generator

December 22, 2024 · View on GitHub

GitHub release Made with Unity GitHub license

A package that Procedurally Generates Closed Tracks from Voronoi Diagrams using C# Jobs System, Splines and Procedural Mesh Generation.

This package also contains a Voronoi Diagram implementation that can be found here.

Unity Version: 2022.3.44f1

spline_4

Performance

Track Size/CellsPerformance in ms
102.5 - 3
258 - 8.7
5021 - 23

Complexity = 1 Smoothness = 500

Installation

You can install the package via UPM (Unity Package Manager)

Configuration

Below are properties of Track Generator that you can configure to generate different types of tracks.

NameTypeDescription
ResolutionintNumber of triangles that'll be used for mesh generation, the higher the value the smoother the generated mesh.
WidthfloatWidth of the generated mesh.
TilingRange[0f, 1f] floatThe tiling of the generated mesh relative to the distance of the generated Spline. A value of 1 will tile the texture once per unit of distance.
ScalefloatScale of the generated track.
ComplexityRangle[0f, 1f] floatThe lower the complexity the fewer sides/segments the track will have.

There are two types of Track Generators available in the package:

1. Random Track Generator

Generates a random track based on size (area).

NameTypeDescription
SizeintNumber of voronoi cells used to generate a random track. The higher the value the bigger the track.

2. Rect Track Generator

Generates a rectangular track based on size (width, height).

NameTypeDescription
SizeVector2IntDetermines the dimensions of the track, corresponding to width (Size.x) and height (Size.Y).

Setup

  • Attach the RandomTrackGenerator or RectTrackGenerator MonoBehaviour to a GameObject in the Scene. This will automatically add SplineContainer, MeshRenderer and MeshFilter components to the GameObject.

  • Assign a Material of your choice to the MeshRenderer component. You can find a default road material in the Materials folder in the package.

Usage

First declare a serialized implementation of TrackGenerator

    [field: SerializeField] public RandomTrackGenerator TrackGenerator { get; private set; }

or

    [field: SerializeField] public RectTrackGenerator TrackGenerator { get; private set; }

then generate track

    private void Update()
    {
        // Generate a Random Track on Space Key Press
        if (Input.GetKeyDown(KeyCode.Space))
        {
            TrackGenerator.Generate();
        }
    }

The TrackGenerator.Generate() method generates a random track based on the transform of the GameObject.

You can also alternatively use the Generate Button in the Inspector.

spline_0

You can use the Spline Editor tool to edit any generated Track Spline in the Scene View. Once you're done editing the Spline you can press the Generate Mesh button to generate a new mesh based on the edited Spline.

spline_1

spline_2

spline_3

You can get the generated Vertices and Spline via TrackGenerator.Spline and TrackGenerator.Vertices.

How it Works

If you would like to know how it works, I've a dev-log entry on it here

Contributing

If you'd like to contribute to the project, you can fork the repository and create a pull request. You can also create an issue if you find any bugs or have any feature requests.


⚠️Caution⚠️

As you go higher up in track size significantly you'll start to get a specific exception Next segment not unique more and more frequently which happens due to floating point precision. In cases where you need a significantly large track perhaps consider increasing scale instead. More on this here.