This post details the breakdown of how we achieve this result and the tricks discovered along the way to create particle physics simulations:
Above is a showcase of our custom-made interactive application where I am dragging our sand emitter around the screen with my mouse cursor. Sand is emitted and continue to fall, bouncing off objects. We manage to achieve and render interactive frame rates in almost real-time, rendering and simulating 100,000 individual sand grains colliding dynamically with moving and rotating objects, with physics. Our scene has been highly optimized to render within seconds instead of hours, allowing us to freely edit parameters throughout our iterations.
Warning: This post is quite technical until the end where we share how this was achieved:
In making this, the main issues to tackle were:
1) Creating believable sand physics (friction, bounciness, spread)
2) Sand colliding dynamically with other 3D objects that are in motion
3) The sheer quantity of particles on screen simultaneously, and keeping realistic render times
First thoughts & Research
Early talks about whether we should do sand simulation in the group wasn’t very positive.
None of us had done it before, nor are we even aware of what techniques we can use to achieve believable sand.
Sand is a complex substance – it is not a viscous liquid in which we can use fluid simulations. Yet at the same time it does not act as a single mold-able solid like clay.
Research on how to achieve this led to pretty complicated techniques and sometimes involve software beyond our understanding. Even in Maya, we had not gone deep enough to understand how to use Maya’s nParticles, which are our guess at how we would execute it in Maya.
I narrowed my search to see if it was possible to create this in Unity. As it is not primarily a simulation software, there were many challenges and question marks.
Thousands of individual sand grains have to be simulated. And what about collisions? Would our sand interact with our gear geometry in the scene? If it had to be done in two separate software, then compositing them together would also be a challenge – we could do sand overlay on the gears, non-interactively, but we had no idea how that would look. If it is too obvious it’s an overlay of two distinct videos, it won’t look good.
I knew that it was possible to use CPU-based particles in Unity that use an underlying physics engine (PhysX 3.4).
But as much as I wanted to use Unity (as our gears are rendered there), the sheer quantity of particles destroyed any hope of this being possible, or is it?
Many youtube tutorials on how to do real-time sand physics within Unity did not achieve the fantastic results we were hoping for. The sand particles were always too big, and too few.
The truth is, physics calculations consist of plenty of mathematical operations that are expensive to compute in large quantities.
The best simulations went to 20,000 particles, maybe more. But this was still a far cry from believable sand. In other words, we had little luck with tutorials. Bumping it up would exponentially take a toll on simulation times and make it difficult to iterate creatively.
Achieving interactive frame rates was a challenge. But I wanted to see how far we could push it.
I gave all the important gears a Mesh Collider, and enabled collision on particles so they would be added to the physics engine and collide.
I started with a basic particle system that simulates 1,000 particles.
Seeing sand interact with the gears for the first tine is cool. But aside from cool physics, the visuals look nasty. How about 10,000?
Better but not good. I pushed for 100,000 particles. This was a huge quantity and the simulation slowed down tremendously.
I decided to be crazy to the extreme, and bump it up by a factor of 10 again!