Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Problems creating procedural rooms for a building (office).

Discussion in 'General Discussion' started by Loris_Accordino, Jun 19, 2023.

  1. Loris_Accordino

    Loris_Accordino

    Joined:
    Mar 19, 2022
    Posts:
    3
    Hi guys. Sorry if I'm doing something wrong, but it's the first time I'm writing in the forum, because I really need it... My problem is the following: I've been looking for a way to procedurally generate rooms for literally MONTHS to create the levels of a building, in this case an office. The problem is that I found various algorithms like the BSP, the Delaunay the A*, but I'm a little confused, especially since I'm not a scientist, a mathematician or anything like that: I just have the necessary notions of mathematics XD .


    In any case, I wanted to ask if any of you know which algorithm (if there is a specific formula, or if you can come up with logic) can help me solve my task. Anyway the problem is that when I try to implement these algorithms they work quite well, but that's not what I want. These are designed for dungeons, while I want rooms in a building, connected by realistically created corridors and not a dungeon with scattered rooms and very "strange" corridors. At this point. Is it possible to do what I want to do? How hard is that? Can it be done with brute force (I tried, but without optimizing with recursion it goes to Stack Overflow after a while XD)? Mathematical considerations or not? Should I let it go? Need more details? Thanks in advance to anyone who replies!
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,317
    The very first and very obvious thing is that you forgot that inside a building walls have uniform thickness. In your example, You're generating caves, not rooms.

    Here's what I would try.

    Start with an empty shape that represents the whole floor. All tiles are floor, there's only outer walls. Then start building walls. Lay down central corridors. That will produce walled off areas. Process those areas recursively, subdividing them with more walls or more shapes.

    upload_2023-6-20_1-22-19.png
    A similar approach is sometimes used by various "city generators". There you block off main roads. Those produce regions. Then you divide regions into districts. Then you divide districts into houses.

    See watobu city generator for an example.
     
    Ryiah and angrypenguin like this.
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,503
    So what have you actually done or found from this?

    These are a few steps on from the previous problem, and each only solve a part of it. BSP will help you describe a 3D shape, but you need to first know what that shape is. A* will help you create paths between points in that shape, but again, you need to know what it is.

    You're confused because you've skipped a step. For an office I'd start with exactly what neginfinity drew - an outline of the space, and them sub-divide it into inner spaces. Once you've figured out some way to do that, then you can think about how to convert the space you've defined into a mesh.
     
    Last edited: Jun 20, 2023
    MadeFromPolygons likes this.
  4. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,724
    Pretty sure they're referring to BSP generation in this context:
    https://eskerda.com/bsp-dungeon-generation/

    A BSP tree can easily describe 2D areas.
     
    MadeFromPolygons likes this.
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,503
    I'd have thought so to, except for the first screenie they posted which isn't that, and is clearly labelled as "BSP Dungeon Generation".

    True, that was just an un-founded assumption showing through.
     
    MadeFromPolygons likes this.
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,503
    Actually, I stand corrected.

    For an office, I'd stop at the leaf node step, but this describes "growing" a room in each box which is why it ends up with the cave-like look and feel. To define an office, as is the goal here, I'd let the boxes themselves define where the walls go, with the remaining step being to decide which edges to put doors on, and where.
     
    MadeFromPolygons likes this.
  7. BIGTIMEMASTER

    BIGTIMEMASTER

    Joined:
    Jun 1, 2017
    Posts:
    5,181
    i thought games typically did this by randomization placement of premade, modular models. what is there to gain by creating the geometry or sprites at runtime? especially somebody who isn't good at math
     
  8. Wattosan

    Wattosan

    Joined:
    Mar 22, 2013
    Posts:
    450
    I would also recommend checking out this Catlike Coding tutorial about generating mazes: https://catlikecoding.com/unity/tutorials/maze/.

    It will show you a concrete algorithm and its final result can be tweaked such that it will look closer to a "real office space".
     
    angrypenguin likes this.
  9. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    I agree, especially because IRL office buildings are very samey anyway.
     
  10. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,317
    There are games with mixed approach, notable example being CDDA. They mix modules with dynamically sized fully procedural rooms.

    Here's an example off the web:
    upload_2023-6-20_18-30-59.png
    The place is split into a huge blocks, I think 24x24. That's overmap tile, the game decides what's in there for the region. Within the tile, the game can generate fairly dull completely procedural rooms, or it can place multiple tiny modules.

    Now, the whole overmap tile can be a template. Or it can contain multiple small templates within, position and rotate them. The remaining place is then filled procedurally.

    The reason to go for higher degree of "procedurality" is that block prefabs very quickly become recognizable.
     
    stain2319 likes this.
  11. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,503
    I'd still make the building geo by placing pre-made modular bits - floor, wall, etc. segments.

    And the pros / cons in what type of control you want, and what type of shapes you want. The latter stands out as relevant here, as selecting a combination of large-scale pre-made parts which combine to form a filled in office suite is likely to be tricky / hit edge cases, where a subdivide-and-populate approach with small pre-made parts seems a natural fit.

    And the math, like anything else, can be learned and practiced. No real way around doing some of that in game dev.
     
  12. Loris_Accordino

    Loris_Accordino

    Joined:
    Mar 19, 2022
    Posts:
    3
    HI. To everyone. Thanks for your help. I managed to have a quite satisfactory result thanks to your advice. But at this point there is another big obstacle, and perhaps more complex: how do I generate the interiors to put in my building? I had an idea in mind. Essentially, I group multiple objects together (e.g. a "desk corner", which includes: desk, computer, paper on the desk, a plant, a bookcase on the wall and other furnishings... and so on) and then based on certain criteria and constraints (which I would like you to help me understand the most appropriate ones) I place them in the rooms. I have already generated the walls and doors. In any case, I also wanted to know the most appropriate parameters (to put in a Scriptable Object, for example) such as the probability, the minimum and maximum number, whether it is mandatory or not. But I'm not sure. Could you help me with this as well? If you think it's appropriate to open another thread, that's fine by me. Thanks in advance!
     
  13. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,317
    It depends on how you want to go.

    One way to go about it is "list of possible prefabs within list of possible prefabs within prefabs".
    For example, a desk can have several possible configuration of child objects attached to it, from which one will be chosen randomly and spawned. That means desk prefab will store links to possible child prefabs, and one of them will be chosen and spawned when the desk is initialized. The child prefabs could have similar configuration.

    On top of that you could have dynamically placeable objects there. For example a desk could have a beer can on it. Or several. You could define an area in which they could spawn on desk, and when the desk is initialized, select location randomly, and place can there with random rotation. Except you'd need to spawn a location where the can can fit, meaning it should collide in already existin object.

    I've done something like this before, this can be a bit tedious.
     
  14. Loris_Accordino

    Loris_Accordino

    Joined:
    Mar 19, 2022
    Posts:
    3
    Okay. As far as the assets are concerned, it's quite clear and I too had in mind to do so, but unfortunately I won't be able to have such a high level of detail and attention because I have hundreds of assets, so I will only be able to "group some assets" and create prefabs . At this point though. How do I place them in the room in a logical way?
     
  15. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,317
    And that is a difficult problem, that will require a lot of experimentation. Logically difficult.

    Normally all games deal with this through prefab or preconfigured room layouts. Meaning you have a room plan, for the room plan you have several configurations that could spawn with it, either all things at once, or partially swappable items.

    You could try to logically define all rooms in the game through relationships.

    For example, you start with an empty room. What type of Room it is? It could be kitchen, bedroom, living room, bathroom.

    Let's say it is a bedroom. What items could be present in there?
    Bed, bedside table, clock, lamp, mirror, dresser, for example.

    Now you'd need to decide what kind of bed, what kind of bedside table and so on, and whether they share the same style or palette. For example, a single bed, or a bunk bed can be placed next to a wall, but a double bed would require walking space around it. A bedside table goes next to the bed, clock goes onto the table, and so on.

    And you'll need to define those sort of relationships for every room type in the world.

    You also could define a "flexible room layout" of sorts, which will stretch to certain range of rooms, like an UI, and store positions of items within.

    You could also approach it like group of placeables. For example a kitchen can have a dining table, which is a table with several chairs around it which makes it a group of object. Once you select the group, you need to fit it into the kitchen.
     
  16. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    571
    Since offices are pretty much the same, I would use a few prefabs for the rooms and corner offices to build the walls and basic layout. Maybe have a door on the left or right, but basically not worry about the basic structure being the same. Then, use procedural tricks to populate, paint and decorate the rooms to hide the repetition of the walls and doors.

    Edit: yep, pretty much what everyone is suggesting.

    I might add that you could use tags to label some things, like desk surfaces, carpet areas and have spawned objects place themselves based on the tag.
     
  17. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,317
    It depends on how far you want to go. If you have a cubicle room, and 5 cubicle prefabs, but 64 decks, they'll repeat and it'll be noticeable. To make it less noticeable, it'll be necessary to add some procedural component. For example, make some things spawn at random or semi-random location on the desk. It won't fully fix the problem, but it'll help a bit.

    On other hand, real world has a lot of copy-pasting. For example, an apartment block building will have same layout on most floors, and it is highly likely that bathroom and toilet will be in the same spot for all apartments. So if you have 9 story building with many apartments, you could start with basic layout, then select style (wallpaper, basically), for each apartment, then maybe move furniture around a little.
     
  18. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    571
    Yeah, I would say don't worry about things like doors, toilets, etc. being repetitive, that's how most building are anyways. I have no idea what your game is, but it would be a lot more fun to place personalized items that are randomized. For example, a desk with a tag like 'lawyer' or 'introvert', 'extrovert' would choose items that give some back story for who is in this particular office. Maybe family pictures, or pictures of cars, etc. I think that's where procedural items become fun, not in the actual layout of the floor or room.
     
  19. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    Generating nesting dolls like rooms and whatnot is done through directed vertical graphs. All nodes represent a decision of spawning something from that level of complexity. The important thing is, every node have exactly one "in" side and one or more "out" side. The top is a choice of the type of the "room", then the biggest "area" choice, then going more and more details as deep as you choose to go (like the hair on the hairbrush on the desk in the front of the mirror in the top-left corner of the bedroom).
    Now, you can write all this yourself or you can utilize a node-graph design tool like Behavior Designer or Flow Canvas or other node based designer tool and just write your spawn logic and data blackboarding as actions invoked by the graph. I hate visual, node based design tools in general, but they're great for this exact purpose. They usually even have things like executing a path randomly or in order and whatnot.
     
    neginfinity likes this.
  20. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,874

    If you have genuinely spent months on this, I would first recommend looking into simplifying the scope of what you are trying to do, if you really want to finish whatever project this is.

    It shouldn't take months to figure this out, and that's a really strong sign this project it outside of the scope of your (current) abilities.

    It would be better to scope down, finish something, and come back to a procedural style project once you have the abilities, than to keep banging your head against a wall on the same problem for so long.

    Ultimately its your project and feel free to ignore this, but it doesn't sound particularly healthy to be whacking away at the weeds getting nowhere when you are still learning.
     
    Ryiah likes this.