In order to get more familiar with Compute Shaders in Unity, I made a BOID simulation using the many threads of a Compute Shader in Unity. BOIDs are agents with flocking behavior and can appear like a swarm of fish or birds. My first simulation could handle 2^14 BOIDs with decent performance: a number impossible to achieve on the CPU alone.
The BOIDs work by following a few simple rules. They avoid nearby BOIDs, move toward the center of the flock, and try to match velocities with other nearby BOIDs. My first simulation also included a box that they would avoid and get teleported to the other side should they cross it. I also ran this simulation in debug mode so I could have gotten better performance.
I made a special BOID that was marked so that it would behave differently and the others would treat it differently. My idea was a “Smelly BOID” that the others would avoid much more strongly, but it would feel lonely and muscle to the swarm's center. To make it more distinguished, I made it huge and green with the old high-poly mesh. Thanks to some optimizations I made, I was also able to have twice as many BOIDs.
In my second simulation, I made some changes so they would run a little quicker, short-circuiting and avoiding costly square roots whenever possible. The mesh was reduced to a triangular pyramid, substantially reducing the number of triangles. I also got rid of the teleporting to the other side because that was upsetting the swarm in a weird and seemingly unnatural way.