Getting The Correct Depth In Ray March Cloud Example

Okay, after a few days of banging my head against the wall trying to get depth working in the volume cloud ray march example, I need some help. This is what I have come up with. I’m getting partially decent results, but I’m not sure what’s wrong. At certain angles, when the range and opacity are lowered, the object isn’t occluded by the clouds where it should be.

1 Like

General note for that cloud-march example - most of the fragment shader code there is kinda “artistic”, the noises, alpha drop-off, and steps especially - and not viable for actual cloud rendering besides that specific example.

To do depth testing for volumes you just (a) do the usual ray marching, then (b) stop the march as soon as distance from camera exceeds opaque world depth, then (c) if depth of volume never exceeded opaque world depth, just use density as alpha factor to blend volumes with opaque world.

Keep in mind for all this to work: you need need to set material side of the volume material to double sided, you need to disable depth and frustum testing of the volume cube, and you have to make sure volume samples and camera samples of depth are in the same coordinate space (so when testing, place both volume and the camera away from 0/0/0 coordinates, otherwise it’s possible stuff will stop working as soon as you rotate or move the camera.)

I think I have a solution and can package the code (the setup is a bit more involved than the original post) and post it if anyone is interested.


I am desperately looking for a solution to this problem. Can you share your solution please?