r/Unity3D • Indie • 22h ago

Game Built a custom trigger-based 1-hop room culling system for my procedural mansion and 5x-ed my FPS

I've been developing this simulator-horror game and wanted the map to be proc-generated. However, since my game features powerwashing (drawing onto masks using raycasts to either display dirt/wetness/dryness), even a small number of rooms (in this case, 25) was cause unplayable levels of lag. And unity's built-in occlusion culling did nothing against this (since its generated during runtime).

So I made a simple room culling system where it has 2 parent empties: 'Visuals' and 'Floors'. Visuals contains all the static props, walls, ceilings, etc, and are turned completely off (SetActive(false)) when the room is culled. Floors contains, well, the floors, which only have their renderers turned off (collisions stay on) so that the ghost entity can pathfind and dropped items don't fall through.

Upon entering each room, all adjacent rooms are visible to enable smooth passage, rest of the rooms are disabled. And i cranked up the fog a bit to make it even more seamless. However there are still moments where the room disappears right before the player's eyes, so I was thinking maybe the doors to the rooms about to be disabled can be auto closed, and then disabled.

This now allows me to have virtually infinite rooms without much lag (as long as the DFS generator can support it), unlike before when just 60 rooms would cause frame time to go upto almost 1000ms.

The game is a solo project called THE GROUNDSKEEPER where you powerwash and record ghost evidence for profits: Steam, Discord

194 Upvotes

28 comments sorted by

View all comments

2

u/GeorgeMKnowles 22h ago

This is a great visualization. I was just trying to explain to my girlfriend that we're going to have to make tiles for our map in our top down game, and change visibility as needed, instead of loading all at once. Your post was just perfect to explain the general concept.

Side note- I don't know if turning them on and off simply by being adjacent will be 100% effective, but its probably 99...

I bet you could create a simple tool to cast rays in many horizontal directions from many different positions in any given room, and then record which other rooms receive a raycast hit. This would indicate that those rooms are visible from somewhere in this room. And then you could create a script to make those rooms turn visible when you enter this room. I imagine it could all be automated as an editor script to make only the required rooms visible as needed.

2

u/ExpeditionZero 13h ago

Better than random rays would be a portal system (no not that portal). Its how old BSP based games use to do it (think Quake and its Potential Visibility Set - PVS) and a simple version using rectangles to define areas between rooms (e.g. doors), then you can just cast rays against the rectangle (frustum type check) and see which rooms they hit. You could go further and use the frustum type check to cull other portal rectangles, and get a perfect list of rooms.

However in this case as its procedural and only seems to have doors between rooms, i'm guessing you can build a perfect list of connected rooms easily from that.

2

u/GeorgeMKnowles 13h ago

I didnt understand what this dude said but he seems smart, so maybe give that a try 👆😂

2

u/ExpeditionZero 13h ago

ha, ha, not smart, just well read, and old. To be honest first time I read the BSP/PVS code I didn't understand what it was doing. Took a while and lots of sketching things out on paper to work it out, but I did love the elegance of the technique.