[The destruction and physics system in Instruments of Destruction has been a hot topic of discussion, so I wanted to give an overview of how it works. Warning: This is pretty technical and a long read. I may do a video version of this someday when I'm bored. But for now, written words are the way...]
The primary thing that makes the destruction in Instruments look good is having LOTS of things going on and interacting. The physics/destruction aspect of the game has changed a little bit since last showing the demo in October, but it's pretty much the same game everyone saw back in April of last year. Here's a decent trailer to see the destruction in action from that version of it if you want to see (you can just watch the first 10 seconds or so): https://www.youtube.com/watch?v=3QUNIqaTJfI First let's start with what IS in that trailer, then I'll go over what has been added since then to make it better:
THE CORE DESTRUCTION SYSTEM
At the core, every structure in the game is made out of a bunch of boxes. This is pretty obvious, but I just wanted to state that it's not voxels. The box sizes are pretty limited in the values they use for thickness/size/etc, but that's just for visual (and physics) consistency. In game code, these are called blocks, so I'll keep referring to them as blocks here.
Structures are made up of anywhere from 10 to 500 blocks. Currently, there is a maximum of 2,500 blocks at any time. No island has more than 2,300 to start, because you generate more of them from destruction before the number starts to drop over time.
Each block tracks its own hitpoints, which is based on the material (5 materials) and size. The structure also has a list of "links", which means which blocks touch each other and the ground. When a block takes enough damage, it detaches from the structure.
Once a block detaches, it gets some of its HP back, and can then break into smaller blocks (like Asteroids) based on its size. If it's small enough and loses all it's HP, it breaks into debris pieces that have physics but no HP, and that go away after a certain amount of time, or when we max out on debris.
Note that debris collides with the player/structures/ground, but *not* blocks (or other debris).
There's an option to enable these debris collisions, but it slows down the game quite a bit with lots of destruction going on.
There are also particles that spawn when a block detaches/breaks/etc. Particles are one thing that changed significantly, so I'll come back to them, since all the old particles from before are gone.
Getting back to the structures: After a block or blocks detach, structures will check if they should split into more structures. After a short while, it'll also check if any of the "layers" of blocks is looking especially weak (low HP vs. mass supported). If so, it applies damage to that layer, and a bit of random damage to the layers below/above it, and does the same check again in a moment. 95% of the time this eventually leads to a collapse if it appears weak, but once in a while enough other stuff breaks off to lighten the load and it stabilizes.
There are some more minor details about how collisions are handled/etc, but that's the core system right there, and it hasn't changed since October.
IMPROVEMENTS SINCE OCTOBER
So now we come to everything that's been layered on top in the last 4 months. It's a lot, so I'm just going to make a list, and I will say: Getting this stuff below to all run relatively fast is not easy. Using compute shaders is difficult, because it's easy to break them and hard to tell what is broken. Let's start with one of the most important items:
1) A critical optimization for everything in this list: I use Graphics.DrawXXX calls (with compute buffers) instead of letting Unity draw things with MeshRenderers. GPUs like it when you send them a large chunk of data and tell them to render it all at once, so I do that with everything that I can (that means creating compute buffers with the right data in them). Some parts of the game still render normally (ground, vehicle, trees/tires/etc), but the majority of things are custom-rendered now.
2) Blocks/structures, debris, block-decals, and rebar all render using Graphics.DrawMeshInstancedIndirect. It is much faster rendering using this instead of letting Unity sort out thousands of meshes/objects. And all of these are GameObjects in Unity with physics, so GameObjects aren't the problem. This way is faster because I know exactly what should be instanced/drawn with the same shader, whereas Unity had to sort through thousands of MeshRenderers' materials and figure out how to group them.
3) Blocks and structures are the same thing in drawing, and draw with the same shader whether they're damaged or not. I went through TEN different methods for drawing blocks before I was happy with the results. Mostly the other results ran too slow, or didn't look good enough. There are two meshes of each material type (normal and damaged), and it switches betwen that mesh data per side (based on if was linked to something) while adding some noise for variation.
4) The debris all renders in one pass regardless of material. It's basically a bunch of boxes with some noise. Only blocks render with separate draw calls per material. Once you get smaller it's just some different color/specular/size parameters to make them look different. So glass debris is not transparent at all, it's just pretty shiny and gray.
5) NEW: The block-decals are a totally new system since October. They are basically mini-blocks that you build as part of the structure. They render essentially the same way as debris, but with no noise until they're damaged. They have no collision while on a structure, and use the same collision as debris when they break off. Basically, any thin/tiny block on a structure is superficial until it breaks off, then it's just fancy debris.
6) NEW: Rebar is used for concrete and steel, but the system is general and was used for other materials at one point. It uses the same type of unified-material as block-decals and debris. Rebar has no collision, and only shows up on damaged sides.
7) Ropes didn't change much visually, aside from getting Reflection Probes support, but under the hood the mesh generation is all compute-shader-driven now. Previously they were generated as Unity meshes. GPU code is just way faster, so ropes are much faster. [They were consistently one of the slowest things previously.]
8) Water isn't really any faster than before, but it's now all custom and compute-shader driven and draws to the horizon (or 2-3km, really). The mesh is also interactive now, with the ability to create splashes that ripple away. The only LOD-system is just making the triangles bigger and bigger as you get further from the origin. I may revisit the water sometime, and it's definitely an area where getting away from flat-shaded polygons may be a good idea. That could improve rendering speed, and I may use textures (which I hardly ever use in the game) to create better detail.
9) NEW: The GPU-driven grass system is one of the slowest new systems in the game, particularly on large islands, but it's also one of the most impactful in terms of visuals and game feel. It doesn't affect destruction, but it is affected by it. Grass is rendered in clumps of 3-5 blades, and each clump processes individually in a compute shader. Thanks to a custom z-buffer, the grass can be temporarily flattened by most objects in the game. There are also large impacts from the player, structures, and explosions that send a shockwave across the grass. You can also damage/darken the grass, though that also fades over time (like flattening).
10) NEW: Ground-rocks process entirely on the GPU, and render as simple distorted meshes. They get affected by impacts a little differently and don't use the z-buffer trick that grass does, but they are essentially just a simpler kind of grass.
11) The particle systems in the game are now entirely custom, and process on the GPU. They collide with the world through a custom z-buffer, and they are affected by explosions/etc through an attractor/repulsor function. There are two "point" particle types, dots and streaks. Aside from the rendering shader, they use the same code, but that will eventually change for optimization reasons. They currently use geometry shaders (turns a point into a small mesh), but that may change as well in the future. The dots were rendered as literal pixels for a while, and I liked that look, but it made for a lot of resolution- and world-scaling issues.
Both types are also affected by shadows thanks to some shadowmap code I found in another asset (see below) that I ended up not using. I actually wrote a custom shadow system that used raycasts around the player to shadow them at one point. It was slow and crude but pretty effective. The shadowmap version is just better in everyway, and the shadows help give them volume that wasn't present before. There are around 2 million total of these particles on High, but I'd like to push that higher in the future, and do more to prevent them from visibly popping out of existence in crazy destruction scenarios.
12) There are 5 types of particle meshes, with each one having the same compute shader (for now) but different rendering shader. The debris meshes are probably the most important, as they add a lot of thickness/depth to the destruction. Their z-buffer-powered collision isn't great, but having them bounce off stuff really helps sell the physicality of the world.
That covers the stuff that uses compute shaders or buffers in some way. But that's not all that changed. Here's a few more that helped the game look/feel better:
13) I switched to PBR shaders for almost everything. The biggest impact really came from using a reflection probe to make shiny things feel a lot better and less shiny things feel more grounded. I switched to using a single reflection probe for the entire world for speed reasons (after writing a system to generate a bunch of probes over the islands). Someday I should think about having a reflection probe centered on the player that updates in realtime to make the shiny stuff like wrecking balls look even better.
14) I turned on fog for everything. That helped give the world more depth. Had to update a lot of shaders, but that was relatively easy. Blending the sky/water/fog was a bit trickier but nothing crazy.
15) I wrote multiple depth of field/tilt-shift blur variants. I wanted a bit more control over how it worked with the game, so I spent a decent amount of time enabling, tuning, and optimizing depth of field. It's not even close to being film-quality, and it doesn't really interact with particles well, but it adds some nice realism without being overly expensive.
16) Each region (general island settings for how it looks) now updates a lot more stuff than previously, including the color grading.
17) More interactivity between systems/objects. This mostly means making things generate particles in more situations, or creating attractors/repulsors for particles. I wish I could say I made a general system for impacts/etc, but I didn't. It's all custom per object type, so vehicle parts, structures/blocks, and tires/containers all use separate code paths to generate impact effects/sounds/etc.
17a) More stuff creates camera shakes now, and I tweaked the shake system a decent amount (it was already pretty good before).
18) A more distinct/important example of #17: Debris pieces can create particle trails when they go flying. This is a combination of two systems that required a bit of specific code to work, and it was very important to the feel of destruction.
19) Blocks can now partially break off from collisions. One sub-block--the one furthest from the impact--will stay attached to the structure, while the other 3 blocks go flying. Previously this was not possible, and it helps running into structures feel better.
20) Large structures collapse better, particularly when hitting the ground. If a large structure impacts the ground, it'll now do damage to itself for all blocks that are near the same height as the point of impact.
21) There's a new delayed push after heavy impacts or explosions. Helps sends things flying better (this is very recent, so most footage doesn't show it).
I think that covers most of what changed since October (that affects destruction). I'm probably missing some other things, but the important parts:
* It was a LOT of changes/stuff that added up.
* I wasn't afraid to just throw things out or redo things if they weren't good enough.
STILL NOT DONE
Biggest visual thing that didn't make it in was volumetric effects. I don't use many assets, but I found a great asset while looking into volumetric fog (Volumetric Fog & Mist 2). In the end it didn't interact well with the depth of field or transparent particles, and I might have wanted to create a volumetric smoke/dust system (based on a grid, like most high-end smoke effects) if I switched to using it.
The shadowed fog looked amazing though. I quickly realized it was using the exact method I had wanted to use with my particles (but couldn't figure out the math/functions), so I just adapted that for my purposes and the particles were even better than before. I'm not planning on adding volumetric effects to the game, but there's still more to be improved...
One part of destruction that still really bothers me: Debris pieces from destroyed blocks will often go flying up faster than they should (this predates #21). I've tweaked it a bit, but it's still fairly common for a block to hit the ground and see debris go flying up far faster than you'd expect. Will hopefully be able to tame this soon.
Optimization of the particle systems is something I want to do sooner than later. More particles would mean the effects could scale down less in crazy situations, and pop away far less frequently. I might be able to make it render faster when there are fewer particles.
THE PHYSICS SYSTEM
You'll notice I've barely mentioned the underlying physics system during this. I don't mean to downplay it, but most physics systems are pretty similar in speed/functionality, and don't really affect destruction much. I've been using Unity 2019, PhysX, and the built-in renderer. No cutting-edge stuff like ECS or Jobs or URP/HDRP. The game runs physics at 120hz by default, but you can lower it to 60hz if you need to. Some elements aren't quite as stable/solid-feeling at 60hz but it still works.
As far as what was hard about the physics from a code/design side, most of it has had to do with the vehicle building (getting everything to combine correctly), ropes, and treads. None of that is destruction. I've done some tweaking of global settings to optimize stuff, and I'm always tweaking numbers to make things feel better. Destruction and physics work pretty well together by default, and especially so if you're willing to supplement it with extra tweaks to make it feel better.
For reference, Red Faction Guerrilla (which I worked on for 5 years) used Havok and Teardown is all custom (I had nothing to do with it, but everyone knows it now). The underlying physics system just isn't that important to destruction. It's just a bunch of math functions that should work in a certain way. It's what you put on top of it that matters.
LOTS OF LAYERS
I don't really care about new tech or doing things in a fancy way (I especially hate fancy code). I use what works, and GPUs work really well. I figured out how to use them better than I had before, which let me do more stuff, and I just kept doing more stuff.
Nothing about the destruction system is particularly fancy. It just has a lot of layers that run faster than usual and have been tweaked to work well together.
[If you have any questions, please post publicly I'll answer them below.]
