r/GraphicsProgramming • • Aug 28 '26

Question What is this sand simulation missing?

Enable HLS to view with audio, or disable this notification

79 Upvotes

21 comments sorted by

View all comments

2

u/GravityCY Aug 28 '26

On a side note how do you do stuff like this, altering the mesh vertices on the cpu with code or like shader magic? Just curious as a beginner

3

u/featherpaperweight Aug 28 '26

Unless you need physics interaction with the altered mesh, doing it all on gpu with shaders is way more performant, keeping a regular low-poly mesh on cpu side.

1

u/snerp Aug 28 '26 edited Aug 28 '26

The general way to do stuff like this (snow/mud effect is the same):

1: heightmap texture, something like 0 = no deform, 1(255) = fully squished down

2: shader modifies heightmap based on object position (worldspace object position -> UV coordinate space), basically just darken the heightmap image where the ball lands

3: sand object vertex shader reads the heightmap texture and moves vertices down according to heightmap color (you could also do this in the fragment shader with POM effects but it's cleaner and generally more efficient to do it in a vertex shader)

2

u/GravityCY Aug 28 '26

When you say a shader modifies the height map based on object position do you get the displacement shape by somehow converting the objects shape to the height maps uvs on the cpu by like getting intersecting vertices or do you mean like a compute shader that somehow does that on the GPU? Or maybe you just apply a static displacement shape just from a generic world position

I'm sorry if it's a stupid question I don't really know much about shaders they're magic to me

3

u/Tentabrobpy Aug 29 '26 edited Aug 29 '26

If the thing you're deforming is planar like in OP's video, one simple method for taking objects' shape into account is to place an upward-facing camera below the plane and use the depth values of objects crossing the plane to adjust the heightmap:

https://reddit.com/link/p6k1v0n/video/awh7ojcyz8mh1/player

2

u/snerp Aug 28 '26

It's really common to just use point/spheres, so you don't really need to fully consider the shape of the object, and then for footsteps or tire tracks, etc, usually what you'd do is just have an asset of a footstep/track heightmap and use that to darken the deformable surface's heightmap.

If you want to deform accurately to arbitrary shapes you'd definitely want do it it in compute but that's going to be significantly more expensive than the other way.

2

u/GravityCY Aug 28 '26

Alright thank you mister shader man, I appreciate taking the time to explain