Hacker News new | ask | show | jobs
by timtimmy 2272 days ago
Boids are my favourite hello world for 3D simulation environments. For the last few weeks, I've been using boids to update my GPU programming skills and knowledge of related algorithms in Unity and Unreal.

Yesterday I got 0.5 million boids simulating in Unreal Engine at 45 fps on a GTX 1080 [0]. The bottle neck is rendering so many instanced cone meshes, the simulation without rendering can do 1 million boids at 120 fps. I'm borrowing a common data structure used in GPU fluid simulations, the dynamic hashed grid, to calculate the neighbourhood of each boid (cool tutorial here with full source code [1]). We need the neighbourhoods to calculate their urges. My code is a mess at the moment, but if there is interest I can post it to GitHub (Unity and Unreal).

Boids are an example of agent-based modelling and swarm systems. I used agent-based modelling in my PhD thesis to construct and simulate realtime, interactive 3D biomolecular models of mitochondria and neuromuscular synapses [2,3]. Now that I'm free (graduated), I'm building an iPad app to teach and communicate structural and molecular biology with immersive, and realtime simulations. Our vision is to make molecular and structural biology immersive and fun. It's not a textbook, it's a little more like Neil Stephenson's young lady's illustrated primer! :D

[0]: https://twitter.com/timd_ca/status/1243941167005192192?s=20

[1]: https://wickedengine.net/2018/05/21/scalabe-gpu-fluid-simula...

[2]: https://prism.ucalgary.ca/handle/1880/111493

[3]: https://youtu.be/6CsxADBpal0

7 comments

It wasn't Unreal Engine that gave you that perf. It was your GTX 1080. Unreal Engine (or any Engine) in this situation is doing next to nothing.

Sorry it's just this kind of thing pushed my buttons. I see someone showing their engine and claiming perf when their example is 99.999% GPU and 0.001% anything to do with their engine.

Sorry, I didn't mean to mislead you. This has little to do with Unreal (or Unity) and everything to do with GPU programming.
Here is a thematically-related boids simulation from 2006: https://www.red3d.com/cwr/papers/2006/PSCrowdSandbox2006.pdf

Did 10,000 to 20,000 agents at 60 fps. Puny compared to yours, but not bad for 14 years ago! https://www.youtube.com/watch?v=Jpy5rJSVLj8 https://www.youtube.com/watch?v=ZZoy-8xK0YI

Wow. There are some great ideas in that paper.
> https://wickedengine.net/2018/05/21/scalabe-gpu-fluid-simula...

It's unrelated to boids, but I would not recommend using the Poly6 and Spiky kernels. That design originates from "Particle-based fluid simulation for interactive applications" (2003) by Müller et al., and they choose the spiky kernel on the assumption that a zero gradient at the origin is the reason why particles sometimes get stuck together. However, the clumping phenomenon was more closely studied by Dehnen and Aly in "Improving convergence in smoothed particle hydrodynamics simulations without pairing instability" (2012) [https://doi.org/10.1111/j.1365-2966.2012.21439.x]. They showed that you can predict pairing instability based on neighbour count, and that kernel functions that are everywhere positive within the Fourier domain do not exhibit that instability.

The Wendland family of kernel functions are thus recommended, as they fulfill that criteria. Though, for low neighbour counts a quartic B-spline is a good alternative.

Also, congrats on escaping--err... graduating!

Thank you for that Firehose of Fun! Especially the GPU dynamic hashed grid, which looks super useful.

Amazing work! LifeBrush is Emacs for Cytoplasm! I've read that those little thing-a-ma-bobs in your cells move around at something like 30 miles an hour, bumping into each other. Is that accurate? Does that simulation show them slowed down a lot? Would it just be a blur in real time? Is that software available, please?

You are right, they move insanely fast. The average speed of a medium-sized protein is about 5 m/s. Inside an E. coli, any two proteins will encounter each other at least once a second [0] (this book is incredible).

My simulation is inaccurate in terms of Brownian motion. It is slowed down and smoothed for illustrative purposes. It should be more violent. Nice idea with blurring, I'll think about that. The code is on GitHub. It requires a VR headset, and it is frankly a little clunky and "research code" in terms of the UI. But check it out :D [1]. The next version will support larger simulations on the GPU and a more friendly UI. I have friends at the Scripps institute who work on similar stuff, check them out [2].

[0]: David Goodsell (2009) The Machinery of Life https://www.amazon.ca/Machinery-Life-David-S-Goodsell/dp/038...

[1]: https://github.com/timdecode/LifeBrush

[2]: https://ccsb.scripps.edu/cellpaint/

Thank you for publishing the code as open source! I'll check it out. Do you have any idea how much work it would be to adapt it to Unity3D's ECS (DOTS: Data-Oriented Technology Stack) and shader system, or somehow get it running on WebGL? At least the simulation and rendering part, if not (yet) the VR stuff, so it can reach the widest audience.

https://unity.com/dots

I really love the part in your interactive simulation overview, at 2:58:

LifeBrush - An overview of our illustrative simulation canvas

https://www.youtube.com/watch?v=5LBHRyGxLGI

2:58> The ribosome uses tRNA to match mRNA codons to amino acids. We paint a cloud of tRNA near the ribosome. tRNA, molecular agents, need amino acids. So we paint a cloud of of amino acids nearby. Brownian motion brings tRNA and ribosomes together. But that takes a long time. However, our system is interactive and immersive. So we grab the ribosome and move it around. Now we see how the ribosome marches along the mRNA, with tRNA depositing amino acids onto a growing poly-peptide chain in blue. We reach the end of the mRNA, and the mRNA and poly-peptide chains are released.

So in order to avoid waiting for brownian motion to do its thing over time, you just grabbed the ribosome's lazy ass and dragged him over near the mRNA, then threaded it into his jaws, and waved him around through a cloud of amino acids and tRNA to feed him, so he sucks up the tRNA pasta and mRNA meatballs and amino acid sauce, just like Lady and the Tramp eating spaghetti to Bella Notte, and then he pumps out useful proteins, just like the Golden Goose.

Lady and the Tramp (1955) Scene: 'Bella Notte'

https://www.youtube.com/watch?v=XNQWbZ9sO3Q

The immersive interactivity is (in a good way) like the Libertarian wet dream wish-fulfillment of the invisible hand of unobservable market forces, or the Intelligent Design pseudo-scientific fantasy of God directly intervening in the affairs of molecules! Ribosomes could be the next Tamagotchi! ;) Ribosomegotchi?

You obviously know this stuff well, but I want to mention that your videos "LifeBrush: Discrete element texture synthesis overview" and "LifeBrush: Bunny-planet design session" using Greg Turk's Stanford bunny impress me as an homage to his work on mapping bunnies with 2d textures, and Andy Witkin's work on reaction-diffusion textures, and also looks inspired by the Photoshop smart clone tool.

LifeBrush: Discrete element texture synthesis overview

https://www.youtube.com/watch?v=624F5iaCKco

LifeBrush: Bunny-planet design session

https://www.youtube.com/watch?v=L482dpH9S8o

Greg Turk

https://en.wikipedia.org/wiki/Greg_Turk

Stanford Bunny

https://en.wikipedia.org/wiki/Stanford_bunny

Andy Witkin

https://en.wikipedia.org/wiki/Andrew_Witkin

Andy Witkin's gallery

https://www.cs.cmu.edu/~aw/gallery.html

Reaction Diffusion Textures. (animation) 1991. Nonlinear partial differential equations as a means of artistic expression. Shown at SIGGRAPH '91 electronic theater. mpeg (209K).

https://www.cs.cmu.edu/~aw/mpg/rd.mpg

Reaction Diffusion Textures. 1991, with Michael Kass. Nonlinear partial differential equations as a means of artistic expression. This image won the Prix Ars Electronica '92 grand prix for computer graphics. postscript. pdf. citation.

https://www.cs.cmu.edu/~aw/pdf/texture.pdf

Using Particles to Sample and Control Implicit Surfaces. 1994, with Paul Heckbert. Quasi-physical particles that float on an implicit surface. We use mutual repulsion and fissioning to quickly obtain a good sampling of the surface, and to maintain it as the surface deforms. postscript. pdf. citation. mpeg(23K). BIG mpeg (868K.)

https://www.cs.cmu.edu/~aw/pdf/particles-reprint.pdf

Impressive! Mine linked here starts having trouble at around 2000 units on my CPU. Shows how ridiculously parallelisable this is.
I love the interactivity in your workshop sandbox. Is the bottleneck your Boid.findNeighbours function? (that was the first thing I tried on the GPU, except GPU parallelism let's us get away with 50,000 boids).
Yup, it's the neighbour function being the problem. And of course updating even more boids, but that just scales linearly, but its noticeable when decreasing the neighbour radius which gives each boid less other boids to adjust to.

My neighbourhood implementation is the naive n^2 loop, so it's probably trivial to make it better. Quadtrees, or even just simple caching / zoning would probably allow me to dish out many more boids.

It also doesn't help that in order to be easy as a workshop, it's written in a way that's easy to implement, but slow to execute.

Have you thought of making this into an educational game like Factorio or Satisfactory for life instead of machines?

https://www.youtube.com/watch?v=Oh2oF-eZTD8&list=PLrBjj4brdI...

That's a cool idea. Imagine creating a little assembly line out of vesicles, each producing its own proteins, and feeding into each other. I like it.
In that Satisfactory demo, the guy took it to flabbergastingly ridiculous extremes of complexity, far beyond what the game designers intended, with thousands of factories consuming resources and pumping out and products, miles of tangled conveyor belts strung all around through the sky, stitched together with mergers and splitters, collected and buffered in storage containers, moving, mixing and diffusing millions of individual items all around the environment. But I get the impression that's hardly scratching the surface when it comes to biology.

Your Bunny Planet design session really illustrates how wonderfully your work applies to computer games:

https://www.youtube.com/watch?v=L482dpH9S8o

https://www.youtube.com/watch?v=624F5iaCKco

Chaim Gingold (one of the developers of Spore) made "Earth: A Primer", a cool interactive educational simulation science book, also like your work, inspired by Neil Stephenson's young lady's illustrated primer, but on a somewhat different physical scale:

https://www.earthprimer.com/

>Earth: A Primer is a science book you play with.

I sent Chaim some links to your work, and he loved it and said he has collaborator who will be delighted to learn about it too. He also teaches computer game design, and did his PhD thesis on "Play Design". You should work together, and fill in all the levels between planets and cytoplasm! ;)

https://en.wikipedia.org/wiki/Chaim_Gingold

http://chaim.io

https://pqdtopen.proquest.com/doc/1806122688.html?FMT=AI

That's insanely cool!