- Let the player fly through buildings to see their interiors. (There's already a key/mode that disables collision detection and allows for flight.)
- Add a key/mode that will disable drawing the exterior walls of nearby buildings so that the interior becomes visible.
- Allow the player to open building doors and enter (somehow).
- Make windows transparent so that the player can see the interiors through the windows.
Windows
The first task was to cut holes into the exterior walls for real transparent windows. I began with the smaller secondary houses and buildings because they already have windows and doors applied as a separate texture layer. Well, office buildings actually have doors now, but they're placed better in houses and smaller buildings. These generated windows look too simple and ugly and need to be replaced anyway. All I needed was a way to cut square holes into the brick and concrete block walls.
It turns out that this isn't so simple. The obvious solution is to generate quads (rectangles) for each window and subtract them from the walls using CSG (constructive solid geometry) operations. I already have this code from the ground mode collision cube framework. Okay, but how many windows are there? Let's see. There are about 14,000 total buildings. Each one has some number of sides, at least 6 on average. Some of the buildings are tall, but some are small houses, so there are maybe 4 floors on average. Each wall seems to have an average of 8 windows (skewed by the large buildings). So that would give us 14K*6*4*8 = 2.7M window quads, or 5.4M triangles (1 quad = 2 triangles). And that's just the window panes themselves. We also need frames to make them look nice, and all the wall geometry around the windows. That's ... quite a lot of geometry, even with these crude estimates. How many triangles is it exactly? I don't know, I didn't implement it this way. I can't imagine the generation times, draw times, and memory usage would be good!
We need to use a different approach. Opaque windows are currently drawn as repeating textures that cover each wall of the building that's intended to have windows. The same technique can be used here to mask off areas of the walls to not draw, then draw windows and frames in additional passes later. This took quite awhile to figure out. I wasn't able to get anything working that allowed the player to see into the front side windows, through the building interior, and out the back side windows. The best I was able to do was to cut out the windows from only the front facing walls of the buildings. That's probably good enough, considering walls will generally block the view later when I add them in. The steps/draw passes I used are:
- Draw building interiors because they're not affected by windows (more on this later).
- Draw back facing walls of buildings (for fragments not blocked by an interior wall). Walls are drawn as two sided surfaces so the backs are still visible.
- Draw window panes with depth write only (no color writing).
- Draw front faces of buildings. Any fragments/pixels that fail the depth test from the previous pass are not drawn. This is what produces the window holes.
- Draw window panes + frames with dept test set to less-than-or-equal to fill the window holes.
This black magic allows 3DWorld to draw walls, window holes, and frames with only a few hundred thousand quads rather many millions of them. It's not perfect. For example, there are some non-anti-aliased edges on the window frames, and things get messed up if the player flies through the windows, but it's good enough for now.
Here is an early screenshot showing buildings with windows cut into the exterior walls but before interior walls were added.
Large house with transparent window rectangles cut into the concrete block exterior, showing interior floors and ceilings. |
Many buildings with window cutouts. There are too many windows across all of the buildings to use real geometry. |
Walls, Ceilings, and Floors
The next step is to draw the basic building interiors: walls, ceilings, and floors. I've decided to only add these to Manhattan buildings formed from combining adjacent and intersecting cubes. This includes all houses and most of the smaller office buildings in the secondary city. These simpler types of buildings can be filled with Manhattan interiors and is a good place to start.
Ceilings and floors are the easiest. There's one of these for each story of the building. Ceilings and floors together form the separators between the different stories of the building. The first separator has only a floor and the last one has only a ceiling. [There are no ceilings in the attics of houses because there are no windows through which the player can see them.] These are added separately for each cube of the building's footprint where the number of stories is determined from the cube's height. At this point I haven't added any cutouts for stairs or elevators.
Walls are more difficult because they require procedural generation for placement and cutouts for doorways. I decided that all stories can have the same floorplan, which makes generation much easier. Walls can extend the entire height of the building cube, through all floors, greatly reducing the number of quads needed for drawing. This brings the number of walls down to a reasonable number so that I can generate and draw them all up front. Once I have enough details I'll have to start batching the generation and drawing by tile to spread the time out over many frames.
Walls are placed randomly one at a time into each cube of the building. Any walls that extend to an interior face of the cube where two parts of the building meet are converted to T-junctions. I had to spend many hours removing all of the interior parts of the exterior brick/stone walls so that they don't intersect with the interior plaster walls in strange ways. Each cube is recursively subdivided with a randomly placed wall, generally in the longest dimension, until the resulting space is below a certain user-configurable size. This forms individual rooms. Then I go back and remove sections from the walls to make open doorways that connect the rooms together. The resulting geometry is somewhat maze-like and not exactly representative of a real building. However, it's hard to tell by looking at it from outside through the windows. It should at least be the case that all rooms are connected together in any given floor.
There is the occasional misplaced wall that I can't figure out how to fix. Sometimes walls end in the middle of a window. It's difficult to prevent this at this early stage because window positions haven't been generated yet. I think it should be possible to calculate window locations at this point int he generation though, and I'm working on solving it. Sometimes doorways are cut too close to T-junctions so that a wall ends in the middle of the doorway. This isn't easy to fix because the individual cubes don't yet have a way to access the walls placed by their neighbors. I think I need to add cubes at wall end point and check for them when placing later walls. This is still a work in progress, but it's a good start. Now I need to place some furniture into the rooms!
Here are some buildings with interiors added. I didn't add too many walls yet. I think I need to find some better wall placement algorithm that divides interior space in a more interesting way. There also isn't any direct lighting for interiors as they're generally always in shadow. At this point I only have ambient lighting, though at some point I may add some sort of room lights. I'm not sure how to add the thousands of lights that I need for nearby buildings.
Buildings with some (sparse) interior walls dividing internal space into rooms. |
More buildings with interior walls, ceilings, and floors. |
After writing the text of this post I went back and attempted to generate a more regular building floorplan for rectangular office buildings consisting of a central hallway along the longer dimension with rows of rooms on each side connected by single doorways. I think it will be easier to place furniture and other items in rooms like this because the dimensions are regular and known. I suppose this is more realistic as well, though a bit too simple for a large building.
Rectangular office building with rows of rooms on each side of a central hallway. |
Drawing Performance
Let me go off on a tangent about draw times. You can skip this if you're only interested in the procedural generation part. The original idea was to generate and draw interiors for only the visible nearby buildings. It turned out that generation time was only about 200ms, and draw time wasn't much affected by visibility culling, so I ended up generating and drawing them all. Once I add more interior geometry I'll likely have to go back and change the way this works.
One more side note. I find it interesting that performing view frustum (visibility) culling on the CPU on buildings doesn't help much. The GPU is very fast at drawing all that geometry. It helps somewhat when the buildings are mostly off-screen, but it actually hurts when the player is outside the city looking into it because the culling doesn't reduce the number of shapes that need to be drawn. There's some CPU runtime overhead for doing the culling that isn't helping to reduce draw time. This is actually the worst case, so if we want to optimize for worst case framerate it's better to disable view frustum culling.
With this rendering system I get around 140 FPS (frames per second) without interiors and 125 FPS with them in the default view (where the player spawns). This view is at the edge of the city looking away from it. You might think that this would be an easy case, but the framerate doesn't drop if I turn around to look at the city center. There are two reasons. First, I'm sending all geometry to GPU vertex pipeline each frame. The GPU does a good job of culling but it's generally pretty fast about handling buildings. Second, buildings aren't the slow part, grass is. If I disable grass the framerate increases to 260 FPS. Grass isn't drawn where buildings are placed. This means that if I'm looking at buildings, then I'm not looking at dense grass. If I'm looking from the city outward where there are few buildings, then what I see is a huge field of grass. The framerate is actually a bit lower in that case.
Other Changes and Future Work
I made a few additional changes to improve buildings in the process of adding interiors:
- Added a cross-shaped building type/template.
- Removed windows that were partially occluded by sloped house roofs.
- Removed partial windows that were clipped by the other sides of a building.
- Buildings are now an exact number of floors and no longer have floors of different spacing in different parts.
- Building exteriors no longer have intersecting geometry that causes problems with z-fighting.
- Fixed incorrect house brick texture alignment on roof triangles.
- Windows are still drawn behind small structures such as porch roofs that partially block them.
- Interior walls can end at windows so that the wall edge is seen through the window. [I think I know how to fix this one.]
- Interior walls can end in the middle of doorways in the random maze floorplans.
- Some rooms are odd sizes/aspect ratios or simply too large.
- Players can't see completely through buildings (back facing windows aren't cut out of exterior walls, making them opaque).
- Building interior lighting is flat and uninteresting.
- No furniture or other interior details!
You might be interested in Interior Mapping: http://interiormapping.oogst3d.net/
ReplyDeleteThanks for the link. Yes, I'm familiar with interior mapping and I've read that paper and some others. I considered using interior mapping for my buildings but decided to use actual geometry for several reasons:
Delete* It's difficult to create many different textures for use with interior mapping. This is more of an art task, and I'm not good with art. I would rather create procedural geometry. Maybe once I have the geometry I can render it into the 5-sided cube map needed for interior mapping and use that approach as an optimization.
* I plan to allow the player to enter buildings at some point, and maybe also partially destroy buildings. I need to have real interiors for that.
* Interior mapping has some artifacts that I would like to avoid. In particular, it doesn't look correct when you look through a window up close near the corner of the building.
* Interior mapping has already been done and I want to try something different.
I added some stats calculations for buildings and find that there are 13,746 buildings, 35K building shapes, 17K doors, 101K total stories across all buildings, 279K walls, and 523K rooms (excluding hallways).
ReplyDeleteAs mentioned in my comment to the previous post, there really should be a significant space between the ceiling and the floor above. In residential buildings this is usually about a foot (just the floor and 2x12 floor joists). In office buildings it's more like 2-3 feet. For industrial buildings, you might have just one ground floor and the rest is open headspace. Upwards of 12 feet is common for an industrial high-bay.
ReplyDeleteI think all buildings have the same floor spacing because the windows are generated before it's decided what's a house vs. office. All floor/ceiling widths are around a foot. Maybe that's wrong. I don't really notice it because most of the buildings that have interiors use a brick or stone texture where there's no real visual reference for floor spacing. Maybe I'll do something with it, at least for office buildings.
ReplyDelete