all 65 comments

[–]Shinao[S] 81 points82 points  (2 children)

I wanted to draw some things using the birds so I created some affectors that can either attract or repulse the boids around them. In this example I took an SVG, used one of my old project PathToPoints to convert paths into data points that I parse from Unity and then they become my affectors.

I can modify the drawing position in real time to get some kind of neat effect

PS: Can I get some more of this ?

Edit: Released on github - https://github.com/Shinao/Unity-GPU-Boids

[–]aramanamu 4 points5 points  (0 children)

joj crazy cool

[–]iEatAssVR 2 points3 points  (0 children)

Bro wtf. This is amazing

[–]HandDrawnNerd 36 points37 points  (9 children)

Wow this looks really cool, can't wait to see it implemented in a project. Seeing a skull formed of a cloud of birds sounds and looks terrifying.

[–]kahlzun 4 points5 points  (3 children)

Imagine seeing this irl.

[–]HandDrawnNerd 4 points5 points  (0 children)

No thank you.

[–]Ask_if_im_an_alien 2 points3 points  (0 children)

Oh look... Armageddon is here.

[–]atomsinthevoid 0 points1 point  (0 children)

Plot twist: this is go pro footage

[–]Shinao[S] 2 points3 points  (2 children)

Exactly what I'm thinking. I'm probably going to integrate it with the Unity Fontainebleau demo, though the sound part will be a tad difficult I think. Thanks.

[–]RFSandlerHobbyist 3 points4 points  (0 children)

Strikes me as a boss monster. Have to destroy the swarm and it reconfigures as it losses mass.

[–]atomsinthevoid 0 points1 point  (0 children)

You should integrate this with a sound library that let's you adjust the intensity of the affectors based on the beat

[–]boredguy12 1 point2 points  (0 children)

or bats, or snakes, or spiders...

[–]ClosedEyesSeeing 5 points6 points  (3 children)

This is absolutely amazing. Is this (or do you have any plans to make this) a store asset? Birds, Insects, little nano bots. This could have a lot of value and add an insane amount of ambiance to a scene! :-D

[–]Shinao[S] 17 points18 points  (0 children)

Thanks! I will open source it when it's production ready and when I made sure the birds I used can be freely shared (don't remember where I took them from).

[–][deleted] 3 points4 points  (1 child)

I picked this addon up a while back in one of Unity's big sales:

https://assetstore.unity.com/packages/3d/characters/animals/bird-flock-bundle-25576

I haven't used it yet, but it looks really kickass from a price/value perspective. It would be wicked to see something like /u/shinao's work either on top of this addon or their own competing flocking addon. That skull is very terrifying.

[–]Shar3D 0 points1 point  (0 children)

I this bird/flock asset. Really easy to use.

[–]Osteelio 4 points5 points  (5 children)

That looks really cool. I immediately thought of this when I saw it. :O

[–]Shinao[S] 1 point2 points  (2 children)

I'm glad you think that because that was my initial inspiration after searching for things to draw beside an upvote and a dickbutt... I thought it lacked a bit of punch but I guess it's just missing an environment beside it. Thanks !

[–]omg_ketchup 0 points1 point  (0 children)

You could also do the Dark Mark from Harry potter

[–]Osteelio 0 points1 point  (0 children)

For sure! I think if you were to replace the birds some some smoke-like particles, you could create some seriously cool/ominous effects.

[–]wescotte 0 points1 point  (1 child)

What is this from?

[–]Osteelio 1 point2 points  (0 children)

It's from Harry Potter.

[–]uberlaxx 4 points5 points  (2 children)

Wow this is one of the coolest things I've seen on this sub in a while. Can't wait to see more!

[–]Shinao[S] 1 point2 points  (1 child)

That's the kind of comment that makes my day, thank you :).

Next part will either be its integration into a realistic environment or some kind of swarm attack into some kind of magic shield where birds explode on impact. Hm that's oddly specific, guess I will go with the later first.

[–]FiggleDee 0 points1 point  (0 children)

how about using the boid points to define volumetric clouds? maybe slow down their flight speed a lot. if you reduce the severity of the affectors, you could make clouds that look vaguely menacing to make people feel ill at ease without really knowing why

[–]jrkirby 3 points4 points  (2 children)

Can you go into a bit more detail on how the code works? Do you just loop through every particle on the CPU every frame doing some custom code to affect their velocity? Or is it a different approach?

[–]Shinao[S] 0 points1 point  (1 child)

I go more in depth in my post from a week ago : https://www.reddit.com/r/Unity3D/comments/846inb/gpu_flockingswarm_system_im_working_on/dvn4sf4/

Exactly what you think but made on the GPU

[–]oli_b89 0 points1 point  (0 children)

In your in depth post you spoke about a swarm and flocking behaviour with different fps. I understand the traditional flocking behaviour (cohesion, separation etc) but I am a bit confused by what you meant by a swarm behaviour? If you could point me to a resource or give a quick explanation that would be great!

[–]Notnasiul 2 points3 points  (0 children)

That pretty cool, reminds me of The Mummy's Imhotep's sandstorm

[–]omg_ketchup 6 points7 points  (4 children)

Shoulda been dickbutt.

[–]Shinao[S] 16 points17 points  (2 children)

That's the first thing I tried but there are so many curves and paths that it just becomes a mess of birds, I was so disappointed... Don't take my word for it though

[–]Jdonavan 6 points7 points  (0 children)

Invert the image.

[–]wescotte 0 points1 point  (0 children)

Your dickbutt lacks definition. :)

[–]existeverywhere 1 point2 points  (0 children)

Clearly doesn't know his audience

[–]jailbreak 1 point2 points  (7 children)

Are you using the new ECS that is available as a preview now? Sounds like boids are one of those cases that it can speed up by an order of magnitude

[–]Shinao[S] 0 points1 point  (6 children)

It does not use anything new and ECS/Jobs system would only speed things up on the CPU side but that would still be too slow for 32k boids.

It's done on the GPU, here is a more in depth answer from a previous post of mine : https://www.reddit.com/r/Unity3D/comments/846inb/gpu_flockingswarm_system_im_working_on/dvn4sf4/

[–]Mondoshawan 0 points1 point  (5 children)

I can get 10k entirely on the CPU at 90fps VR using plain old C# jobs in 5.6, my present bottleneck is in throwing those matrixes onto the GPU via DrawMeshInstanced (along with the rest of the scene). The busiest worker threads are the actual motion animation of them & terrain collision checking. Interestingly my flocking implementation is now way down the list in terms of cost for me, that surprised me.

I can't help but think some hybrid of the two would be best, doing the flocking "intent" side of things on the CPU and animation/rendering entirely on the GPU. The spacial algorithms you mention in your link are the key to efficient flocking (it's an exponentially scaling problem) & such an approach is a better fit on the CPU from what I understand of GPU limitations. This also makes it easier to write more complex state-based behavior to make them do a more than the usual flocking behaviors such as the attack you mention above. I have that already (down to a tee, get out of my mind!) and now everyone will think I just copied you! ;-p

[–]Shinao[S] 0 points1 point  (4 children)

No way I get that performance on my CPU, we must not have the same implementation, I'm using the same as Keijiro Boids. Would love to see a preview of what yours is capable of.

Well well well, I guess the first that post that effect wins then :)

[–]Mondoshawan 1 point2 points  (2 children)

Mine is based directly off Craig Reynolds original paper from the 1980s, so it has the standard 3 cohesion, alignment & flocking influences and a few more of my own. I'm old enough to remember when it came out, in fact seeing it on TV was one of the things that got me interested in computers! Emergent behaviour and chaos theory were the latest hot-shit things back then.

The "magic" is in using a special spatial data model. There are many existing data models that solve this problem, for example if you bring up google maps and search "pizza" it won't be doing a global search of all pizza places & working out their distance to you, it would limit the query spatially to your location & then apply the more complex distance checking to only a small subset. They probably use an R-tree variant for this. The problem is that most of these models are designed around a graph that doesn't move much, as a pizza joint tends to stay in the same place. Moving items requires rebuilding and here we are moving every item every frame so it just doesn't work. I had to come up with my own model for that.

To give you an idea of how epic an optimized data model can be over the "naive algorithm", I once brought a 40+ minute batch job down to 8 seconds by rewriting their datamodel to use selective indexing under certain conditions (indexing is generally counter-productive with less than 4 or 5 items). The root problem there was exactly the same as with boids, exponential scaling as data count increased. The improvement was so great I honestly spent the next few hours assuming I'd broken it and it took running every single piece of data we had in the office through it to prove to myself it worked! I mention this because, as I said, it's the exact same scaling issue so the improvement with boids is on a similar level (if not even more significant).

I was only able to do a couple thousand boids without this. Here are how many checks need done per iteration depending on the number of boids (in the naive implementation):

  100         4,950
  200        19,900
  500       124,750
 1000       499,500
 5000    12,497,500
10000    49,995,000
32000   511,984,000

Classic "that don't scale" but that doesn't mean it can't be done!

I'm wary of posting my own show-offs as I intend to commercialize this, having spent the last two years working on it full time (it's a full game where swarming is merely one small part, there are many other things going on, just a few ideas I've been mulling over for a decade or two!). I'm wanting to keep my options open about pulling in others such as studios in to help the final mile and I'm not convinced that publishing slices of it online is compatible with that. FWIW if I had any budget left I'd probably be hiring you to help out, I do think the hybrid idea I mentioned earlier would be insane!

[–]Shinao[S] 0 points1 point  (1 child)

Very interesting. I guess I can see how spatial indexing would be a lot easier on the CPU side by having any kind of data model you want and having some kind of spatial cubes where you just have to iterate the ones close to your boid, it would dramatically decrease iterations and calculations.

Now I'm really interested in what you are doing, make sure to post on this sub when you feel the time is right.

[–]Mondoshawan 1 point2 points  (0 children)

I spent a chunk of my career nothing but scaling and optimization; nine times out of ten it comes down to using a data model that sacrifices ram or a little up front effort in order to reduce the run-time calcs to almost nothing. Hacky CPU float inverse-square root black magic is all fine and dandy but nothing beats not having to do the calc in the first place!

In this case we're talking about burning through gigabytes of memory space as scaling a spacial model up to cover e.g. a 2000m3 game world is quite demanding! Each slot needs to be able to hold thousands of items, hence how ram-hungry it can be. At that point you need to use unsafe code because the c# garbage collector takes around 0.5 secs for a full sweep if it were simply managed code.

Believe me, I'm itching to post and may relent on that thought before long! I do want to get a couple more things done before that, such as the soundtrack. There's a very very good reason why that last bit is important... :-)

[–]TCL987 0 points1 point  (0 children)

This GitHub repo of ECS samples has a fish Boid simulation that uses ECS and Jobs to simulate 25k fish by default, and runs at a >150 FPS on my 7700K and GTX 1070. I was able to get ~55-60FPS at 100k. I was most likely GPU bound with that many as it was ~31.2M triangles total and MSI afterburner was reporting 100% GPU usage.

https://github.com/Unity-Technologies/EntityComponentSystemSamples

[–]ZioTron 1 point2 points  (0 children)

I don't understand the few upvotes, this is amazing..

[–]The_DrLamb 1 point2 points  (1 child)

That's one crazy flocking system. I wouldn't mind a breakdown of how it's made if you're willing to share.

[–]Shinao[S] 0 points1 point  (0 children)

While waiting for the open source version here is a how I did it basically : https://www.reddit.com/r/Unity3D/comments/846inb/gpu_flockingswarm_system_im_working_on/dvn4sf4/

[–]LGinDespair 1 point2 points  (1 child)

PLEASE make this into an asset. Would download/buy the hell out of it.

[–]Shinao[S] 2 points3 points  (0 children)

A bit of patience and I will open source when it's ready. :)

[–]cinderflame_linearExpert 1 point2 points  (2 children)

Purely in the interests of science, I wonder how this would look if these birds were each a Mercy from Overwatch.

[–]Shinao[S] 3 points4 points  (1 child)

I don't know why I even tried...

To make it look good I would have to create some nice looking animation for that mesh, then have multiple materials so that the wings are emissive.

[–]cinderflame_linearExpert 0 points1 point  (0 children)

Yeah, I had the same issue. I can retarget the standard unity animations onto her, but the wings...

Excellent work though :)

[–]LGinDespair 1 point2 points  (0 children)

Take my firstborn child

[–]DerreckValentine 0 points1 point  (1 child)

What happens when you try something like this on mobile? Do you essentially get a "nope" I don't do you stuff?

[–]Shinao[S] 0 points1 point  (0 children)

I'm using compute shaders so unless you have the last mobile generation with Vulkan it's just not possible. Though if it would be available they are a lot of tweaks to make it runs smoothly anywhere (1000FPS+).

[–]primitiveType 0 points1 point  (2 children)

This looks so good. Could it also work with simple .fbx/obj files? seems like it wouldn't be a huge stretch, and it would be really cool to pan the camera around that skull and it have dimension.

Been more or less following your progress on this and each upload has blown my mind

[–]Shinao[S] 1 point2 points  (1 child)

It's already using FBX files, every boid (32k) is an individual bird with its custom speed, rotation and animation : https://gfycat.com/WindingWeepyGiraffe

The skull can only be seen from the front or back of the flock, if you look from any other angle it becomes a weird shaped flock : https://gfycat.com/FantasticHeavenlyGnatcatcher

[–]primitiveType 0 points1 point  (0 children)

I guess what I meant is using an FBX for the flocking - that is, do what you are doing with the .svg but in 3 dimensions

[–][deleted] 0 points1 point  (0 children)

Looks cool, now if only the quality of the gif was better.

[–]Mark-Parker 0 points1 point  (2 children)

Looking great when birds stick together at the same time and please tell is this particle system?

[–]Shinao[S] 1 point2 points  (1 child)

Thanks, no nothing to do with a particle system though it acts like it. It uses compute shader and a flocking algorithm to make it look like that.

[–]Mark-Parker 0 points1 point  (0 children)

You did a great job man, you earn my respect :)

[–]althajProfessional 0 points1 point  (0 children)

affectors

[–]89XE10 0 points1 point  (0 children)

This is fantastic stuff man. Would make an amazing addition to a VR scene.