I woke up today and decided it was finally time to tackle the metaballs implementation in my project. And by the grace of God, when I reopened Dario Zubovic’s metaballs implementation, I realized it had everything I needed already. It’s funny how my perception of that repo has changed over time. I went from being convinced it was useless to me, to discovering it had a compute shader and figuring I could use parts of it, to finally realizing that it used its compute shader to write back data to a mesh, which is exactly what I’d hoped to do. It’s funny how knowledge adds perspective.

I imported the example scene from this fork into my project. I had to play around with it for a while, but after removing lights from the scene, I got FPS in the stats window all the way back up to 300. Still, Unity warned of several memory leaks, all related to initializing the different compute buffers. I attempted fixing them to no avail. This stuff is still mostly outside my realm of know-how, but that doesn’t stop me from hitting buttons and hoping stuff gets fixed.

For a bonus optimization challenge, I tried matching the dispatch instructions in the compute shader to the one from this fork, which uses a vertex shader and seems to be the most neatly written of the bunch. It ended up allowing me to crank up the boundary size of the metaballs to a 3 by 3 cube without losing FPS. Cool!

After enough fumbling with borrowed code, I decided it was time to test converting the metaballs mesh to an SDF. I added a BakeSDF() function to the metaballs script and gave it a go. The results were not pretty, no matter where I inserted BakeSDF() in the code.

The mesh data was clearly not translating properly to my VFX graph, so I decided that I needed a way to save a snapshot of the generating mesh as an asset, and then send that asset to the SDF Bake Tool for testing. I quickly found a way. This script is great because when I choose to save the mesh, the script updates the saved asset in real time.

This testing endeavor revealed the source of all my struggles. Unfortunately, the preview shown in the Bake Tool does not match the actual SDF output, and therefore, my graph gets fucked.


Tags: unity metaballs shaders vfx sdf optimization debugging