I’ll start with a teaser screenshot!
I just finished coding a program in which I can switch between no sorting, CPU particle sorting and my OIT (order-independent transparency) implementation. Here’s a screenshot of a white sphere with diameter 200 inside a 200x200x200 cube, so at the center of the cube’s faces, the white sphere should be visible, but not anywhere else.
No sorting
Simply rendering all the particles in their random order looks, as expected, horrible. The white particles inside the cube should only be barely visible, but since the particles aren’t ordered correctly, the blending screws up.
CPU-side depth sorting
Sorting the particles by depth seems like a decent fix at first, but even though it should be 100% correct it does not look good in this case. This isn’t visible in the screenshot, but if two smoke particles (that are supposed to each represent a volume) that intersect suddenly change z-order, we see a sudden pop. Perfect depth sorting therefore doesn’t look particularly good for volumetric particles.
My OIT-implementation
What we actually want is some kind of soft blending that does 50-50 blending if two particles intersect, but also sorts the particles perfectly if they’re not intersecting along the Z-axis. As an example, if we have a red and a green particle that are almost at the exact same position, we don’t want to pick the one that just happens to be a tiny millimeter closer to the camera, we want an almost even mix of the two. If we however have a thick green particle close and a far one behind it that’s red, we obviously want the green one to occlude the red one. My algorithm handles this perfectly. At first look, it looks a little bit blurry, but this is simply because the particles that are along the surface of the cube are blended together (as they should be!), but the center of the cube is still 100% obscured. This is what we want EXACTLY when we render particles trying to simulate a volume.
Again, this is without any pre-sorting at all. I simply throw the data at the GPU in any order I feel like, meaning I can draw particles, transparent streak effects, actual volumetric smoke, god rays, transparent hair, etc in any order I want them to and get a correct color blend between them.