1. The Scouring
  2. News
  3. The scouring engine. Buildings destruction in RTS. Tiny Teams Fest!

The scouring engine. Buildings destruction in RTS. Tiny Teams Fest!

[p][/p][p]Hello orcs and humans![/p][p]We are taking part in TinyTeam fest! [/p][p]To celebrate, I'm writing a short article about how destructibility works in our engine.[/p][p]I've been working on my RTS game for about 3 years, using my personal game engine, and one of the things I'm really happy with is the way buildings are demolished, have a look
[/p][p]We are a very small team, so I had some important conditions for destruction system[/p]
  • [p]It need to be easy to set up, because we don't have time and resources to i.e. decompose meshes and then animate them[/p]
  • [p]It need to be reliable because I can't be coming back to it once it's done[/p]
  • [p]It need to look cool and make my game stand out among dozens of other RTS games[/p]
[p]Those points are especially important because we expect the community to make mods for our game, and our destruction solution should work out of the box for them.[/p][p]So here's how it's done in The Scouring.[/p][p][/p][p]Part 1. Setup[/p][p]All the artist need to do is attach mesh blobs to parts of the building that he wants to come off, like this[/p][p][/p][p]We use about 100-150 blobs for buildings, and it takes just a couple of hours to set them up.[/p][p]And that's it.[/p][p][/p][p]Part 2. Mesh Processing[/p][p]Most destruction systems use geometric intersections (boolean operations) to break mesh into pieces. However, it is a very slow and complex process that generally requires the mesh to be constructed in a specific, “correct” way.[/p][p]Instead we use alpha-clipping to break the mesh into pieces.[/p][p]2.1. So the first thing is to generate blob intersections against the original mesh.[/p][p][/p][p]These are our chunks that will break off the building.[/p][p]We also generate the “backsides” of each hole.[/p][p]This might be the trickiest part of the whole thing, but I would not like to go into details of it as it is very technical. Get in touch with me if you need more details.[/p][p]2.2. Next, generate a texture atlas for our building[/p][p][/p][p]There are publicly available libraries that can do that, I used [/p][p]https://github.com/jpcy/xatlas[/p][p]For an RTS camera and a building like that, 512x512 atlas has enough accuracy.[/p][p]2.3. Iterate every pixel in the atlas, and mark it with chunk id when the corresponding 3d position on the building surface is within one of the “blobs”[/p][p][/p][p]This is the most time-consuming process (relatively speaking, the whole process described in this paper takes 0.25 ms on my 10 years old PC).[/p][p]The most important thing to speed things up is to assume all blobs to be “convex” (and there is no reason not to do so).[/p][p]Checking if a given point is inside a 3d convex hull is trivial. And there is a public library that generates convex hulls - [/p][p]https://github.com/akuukka/quickhull[/p][p][/p][p]Part 3. Runtime[/p][p]So when a unit hits a building, we detect the chunk that is closest to the hit position, and mark it as active. Each building has a texture buffer with the size of “n chunks”, storing a boolean for each chunk, whether it is active or not.[/p][p]In pixel shader, we read the texture atlas storing chunks id’s, and do a clip if this chunk is active.[/p][p]We also need to show the “backside” of a clipped hole. We render the “backsides” mesh, with the inverted logic of the check mentioned above (if a chunk is active - DON’T clip the backside).[/p][p]And of course the chunk itself. If the building is visible, we spawn a physical body for it. If not, we simply activate it in its resting position (next to the building).[/p][p][/p][p]We only use 3 calls to draw the whole thing:[/p]
  1. [p]Building itself[/p]
  2. [p]Backsides[/p]
  3. [p]Chunks. We put all of the chunks geometry into a single geometry buffer, and use the same texture buffer in pixel shader to clip-out inactive chunks.[/p]
[p]When the building is being repaired, instead of activating the chunks, we deactivate them.[/p][p]Last (but not least) a few notes about additional effects.[/p][p]We separate the building into “burnable” and “static” parts. [/p][p] [/p][p]The “burnable” part slides down when the building is destroyed, and has some fancy shader effects applied to. The “static” parts simply dissolves (using a screen-space per-pixel technique)[/p][p][/p][p]Thank you for reading![/p][p]If you have any questions, please fire at [/p][p][email protected][/p][p][/p][p]Pavel Zagrebelnyy, Orc Boss[/p][p]&& Game Director[/p][p]&& The only one Сode Developer[/p]