Friday, October 25, 2024

Procedural Buildings: Sewer Tunnels

I was watching some YouTube videos of urban exploration where people were exploring storm drains under a city, and this gave me something else to add to 3DWorld's procedural cities. I've improved basements in the past few posts by adding interrogation rooms and machine rooms. Now it's time to add underground tunnels. I originally thought of connecting these to the water (ocean), but none of the cities are close to water. Also, the water is far from the player spawn point near the center of the island. So instead I decided to connect tunnels to basement rooms instead.

I started by iterating over all of the locked ("false") doors I had added at the end of extended basement hallways and long rooms to make them appear larger. Any door that was far enough underground and had empty space surrounding it on three sides was a candidate for placing a sewer tunnel. I added a vault-style heavy metal door with a wheel handle connecting the room to the tunnel that was open to the player. Tunnels run perpendicular to the room and door in both directions and may have right angle bends in their paths. I added metal grates in the form of vertical bars near the ends of the tunnels and made the very ends black. This, combined with dark fog, made the tunnels appear to continue past the grates.

Here is a view of a sewer tunnel system under the ground with the terrain and grass drawing disabled. This network runs around the perimeter of the extended basement of an office building, but in general it can go anywhere there's available space. I may even connect tunnels between multiple buildings in the future like I already did with extended basement hallways.

View from outside a tunnel system that runs under the ground. The terrain and grass drawing has been disabled. The dark rectangle on the bottom right is the sewage quad drawn under the bend.

Tunnels have dark brown sewage flowing inside them with a randomly chosen level and flow rate. They also have smaller pipes connecting to them on the sides and thin metal pipes passing through them along the top. I added rats on the ground in some segments that squeak and run to the nearest grate when the player approaches. I also added spiders that walk back and forth upside down along tunnel ceilings. Spiders don't currently interact with the player.

Tunnels are drawn as cylinders, with quarter torus sections for bends. It actually took me quite a bit of experimentation to figure out what the correct bend geometry was. Fortunately, I had already added half torus drawing for bucket handles, so a quarter torus was simple to add. I used a concrete texture for the walls and applied the wet effect to these to give them streaks of darker color. Speaking of darkness, there's no light inside these tunnels, so the player must bring their own flashlight or candle to see anything. See how dark this is? It's completely disorienting to walk through here without a light.

Flashlight view of a sewer tunnel near the place where it connects to a basement room, with a spider visible on the ceiling.

I did add logic for zombies to follow the player into the tunnels with a recursive path finding algorithm that forms a path between the connected cylinder and quarter torus sections. These are currently dead-end death traps for the player. Maybe I should add some reward at the end of the tunnel? I'm not sure what to place here. A vertical shaft leading back up to the surface that serves as an escape path? That could work in cities where the ground above is flat.

I'm considering adding T-junctions to sewer systems. I haven't yet figured out the mathematical function needed to join cylinders into a T-junction in a smooth and clean way. I do have a T-junction where the tunnel meets the room doorway, but this is square on the doorway side. It's easier to cut a square hole into a cylinder (remove half/180 degrees of the cylinder) than a circular hole. 3DWorld has CSG support for cubes, some types of cube + axis aligned cylinder operations, and a few others, but no cylinder vs. cylinder operations. This would be a problem for vertical escape shafts as well. It would be so much easier if I used rectangular tunnels. The problem is that I already have extended basements, which aren't much different from rectangular tunnels. The main difference is the lack of sewage on the floor!

Here is an earlier video where I explore a sewer tunnel with a flashlight and candle as light sources.

The player can find both light sources in buildings. Flashlights shine further with a narrow beam. Candles light only nearby objects, but in all directions. There are no ceiling lights, shadows, or indirect lighting.

Here's another video where I added spiders to the ceilings.

I think I'm done with basements for now. I may add underground shopping malls sometime in the future if I can figure out how to do it efficiently. The next item on my task list is writing the logic for cars to navigate city parking lots. I'm sure that will be challenging, considering how difficult cars have been to work with in the past.

Friday, October 11, 2024

Procedural Buildings: Machine Rooms

I've been adding new room types to 3DWorld's procedural buildings lately. I wrote a blog post on conference rooms, then a short post on interrogation rooms. The topic of this post is machine rooms. I've been wanting to add procedurally generated industrial type machines/devices to basements for a while, ever since I revisited basement pipes. I've also wanted to add more variety to extended basement rooms.

I've been working on adding code to route basement and parking garage pipes down the extended basement hallways and into leaf rooms. But it only makes sense if there are objects in these rooms to connect the pipes to. I do have bathrooms with sinks and toilets down in the basement, though not every building has these. Another option is to add more water heaters and furnaces similar to the ones placed in house basements and office building utility rooms. The appliances I've created are more residential sized and look out of place in a large open basement room though.

I added a new type of building room object called a "machine" that's represented as a bounding box placed on the floor and against a wall of the room. All placement and collision detection handles this as a box. However, the drawing code is free to add any details it wants when the vertex data is generated and sent to the GPU. None of this geometry is stored CPU side, it's all created as temporary data that's drawn later. An added benefit of machines being placed in basements is that the vertex data doesn't need to be generated until the player enters the building. This allows me to create very detailed geometry without the need to worry about optimizing for memory or generation time, since these are only found in one building at a time. I do technically create machines for multiple buildings ahead of time to help with visualization and debugging, but I don't think it makes much of a performance difference.

Each machine consists of a concrete slab base and one or two main components, which can either be rectangular/cube shaped or vertical cylinders (such as storage tanks). These larger components have smaller boxes and cylinders attached to their exterior surfaces with different colors and textures. I then added some special decoration objects that are already used in other places in buildings. These include AC units, vents, valve/wheel handles, and breaker boxes. Some breaker boxes are open to expose the interior. To finish things off, I added a variety of pipes and ducts connecting the components together and going into the walls, ceilings, and floors around the machines. These use multiple materials including plastics and metals (copper, brass, bronze, steel, and rusted). I used special checks to make sure these added detail objects don't intersect each other or clip through adjacent walls or machines in incorrect ways.

I found ten free textures online to use with these machines. Most are some type of metal panel or plate texture, some of them with rust and scratches. Only one of them has a matching normal map. I may look for better textures with normal maps later, but these are fine for now. I haven't put any effort into aligning textures to geometry because it's too difficult to automate this for this many different textures.

Here is an example of a long and narrow machine room.

Machine room with random machines lined up against the walls. Each one is unique.

 

Note that these rooms are somewhat brighter than they normally appear to make them easier to see. I prefer to have these deep basement rooms be darker. Here I have indirect lighting enabled, though it doesn't work very well due to the way rooms are randomly and sparsely placed, compared to the dense rectangular floorplan of above-ground building interiors. At least the shadows on the lower walls and floors are correct.

Machines are placed along the sides in a way that allows space for the player and building AI people to cross the room between them. I added extra clearance for doors so that they're not blocked. The rooms in these screenshots have up to 10 machines for testing purposes, though for normal buildings I'll probably set the max count to 4.

A different machine room with different machines and many cracks on the walls, ceilings, and floors.

I definitely like the way the cracks and water damage fit with the scratched and rusty metal textures I'm using with machines. The crack effect only applies to the walls, ceilings and floors though. I don't think cracks go well with metal surfaces. I did apply the wet/water damage texture effect to machines. This give the surfaces a darker but more shiny appearance. Here's another machine room with water damage and stains but no cracks.

A machine room in the basement of a house, with plaster walls.

I think I'm done with new room types for now. I plan to continue to work on extended basements in the near future. Next up is underground tunnels/pipes that the player can walk through. I'll likely have a blog post on that topic within the next few weeks.

Tuesday, October 1, 2024

Procedural Buildings: Interrogation Rooms

The interrogation room can be found deep inside the extended basement, after a long walk down dark and narrow passageways. A single empty chair sits in the center of the room under the light, facing the door.  Buckets of unknown colored liquids are placed around the chair. Bottles and bits of trash are strewn across the dirty floor. The walls, ceiling, and floor are wet, stained, and cracked. A ladder with an unclear purpose leans against a wall.

An interrogation room with cracked walls. It's not very bright in here, so I lit it up with my flashlight.

Interrogation rooms are relatively rare and are only placed in extended basements of buildings with water damage. They can appear in both houses and office buildings. Sometimes the chair has fallen over backwards, and sometimes the light doesn't work. Broken lights shower sparks over the chair. The ladder is optional. This is the only room that currently contains the new bucket-with-liquid object.

These rooms don't serve a purpose yet. They're mostly just there to be creepy, though I didn't go so far as to add blood, tools of torture, or anything like that. I might not add this at all, since it's not supposed to be that type of game.

Also, take a look at my new basement cracked surfaces effect in the screenshot above. I replaced the old shader code with new code that uses a Voronoi grid and an intensity mask to apply cracks to selected areas. It's based off of this Shadertoy shader. Cracks are added as a layer that modifies the material properties of the surface it's applied to.