Search Unity

Town Maker - procedural village/town/city generator

Discussion in 'Works In Progress - Archive' started by Tom163, Mar 23, 2015.

  1. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    I've started work on an editor script procedurally generate villages, towns and cities. This is for one of my own projects, where I have about 2000 such, making procedural generation the only reasonable approach. But I also plan to put it on the Asset Store when it's done. That's why I will write about the development progress here and invite your feedback to help me make it the most useful for the most interested developers.

    The scope of the project is much simpler than City Engine and such - Town Maker will use existing assets (buildings), for example, and the generation code will be much easier. Instead of rules files and a definition language, it will use configurable parameters.

    One important aspect that I am trying for is that it should be able to generate the same town in different sizes, so if you change the size parameter, the town should grow, but most of the layout should stay the same. This is probably going to be one of the most challenging parts.


    At the moment, I am in a very early stage. These first images are the very beginning of the road system, which will be the foundation of the generator:
    Bildschirmfoto 2015-03-23 um 13.15.10.png Bildschirmfoto 2015-03-23 um 13.16.08.png
    As you can see already, with a few parameter tweaks I can make the road network more or less dense and more or less orderly.

    I'm using a jittered grid algorithm as the basic, because it allows for the most easy parameter tweaking, but also because it makes it very easy to define the grid on top of it. At very low jitter values, this creates american-style cities like in the right screenshot, at higher jitter values the cities are more chaotic, medieval or european style road networks like on the lieft.

    To make everything more interesting, I decided to add a secondary road grid of smaller streets. These fill in the holes in the main streets network, and are tuned with their own set of parameters, so the main grid could be square and orderly, and the small streets chaotic:

    Bildschirmfoto 2015-03-23 um 22.47.07.png

    Here you can see the grid points as well. It's visible here that the algorithm is not yet perfect, but already generating a nice street layout. The base layout of the whole thing is square at the moment, I'll keep it like that for now.

    I create everything in 2D for starters, but the way I design it, it will be very easy to put it on top of a mostly flat terrain. For terrain features such as cliffs, rivers, coastlines, etc. they will simply cull grid points or roads.

    My next step will be to define a density value for every grid point and/or road segment, which I can then later use to define where bigger/higher and smaller/lower houses should be placed.
     
    one_one and Venryx like this.
  2. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
  3. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    Todays progress is the first buildings (well, placeholders):

    Bildschirmfoto 2015-03-24 um 12.31.12.png

    This is the result of refining the road network generator a lot. It will now detect intersections and properly split roads at intersection points. Based on the primary/secondary road distinction and the distance from the center of the town, it will then assign a density value to all grid points, and use that to scale the building placeholders seen above. Later on, it will use this parameter to pick from a list of building prefabs.

    Bildschirmfoto 2015-03-24 um 12.31.37.png
    Right now, building placement is just one building in the center of each road. The next step will be to actually fill the roads with buildings, checking for collisions, of course, and to do all that nicely I will most likely have to iterate through the road network one more time to clean up places where two roads meet at very narrow angles (i.e. there would be no space to place buildings between those two roads).

    I'm also thinking about evolving some grid points to squares, especially where multiple primary roads meet. Additionally, I'm going to add some external grid points to create the roads that lead outside the town.
     
    rame16 likes this.
  4. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    One more progress report for today: Buildings are a go.

    I'm using a fairly simple algorithm here. While high-end procedural generators like CityEngine go through a process called Lot Subdivision, I've decided to take a much more simple approach and simply line the streets with buildings, checking for overlaps and using a configurable spacing parameter to define how far buildings should be away from each other.

    At this stage I'm still using simple cubes. One of the next steps will be to use some building prefabs.


    Bildschirmfoto 2015-03-24 um 18.53.31.png
     
  5. ivyroxy

    ivyroxy

    Joined:
    Mar 23, 2015
    Posts:
    38
    Sweet!

    Are the building next to the main street different to the secondary smaller grids? (ie. Downtown, suburbs)
    Also, how about some clutter in smaller cubes? Later Streetlights, Recycling bins, Newspaper Stands, etc...
     
  6. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    Right now the only difference between buildings is driven by the density parameter, and density is higher in the center and along main streets. One day soon I will add roads as entities to define them in more detail.

    I'm working top-down, so small details will be added last.
     
  7. ivyroxy

    ivyroxy

    Joined:
    Mar 23, 2015
    Posts:
    38
    Cool :)
    I assume that bigger density also means higher buildings?
     
  8. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Very cool! Reminds me a little of a simple version of "ghosttown" (plugin for 3ds max). Keep it up, I'm sure it would do alright on the asset store.

    http://kiladghosttown.blogspot.com/
     
  9. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    I will not come close to that functionality, but my goal is to create something that works in real-time, inside Unity, with existing prefabs for buildings. I'll not go into procedural building generation, though nothing stops you from using something else to do that and build up a building inside the prefab that my tool places on the map.
     
  10. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    Mesh roads, cleanup, the whole script now runs in play mode instead of editor mode (due to the need of collision detection to prevent buildings overlapping, and because I need it in play mode anyway).

    Some additional parameters for even more fine-tuning. And I've made a short video showing what it can do and how fast it runs. Spoiler: Blazing fast. :)

    Bildschirmfoto 2015-03-25 um 14.12.24.png


    Video link:
     
    Brucas, MD_Reptile and Marceta like this.
  11. Teila

    Teila

    Joined:
    Jan 13, 2013
    Posts:
    6,932
    Love the video. So this is done proceduraly in run-time. Will the town then be persistent? Could I go out of play-mode and back in and see the same town or will it generate a new one? I could see this as a nice way to make a variety of layouts for villages on a map, even if you don't need to use it in run-time. Is that possible?

    We could use something like this for a strategy/rpg game we are looking at. Thanks.
     
  12. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    At the moment, it is not persistet. But if you run the script again with the same seed, the identical town will appear.
     
  13. Teila

    Teila

    Joined:
    Jan 13, 2013
    Posts:
    6,932
    Okay, not useful for us then. I hope you will consider making it something that will generate villages and towns in the editor that we can then go in and use for our game scene. That would be very useful. Your generated towns look very realistic and organic. I like that. We have to make a lot of towns and it is tedious so this would be nice. We could then go in and add details but at least the road patterns would be present.
     
  14. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    I started writing it as an editor script, and I still have that script here. I've moved it to runtime now because there is no useable collision detection for editor scripts and I need collision detection for the building placement.

    I'm sure that with a bit of research there's a way to persist changes from runtime to editor state. So keep watching. :)
     
    Teila likes this.
  15. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    I'm working on integrating prefabs so instead of white blocks, there can be actual houses. Since I'm doing all this for a medieval / fantasy projects, my houses are in that style - but nothing prevents you from using modern, ancient or whatever houses you want. Here's some screenshots from the current status - also notice that all houses are nicely aligned to the road, with their entrances pointing that way. :)
    (yes, even the one in the bottom, left middle - the entrance is behind that corner)

    I don't have much variety in houses as I'm only using about 10 different prefabs for this. Of course for a real town there should be much more to choose from.

    Bildschirmfoto 2015-03-25 um 19.39.07.png Bildschirmfoto 2015-03-25 um 19.40.31.png
     
    Venryx and MD_Reptile like this.
  16. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    Actually, I've tested this and it's as easy as Ctrl+C / Ctrl+V -- the Town Maker puts all buildings and roads into two parent game objects (one for buildings, one for roads). Select that in play mode, copy it, exit play mode, paste it, done. :)
     
    Teila likes this.
  17. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    A number of small improvements, a lot more building prefabs to get more diversity, and a function to place all the buildings nicely on underlying terrain or ground. Roads do not yet adapt to terrain, that will be more complicated and I will have to replace the simple boxes I use right now with proper meshes that can follow terrain. That is going to be a small side-project all by itself.


    Bildschirmfoto 2015-03-26 um 13.40.25.png Bildschirmfoto 2015-03-26 um 13.44.11.png
     
  18. msl_manni

    msl_manni

    Joined:
    Jul 5, 2011
    Posts:
    272
    Can we define or load the Main Grid for predictable Town Generations. (Car Race Tracks etc.)
    How big can the Main Grid be.
    Are the Roads connected using prefabs or generated etc.
    And how is the Road going to be textured.


    There are more questions but above are the main.

    And the obvious question is when is it coming and at what price. :)
     
  19. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    Some of those questions I cannot yet answer, because I'm not yet at that point in development.

    I will put additional fine-control into the script very soon to allow stop and start at different points in the generation process. For example, you can have just the road network creation run and then manually modify it before running the building placement part. This you could also use to completely replace the road network. Another idea would be to decouple primary and secondary road generation, so you can load in a network of your primary roads and the Town Maker will generate the secondary roads. For a racing game, that would be very useful.

    At the moment, roads are simple boxes, scaled appropriately. I'm thinking about road generation next, and my plan is to offer a simple mesh generation or terrain painting as alternatives. Since you also have the grid points for the roads, you can also plug in one of the various road generators from the Asset Store.


    As to ETA and cost - I have no idea, completely undecided at this point. As you can see from the dates above, I'm making very fast progress, but there are a few tricky things that are not that quick. I'm working on cleaning up the road network and it's far from easy.
     
  20. msl_manni

    msl_manni

    Joined:
    Jul 5, 2011
    Posts:
    272
    Thanks for details.
    Can you suggest specific road generator from Assets Store that can generate road using Road(From, To).
     
  21. Teila

    Teila

    Joined:
    Jan 13, 2013
    Posts:
    6,932
    Terrain painting and simple mesh generation would be great alternatives. I could see both being useful for different things.

    BTW, thanks on the idea to copy from play mode into the editor. Fantastic! Looking forward to watching your asset develop. :) Love the pictures.
     
  22. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    I really can't, because I don't know most of them.
     
  23. Tiny-Tree

    Tiny-Tree

    Joined:
    Dec 26, 2012
    Posts:
    1,315
    interested on how do you plan to make the props aligned on the terrain slope ? how will you deal if some spot have holes etc? will you modify terrain or find an other spot ?
     
  24. msl_manni

    msl_manni

    Joined:
    Jul 5, 2011
    Posts:
    272

    I have started my own Road System. And its coming together very well, for as I like it to work. Flexible in Editor and Run time too :)

    Have you made any progress with the Grid system. Would like to know what exact problems are you facing with it?
     
  25. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    I don't plan to do that at all. This is so dependend on the prefab that it should be handled by a script attached to the prefab that runs in Start(). Because some things might want to align to terrain (it's easy to do) while some don't want. Many building prefabs are made with walls extending downwards so they can be placed on slopes, for example.

    What I'm doing is placing them on the terrain in a way that ensures as best as possible that no walls are off the ground.
     
  26. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    Road network still contains too many silly angles, where roads intersect at such an angle that no buildings fit inbetween them.
     
  27. msl_manni

    msl_manni

    Joined:
    Jul 5, 2011
    Posts:
    272
    Jitter the Grid using a radius factor for each cell, so that they don't get too close.
    For odd angles compensate the odd angle as you can write a function to find the nasty ones and either remove them or re-adjust the angles.

    Have you found a good solution to super impose Main Grid on Second Grid, or are you doing collision volume at runtime?
     
  28. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    I jitter the grid already, it was one of the first things I do. The factor is configurable, so getting close is only a problem at extreme values. The silly angles still can appear because I calculate a primary and a secondary grid, with mutually exclusive grid points, but in edge cases, some silly angles can appear.

    It's not a biggie, I just need to include a cleanup step.
     
  29. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    Thanks to my massive experience with Unity Terrain (from my Terrain Tools project, see footer), it was easy and fast to get a basic terrain painting set up for the Town Maker. Very first result below, already not too bad. Intentionally no anti-aliasing.

    Problem with this approach is that changes to terrain are always persisted, so upon exiting play mode, this stays behind. Solution in my head: Save the current terrain splat maps before painting, store it somewhere persistent (textures, probably), and add a small editor script to restore it.

    Bildschirmfoto 2015-03-28 um 09.33.41.png
     
    MD_Reptile likes this.
  30. msl_manni

    msl_manni

    Joined:
    Jul 5, 2011
    Posts:
    272
    The angles are too Acute.
    Anyway, you could Build a Polygon Strips over the terrain to make Roads.
     
  31. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    As mentioned above, that is one of the things I'm still working on.
     
  32. msl_manni

    msl_manni

    Joined:
    Jul 5, 2011
    Posts:
    272
    Can't tell without knowing how you are jitter-ing. Ie. cell content or cell - content- positions.
    But if you jitter with a radius small enough, then you wont have acute angles. And also either use a Single Grid for whole scheme, or find a solution to super-impose the main grid on the bigger grid. And also I can tell by looking at your pics that your second grid cell size is too small, which is giving acute angles.

    I am using a radius factor to jitter some cell - content- positions to give randomness and chaos to street layout. Which can be zone specific, ie outer-inner or through tags to specific cells. And it is coming very fine. :)
     
  33. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    Yes, by using a small radius acute angles are avoided, but then the grid is very rectangular and not organic.

    All those parameters are settable by user, which is why I can't guarantee they might be small or large. I'll have to manage it no matter what.
     
    Teila likes this.
  34. Teila

    Teila

    Joined:
    Jan 13, 2013
    Posts:
    6,932
    I personally like the organic feel of the town. In medieval towns roads often formed where people cut across blocks and created paths. This sort of looks like that. It might be nice if the corners were rounded but I imagine you could use terrain paint to easily fix that after the roads were laid out.
     
  35. msl_manni

    msl_manni

    Joined:
    Jul 5, 2011
    Posts:
    272
    Well my tool is for personal use so I am using it differently, so i can tweak it for all kind of needs, modern, towns, villages etc... . But as you are making the tool for others, so you have to be more cautious. You can make a grid tweaker to adjust the pattern, as it will be adjustable by the user easily.
     
  36. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    Rounding corners on roads is actually something I'm working on.

    My goal is something that produces a "good enough" town by default, for games where it's not important that every single detail is perfect. Like when you only fly over them. For FPS-style games, the generator will allow you to stop after road network creation and allow modifications before it finishes. That way you can tweak it before the final steps.

    Right now, I still have a long list of things I want to do with it before I consider it finished. I'm just posting progress reports here and looking for feedback.
     
    SAOTA and Teila like this.
  37. Xtense

    Xtense

    Joined:
    Feb 18, 2015
    Posts:
    34
    Whoa, awesome! This looks exactly like what we need for our flight-sim project to quickly mass-generate cities, great job! Any idea when we might expect a public version? Or maybe there's a way to sign up for some beta-testing, wink wink ;) .
     
  38. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    A good change in my business career means I stopped development on this for the past two weeks. But I'm interested in moving forward with this project very much.

    Unfortunately, at this time it's not ready for an Asset Store release. But if you need it right now, knowing that it's unfinished and not polished, I'm sure we can arrange something.
     
  39. Brucas

    Brucas

    Joined:
    Jan 14, 2014
    Posts:
    17
    Looks awesome. I will be a buyer (as long as it isn't out of my price range) when this is released. Would be cool if you could get the roads to be generated by the easy roads tool- that would be pretttty. I guess it would be pretty easy to have certain "objective" type buildings where it only allows 1 or 2 or some defined amount of that prefab to be placed, right?
     
  40. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    It's easy to modify the script so it triggers whatever other road construction tool you have and feed it the two endpoints. Intersections will be more of a challenge.
     
  41. BluntiePP

    BluntiePP

    Joined:
    Apr 22, 2014
    Posts:
    2
    This thing looks fantastic. Here's hoping you'll continue development on it. :)
     
    Teila and MD_Reptile like this.
  42. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    At the moment, no. My spare time is very limited right now. I would love to finish this, but the project that I started it for is on hold, so...

    It's sad, really. But the indie game market has become unsustainable and if you make something outside the mainstream, it's incredibly difficult to find even enough people looking at your game.
     
  43. BluntiePP

    BluntiePP

    Joined:
    Apr 22, 2014
    Posts:
    2
    Sorry to hear it. But completely understandable. :)