Search Unity

Procedurally Generated Road Networks and Intersections

Discussion in 'Scripting' started by frosted, Mar 24, 2014.

  1. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    So I've put in a few weeks of work into a procedurally generated world map. It was way, way harder than I expected and I ended up spending well over two weeks with little usable output (a major setback with my timeline). The environment that I'm trying to make is something like mount blade, or a sid meier's pirates (only on land), so an environment with maybe 50 or so cities, towns and villages.

    Part of the problem is that I'm trying to generate a realistic 'feeling' road network between these locations and a single large road mesh.

    Generating a single road segment mesh against a spline is pretty simple, but road intersections and branches sort of have me stumped.

    The easiest method (given the code that I've already worked out) would probably be running a constrained delaunay triangulation against the outline of the road, with branches represented by 5 points (one constraining the forked area, and 2 bordering the 'pre fork' road, one on either side of the fork constraint). So even using this method (the 'easiest') will actually be quite difficult.

    Another method, perhaps, would be simply generating each road as a discrete segment against a spline then somehow merging them at the end. In this case I can simply use https://docs.unity3d.com/Documentation/ScriptReference/Mesh.CombineMeshes.html - the problem being that the textures will... intersect at intersections. So if my road has some kind of outline, it'll look like a mess when roads connect...

    Am I missing something? Is just generating a simple, rule bound triangulation for intersections achievable by someone with a limited math background?
     
  2. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    Is this stuff really that specialized and difficult?

    I bit the bullet and purchased superspline since it had special "spline junction" support... sadly, it just uses a shared point for multiple spline meshes, so texturing roads at intersections is just as much of a problem. EasyRoad3d seems to have actual support for junctions, but I'm concerned that it has limited api support (and is really intended to be used visually).

    Is this stuff actual rocket science?
     
  3. Dan-MacDonald

    Dan-MacDonald

    Joined:
    Oct 25, 2013
    Posts:
    17
    It's surprisingly hard, but also fun. :) $CityCenters.png Here's where I'm at with mine.
     
    Last edited: Mar 26, 2014
    frosted likes this.
  4. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    I got a bunch of other stuff for the world generation logic... but here is where I currently am with the mesh generation for spline intersections.

    I've got some number of splines that share a common key vector (in the attachment, all 4 splines share a common start/end). Then I generate a set of vertices for the intersection (currently a separate mesh) then I clip out the road vertices that fall within the intersection.

    the uv mappings are super simple at the moment, but you can see that at least the texture for the individual road segments and the intersection blend properly - and you don't have weird edges...

    There's still some work left with the uv map, and there are some bugs with the vertex removal logic, but it's coming along I guess.
     

    Attached Files:

    Last edited: Mar 26, 2014
    Martin_H likes this.
  5. Dan-MacDonald

    Dan-MacDonald

    Joined:
    Oct 25, 2013
    Posts:
    17
    Looks really good! Those nearly parallel intersections are a pain. I haven't got to mesh generation yet but my roads are stored much like yours are, except I use a list of segments between endpoints instead of splines.
     
  6. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    So I'm almost done with this. The UV map is a bit smarter.

    I added y coords based on terrain height.

    I copied a shader from easyroads because otherwise the mesh was bleeding into the terrain too much.

    The result is pretty close to what I was looking for. But so far this has taken almost 4 days, way longer than I could afford ;P


    $intersection.png
     
    Last edited: Mar 28, 2014
    Martin_H likes this.
  7. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    and finally, after a long, difficult labor ... the uv's are perfect. a dream realized.
    ...a dream... that took way too f'n long.

    $intersection.gif

    man, it sure would be nice if someone said, "hey wow thats awesome!"
     
    AlanMW and Martin_H like this.
  8. Fellshadow

    Fellshadow

    Joined:
    Dec 2, 2012
    Posts:
    169
    That's pretty cool, awesome work!








    ....What are you gonna do for streetlights?
     
  9. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    Streetlights? Screw off!! :D

    Anyway, so I spent a bit longer on this and I wrote my own shader (from scratch!). It was my very first shader!

    Here is the AWESOME result!

    $intersection.gif
     
    Last edited: Mar 30, 2014
    Martin_H likes this.
  10. Skyfly

    Skyfly

    Joined:
    Jan 25, 2014
    Posts:
    110
    Everything is awesome!
     
  11. Dan-MacDonald

    Dan-MacDonald

    Joined:
    Oct 25, 2013
    Posts:
    17
    Looks great! I'm still picking away at city street layout generation.
     
  12. tomatosource

    tomatosource

    Joined:
    Dec 12, 2014
    Posts:
    12
    Don't suppose there's any chance you could put a demo project up? Currently struggling with a very similar problem!
     
  13. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yeah, well done! Looks awesome!
     
  14. AlanMW

    AlanMW

    Joined:
    Feb 11, 2017
    Posts:
    2
    Been 4 years now, but oh well. Like someone else requested, sharing a demo project would be amazing!
     
  15. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    sorry man, that code is long gone.

    It's funny seeing this post from four years ago. Roads, roads, my life for good roads.