Archives for : Coders Corner

CC: Building Generation

aptsWe have a game map of considerable size which means we are going to need a lot of buildings. . This would be stupid amounts of work to do by hand and just copying the same building over and over wont cut it either so we decided to generate the majority of the buildings.

Buildings are made using a whole bunch of premade panels. This makes it easy to just place a ton of them through code to build a structure. It also allows us to change the width and length of the building quite easily. After generating our structure we simply place a door in the middle and we’ve got a building. So that was easy, huh? Well, that’s what I thought too, but then the game designer suddenly wants to the building to be enterable and feature apartments. This is where trouble started to show its ugly head.

After much back and forth we ended up going with a “hallway in the middle and apartments along the walls”-kinda setup. The one caveat was that the apartments would need some kind of minimum size. This would end up crippling our ability to resize the buildings due to apartments being cut short when the length of the building wasnt divisible with the length of the apartments. Another question was how we were going to create the apartments. Placing a thousand different objects with poorly configured pivot points through code wouldn’t be easy so we ended up compromising. Apartments were to all be the same size and buildings would just have to be generated based on that. The apartments are just big enough to feature a reasonable amount of furniture without compromising the players ability to maneuver it.

We now had both floors and apartments on those floors but no way to get to them. Since we had no plans of implementing noclip as a part of our core gameplay mechanics, it would seem I’d have to go at it again and generate some stairs. So I promptly removed my self from my lazy ass and went to work.

Hardly had I finished those god forsaken stairs before another team member, annoyingly, noted that my beautiful buildings would take up way too much performance containing all those apartments. Bitterly, I had to let the ousted thought enter my brain again as I started plotting the murder, I mean plan, for how I was going to save the GPUs of all our future players. Honestly, the solution ended up being simpler than the mind of a trump supporter and creating it was even easier than lying to one. We were simply going to wait to generate the interior until the player entered the building and then destroy it all again once they left.inside_apt

Fool proof plan but once again it was foiled by our game designer who pointed out that the interior wouldn’t be the same if we just generated it all over again every time a player entered. I was back to the drawing board. Luckily computers are magic and setting up a seed was easier than finding pixelart on a ludum dare submission page.

With trouble slowly receding back to its lair, we went to, as Jonathan Blow would say, “put it in the game.” Turns out it’s kinda hard to visualize just how your city was going to look like based on a bunch of invisible points, that were supposed to resemble buildings. This is where my microscopic experience with Editor scripting came into play. I created a couple buttons on our building generating orb. These allowed the building to be generated in the editor so you could see wether or not your newly placed building was going to block half the highway. This made building our map much easier.

Confident in our generated little wonders, we booted the game and in a display of pure strength our underweight 720Ms mustered an astounding 15 fps. Although highly cinematic, it didn’t quite feel right so back to the drawing board once again. We noticed that Unity’s standard culling technique, fresnel culling, was still rendering all the apartments in the building even when they weren’t visible. This lead us to use occlusion culling instead. Occlusion culling basically only renders what you see. This means that looking at any given building, you’ll only load what you actually see and not, say, the apartments behind it.

And that’s the story of how, I, Hjalte Tagmose, writer of this blog entry, spent way too many hours on something that wasn’t even a major priority! Viva la REVOLUTION!


Did this post not contain enough nitty gritty technical details and complicated code snippets for you? Well then tune in again next friday, when we’ll be talking about networking and how it has reduced our lovely founder, Stefan, into a sleepless mess of a man.