Finding 'minimum' and 'maximum' position of particles (or finding the bounding box using a compute shader)

Hey there!

The title is confusing and this is probably another fuse-newbie question :D But I hope that I can explain my problem more in-depth here and that it helps other people with other related problems (like finding an average position of their particles).

In my main patch I have a particle system with around 1M particles which arrange in somewhat unpredictable ways. My current idea was to get a bounding box of the particles so I can then automatically position a camera to capture all of them reliably.

I have attached a minimum working example down below of my current approach. This just scatters them in a cube and moves them around a bit. The whole camera thingy doesn’t really apply here (but it does in the main patch)

Right now, the patch does this:

  • Read back a fraction of the particle positions to the cpu
  • Loop over them and accumulate min and max positions (they need to be initialized with fairly high values outside of the actual bounds (100,100,100 in this case). If all particles are above 0,0,0 this function would return that instead of the actual particle minimum for example)

This of course has a few drawbacks:

  • it runs on the cpu and is slow as hell with everything else going on in the main patch
  • I can only use a fraction of the total particles before it starts lagging. This isn’t that bad if you don’t need 100% accurate results. It might also be okay to do this asynchronously in case you don’t need to update the bounding box for every single frame.

My preferred solution would ofc be to do this stuff on the GPU. I just don’t have a clue on how to do that. Everything I have tried thus far has resulted in the program crashing :D

Maybe there is a smarter way of doing this entirely. Maybe by updating a min and max position variable during the simulation stage instead of finding it afterwards. Heck, there might even be an obscure node to do this directly.

MinMaxPosition.vl (53.3 KB)

Feel free to ask if you have any questions about what I’m trying to do.

And thank you so much in advance!

Just a short hint, you should ONLY use the ReadBack[Type] nodes for debugging because they will block the main loop until the data is downloaded to the CPU. In that time no other GPU work can be done, so they are very wasteful in terms of resources.

The better node here is ReadBackBuffer with a certain frame delay that downloads the buffer asynchronous from the main loop. It has an output that will tell you whether the main loop was blocked. You will have data from a few frames before, but that should not be an issue in your specific case.

1 Like

Thanks for the hint!

Is there a way to read back a single value (one that is on the GPU but not in a buffer)?

Because if I had some way to get the minimum and maximum positions on the gpu I would still have to read those single vectors back to then use them to control the camera, right?