|
1 |
| -# Voronoi |
| 1 | +# Track Generator |
| 2 | +[](https://github.com/rob1997/TrackGenerator/releases) |
2 | 3 | [](https://unity3d.com)
|
3 |
| -[](https://opensource.org/licenses/MIT) |
| 4 | +[](https://opensource.org/licenses/MIT) |
4 | 5 |
|
5 |
| -A Vornoi Diagram Generator on Unity using C# Job System (DOTS). |
| 6 | +A package that Procedurally Generates Closed Tracks from Voronoi Diagrams using C# Jobs System, Splines and Procedural Mesh Generation. |
6 | 7 |
|
7 | 8 | Unity Version: `2022.3.44f1`
|
8 | 9 |
|
9 |
| -  |
| 10 | + |
10 | 11 |
|
11 | 12 | ## Performance
|
12 | 13 |
|
13 |
| -| Diagram Size | Performance in ms | |
14 |
| -|--------------|-------------------| |
15 |
| -| 5x5 | 0.4 - 0.5 | |
16 |
| -| 25x25 | 98 - 100 | |
17 |
| -| 50x50 | 1725 - 1800 | |
18 |
| - |
19 |
| -## How to use |
20 |
| -#### 1. Install Dependencies |
21 |
| -Install dependency package `Burst` and `Collections` Packages from the Package Manager. |
22 |
| -#### 2. Import `Voronoi.unitypackage` in your project |
23 |
| -Download and import `Voronoi.unitypackage` into your project found [here](https://github.com/rob1997/Voronoi/releases/). |
24 |
| -#### 3. Usage |
25 |
| -- Create a serialized `VoronoiPlane` object in your MonoBehaviour script and Generate it. |
| 14 | +| Track Size/Cells | Performance in ms | |
| 15 | +|------------------|-------------------| |
| 16 | +| 10 | 2.5 - 3 | |
| 17 | +| 25 | 8 - 8.7 | |
| 18 | +| 50 | 21 - 23 | |
| 19 | + |
| 20 | +Complexity = 1 |
| 21 | +Smoothness = 500 |
| 22 | + |
| 23 | +## Installation |
| 24 | +You can install the package via UPM (Unity Package Manager) |
| 25 | +- Open the Unity Package Manager from `Window > Package Manager` and [Import Package from Git URL](https://docs.unity3d.com/Manual/upm-ui-giturl.html). |
| 26 | +- Input the following URL: https://github.com/rob1997/TrackGenerator.git |
| 27 | + |
| 28 | +## Configuration |
| 29 | + |
| 30 | +Below are properties of Track Generator that you can configure to generate different types of tracks. |
| 31 | + |
| 32 | +| **Name** | **Type** | **Description** | |
| 33 | +|------------|------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------| |
| 34 | +| Resolution | `int` | Number of triangles that'll be used for mesh generation, the higher the value the smoother the generated mesh. | |
| 35 | +| Width | `float` | Width of the generated mesh. | |
| 36 | +| Tiling | `Range[0f, 1f] float` | The 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. | |
| 37 | +| Scale | `float` | Scale of the generated track. | |
| 38 | +| Complexity | `Rangle[0f, 1f] float` | The lower the complexity the fewer sides/segments the track will have. | |
| 39 | + |
| 40 | +There are two types of Track Generators available in the package: |
| 41 | + |
| 42 | +### 1. Random Track Generator |
| 43 | + |
| 44 | +Generates a random track based on size (area). |
| 45 | + |
| 46 | +| **Name** | **Type** | **Description** | |
| 47 | +|----------|------------------------|-----------------------------------------------------------------------------------------------------| |
| 48 | +| Size | `int` | Number of voronoi cells used to generate a random track. The higher the value the bigger the track. | |
| 49 | + |
| 50 | +### 2. Rect Track Generator |
| 51 | + |
| 52 | +Generates a rectangular track based on size (width, height). |
| 53 | + |
| 54 | +| **Name** | **Type** | **Description** | |
| 55 | +|----------|--------------|----------------------------------------------------------------------------------------------| |
| 56 | +| Size | `Vector2Int` | Determines the dimensions of the track, corresponding to width (Size.x) and height (Size.Y). | |
| 57 | + |
| 58 | +## Setup |
| 59 | + |
| 60 | +- Attach the `RandomTrackGenerator` or `RectTrackGenerator` `MonoBehaviour` to a `GameObject` in the Scene. This will automatically add `SplineContainer`, `MeshRenderer` and `MeshFilter` components to the `GameObject`. |
| 61 | + |
| 62 | + |
| 63 | +- Assign a Material of your choice to the `MeshRenderer` component. You can find a default road material in the Materials folder in the package. |
| 64 | + |
| 65 | +## Usage |
| 66 | + |
| 67 | +First declare a serialized implementation of `TrackGenerator` |
| 68 | + |
| 69 | +```csharp |
| 70 | + [field: SerializeField] public RandomTrackGenerator TrackGenerator { get; private set; } |
| 71 | +``` |
| 72 | + |
| 73 | +or |
| 74 | + |
26 | 75 | ```csharp
|
27 |
| - [field: SerializeField] public VoronoiPlane VoronoiPlane { get; private set; } |
| 76 | + [field: SerializeField] public RectTrackGenerator TrackGenerator { get; private set; } |
| 77 | +``` |
| 78 | + |
| 79 | +then generate track |
28 | 80 |
|
| 81 | +```csharp |
29 | 82 | private void Update()
|
30 | 83 | {
|
31 |
| - // Generate Voronoi Diagram on Space Key Press |
| 84 | + // Generate a Random Track on Space Key Press |
32 | 85 | if (Input.GetKeyDown(KeyCode.Space))
|
33 | 86 | {
|
34 |
| - VoronoiPlane.Generate(transform); |
| 87 | + TrackGenerator.Generate(); |
35 | 88 | }
|
36 | 89 | }
|
37 | 90 | ```
|
38 |
| -- The `VoronoiPlane.Generate()` method takes a `Transform` object as an argument that determines the position and rotation of the diagram, you can also alternatively use `VoronoiDiagram.Generate(Vector3 origin, Vector3 forward, Vector3 up)` where `origin` will be used to position the diagram while `forward` and `up` are used to rotate the diagram. |
39 |
| -- You can also use the `VoronoiPlane` object to get the generated Voronoi Diagram Cells. |
40 |
| -```csharp |
41 |
| - // Get the Voronoi Diagram data |
42 |
| - Cell[] cells = VoronoiPlane.Cells; |
43 |
| -``` |
44 |
| -- Each cell contains the center point of each cell as `Cell.Center` and an array of segments and vertices for each cell arranged in a **clockwise** order as `Cell.Segments` and `Cell.Vertices`. |
45 |
| -```csharp |
46 |
| - Cell[] cells = VoronoiPlane.Cells; |
47 |
| - |
48 |
| - for (int i = 0; i < cells.Length; i++) |
49 |
| - { |
50 |
| - Cell cell = cells[i]; |
51 |
| - |
52 |
| - // Center point of a Voronoi cell |
53 |
| - Vector3 center = cell.Center; |
54 |
| - |
55 |
| - // Segments of a single Voronoi cell arranged in a clockwise manner |
56 |
| - Segment[] segments = cell.Segments; |
57 |
| - |
58 |
| - // Vertices of a single Voronoi cell arranged in a clockwise manner |
59 |
| - float3[] vertices = cell.Vertices; |
60 |
| - } |
61 |
| -``` |
62 |
| -- Each segment contains the start and end points of the segment as `Segment.Start` and `Segment.End` ordered clockwise. |
63 |
| -- You can also visualize the Voronoi Diagram by either adding a `Drawer` instance in your scene and enabling `VoronoiPlane.drawGizmos` in the inspector or by calling `VoronoiPlane.Draw()` in `OnDrawGizmos`. |
| 91 | +The `TrackGenerator.Generate()` method generates a random track based on the `transform` of the `GameObject`. |
| 92 | + |
| 93 | +You can also alternatively use the Generate Button in the Inspector. |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +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. |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +You can get the generated Vertices and Spline via `TrackGenerator.Spline` and `TrackGenerator.Vertices`. |
| 106 | + |
| 107 | +## Contributing |
| 108 | + |
| 109 | +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. |
64 | 110 |
|
65 | 111 | ---
|
66 | 112 |
|
67 |
| -**⚠️Caution⚠️** |
| 113 | +##### **⚠️Caution⚠️** |
68 | 114 |
|
69 |
| -As you go higher up in diagram size significantly (> 2500 cells | 50x50) 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 diagram perhaps consider more than one adjacent diagrams or consider refactoring the source code to use `double` and `double3` instead of `float` and `float3`. |
| 115 | +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](Runtime/Scripts/Voronoi/README.md#caution). |
70 | 116 |
|
71 | 117 | ---
|
0 commit comments