A Custom Level Editor For An Open World Game
In 2023, I started prototyping a game for Funktronic Labs, at the time titled Scramble Knights Royale (which later became Scramble Knights Online).
This was an ambitious title. One of the level-design goals were to have a large-scale open world map, potentially several miles in diameter. Much of the original design was inspired by popular battle royales of the time, such as PUBG and Fortnite.
In order to support development of this goal, I first talked it over with the studio's art director (Pat Vince) and later with the level designer (Eddie Peters), to ensure what I was about to make would cover any use cases they might need.
Here's a short list of the wants:
- The ability to work in a tile-based fashion.
- Automatic tile selection by default, with override abilities.
- Keep everything as "Unity-friendly" as possible - do not break support for basic prefab / component modifications.
- Runtime level streaming, do not wait on the whole level to load before jumping into a match.
- Some in-Unity way of quickly placing props too, not just tiles.
- Always be able to immediately test changes.
The trickiest ask here was the tile based world editing. I sat down to think about it, and sketched out a few ideas.
In the past, I've worked on a lot of voxel-based systems (for example, as far back as 2021 I was tinkering with Marching Cubes, and then in 2022 I released a voxel plugin on the unity asset store). I thought in-depth about how Marching Cubes worked, in particular, and how it could potentially be applied here.
Marching Cubes
Because I had spent so much time in the past thinking about Marching Cubes, my initial gut reaction about how to handle level-development of this "clay aesthetic zelda-inspired" artstyle was a voxel terrain. However, I knew that the level design wishlist did not really contain realtime modification of levels or anything fancy like that, so I did not really want to go overboard and dive straight into another voxel project. Instead, I was pondering on if there were any learnings I could take from my experience here and if they could be applied to an entirely new system.
There are tons of writeups about marching cubes, which are likely going to do a far better job at explaining the algorithm in-depth than I would here. But, I'll give a brief overview:
Given some surface defined by a density function (or data), you can generate topology around that surface by using simple rules defined by a lookup table. When you dig into it, there's just a few distinct rules, which can be rotated / flipped into a full lookup table.
If you think about it, these rules are just iterating over each 'voxel' and seeing "I'm blocked above? I'm blocked below? I'm blocked to the side?" over and over. Through all these rules, you can generate pretty much any surface's topology (there's some ambiguity in some edge cases - but I'm glossing over them here).
Marching Cubes packs these rules into a single lookup table, which is indexed into via some bitmasking of the 'rules', and it just works. It's fast, it's easy, and it looks pretty good for something so simple. However, in this case we don't really need to be fast or easy, and I want it to look VERY good.
Rules
What I came up with was to take this 'rules' idea from Marching Cubes and expand on it. Skip all the optimization of bitmasking and lookup tables for now, and get soemthing going. The plan:
- Write a
Tile Rulesetscriptable object, with a custom inspector for all the rules. - Write a
Tile Groupcomponent, which could be placed in the scene. - Write a custom level editor that allows placing
Tile Groups (among other things), keeping it unity-friendly and fully in theSceneView.
The editor for rules was made to look like a 3d grid of state checks. 3x3x3, so any case could be handled (similar to marching cubes). I kept a strict restriction of "neighbor only" checks, at least at first, to keep thinks simple to implement in code and to hopefully keep things fast in the editor moving forward.
The rule-editor looked like this. A preview of the prefab on the left, with the rules defined on the right. Below (y=-1), self (y=0), above (y=+1) 3x3 grids, where clicking these buttons would allow selecting a particular 'state' for the rule to check. In this image, you can see there are many more states than in the enum I described above, as these states were eventually added later in development.
In theory, these rules could be setup to mimic the base rules from marching cubes, and have code handle rotation/flips as a level designer or artist would prefer on a rule by rule basis.
I explained this thought process to the artist (Pat Vince) in charge of actually concepting/modeling the tiles themselves. I offered a sytem for automating those rules, but he opted for doing them by hand, suggesting that not every tile group would need the full ruleset. In hindsight, I did regret not simply baking in the default marching cube rules as a 'default' as later in development we did continuously run into bad rules/tiles that needed to be hand adjusted. Regardless, we pressed forward!
After getting the initial tile group art made, the level designer was able to go for a test drive.
While not perfect (you can see issues in meshing even in this gif above), this workflow quickly proved to be a huge success. The level designer was able to quickly block out large chunks of the overworld and unique dungeons.
The level designer did have a few more requests, though.
Making Things Interactive
Now that the overworld development was underway, the level designer asked for sytems to allow him to make the world interactive.
Unity, unfortunately, does not have any sort of built in node-behavior-editor. Nothing blueprint-like. I was tempted to start development of a Unity plugin for something similar, although there are many on the asset store already. But, given the time constraints of the project, I opted for something a lot simpler: an in-level-editor signal system. This was mostly inspired by the "Signal System" in Wave Break's level editor (a previous thing I had developed), which itself was heavily inpsired by Minecraft's redstone system.
The signal system was pretty straightforward:
- Define a
DeviceTriggerthat sends out a 'signal' when triggered. - Define a
DeviceMechanismthat listens for 'signals' and does something. - Define a
DeviceGatethat sits between triggers and mechanisms, for complex logic. - Define a
DeviceWirewhich connects triggers to gates and mechanisms.
These pieces were all just basic Unity prefabs and components, nothing fancy going on really. The level editor was made to handle custom rendering of wires and states, to make it easy to debug issues.
The signal system included goodies such as:
- Push blocks, Damage blocks, Pick-up blocks
- Buttons, Levers, Pully triggers
- Doors, Gates
- Activators, Spawners, Despawners
- Logic gates: AND, NAND, OR, NOR, XOR, NOT, DELAY, and more.
With all of these in place, sophisticated dungeons became very easy to make.
Okay, but maybe we do want realtime editing?
While this was not an initial goal of the system, I did try to keep the level editing experience as snappy as possible. Clicking and waiting is horrible, so any time that performance became an issue I hopped back on the editor to profile and fix whatever the issue was.
The TileGroup resolve pass ended up being a burst-compiled job, and it was pretty fast. Fast enough that it typically did not even register on the profiler on it's own (the UnityEngine.Object Instantiation of those tiles was another story..)
Because of this, the idea kept coming up in the office "can we get runtime modifiable groups?" Honestly, I really wanted to avoid this, because I had planned on static-baking various parts of this system. So instead, I provided a toggle for certain TileGroups to opt into realtime modification support. The build system would skip over these when doing built-time optimizations, and these groups were allowed to be modified at runtime.
The first real use-case of this was tall grass tiles.
There were quite a few use-cases for this that ended up popping up throughout the project (breakable tiles, unstable floors, things of that nature). Looking back, I'm glad the system stayed flexible enough to allow this to work!
What about runtime?
At this point, I was pretty happy with the state of the level editor, at least on the Unity editor side of things. Unfortunately, the first build of the game with a large chunk of the world actually created was not doing too hot.
The first implementation of this system simply spawned in the whole overworld when loading a level, which of course was not sustainable long term. However, I had already been thinking of a way to deal with this and was working on a plan.
Inspired again by previous voxel work, my plan was:
- At game-build-time, gather all of the level assets and group them into spatial chunks (16x16x16?).
- For each chunk, generate a Unity prefab and serialize it to disk.
- At runtime, take the player's positions and spawn chunks within view of the camera, while despawning chunks far outside of view.
The first pass of this was pretty successful. Things streamed in and out, all was well. However, runtime performance was generally still very poor. On my Steam Deck, I was only getting ~30 fps. My goal was to hit 60 fps with plenty of overhead on that little Steam Deck.
After some profiling, I found that Unity was simply spending way too much time on submitting draw calls from the CPU to the GPU. The CPU was spending nearly 20ms on just issueing these commands. For a top-down game that seemingly wasn't rendeirng that much, I really thought this was a bug, at first.
This was only my second project using the Universal Render Pipeline (URP) in Unity, so I did a bit of research to see if we were missing something vital. We were already using the SRP Batcher appropriately, which had given us big gains in the past. I did discover that URP at that time had just released it's GPU Resident Drawer support (in a version right above where we were). I spent some time upgrading our Unity project and give this a whirl.
The GPU Resident Drawer has some.. somewhat annoying requirements. I had to make sure all of our shaders supported the new Forward+ renderer, and our lighting code was all custom. However, I did get this working after some fiddling, and got to profile the game using the resident drawer. It did increase performance (roughly 2x improvement, was only 10ms spent issuing commands). But, I did not find this acceptable at the time. Surely it could be much faster, there really isn't much going on!
Static all the things?
Because of the nature of spawning in-out meshes, I had not ticked on isStatic on our generated prefabs, but tried out using Unity's built-in StaticBatchingUtility API at runtime, running it over the whole chunk. A world chunk would spawn, we'd statically batch it.
This actually increased runtime performance significantly, only spending ~5ms per frame on submitting these draw commands. However, the huge tradeoff was that the static batch pass was pretty slow. So, maybe we could do it at build time? Unfortunately this did not seem to be a built-in option, so I wrote a tool that would iterate over the built level chunk prefabs and mesh combine them manually.
Doing this manually had a few extra benefits:
- No runtime cost at generating these meshes. They were just there, ready to go.
- Precision of the meshes thesmelves could be lowered, saving GPU memory and disk space.
One thing that did suck about this baked mesh approach was that the filesize of the built player exploded. The world was large, so the disk space for the game install went from ~2 GB to ~10 GB immediately. However, the runtime CPU savings were well worth it: down from the initial ~20ms to ~4ms.
What else can be saved?
Because this approach worked so well for rendering savings on the CPU, I had to wonder: are there other things that can be trimmed down in a similar way?
- There's a huge runtime cost to instantiating prefabs with deep dependenies (prefabs nested in prefabs nested in prefabs).
- The physics pass was actually pretty slow when a lot of the world was loaded (literally over 1 million static box colliders at times).
These two big things were not too bad to deal with. The build system simply stripped out unmodified prefabs (no component overrides) from the built-level-chunks and stored them in a list, to have Unity later run an Async multi instantiation pass on. This had big frame hitch savings as players ran through the world, with the tradeoff being slight pop-in sometimes being visible. The physics pass was a bit tricker, but I managed to run a sort of 'greedy meshing'-like approach on box colliders throughout the built-level-chunks, which drastically reduced the typical number of loaded box colliders (down from 1 million to ~50,000 as the new peak).
After all of these changes, the game ran at 60 fps on my Steam Deck (maintaining a smooth 144 fps on my desktop PC).
The Pivot
Scramble Knights later pivoted from "Battle Royale" into "MMO-lite" (a very Monster Hunter inspired take on the genre). With this change, the level editor mostly stayed the same, but there were some new requests for the runtime support of the systems.
One big ask was to have "instanced dungeons" exist, akin to something like going into Deadmines in World of Warcraft.
Because of how the level editor already structured existing dungeons as scriptable objects, it really was just a matter of changing how they were spawned in the world. Instead of spawning them somewhere underground, instanced scenes instead would use Unity's additive scene loading and physics world support. This worked pretty seamlessly, and had the added benefit of splitting up the physics simulation workload into smaller bite-sized chunks, which improved performance as the level and number of dungeons grew in size.