Home

Awesome

July 2023 Oh goodness, this is still around? I'm sorry, I completely forgot about it. ECS is now 1.0 and I am working on a replacement for this code, so you know, don't use this, it's CRAP!


Jan 7rd, 2020. Removed a couple of components that are unnecessary for this example. A testbed is also now available for download


I now have a testbed available: http://blissgig.com/downloads/ER3D_DOTS_Traffic.zip

You will need to download and import the Easy Roads 3D from the Unity Store right after you open this project. You will also need the current alpha version of Easy Roads 3D: https://easyroads3d.com/v3alpha.php It contains the code that has lane data.


This code is designed for a hover car game I am creating. Because I wanted to have autos at multiple heights ("Levels") and wanted the scene to feel filled with autos, not just one or two, hear and there. I am also creating this game for VR, so frame rate is a REAL concern.

Thankfully, Unity has been working on a new technology called DOTS. This document will not talk in detail about DOTS, for a primer I would recommend starting here: https://unity.com/dots

I watched this video for the list of all the DOTS/ECS packages: https://youtu.be/C56bbgtPr_w Make sure that "Hybrid Renderer" is also installed as without it the Auto entities will not be visible.

DOTS allows for many more active objects within a scene, but getting it working can be a bit tricky. You cannot access the Easy Roads 3D API in DOTS. So I create a collection of entities; Roads, Connections and Autos. With these groups I can iterate through the autos in a Job, if the Auto is at the end of it's current path (a series of points along a ERRoad and ERConnection) it then iterates through the Roads to find the next Road and next Connection. This allows the traffic to be as random as possible.

This code is MOVEMENT process only. There is no collision.

Because string values at not blittable, see https://docs.microsoft.com/en-us/dotnet/framework/interop/blittable-and-non-blittable-types for details, ERRoads and ERConnections need to have an idientity added to them. So the first code is; ERRoadConnectionIdentity.cs This is simply a holder for an INT value, which is added to all Roads and Connections at runtime.

The two main code files are: ER3D_Traffic.cs and ER3D_TrafficSystem.cs ER3D_Traffic needs to be added to a game object as this code creates the Auto, Road and Connection entities.

ER3D_Traffic has a few options:

When ER3D_Traffic starts:

  1. Adds "ERRoadConnectionIdentity" to each Road and Connection and updates it's Identity value. See function; "AddIndentityMono"
  2. Converts the Auto game objects to a new list of Entities. This saves a ton of time of converting each Auto during the process of adding the Autos to the scene. See function: "CreateAutoEntities"
  3. Creates the ER Road and Connection entities. Each Road or Connection has a collection of Lanes, each lane has collection of Points. Connections have an Entry Lane and Exit Lane, as well as an Entry Road and Exit Road. So entities are created for all instances of these options. These are used in ER3D_TrafficSystem.
  4. Remove ERRoadConnectionIdentity from roads and connections after the entities have been created.

Data: Within ER3D_Traffic.cs the Road, Connection and Auto entities are created. The data on each object is used to get the next component. eg: An Auto has a series of points that are obtained from a road and a connection.

The Road entity has: 1 - Road Identity 2 - Lane Index 3 - Connection Identity Start 4 - Connection Identity End 5 - Connection Index Start 6 - Connection Index End

The Connection entity has: 1 - Connection Identity 2 - Lane Index Start 3 - Lane Index End 4 - Road Identity Start 5 - Road Identity End 6 - Connection Index Start 7 - Connection Index End

All of this data that seems to be duplicated on each type of entity is to insure that the correct. When an Auto is created it is assigned road and connection points. The values of the Connection entity; Connection Identity, Lane Index End, Road Identity End, Connection Index End. These are used when the last point in the Auto entity has been reached to get the next Road. These are matched, in order, to the Road entity's Connection Identity Start, Lane Index, Road Identity, Connection Index Start. See ER3D_TrafficSystem.cs in the Job's Execute function for the full details.


Process:


Notes:


Additional Info: First off I want to thank Raoul from http://www.unityterraintools.com/ His asset, Easy Roads 3D is very good, however it is his support of this product that impresses me so much. Bravo Raoul!

Second is that Unity's DOTS technology is still in beta, as is the Lane data in ER3D, so this code works as of today; Dec 7th 2019.

Third, this release is to help enlighten others to the power of DOTS, but beware it took a lot of hours to get this information and the code working. So if you find a problem with this code, or with DOTS in general, give yourself some time and some patience. It's a great technology and it's brand new. I absolutely hear your frustration, but this is going to be SUCH a help for VR development.

If you have any comments or improvements, please let me know.