r/Unity3D • Programmer • Feb 20 '18

Show-Off Real-time recursive destruction effect

https://www.youtube.com/watch?v=f3T5jtsokz8
92 Upvotes

22 comments sorted by

19

u/hi_im_new_to_this Programmer Feb 20 '18

Hi! I was interested in implementing a Delaunay triangulation/Voronoi diagram algorithm, so by law I also have to make a destruction effect to accompany it :) I wanted this to be "real time", in the sense that the fracturing isn't pregenerated, it's generated at the time of impact. That means you can make fracture patterns that take into account where the thing broke, so that the smaller shards are concentrated around where the surface was hit.

Another thing I wanted to do was to fracture it "recursively", meaning that if you hit generated shards, they will further fracture into smaller pieces. Which I think looks kinda neat! I was going to go further with this, but Unity's physics engine basically freaks the fuck out with this many small shards. I mostly was just interested in the Delaunay/Voronoi part, and messing with Unity's physics engine is a bummer, so that part of the code is a bit messy.

More details and source code can be found on GitHub. Fun fact: when doing this project, I discovered that virtually every description and implementation of the Bowyer-Watson algorithm for generating Delaunay triangulations that are available online is wrong! (for more details, see the github write-up). Everything is MIT licensed, so you can use it for whatever (though if you do use it for something, let me know! credit would be nice, but is not required)

If you have any more questions, I'll answer as best I can.

4

u/Hightree Feb 20 '18

Looks awesome, thanks for sharing!

2

u/[deleted] Feb 20 '18

Love. That is all. Also Thanks.

5

u/[deleted] Feb 20 '18

Thanks, this looks awesome. How is performance for this?

4

u/hi_im_new_to_this Programmer Feb 21 '18 edited Feb 21 '18

It's pretty good! Generating the Voronoi diagrams and meshes take no time at all, easily real-time (I think around 1 ms last time I checked, but it was a while ago (edit: just checked again, it's well below 1 ms to generate the voronoi diagrams)). There's also rooms for optimizations, like generating the meshes and voronoi diagrams on background threads. That would mean that the main thread would spend basically no time at all on generating the shards.

The runtime is dominated by Unity's built in physics engine. There's a lot of tiny shards flying around, and they can destroy the framerate. Notice that in this demo, they fall down into the abyss: if you put a floor underneath them, they start piling up and the framerate drops significantly. If you then fire a ball into the pile, you basically get 3 frames per second or something, though it's fun to see the shards flying everywhere!

2

u/skedra Feb 21 '18

An easy way to improve that is to add a threshold of volume of the bounding box after which you either turn the collision detection off / change it to tiny box or sphere colliders / remove the collision between the shards themselves. If they are tiny anyway you'd hardly be able to tell anyway :)

Amazing work though, gonna look into making this multithreaded since I actually want to make a prototype with this :)

1

u/hi_im_new_to_this Programmer Feb 21 '18

Let me know if you make something with it, I'd be curious to see it :)

To be clear: the generators aren't thread-safe, but if you put them on background threads, they will do their calculation on that thread without touching any engine parts or anything like that.

2

u/skedra Feb 21 '18

Will do, if I manage to get a good threaded implementation I'll do a pull request :)

2

u/VID44R Programmer Feb 21 '18

You could replace pos.magnitude with pos.sqrMagnitude in BreakableSurface.FixedUpdate() and see if you could shave off some ms that way.

1

u/MrKnowID Feb 21 '18

You could link an object pool of rigid bodies that can get attached to the destruction and after some condition just the rigidbody returns to the pool. I've done that with some destruction effects before and it did nicely. You can even link effects or transitions to the return to pool call for the object. I'll think I'll try that this weekend if I have the time haha. But great work!

2

u/_kajta Professional, Programmer, VR Feb 21 '18

Looks good, do you have any numbers on ms/garbage by just generating the Delaunay triangulation?

4

u/hi_im_new_to_this Programmer Feb 21 '18

No garbage! There are three "generator" classes (one for generating a Delaunay triangulation, one for converting a Delaunay triangulation to a Voronoi diagram, and one for clipping a Voronoi diagram to a specific polygon). All of these generator classes reuse their internal buffers they need to run their algorithms (a bunch of List<T>'s, basically). There are also "result" classes that return the results of the computation, and these can be reused as well.

In my demo, I don't actually do this (because it's just a quick demo) so I just create the generators anew every time. But it would be easy enough to make the generators/result classes persistent, and then it wouldn't generate any new garbage (obviously except for when the internal List<T>'s have to resize themselves, which should be very rarely).

I should say: I haven't quite put these classes through their paces enough to guarantee this, but I'm fairly certain that that is the case. If you find that they generate garbage somewhere, please let me know about it.

I just checked the time taken to generate voronoi diagrams is something like 0.25 milliseconds with assertions turned on (these are fairly small diagrams, all things considered). I could probably improve that with some things in the code, but I think this is good enough. If you want to erase that cost entirely, you could do the calculation and mesh generation on a background thread. Obviously new game objects, rigidbodies and colliders for all the shards also add a cost, but it's small enough to keep it real time.

2

u/_kajta Professional, Programmer, VR Feb 21 '18

That is quiet impressive! I'm planning on integrating it into a Mesh Slice asset of mine, and with these performance results I have to at least give it a try ! :D

1

u/volfin Feb 21 '18

Really awesome! though when I saw the "// TODO: add UVs" part I was sad. If that was in there, it would be even nicer. I may try to implement UVs, even though I have absolutely no idea how I'd do that.

I was curios, does the code purposely support parts that are still near the "holders", or is it just chance/friction?

3

u/hi_im_new_to_this Programmer Feb 21 '18

They're just static colliders holding up the things with friction :) Probably wouldn't do that in a real game, but it worked nice for the demo.

Implementing UVs should not be particularly hard, you could probably just use the coordinates that you get from the Voronoi diagram directly. You'd probably have to split the vertices that make up the surface of the shards from the edges, and you'd have to figure out a strategy for how the edges should be UV mapped. If you're wondering how to do that from the API perspective, you just assign the uv component of the Mesh to an array. You can read up on "procedural meshes" if you're wondering how that whole thing works.

I did something like that a while back, where I pregenerated the shards with a proper UV map. Here's a demo of that effect and source code. For that one I downloaded someone elses Voronoi library, this time I made my own :)

1

u/volfin Feb 22 '18

ok, cool, thanks :)

2

u/master50 Feb 20 '18

Wow, this is really cool. Reminds me a lot of Smash Hit!

1

u/mradfo21 Feb 21 '18

dear god SOMEONE needs to make this for unity.

6

u/Warcrea @stillnotsuredev Feb 21 '18

This made me check I was on the right subreddit... didn't he just do that?

1

u/mradfo21 Feb 21 '18

Oh the internet. God why am I on here.

1

u/phero_constructs Indie Feb 21 '18

Impressive and definitely relevant to my interests!

1

u/ScattershotShow Feb 23 '18

Fantastic job! This would work wonders for dynamic window breaking.