r/GraphicsProgramming • • 21h ago

I added depth occlusion to my GPU particles system

Recently I wanted to make my GPU particle system more dynamic and easier to develop with and given how vital heavy rain is to the tone and visuals of the game, I wanted to make rain be dynamically occluded based on the meshes.

First I flag the meshes which should be occluders, which is shown in the second photo. These occluders are rendered from an orthographic camera. The third photo is the depth texture from this render pass.

Particle emitters can then have a flag for height occlusion and height snap. In the particles compute pass, the depth texture is sampled and remapped to view space to get the distance. For emitters with height occlusion enabled, their lifetime is set to zero if they're greater than this distance. For emitters with height snap, the particles are set to this sampled height.

A limitation of this approach is that wind doesn't really work since the particles aren't actually colliding with anything. If there are any ideas of how to rectify this I'd love to hear them.

Overall, I'm pretty happy with this approach, and it has made iterating on the level much faster.

Edit: No AI used.

20 Upvotes

12 comments sorted by

1

u/TreyDogg72 21h ago

Very cool! Perhaps you can get that wind support you’re looking for by just passing a wind speed vector into the shader where you compute the particle effects?

0

u/Zestyclose_End3101 21h ago

Yeah I tried passing a vector, however the problem is that with this approach, the depth acts as a hard wall, which automatically occludes particles below that depth. Which means rain particles that shouldn't be occluded since they went below that depth from the side will end up being cut off. I think to fix this, I would have to rotate the orthographic camera to match the rain direction with the wind, but I wasn't able to get that to work. I may have another crack at it later though.

1

u/TreyDogg72 20h ago

Have you thought about doing the wind transformation after your occlusion runs? Is this on GitHub for us to take a look?

1

u/Zestyclose_End3101 19h ago

Hmmm I haven't thought of that, however that would probably run into a problem of clipping through walls. I don't have the project on github, though I'm not doing anything new. I've looked at a few other more prominent particle systems and they generally use a particle system with collision shapes which is out of my scope. I still have a couple of ideas for the wind so I may post an update later if I get it working :)

1

u/TreyDogg72 19h ago

Yeah I agree that they’d clip through the walls. Perhaps you could do some math similar to how you calculate occlusion to determine when a particle passes through a surface and then spawn another “splash” particle effect with an animated quad for the splash effect

1

u/Zestyclose_End3101 19h ago

Unfortunately, that would require the particles to have collision logic and I'd have to have a representation of the level using simple collision shapes like rects and spheres that I can pass to the gpu. It's definitely a good thought and I considered it but I think that's a bit too far out of my scope.

2

u/fastcar25 4h ago

You could try voxelizing the scene or generating a scene SDF, I think?

I've seen variations of that + using the main camera depth buffer as well, not solely a specific rain shadowmap like that.

Something like sdf/voxel test if the particle is off screen, but if it's on screen you can test with the actual depth buffer?

When testing against the depth buffer (either of them), what you probably want to test is specifically if the particle moved to or past the depth buffer value since the last frame, so that would probably fix the rain occluder issue? It would only care about the orthographic depth buffer if the particle specifically crossed the plane at that point in the depth buffer during a frame, and that would mean that your example of the wind causing the depth buffer to kill particles wouldn't happen, I believe.

1

u/Zestyclose_End3101 31m ago

Thanks for the suggestion! I actually ended up getting wind working with the orthographic camera after I fixed a stupid bug. A minor visual issue now is that in places where depth changes very sharply, there is visible banding due to minimal depth information. I've tried to fix it with some filtering of the depth sample but it's still not great. I think to fix that I would have to do something like you're suggesting and have some sort of physical representation of the scene which the particles can actually collide with.

1

u/AutoModerator 21h ago

Recently updated rules. Please check that your post is in compliance with Rules 1, 4, 5, and 6 on the sidebar. TL;DR:

  • No showcases.
  • Disclose AI usage in post or project.
  • Present a topic to talk about, e.g. how something works.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/snerp 3h ago

I took a ridiculously heavy approach to this since my game has significant wind and odd shaped geometry. I set up a background thread to do thousands of tiny ray casts against a copy of the physics world which is used to constantly cull the particles

1

u/fastcar25 3h ago

If you have RT support you could probably reuse the scene BVH for short rays if the number of particles checking that isn't too high.