Building a procedural city generator in Unity
Updated 8 months ago
1.7 K
Procedural city generation is one of my main interests, and this article is a record of how I developed my own version from late 2016 till now.
rsion was an adaptation of Unity's Roll-a-ball tutorial. I started learning Unity on 2016, so with my limited understanding, I simply tried creating single Gameobject blocks with window textures, and tested the ability to randomly place the Gameobjects.
Was trying various projects with Unity to understand the engine back in 2016, so the procedural generation project was put on hold until late 2017. The version on December 4th, 2017 shown below was composed of stacked generated cubes of various materials, with a random placed object from a set of Gameobjects placed on top.
This was obviously non-performant, but my goal back then was to identify the rules I needed to establish to make a generated city believable. This version was to test if I could generate various window patterns on the wall surfaces and have the code randomly place objects as needed. There were observable issues - e.g. middle bottom building has windows that occur in one corner and not the other - but seeing issues like that meant I can figure out new rules to fix them.
The next version on January 8th 2018 refined the arrangement of blocks to make the layout more realistic (e.g. adding believable corners so that the windows seem feasible), and added a ground section to the buildings. The ground section allowed me to define the area the building would occupy, and randomize the scale of the upper section of the buildings. The windows are now thin slices instead of blocks, thus they can be arranged within a defined wall region.
On January 12th, 2018 I added the ability to generate finer detail on the rooftop and grid-like facades on the building walls. The added edges on top of the building created a more believable look, and I was able to adapt the code to create randomized grid patterns on the walls.
The code still isn't perfect; top building on the left has a definite overhang. Part of the work was figuring out where the rules failed in generating believable content and correcting as I go along.
The next version on January 19th, 2018 added rules to window generation, and applied the edge algorithm around the windows. With materials assigned to the generated Gameobjects, the generated buildings became more attractive to the eye.
Around April, I was trying out one of Brackey's tutorials where he demonstrated a Raycasting code to direct AI units. I modified the code to be able to create a selected area by mouse, and to generate a building out of that area.
On June, I experimented with generating different types of buildings. The new building type is a South East Asian shophouse that is commonly seen in Malaysia and Singapore; the row of shops chained in a single long building that has 2-5 floors on top with a shaded 5-footway or "kaki lima" on the ground floor front.
Since I was able to generate a building on any physics-enhanced surface in Unity, I can add the code into any scene, click-and-drag on a physics enabled surface and generate a building on it. Thus, when I got Gaia around the end of June, I was able to generate procedural buildings within a generated landscape scene.
Added a Post-Processing Stack effect for fun.
So far, the buildings generated were accurately scaled. On August 2018 I started experimenting with AR, which I discovered that a real-size skyscraper in AR isn't that easy to view on a phone! So I added a Minimize mode, where the buildings could be scaled down to be viewable on an AR-detected floor.
The implementation is admittedly a hack, and since some parts of the code couldn't figure out what to do with the new scale, issues with z-fighting and object placement appeared. But on the whole, it's a version I can show to people on my phone, so I just need to clean up my code.
Took a break from this project between September 2018 and July 2019; had switched jobs and had to spend time adapting to being a Producer again. However, in mid-August of 2019, Unity released a set of prototyping assets called SNAPS. Was surprised to discover that the height measurement for SNAPS was precisely the one I used, which implied adapting the SNAPS prototyping assets for use in my Procedural City Generator would be doable.
Within a month, I was able to generate the same buildings but with more intricate details thanks to the SNAPS parts.
On the last week of May 2020, I tested the functionality to build the parts gradually with IEnumerators. This helped with the user experience and made the process more pleasing to the eye. Also build generators that would arrange furniture within a given space.
Built desks that procedurally add props around themselves when they’re placed within a scene, enabling a variety of content if a bunch of them are placed together
Built desks that procedurally add props around themselves when they’re placed within a scene, enabling a variety of content if a bunch of them are placed together.
This is the progress on the procedural city generator thus far. I will update this article once I have added more to the project. Thank you for reading!
Hilmy Abdul Rahim
Experiments with Unity - Producer